Newer
Older
from earthdiagnostics.diagnostic import Diagnostic, DiagnosticOption, DiagnosticComplexStrOption, DiagnosticDomainOption
from earthdiagnostics.utils import Utils
from earthdiagnostics.modelingrealm import ModelingRealm
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
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'))
Javier Vegas-Regidor
committed
options = cls.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)