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
# coding=utf-8
import iris
import iris.analysis
import iris.exceptions
from earthdiagnostics.box import Box
from earthdiagnostics.diagnostic import Diagnostic, DiagnosticFloatOption, DiagnosticDomainOption, \
DiagnosticVariableOption, DiagnosticChoiceOption
from earthdiagnostics.utils import Utils, TempFile
from earthdiagnostics.modelingrealm import ModelingRealms
class VerticalMeanMetersIris(Diagnostic):
"""
Averages vertically any given variable
:original author: Virginie Guemas <virginie.guemas@bsc.es>
:contributor: Javier Vegas-Regidor<javier.vegas@bsc.es>
: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
:param box: box used to restrict the vertical mean
:type box: Box
"""
alias = 'vmean'
"Diagnostic alias for the configuration file"
def __init__(self, data_manager, startdate, member, chunk, domain, variable, box, grid_point):
Diagnostic.__init__(self, data_manager)
self.startdate = startdate
self.member = member
self.chunk = chunk
self.domain = domain
self.variable = variable
self.box = box
self.required_vars = [variable]
self.generated_vars = [variable + 'vmean']
self.grid_point = grid_point
def __eq__(self, other):
return self.startdate == other.startdate and self.member == other.member and self.chunk == other.chunk and \
self.box == other.box and self.variable == other.variable
def __str__(self):
return 'Vertical mean meters Startdate: {0} Member: {1} Chunk: {2} Variable: {3}:{4} ' \
'Box: {5}'.format(self.startdate, self.member, self.chunk, self.domain, self.variable, self.box)
@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 (meters), maximum depth (meters)
:type options: list[str]
:return:
"""
options_available = (DiagnosticVariableOption(),
DiagnosticFloatOption('min_depth', -1),
DiagnosticFloatOption('max_depth', -1),
DiagnosticDomainOption(default_value=ModelingRealms.ocean))
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
options = cls.process_options(options, options_available)
box = Box(True)
if options['min_depth'] >= 0:
box.min_depth = options['min_depth']
if options['max_depth'] >= 0:
box.max_depth = options['max_depth']
job_list = list()
for startdate, member, chunk in diags.config.experiment.get_chunk_list():
job_list.append(VerticalMeanMetersIris(diags.data_manager, startdate, member, chunk,
options['domain'], options['variable'], box, options['grid_point']))
return job_list
def request_data(self):
self.variable_file = self.request_chunk(ModelingRealms.ocean, self.variable, self.startdate, self.member,
self.chunk)
def declare_data_generated(self):
self.results = self.declare_chunk(self.domain, self.variable + 'vmean', self.startdate, self.member,
self.chunk, box=self.box)
def compute(self):
"""
Runs the diagnostic
"""
iris.FUTURE.netcdf_no_unlimited = True
iris.FUTURE.netcdf_promote = True
var_cube = iris.load_cube(self.variable_file.local_file)
lev_names = ('lev', 'depth')
coord = None
for coord_name in lev_names:
try:
coord = var_cube.coord(coord_name)
except iris.exceptions.CoordinateNotFoundError:
pass
if self.box.min_depth is None:
lev_min = coord.points[0]
else:
lev_min = self.box.min_depth
if self.box.max_depth is None:
lev_max = coord.points[-1]
else:
lev_max = self.box.max_depth
var_cube = var_cube.extract(iris.Constraint(coord_values={coord.var_name:
lambda cell: lev_min <= cell <= lev_max}))
var_cube = var_cube.collapsed(coord, iris.analysis.MEAN)
temp = TempFile.get()
iris.save(var_cube, temp, zlib=True)
self.results.set_local_file(temp, rename_var=var_cube.var_name)