Newer
Older
1
2
3
4
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
59
60
# coding=utf-8
from earthdiagnostics.diagnostic import Diagnostic, DiagnosticVariableOption, \
DiagnosticDomainOption, DiagnosticChoiceOption, DiagnosticOption
from earthdiagnostics.utils import Utils
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'
"Diagnostic alias for the configuration file"
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'),
DiagnosticVariableOption('variable'),
DiagnosticChoiceOption('cell', ('t', 'u', 'v', 'f', 'w'), 't'),
DiagnosticOption('grid', ''))
options = cls.process_options(options, options_available)
mask_file = Utils.openCdf('mask.nc')
cell_point = options['cell']
# W and T share the same mask
if cell_point == 'w':
cell_point = 't'
mask = mask_file.variables['{0}mask'.format(cell_point)][:].astype(float)
mask[mask == 0] = np.nan
job_list = list()
for startdate, member, chunk in diags.config.experiment.get_chunk_list():
job_list.append(MaskLand(diags.data_manager, startdate, member, chunk,
options['domain'], options['variable'], mask, options['grid']))
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)
if not 'lev' in handler.dimensions:
mask = self.mask[:, 0, ...]
else:
mask =self.mask
handler.variables[self.variable][:] *= mask
handler.close()
self.send_file(variable_file, self.domain, self.variable, self.startdate, self.member, self.chunk,
grid=self.grid)