-
Notifications
You must be signed in to change notification settings - Fork 128
/
merra2.py
308 lines (270 loc) · 10.9 KB
/
merra2.py
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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
"""ESMValTool CMORizer for MERRA2 data.
Tier
Tier 3: restricted datasets (i.e., dataset which requires a registration
to be retrieved or provided upon request to the respective contact or PI).
Source
https://goldsmr4.gesdisc.eosdis.nasa.gov/data/MERRA2_MONTHLY/
https://daac.gsfc.nasa.gov/datasets/M2IUNPASM_5.12.4/summary?keywords=MERRA2
Last access
20220913
Download and processing instructions
- For download instructions see the download script `download_merra2.sh`.
"""
import glob
import logging
import os
from copy import deepcopy
from datetime import datetime
import cf_units
import iris
from dask import array as da
from esmvalcore.cmor.table import CMOR_TABLES
from esmvaltool.cmorizers.data import utilities as utils
# iris spits out a large amount of warnings
# logging.disable('WARNING') # careful: this deactivates writing
# to log files in the pytest test environment for other cmorizer tests
logger = logging.getLogger(__name__)
def _fix_time_monthly(cube):
"""Fix time by setting it to 15th of month."""
# Read dataset time unit and calendar from file
dataset_time_unit = str(cube.coord('time').units)
dataset_time_calender = cube.coord('time').units.calendar
# Convert datetime
time_as_datetime = cf_units.num2date(
cube.coord('time').core_points(), dataset_time_unit,
dataset_time_calender)
newtime = []
for timepoint in time_as_datetime:
midpoint = datetime(timepoint.year, timepoint.month, 15)
newtime.append(midpoint)
newtime = cf_units.date2num(newtime, dataset_time_unit,
dataset_time_calender)
# Put them on the file
cube.coord('time').points = newtime
cube.coord('time').bounds = None
return cube
def _var_pairs(cube_list, var_parts, oper):
"""Return a selection composed of two variables."""
selected_1 = [c for c in cube_list if c.var_name == var_parts[0]]
selected_2 = [c for c in cube_list if c.var_name == var_parts[1]]
if not selected_1:
logger.error("Raw variable %s could not be found "
"in str(cube_list) - operation can not be performed.",
var_parts[0])
raise ValueError
if not selected_2:
logger.error("Raw variable %s could not be found "
"in str(cube_list) - operation can not be performed.",
var_parts[1])
raise ValueError
if oper == "-":
selected = [
cube_1 - cube_2 for cube_1, cube_2 in zip(selected_1, selected_2)
]
selected = iris.cube.CubeList(selected)
elif oper == "+":
selected = [
cube_1 + cube_2 for cube_1, cube_2 in zip(selected_1, selected_2)
]
selected = iris.cube.CubeList(selected)
else:
raise NotImplementedError(f"Pairwise variables operation {oper} "
"not implemented yet, you can do it "
"yourself in the MERRA2 cmorizer.")
return selected
def _load_cube(in_files, var):
cube_list = iris.load_raw(in_files)
pairwise_ops = ["+", "-", ":"]
var_parts = []
for oper in pairwise_ops:
split_var = var['raw'].split(oper)
if len(split_var) == 2:
var_parts = [split_var[0], split_var[1]]
break
if len(split_var) > 2:
logger.error("Splitting raw variable %s by "
"operation %s results in more than two"
" raw variables, this is not yet implemented.",
var['raw'], oper)
raise NotImplementedError
if not var_parts:
selected = [c for c in cube_list if c.var_name == var['raw']]
selected = iris.cube.CubeList(selected)
else:
selected = _var_pairs(cube_list, var_parts, oper)
drop_attrs = [
'History', 'Filename', 'Comment', 'RangeBeginningDate',
'RangeEndingDate', 'GranuleID', 'ProductionDateTime', 'Source'
]
drop_time_attrs = [
'begin_date', 'begin_time', 'time_increment', 'valid_range', 'vmax',
'vmin'
]
for cube in selected:
for attr in drop_attrs:
cube.attributes.pop(attr)
for attr in drop_time_attrs:
cube.coord('time').attributes.pop(attr)
cube.coord('time').points = cube.coord('time').core_points().astype(
'float64')
iris.util.unify_time_units(selected)
cube = selected.concatenate_cube()
return cube
def _fix_coordinates(cube, definition):
"""Fix coordinates."""
if cube.ndim == 3:
axis2def = {'T': 'time', 'X': 'longitude', 'Y': 'latitude'}
axes = ['T', 'X', 'Y']
elif cube.ndim == 4:
axis2def = {'T': 'time', 'X': 'longitude',
'Y': 'latitude', 'Z': 'plev19'}
axes = ['T', 'X', 'Y', 'Z']
for axis in axes:
coord_def = definition.coordinates.get(axis2def[axis])
if coord_def:
coord = cube.coord(axis=axis)
if axis == 'T':
coord.convert_units('days since 1850-1-1 00:00:00.0')
elif axis == 'Z':
if coord.units == "hPa":
coord.convert_units('Pa')
else:
try:
coord.convert_units('Pa')
except ValueError as exc:
logger.error("Attempting to convert units for "
"coordinate %s to Pa", coord)
raise exc
coord.standard_name = coord_def.standard_name
coord.var_name = coord_def.out_name
coord.long_name = coord_def.long_name
coord.points = coord.core_points().astype('float64')
if len(coord.points) > 1:
coord.guess_bounds()
else:
# special case for UV and 3-dim cloud variables:
# variables come with "alevel" instead
# of "plev19" in the table; "alevel" has empty fields for
# standard_name, out_name etc. so we need to set them; it's safe
# to do so since the cmor checker/fixer will convert that during
# preprocessing at cmor fix stage
specialvars = ('uv', 'cl', 'cli', 'clw')
if cube.var_name in specialvars and axis == "Z":
coord = cube.coord(axis=axis)
coord_def = definition.coordinates.get('alevel')
coord.standard_name = "air_pressure"
coord.var_name = "plev"
coord.long_name = "pressure"
coord.points = coord.core_points().astype('float64')
if len(coord.points) > 1:
coord.guess_bounds()
if coord.units == "hPa":
coord.convert_units('Pa')
else:
try:
coord.convert_units('Pa')
except ValueError as exc:
logger.error("Attempting to convert units for "
"coordinate %s to Pa", coord)
raise exc
return cube
def _extract_variable(in_files, var, cfg, out_dir):
logger.info("CMORizing variable '%s' from input files '%s'",
var['short_name'], ', '.join(in_files))
attributes = deepcopy(cfg['attributes'])
attributes['mip'] = var['mip']
attributes['raw'] = var['raw']
pairwise_ops = ["+", "-", ":"]
for oper in pairwise_ops:
if oper in var['raw']:
components = var['raw'].split(oper)
if len(components) == 2:
attributes['component_raw_1'] = components[0]
attributes['component_raw_2'] = components[1]
attributes['component_operation'] = oper
break
cmor_table = CMOR_TABLES[attributes['project_id']]
definition = cmor_table.get_variable(var['mip'], var['short_name'])
cube = _load_cube(in_files, var)
# keep the following raw cube attributes
attrs_to_keep = [
"institution", "Institution",
"institute_id", "VersionID",
"experiment_id",
"source", "Source", # overrides empty string default
"model_id", "ModelID",
"contact", "Contact",
"references",
"tracking_id",
"mip_specs", # described by "mip" already
"source_id", "SourceID",
"product", "Product",
"frequency", "Frequency",
"creation_date",
"project_id", "ProjectID",
"table_id", "TableID",
"title", "Title",
"modeling_realm",
"doi",
"VersionID", # described by "version" already
]
attrs_to_keep_exist = [
att for att in cube.attributes if att in attrs_to_keep
]
for att in attrs_to_keep_exist:
attributes[att] = cube.attributes[att]
utils.set_global_atts(cube, attributes)
# Set correct names
cube.var_name = definition.short_name
# cube.standard_name = definition.standard_name
cube.long_name = definition.long_name
# Fix units (if needed)
# input variable reports m-3 m-3 instead of m3 m-3
if cube.var_name == "sm":
cube.units = definition.units
# Convert units to CMOR units
cube.convert_units(definition.units)
# Add height2m or height10m if needed
if 'height2m' in definition.dimensions:
utils.add_height2m(cube)
elif 'height10m' in definition.dimensions:
utils.add_height10m(cube)
# Fix data type
cube.data = cube.core_data().astype('float32')
# Roll longitude
cube.coord('longitude').points = cube.coord('longitude').points + 180.
nlon = len(cube.coord('longitude').points)
cube.data = da.roll(cube.core_data(), int(nlon / 2), axis=-1)
# Fix coordinates
cube = _fix_coordinates(cube, definition)
cube.coord('latitude').attributes = None
cube.coord('longitude').attributes = None
cube = _fix_time_monthly(cube)
logger.debug("Saving cube\n%s", cube)
logger.debug("Setting time dimension to UNLIMITED while saving!")
utils.save_variable(cube, cube.var_name,
out_dir, attributes,
unlimited_dimensions=['time'])
logger.info("Finished CMORizing %s", ', '.join(in_files))
def cmorization(in_dir, out_dir, cfg, cfg_user, start_date, end_date):
"""Run CMORizer for MERRA2."""
cfg.pop('cmor_table')
if start_date is None:
start_date = 1980
else:
start_date = start_date.year
if end_date is None:
end_date = 2022
else:
end_date = end_date.year
for year in range(start_date, end_date + 1):
for short_name, var in cfg['variables'].items():
if 'short_name' not in var:
var['short_name'] = short_name
# Now get list of files
filepattern = os.path.join(in_dir, var['file'].format(year=year))
in_files = glob.glob(filepattern)
if not in_files:
logger.warning('Year %s data not found', year)
continue
_extract_variable(in_files, var, cfg, out_dir)