Skip to content

Commit 14a9870

Browse files
committed
Store cp and rho for computing OHC anomalies
We don't want to rely on retrieving them from namelists at runtime because the `self.namelist` object may not exist. The mesh filename also needs to be stored since it isn't necessarily available from the streams object at runtime.
1 parent 2a52eb2 commit 14a9870

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

mpas_analysis/ocean/climatology_map_ohc_anomaly.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ class RemapMpasOHCClimatology(RemapMpasClimatologySubtask):
182182
183183
min_depth, max_depth : float
184184
The minimum and maximum depths for integration
185+
186+
cp : float
187+
Specific heat of seawater [J/(kg*degC)]
188+
189+
rho : float
190+
Reference density of seawater [kg/m3]
185191
"""
186192

187193
def __init__(self, mpas_climatology_task, ref_year_climatology_task,
@@ -239,6 +245,8 @@ def __init__(self, mpas_climatology_task, ref_year_climatology_task,
239245
self.run_after(ref_year_climatology_task)
240246
self.min_depth = min_depth
241247
self.max_depth = max_depth
248+
self.cp = None
249+
self.rho = None
242250

243251
def setup_and_check(self):
244252
"""
@@ -255,6 +263,9 @@ def setup_and_check(self):
255263
self.ref_year_climatology_task.add_variables(self.variableList,
256264
self.seasons)
257265

266+
self.cp = self.namelist.getfloat('config_specific_heat_sea_water')
267+
self.rho = self.namelist.getfloat('config_density0')
268+
258269
def customize_masked_climatology(self, climatology, season):
259270
"""
260271
Compute the ocean heat content (OHC) anomaly from the temperature
@@ -298,10 +309,10 @@ def _compute_ohc(self, climatology):
298309
ds_mesh = xr.open_dataset(self.meshFilename)
299310
ds_mesh = ds_mesh.isel(Time=0)
300311

301-
# specific heat [J/(kg*degC)]
302-
cp = self.namelist.getfloat('config_specific_heat_sea_water')
303-
# [kg/m3]
304-
rho = self.namelist.getfloat('config_density0')
312+
cp = self.cp
313+
assert cp is not None, "Specific heat 'cp' has not been set"
314+
rho = self.rho
315+
assert rho is not None, "Reference density 'rho' has not been set"
305316

306317
units_scale_factor = 1e-9
307318

mpas_analysis/ocean/time_series_ohc_anomaly.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@
3131
class TimeSeriesOHCAnomaly(AnalysisTask):
3232
"""
3333
Performs analysis of ocean heat content (OHC) from time-series output.
34+
35+
Attributes
36+
----------
37+
cp : float
38+
Specific heat of seawater [J/(kg*degC)]
39+
40+
rho : float
41+
Reference density of seawater [kg/m3]
42+
43+
meshFilename : str
44+
The path to the MPAS mesh file
3445
"""
3546
# Authors
3647
# -------
@@ -132,17 +143,37 @@ def __init__(self, config, mpasTimeSeriesTask, controlConfig=None):
132143
plotTask.run_after(anomalyTask)
133144
self.add_subtask(plotTask)
134145

146+
self.cp = None
147+
self.rho = None
148+
self.meshFilename = None
149+
150+
def setup_and_check(self):
151+
"""
152+
Store the specific heat and reference density of seawater for use
153+
in OHC calculations.
154+
"""
155+
super().setup_and_check()
156+
157+
self.cp = self.namelist.getfloat('config_specific_heat_sea_water')
158+
self.rho = self.namelist.getfloat('config_density0')
159+
self.meshFilename = self.get_mesh_filename()
160+
135161
def _compute_ohc(self, ds):
136162
"""
137163
Compute the OHC time series.
138164
"""
139165
# for convenience, rename the variables to simpler, shorter names
140166
ds = ds.rename(self.variableDict)
141167

142-
# specific heat [J/(kg*degC)]
143-
cp = self.namelist.getfloat('config_specific_heat_sea_water')
144-
# [kg/m3]
145-
rho = self.namelist.getfloat('config_density0')
168+
# these need to be set at setup time, not at runtime because piclking
169+
# means the namelists and streams objects they come from aren't
170+
# available at runtime
171+
cp = self.cp
172+
assert cp is not None, "Specific heat 'cp' has not been set"
173+
rho = self.rho
174+
assert rho is not None, "Reference density 'rho' has not been set"
175+
meshFile = self.meshFilename
176+
assert meshFile is not None, "Mesh filename has not been set"
146177

147178
unitsScalefactor = 1e-22
148179

@@ -152,8 +183,6 @@ def _compute_ohc(self, ds):
152183
ds.ohc.attrs['units'] = '$10^{22}$ J'
153184
ds.ohc.attrs['description'] = 'Ocean heat content in each region'
154185

155-
meshFile = self.get_mesh_filename()
156-
157186
# Define/read in general variables
158187
with xr.open_dataset(meshFile) as dsMesh:
159188
# reference depth [m]

0 commit comments

Comments
 (0)