Source code for earthdiagnostics.ocean.verticalmean

# coding=utf-8
"""Chooses vertical level in ocean, or vertically averages between 2 or more  ocean levels"""
from earthdiagnostics import cdftools
from earthdiagnostics.box import Box
from earthdiagnostics.diagnostic import Diagnostic, DiagnosticIntOption, DiagnosticVariableOption
from earthdiagnostics.modelingrealm import ModelingRealms
from earthdiagnostics.utils import Utils, TempFile


[docs]class VerticalMean(Diagnostic): """ Chooses vertical level in ocean, or vertically averages between 2 or more ocean levels :original author: Virginie Guemas <virginie.guemas@bsc.es> :contributor: Eleftheria Exarchou <eleftheria.exarchou@bsc.es> :contributor: Javier Vegas-Regidor <javier.vegas@bsc.es> :created: February 2012 :last modified: June 2016 :param data_manager: data management object :type data_manager: DataManager :param startdate: startdate :type startdate: str :param member: member number :type member: int :param chunk: chunk's number :type chunk: int :param variable: variable to average :type variable: str :param box: box used to restrict the vertical mean :type box: Box """ alias = 'vertmean' "Diagnostic alias for the configuration file" def __init__(self, data_manager, startdate, member, chunk, variable, box): Diagnostic.__init__(self, data_manager) self.startdate = startdate self.member = member self.chunk = chunk self.variable = variable self.box = box def __eq__(self, other): if self._different_type(other): return False return self.startdate == other.startdate and self.member == other.member and self.chunk == other.chunk and \ self.box == other.box and self.variable == other.variable def __str__(self): return 'Vertical mean Startdate: {0} Member: {1} Chunk: {2} Variable: {3} ' \ 'Box: {4}'.format(self.startdate, self.member, self.chunk, self.variable, self.box)
[docs] @classmethod def generate_jobs(cls, diags, options): """ Create a job for each chunk to compute the diagnostic :param diags: Diagnostics manager class :type diags: Diags :param options: variable, minimum depth (level), maximum depth (level) :type options: list[str] :return: """ options_available = (DiagnosticVariableOption(diags.data_manager.config.var_manager), DiagnosticIntOption('min_depth', -1), DiagnosticIntOption('max_depth', -1)) options = cls.process_options(options, options_available) box = Box() if options['min_depth'] >= 0: box.min_depth = options['min_depth'] if options['max_depth'] >= 0: box.max_depth = options['max_depth'] job_list = list() for startdate, member, chunk in diags.config.experiment.get_chunk_list(): job_list.append(VerticalMean(diags.data_manager, startdate, member, chunk, options['variable'], box)) return job_list
[docs] def request_data(self): """Request data required by the diagnostic""" self.variable_file = self.request_chunk(ModelingRealms.ocean, self.variable, self.startdate, self.member, self.chunk)
[docs] def declare_data_generated(self): """Declare data to be generated by the diagnostic""" self.results = self.declare_chunk(ModelingRealms.ocean, self.variable + 'vmean', self.startdate, self.member, self.chunk, box=self.box)
[docs] def compute(self): """Run the diagnostic""" temp = TempFile.get() handler = Utils.open_cdf(self.variable_file.local_file) if self.box.min_depth is None: lev_min = handler.variables['lev'][0] else: lev_min = self.box.min_depth if self.box.max_depth is None: lev_max = handler.variables['lev'][-1] else: lev_max = self.box.max_depth handler.close() cdftools.run('cdfvertmean', input_file=self.variable_file.local_file, output_file=temp, options=[self.variable, 'T', lev_min, lev_max, '-debug']) Utils.setminmax(temp, '{0}_vert_mean'.format(self.variable)) self.results.set_local_file(temp, rename_var='{0}_vert_mean'.format(self.variable))