attribute.py 3.89 KB
Newer Older
# coding=utf-8
from earthdiagnostics.diagnostic import Diagnostic, DiagnosticOption, DiagnosticComplexStrOption, DiagnosticDomainOption
from earthdiagnostics.utils import Utils
from earthdiagnostics.variable import Domain


class Attribute(Diagnostic):
    """
    Rewrites files without doing any calculations.
    Can be useful to convert units or to correct wrong metadata

    :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 variable: variable's name
    :type variable: str
    :param domain: variable's domain
    :type domain: Domain
    """

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

    def __init__(self, data_manager, startdate, member, chunk, domain, variable, grid,
                 attributte_name, attributte_value):
        Diagnostic.__init__(self, data_manager)
        self.startdate = startdate
        self.member = member
        self.chunk = chunk
        self.variable = variable
        self.domain = domain
        self.grid = grid
        self.attributte_name = attributte_name
        self.attributte_value = attributte_value

    def __str__(self):
        return 'Write attributte output Startdate: {0} Member: {1} Chunk: {2} ' \
               'Variable: {3}:{4} Attributte:{5}:{6}'.format(self.startdate, self.member, self.chunk, self.domain,
                                                             self.variable, self.attributte_name, self.attributte_value)

    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.attributte_name == other.attributte_name and self.attributte_value == other.attributte_value

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

        options_available = (DiagnosticOption('variable'),
                             DiagnosticDomainOption('domain'),
                             DiagnosticOption('name'),
                             DiagnosticComplexStrOption('value'),
                             DiagnosticOption('grid'))
        options = Diagnostic.process_options(options, options_available)
        job_list = list()
        for startdate, member, chunk in diags.config.experiment.get_chunk_list():
            job_list.append(Attribute(diags.data_manager, startdate, member, chunk,
                                      options['domain'], options['variable'], options['grid'], options['grid'],
                                      options['value']))
        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)
        handler.setncattr(self.attributte_name, self.attributte_value)
        handler.close()
        if not Utils.check_netcdf_file(variable_file):
            raise Exception('Attribute {0} can not be set correctly to {1}'.format(self.attributte_name,
                                                                                   self.attributte_value))
        self.send_file(variable_file, self.domain, self.variable, self.startdate, self.member, self.chunk,
                       grid=self.grid)