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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from box import Box
from earthdiagnostics import Diagnostic, TempFile, Utils, cdftools
class VerticalMean(Diagnostic):
"""
Choose vertical level in ocean, or vertically average between
2 or more ocean levels
Created in February 2012 Author : vguemas@ic3.cat
Modified (more generic, i.e. for any input var) in December 2014
Author : eleftheria.exarchou@ic3.cat
# :param input_file: input file name
# :param output_file: output file name (=> 2D field )
# :param variable: variable name
# :param lev_min: upper depth of the layer (in level number)
# :param lev_max: lower depth of the layer (in level number. If same as the upper layer, then the script just
selects this layer. If its different, a vertical integral (weighted) between upper and lower level is computed
"""
def __init__(self, data_manager, startdate, member, chunk, variable, box):
Diagnostic.__init__(self, data_manager)
self.startdate = startdate
self.member = member
self.chunk = chunk
self.variable = variable
self.box = box
self.required_vars = [variable]
self.generated_vars = [variable + 'vmean']
@classmethod
def generate_jobs(cls, diags, options):
num_options = len(options) - 1
if num_options < 1:
raise Exception('You must specify the variable to average vertically')
if num_options > 3:
raise Exception('You must specify between one and three parameters for the vertical mean')
variable = options[1]
box = Box(True)
if num_options >= 2:
box.min_depth = float(options[2])
if num_options >= 2:
box.max_depth = float(options[3])
job_list = list()
for startdate in diags.startdates:
for member in diags.members:
for chunk in range(1, diags.chunks + 1):
job_list.append(VerticalMean(diags.data_manager, startdate, member, chunk,
variable, box))
return job_list
def compute(self):
temp = TempFile.get()
variable_file = self.data_manager.get_file('ocean', self.variable, self.startdate, self.member, self.chunk)
handler = Utils.openCdf(variable_file)
if self.box.min_depth is None:
lev_min = handler.variables['lev'][0]
else:
lev_min = self.box.min_depth
if self.box.max_depth is None:
lev_max = handler.variables['lev'][-1]
else:
lev_max = self.box.max_depth
handler.close()
cdftools.run('cdfvertmean', input=variable_file, output=temp, options=[self.variable, 'T', lev_min, lev_max,
'-debug'])
Utils.setminmax(temp, '{0}_vert_mean'.format(self.variable))
self.data_manager.send_file(temp, 'ocean', self.variable + 'vmean', self.startdate, self.member, self.chunk,
box=self.box, rename_var='{0}_vert_mean'.format(self.variable))