# coding=utf-8 from earthdiagnostics.diagnostic import Diagnostic from earthdiagnostics.variable import Domain class Relink(Diagnostic): """ Recreates the links for the variable specified :original author: Javier Vegas-Regidor :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: Domain :param move_old: if true, looks for files following the old convention and moves to avoid collisions :type move_old: bool """ alias = 'relink' "Diagnostic alias for the configuration file" def __init__(self, data_manager, startdate, member, chunk, domain, variable, move_old): Diagnostic.__init__(self, data_manager) self.startdate = startdate self.member = member self.chunk = chunk self.variable = variable self.domain = domain self.move_old = move_old def __str__(self): return 'Relink output Startdate: {0} Member: {1} Chunk: {2} Move old: {5} ' \ 'Variable: {3}:{4}'.format(self.startdate, self.member, self.chunk, self.domain, self.variable, self.move_old) 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 @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, move_old=False :type options: list[str] :return: """ num_options = len(options) - 1 if num_options < 2: raise Exception('You must specify the variable and domain to link') if num_options > 3: raise Exception('You must between 2 and 3 parameters for the relink diagnostic') variable = options[1] domain = Domain(options[2]) if num_options >= 3: move_old = bool(options[3].lower()) else: move_old = True job_list = list() for startdate, member, chunk in diags.config.experiment.get_chunk_list(): job_list.append(Relink(diags.data_manager, startdate, member, chunk, domain, variable, move_old)) return job_list def compute(self): """ Runs the diagnostic """ self.data_manager.link_file(self.domain, self.variable, self.startdate, self.member, self.chunk, move_old=self.move_old)