regionmean.py 3.76 KB
Newer Older
# coding=utf-8
from earthdiagnostics import cdftools
from earthdiagnostics.box import Box
from earthdiagnostics.diagnostic import Diagnostic, DiagnosticOption, DiagnosticIntOption, DiagnosticDomainOption
from earthdiagnostics.utils import Utils, TempFile
from earthdiagnostics.modelingrealm import ModelingRealms


class RegionMean(Diagnostic):
    """
    Chooses vertical level in ocean, or vertically averages between
    2 or more  ocean levels

    :original author: Javier Vegas-Regidor <javier.vegas@bsc.es>

    :created: January 2017

    :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 = 'regmean'
    "Diagnostic alias for the configuration file"

    def __init__(self, data_manager, startdate, member, chunk, domain, variable, grid, box):
        Diagnostic.__init__(self, data_manager)
        self.startdate = startdate
        self.member = member
        self.chunk = chunk
        self.domain = domain
        self.variable = variable
        self.grid = grid.upper()
        self.box = box
        self.required_vars = [variable]
        self.generated_vars = [variable + 'vmean']

    def __eq__(self, other):
        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)

    @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 depth (level), maximum depth (level)
        :type options: list[str]
        :return:
        """
        options_available = (DiagnosticDomainOption('domain'),
                             DiagnosticOption('variable'),
                             DiagnosticOption('grid'),
                             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(RegionMean(diags.data_manager, startdate, member, chunk,
                                       options['domain'], options['variable'], options['grid'], box))
        return job_list

    def compute(self):
        """
        Runs the diagnostic
        """
        temp = TempFile.get()
        variable_file = self.data_manager.get_file(self.domain, self.variable, self.startdate, self.member, self.chunk)

        cdftools.run('cdfmean', input=variable_file, output=temp, options=[self.domain, self.variable, 'T', 0, 0, 0, 0,
                                                                           self.box.min_depth, self.box.max_depth])
        Utils.setminmax(temp, 'mean_{0}'.format(self.variable))
        self.send_file(temp, ModelingRealms.ocean, self.variable + 'mean', self.startdate, self.member, self.chunk,
                       box=self.box, rename_var='mean_{0}'.format(self.variable))