Newer
Older
from earthdiagnostics.diagnostic import Diagnostic, DiagnosticVariableListOption, \
DiagnosticDomainOption, DiagnosticChoiceOption, DiagnosticOption
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
import numpy as np
class MaskLand(Diagnostic):
"""
Changes values present in the mask for NaNs
:created: February 2012
:last modified: June 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 to average
:type variable: str
"""
alias = 'maskland'
def __init__(self, data_manager, startdate, member, chunk, domain, variable, mask, grid):
Diagnostic.__init__(self, data_manager)
self.startdate = startdate
self.member = member
self.chunk = chunk
self.domain = domain
self.variable = variable
self.mask = mask
self.grid = 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.variable == other.variable
def __str__(self):
return 'Land mask Startdate: {0} Member: {1} Chunk: {2} Variable: {3}:{4} ' \
'Grid: {5}'.format(self.startdate, self.member, self.chunk, self.domain, self.variable, self.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, minimum depth (level), maximum depth (level)
:type options: list[str]
:return:
"""
options_available = (DiagnosticDomainOption('domain'),
DiagnosticVariableListOption('variables'),
DiagnosticChoiceOption('cell', ('t', 'u', 'v', 'f', 'w'), 't'),
DiagnosticOption('grid', ''))
options = cls.process_options(options, options_available)
cell_point = options['cell']
# W and T share the same mask
if cell_point == 'w':
cell_point = 't'
mask = cls._get_mask(cell_point)
for var in options['variables']:
for startdate, member, chunk in diags.config.experiment.get_chunk_list():
job_list.append(MaskLand(diags.data_manager, startdate, member, chunk,
options['domain'], var, mask, options['grid']))
@classmethod
def _get_mask(cls, cell_point):
mask_file = Utils.openCdf('mask.nc')
mask = mask_file.variables['{0}mask'.format(cell_point)][:].astype(float)
mask[mask == 0] = np.nan
mask_file.close()
return mask
"Diagnostic alias for the configuration file"
def request_data(self):
self.var_file = self.request_chunk(self.domain, self.variable, self.startdate, self.member, self.chunk,
grid=self.grid)
def declare_data_generated(self):
self.masked_file = self.declare_chunk(self.domain, self.variable, self.startdate, self.member, self.chunk,
grid=self.grid)
def compute(self):
"""
Runs the diagnostic
"""
temp = TempFile.get()
Utils.copy_file(self.var_file.local_file, temp)
handler = Utils.openCdf(temp)
if 'lev' not in handler.dimensions:
mask = self.mask[:, 0, ...]
else:
handler.variables[self.variable][:] *= mask
handler.close()