relink.py 3.6 KB
Newer Older
# coding=utf-8
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
"""Create links for a variable"""
from earthdiagnostics.diagnostic import Diagnostic, DiagnosticOption, \
    DiagnosticDomainOption, DiagnosticBoolOption, DiagnosticVariableOption


class Relink(Diagnostic):
    """
    Recreates the links for the variable specified

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

    :created: September 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: ModelingRealm
    :param move_old: if true, looks for files following the old convention
        and moves to avoid collisions
    """

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

    def __init__(self, data_manager, startdate, member, chunk,
                 domain, variable, move_old, grid):
        Diagnostic.__init__(self, data_manager)
        self.startdate = startdate
        self.member = member
        self.chunk = chunk
        self.variable = variable
        self.domain = domain
        self.grid = grid
        self.var_manager = data_manager.config.var_manager

    def __str__(self):
        return 'Relink output Startdate: {0.startdate} Member: {0.member} ' \
            'Chunk: {0.chunk} Move old: {0.move_old} ' \
            'Variable: {0.domain}:{0.variable} Grid: {0.grid}'.format(self)
    def __eq__(self, other):
        if self._different_type(other):
            return False
        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.move_old == other.move_old and \
            self.grid == other.grid

    @classmethod
    def generate_jobs(cls, diags, options):
        """
        Create a job for each chunk to compute the diagnostic
        :param diags: Diagnostics manager class
        :type diags: Diags
        :param options: variable, domain, move_old=False
        :type options: list[str]
        :return:
        """
        options_available = (
            DiagnosticDomainOption(),
            DiagnosticVariableOption(diags.data_manager.config.var_manager),
            DiagnosticBoolOption('move_old', True),
            DiagnosticOption('grid', '')
        )
        options = cls.process_options(options, options_available)
        job_list = list()
        chunk_list = diags.config.experiment.get_chunk_list()
        for startdate, member, chunk in chunk_list:
            job_list.append(
                Relink(
                    diags.data_manager, startdate, member, chunk,
                    options['domain'], options['variable'],
                    options['move_old'], options['grid']
                )
            )
        return job_list

Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
    def request_data(self):
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
        """Request data required by the diagnostic"""
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed

    def declare_data_generated(self):
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
        """Declare data to be generated by the diagnostic"""
    def compute(self):
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
        """Run the diagnostic"""
        self.data_manager.link_file(
            self.domain, self.variable,
            self.var_manager.get_variable(self.variable),
            self.startdate, self.member, self.chunk,
            move_old=self.move_old, grid=self.grid
        )