module.py 4.44 KB
Newer Older
# coding=utf-8
from earthdiagnostics.diagnostic import *
from earthdiagnostics.utils import Utils, TempFile
from earthdiagnostics.modelingrealm import ModelingRealm
import numpy as np


class Module(Diagnostic):
    """
    Scales a variable by the given value also adding at offset
    Can be useful to correct units or other known errors
    (think of a tas file declaring K as units but with the data stored as Celsius)

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

    :created: July 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 domain: variable's domain
    :type domain: ModelingRealm
    """

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

Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
    def __init__(self, data_manager, startdate, member, chunk, domain, componentu, componentv, module_var, grid):
        Diagnostic.__init__(self, data_manager)
        self.startdate = startdate
        self.member = member
        self.chunk = chunk
        self.domain = domain
        self.componentu = componentu
        self.componentv = componentv
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
        self.module = module_var
        self.grid = grid

        self.original_values = None

    def __str__(self):
        return 'Calculate module Startdate: {0} Member: {1} Chunk: {2} ' \
               'Variables: {3}:{4},{5},{6} ' \
               'Grid: {7}'.format(self.startdate, self.member, self.chunk, self.domain, self.componentu,
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
                                  self.componentv, self.module, self.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.componentu == other.componentu and \
            self.componentv == other.componentv and self.module == other.module and self.grid == other.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, domain, grid
            :type options: list[str]
            :return:
            """
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
        options_available = (DiagnosticDomainOption(),
                             DiagnosticVariableOption('componentu'),
                             DiagnosticVariableOption('componentv'),
                             DiagnosticVariableOption('module'),
                             DiagnosticOption('grid', ''))
        options = cls.process_options(options, options_available)
        job_list = list()
        for startdate, member, chunk in diags.config.experiment.get_chunk_list():
            job_list.append(Module(diags.data_manager, startdate, member, chunk,
                                   options['domain'], options['componentu'], options['componentv'], options['module'],
                                   options['grid']))
        return job_list

    def request_data(self):
        self.component_u_file = self.data_manager.get_file(self.domain, self.componentu, self.startdate, self.member,
                                                           self.chunk, grid=self.grid)
        self.component_v_file = self.data_manager.get_file(self.domain, self.componentv, self.startdate, self.member,
                                                           self.chunk, grid=self.grid)

    def declare_data_generated(self):
        self.module_file = self.declare_chunk(self.domain, self.module, self.startdate, self.member, self.chunk,
                                              grid=self.grid)

    def compute(self):
        """
        Runs the diagnostic
        """
        temp = TempFile.get()
        Utils.copy_file(self.component_u_file, temp)
        component_u = Utils.openCdf(temp)
        component_v = Utils.openCdf(self.component_v_file)
        variable_u = component_u.variables[self.componentu]
        variable_v = component_v.variables[self.componentv]

        variable_u[:] = np.sqrt(variable_u[:] ** 2 + variable_v[:] ** 2)

        if 'table' in variable_u.ncattrs():
            del variable_u.table
        if 'code' in variable_u.ncattrs():
            del variable_u.code

        component_u.close()
        component_v.close()

        self.module_file.set_local_file(temp, rename_var=self.componentu)