# coding=utf-8 import os from earthdiagnostics.box import Box from earthdiagnostics.diagnostic import * from earthdiagnostics.utils import Utils, TempFile from earthdiagnostics.modelingrealm import ModelingRealm class AverageSection(Diagnostic): """ Compute an average of a given zone. The variable MUST be in a regular grid :original author: Virginie Guemas :contributor: Javier Vegas-Regidor :created: March 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's name :type variable: str :param domain: variable's domain :type domain: ModelingRealm :param box: box to use for the average :type box: Box """ alias = 'avgsection' "Diagnostic alias for the configuration file" def __init__(self, data_manager, startdate, member, chunk, domain, variable, box, grid): Diagnostic.__init__(self, data_manager) self.startdate = startdate self.member = member self.chunk = chunk self.variable = variable self.domain = domain self.box = box self.grid = grid def __eq__(self, other): return self.startdate == other.startdate and self.member == other.member and self.chunk == other.chunk and \ self.domain == other.domain and self.variable == other.variable and self.box == other.box def __str__(self): return 'Average section Startdate: {0.startdate} Member: {0.member} Chunk: {0.chunk} Box: {0.box} ' \ 'Variable: {0.domain}:{0.variable} Grid: {0.grid}'.format(self) @classmethod def generate_jobs(cls, diags, options): """ Creates a job for each chunk to compute the diagnostic :param diags: Diagnostics manager class :type diags: Diags :param options: variable, minimum longitude, maximum longitude, minimum latitude, maximum latitude, domain=ocean :type options: list[str] :return: """ options_available = (DiagnosticDomainOption(), DiagnosticVariableOption(), DiagnosticIntOption('min_lon'), DiagnosticIntOption('max_lon'), DiagnosticIntOption('min_lat'), DiagnosticIntOption('max_lat'), DiagnosticOption('grid', '')) options = cls.process_options(options, options_available) box = Box() box.min_lon = options['min_lon'] box.max_lon = options['max_lon'] box.min_lat = options['min_lat'] box.max_lat = options['max_lat'] job_list = list() for startdate, member, chunk in diags.config.experiment.get_chunk_list(): job_list.append(AverageSection(diags.data_manager, startdate, member, chunk, options['domain'], options['variable'], box, options['grid'])) return job_list def request_data(self): self.variable_file = self.request_chunk(self.domain, self.variable, self.startdate, self.member, self.chunk, grid=self.grid) def declare_data_generated(self): self.mean = self.declare_chunk(self.domain, self.variable + 'mean', self.startdate, self.member, self.chunk, box=self.box, grid=self.grid) def compute(self): """ Runs the diagnostic """ temp = TempFile.get() variable_file = self.variable_file.local_file Utils.cdo.zonmean(input='-sellonlatbox,{0},{1},{2},{3} {4}'.format(self.box.min_lon, self.box.max_lon, self.box.min_lat, self.box.max_lat, variable_file), output=temp) os.remove(variable_file) self.mean.set_local_file(temp, rename_var='tos')