mask_land.py 3.32 KB
Newer Older
# coding=utf-8
from earthdiagnostics.diagnostic import Diagnostic, DiagnosticVariableOption, \
    DiagnosticDomainOption, DiagnosticChoiceOption, DiagnosticOption
from earthdiagnostics.utils import Utils
import numpy as np


class MaskLand(Diagnostic):
    """
    Changes values present in the mask for NaNs

    :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
    """

    alias = 'maskland'
    "Diagnostic alias for the configuration file"

    def __init__(self, data_manager, startdate, member, chunk, domain, variable, mask, grid):
        Diagnostic.__init__(self, data_manager)
        self.startdate = startdate
        self.member = member
        self.chunk = chunk
        self.domain = domain
        self.variable = variable
        self.mask = mask
        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

    def __str__(self):
        return 'Land mask Startdate: {0} Member: {1} Chunk: {2} Variable: {3}:{4} ' \
               'Grid: {5}'.format(self.startdate, self.member, self.chunk, self.domain, self.variable, self.grid)

    @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'),
                             DiagnosticVariableOption('variable'),
                             DiagnosticChoiceOption('cell', ('t', 'u', 'v'), 't'),
                             DiagnosticOption('grid', ''))
        options = cls.process_options(options, options_available)

        mask_file = Utils.openCdf('mask.nc')
        mask = mask_file.variables['{0}mask'.format(options['cell'])][:].astype(float)
        mask[mask == 0] = np.nan

        job_list = list()
        for startdate, member, chunk in diags.config.experiment.get_chunk_list():
            job_list.append(MaskLand(diags.data_manager, startdate, member, chunk,
                                             options['domain'], options['variable'], mask, options['grid']))
        return job_list

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

        handler = Utils.openCdf(variable_file)
        if not 'lev' in handler.dimensions:
            mask = self.mask[:, 0, ...]
        else:
            mask =self.mask
        handler.variables[self.variable][:] *= mask
        handler.close()

        self.send_file(variable_file, self.domain, self.variable, self.startdate, self.member, self.chunk,
                       grid=self.grid)