Repository navigation
Expand file tree
/
Copy pathclimatology_map_bgc.py
More file actions
375 lines (308 loc) · 14.2 KB
/
Copy pathclimatology_map_bgc.py
File metadata and controls
375 lines (308 loc) · 14.2 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# Copyright (c) 2017, Los Alamos National Security, LLC (LANS)
# and the University Corporation for Atmospheric Research (UCAR).
#
# Unless noted otherwise source code is licensed under the BSD license.
# Additional copyright and license information can be found in the LICENSE file
# distributed with this code, or at http://mpas-dev.github.com/license.html
#
import xarray as xr
from pyremap import LatLonGridDescriptor
from mpas_analysis.shared import AnalysisTask
from mpas_analysis.shared.io.utility import build_obs_path
from mpas_analysis.shared.climatology import RemapMpasClimatologySubtask, \
RemapObservedClimatologySubtask
from mpas_analysis.shared.plot import PlotClimatologyMapSubtask
from mpas_analysis.shared.interpolation.utility import add_periodic_lon
class ClimatologyMapBGC(AnalysisTask):
"""
An analysis task for plotting of BGC variables
Authors
-------
Phillip J. Wolfram, Riley X. Brady, Xylar Asay-Davis
"""
def __init__(self, config, mpasClimatologyTask, controlConfig=None):
"""
Construct the analysis task.
Parameters
----------
config : tranche.Tranche
Configuration options
mpasClimatologyTask : ``MpasClimatologyTask``
The task that produced the climatology to be remapped and plotted
controlconfig : tranche.Tranche, optional
Configuration options for a control run (if any)
Authors
-------
Phillip J. Wolfram, Riley X. Brady, Xylar Asay-Davis
"""
# call the constructor from the base class (AnalysisTask)
bgcVars = config.getexpression('climatologyMapBGC', 'variables')
super(ClimatologyMapBGC, self).__init__(
config=config, taskName='climatologyMapBGC',
componentName='ocean',
tags=['climatology', 'horizontalMap', 'BGC'] + bgcVars)
sectionName = 'climatologyMapBGC'
# read in what seasons we want to plot
seasons = config.getexpression(sectionName, 'seasons')
if len(seasons) == 0:
raise ValueError('config section {} does not contain valid list '
'of seasons'.format(sectionName))
comparisonGridNames = config.getexpression(sectionName,
'comparisonGrids')
if len(comparisonGridNames) == 0:
raise ValueError('config section {} does not contain valid list '
'of comparison grids'.format(sectionName))
preindustrial = config.getboolean(sectionName, 'preindustrial')
obsFileDict = {
'Chl': 'Chl_SeaWIFS_20180702.nc',
'CO2_gas_flux': 'CO2_gas_flux_1.0x1.0degree_20180628.nc',
'SiO3': 'SiO3_1.0x1.0degree_20180628.nc',
'PO4': 'PO4_1.0x1.0degree_20180628.nc',
'ALK': 'ALK_1.0x1.0degree_20180629.nc',
'NO3': 'NO3_1.0x1.0degree_20180628.nc',
'pCO2surface': 'pCO2surface_1.0x1.0degree_20180629.nc',
'O2': 'O2_1.0x1.0degree_20180628.nc',
'pH_3D': 'pH_3D_1.0x1.0degree_20180629.nc'}
# If user wants to compare to preindustrial data, make sure
# that we load in the right DIC field.
if preindustrial:
obsFileDict['DIC'] = 'PI_DIC_1.0x1.0degree_20180629.nc'
else:
obsFileDict['DIC'] = 'DIC_1.0x1.0degree_20180629.nc'
for fieldName in bgcVars:
fieldSectionName = '{}_{}'.format(sectionName, fieldName)
prefix = config.get(fieldSectionName, 'filePrefix')
mpasFieldName = '{}{}'.format(prefix, fieldName)
# CO2 flux and pCO2 has no vertical levels, throws error if you try
# to select any. Can add any other flux-like variables to this
# list.
if fieldName not in ['CO2_gas_flux', 'pCO2surface']:
iselValues = {'nVertLevels': 0}
else:
iselValues = None
# Read in units (since BGC units are variable)
units = config.get(fieldSectionName, 'units')
# Pass multiple variables if working with Chlorophyll to sum
# them to total chlorophyll
if fieldName == 'Chl':
prefix = 'timeMonthly_avg_ecosysTracers_'
variableList = [prefix + 'spChl', prefix + 'diatChl',
prefix + 'diazChl', prefix + 'phaeoChl']
plotField = 'Chl'
else:
variableList = [mpasFieldName]
plotField = mpasFieldName
remapClimatologySubtask = RemapBGCClimatology(
mpasClimatologyTask=mpasClimatologyTask,
parentTask=self,
climatologyName=fieldName,
variableList=variableList,
comparisonGridNames=comparisonGridNames,
seasons=seasons,
iselValues=iselValues,
subtaskName='remapMpasClimatology_{}'.format(fieldName))
if controlConfig is None:
refTitleLabel = 'Observations'
if preindustrial and 'DIC' in fieldName:
refTitleLabel += ' (Preindustrial)'
observationsDirectory = build_obs_path(
config, 'ocean', '{}Subdirectory'.format(fieldName))
obsFileName = "{}/{}".format(observationsDirectory,
obsFileDict[fieldName])
observationsLabel = config.get(fieldSectionName,
'observationsLabel')
refFieldName = fieldName
outFileLabel = fieldName + observationsLabel
galleryLabel = config.get(fieldSectionName, 'galleryLabel')
galleryName = '{} (Compared to {})'.format(galleryLabel,
observationsLabel)
remapObservationsSubtask = RemapObservedBGCClimatology(
parentTask=self, seasons=seasons, fileName=obsFileName,
outFilePrefix=refFieldName,
comparisonGridNames=comparisonGridNames,
subtaskName='remapObservations_{}'.format(fieldName))
self.add_subtask(remapObservationsSubtask)
diffTitleLabel = 'Model - Observations'
# Certain BGC observations are only available at annual
# resolution. Need to ensure that the user is aware that their
# seasonal or monthly climatology is being compared to ANN.
# Currently, this is just with GLODAP.
if observationsLabel == 'GLODAPv2':
diffTitleLabel += ' (Compared to ANN)'
else:
remapObservationsSubtask = None
controlRunName = controlConfig.get('runs', 'mainRunName')
galleryName = None
refTitleLabel = 'Control: {}'.format(controlRunName)
refFieldName = plotField
outFileLabel = fieldName
diffTitleLabel = 'Main - Control'
for comparisonGridName in comparisonGridNames:
for season in seasons:
# make a new subtask for this season and comparison grid
subtask = PlotClimatologyMapSubtask(
self, season, comparisonGridName,
remapClimatologySubtask, remapObservationsSubtask,
controlConfig=controlConfig,
subtaskName='plot{}_{}_{}'.format(
fieldName, season, comparisonGridName))
subtask.set_plot_info(
outFileLabel=outFileLabel,
fieldNameInTitle=fieldName,
mpasFieldName=plotField,
refFieldName=refFieldName,
refTitleLabel=refTitleLabel,
unitsLabel=units,
imageCaption='Mean ' + fieldName,
galleryGroup='Sea Surface Biogeochemistry',
groupSubtitle=None,
groupLink=fieldName,
galleryName=galleryName,
diffTitleLabel=diffTitleLabel,
configSectionName=fieldSectionName)
self.add_subtask(subtask)
def setup_and_check(self):
"""
Check if preindustrial flag is turned on or off.
"""
# Authors
# -------
# Riley X. Brady
super(ClimatologyMapBGC, self).setup_and_check()
# Clarify that the user is doing preindustrial vs. modern
preindustrial = self.config.getboolean('climatologyMapBGC',
'preindustrial')
if preindustrial:
print("""
You are comparing against all available preindustrial
datasets. If this is not desired, set the preindustrial
flag to 'False' under the ClimatologyMapBGC config section.
""")
else:
print("""
You are comparing against modern observations. If you desire
a preindustrial comparison, set the preindustrial flag to
'True' under the ClimatologyMapBGC config section.
""")
class RemapBGCClimatology(RemapMpasClimatologySubtask):
"""
Apply unit conversions to native model output to align with observations.
"""
# Authors
# -------
# Riley X. Brady
def customize_masked_climatology(self, climatology, season):
"""
Sum over all phytoplankton chlorophyll to create total chlorophyll
Parameters
----------
climatology : ``xarray.Dataset`` object
the climatology data set
season : str
The name of the season to be masked
Returns
-------
climatology : ``xarray.Dataset`` object
the modified climatology data set
"""
# Authors
# -------
# Riley X. Brady
climatology = super(RemapBGCClimatology,
self).customize_masked_climatology(climatology,
season)
if 'timeMonthly_avg_ecosysTracers_spChl' in climatology:
spChl = climatology.timeMonthly_avg_ecosysTracers_spChl
diatChl = climatology.timeMonthly_avg_ecosysTracers_diatChl
diazChl = climatology.timeMonthly_avg_ecosysTracers_diazChl
phaeoChl = climatology.timeMonthly_avg_ecosysTracers_phaeoChl
climatology['Chl'] = spChl + diatChl + diazChl + phaeoChl
climatology.Chl.attrs['units'] = 'mg m$^{-3}$'
climatology.Chl.attrs['description'] = 'Sum of all PFT chlorophyll'
climatology.drop_vars(['timeMonthly_avg_ecosysTracers_spChl',
'timeMonthly_avg_ecosysTracers_diatChl',
'timeMonthly_avg_ecosysTracers_diazChl',
'timeMonthly_avg_ecosysTracers_phaeoChl'])
return climatology
def customize_remapped_climatology(self, climatology, comparisonGridName,
season):
"""
Convert CO2 gas flux from native units to mol/m2/yr,
Convert dissolved O2 from native units to mL/L
Parameters
----------
climatology : ``xarray.Dataset``
The MPAS climatology data set that has been remapped
comparisonGridNames : {'latlon', 'antarctic'}
The name of the comparison grid to use for remapping
season : str
The name of the season to be remapped
Returns
-------
climatology : ``xarray.Dataset``
The same data set with any custom fields added or modifications
made
"""
# Authors
# -------
# Riley X. Brady
fieldName = self.variableList[0]
# Convert CO2 gas flux from native mmol/m2 m/s to mol/m2/yr for
# comparison to the SOM-FFN product
if fieldName == 'timeMonthly_avg_CO2_gas_flux':
conversion = -1 * (60 * 60 * 24 * 365.25) / 10**3
climatology[fieldName] = conversion * climatology[fieldName]
# Convert O2 from mmol/m3 to mL/L for comparison to WOA product
elif fieldName == 'timeMonthly_avg_ecosysTracers_O2':
conversion = 22.391 / 10**3
climatology[fieldName] = conversion * climatology[fieldName]
return climatology
class RemapObservedBGCClimatology(RemapObservedClimatologySubtask):
"""
A subtask for reading and remapping BGC observations
"""
# Authors
# -------
# Riley X. Brady
def get_observation_descriptor(self, fileName):
"""
get a MeshDescriptor for the observation grid
Parameters
----------
fileName : str
observation file name describing the source grid
Returns
-------
obsDescriptor : ``MeshDescriptor``
The descriptor for the observation grid
"""
# Authors
# -------
# Riley X. Brady, Xylar Asay-Davis
# create a descriptor of the observation grid using the lat/lon
# coordinates
dsObs = self.build_observational_dataset(fileName)
obsDescriptor = LatLonGridDescriptor.read(ds=dsObs,
lat_var_name='lat',
lon_var_name='lon')
return obsDescriptor
def build_observational_dataset(self, fileName):
"""
read in the data sets for observations, and possibly rename some
variables and dimensions
Parameters
----------
fileName : str
observation file name
Returns
------
dsObs : ``xarray.Dataset``
The observational dataset
"""
# Authors
# -------
# Riley X. Brady, Xylar Asay-Davis
dsObs = xr.open_dataset(fileName)
degrees = 'degree' in dsObs.lon.units
dsObs = add_periodic_lon(ds=dsObs, lonDim='lon', degrees=degrees)
return dsObs