mask_land.py 3.85 KB
Newer Older
# coding=utf-8
from earthdiagnostics.diagnostic import Diagnostic, DiagnosticVariableListOption, \
    DiagnosticDomainOption, DiagnosticChoiceOption, DiagnosticOption
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
from earthdiagnostics.utils import Utils, TempFile
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'

    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'),
                             DiagnosticVariableListOption('variables'),
                             DiagnosticChoiceOption('cell', ('t', 'u', 'v', 'f', 'w'), 't'),
                             DiagnosticOption('grid', ''))
        options = cls.process_options(options, options_available)

        cell_point = options['cell']
        # W and T share the same mask
        if cell_point == 'w':
            cell_point = 't'

        mask = cls._get_mask(cell_point)
        for var in options['variables']:
            for startdate, member, chunk in diags.config.experiment.get_chunk_list():
                job_list.append(MaskLand(diags.data_manager, startdate, member, chunk,
                                         options['domain'], var, mask, options['grid']))
    @classmethod
    def _get_mask(cls, cell_point):
        mask_file = Utils.openCdf('mask.nc')
        mask = mask_file.variables['{0}mask'.format(cell_point)][:].astype(float)
        mask[mask == 0] = np.nan
        mask_file.close()
        return mask

Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
    "Diagnostic alias for the configuration file"

    def request_data(self):
        self.var_file = self.request_chunk(self.domain, self.variable, self.startdate, self.member, self.chunk,
                                           grid=self.grid)

    def declare_data_generated(self):
        self.masked_file = self.declare_chunk(self.domain, self.variable, self.startdate, self.member, self.chunk,
                                              grid=self.grid)

    def compute(self):
        """
        Runs the diagnostic
        """
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
        temp = TempFile.get()
        Utils.copy_file(self.var_file.local_file, temp)
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
        handler = Utils.openCdf(temp)
        if 'lev' not in handler.dimensions:
            mask = self.mask[:, 0, ...]
        else:
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
            mask = self.mask
        handler.variables[self.variable][:] *= mask
        handler.close()

Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
        self.masked_file.set_local_file(temp)