Skip to content

Commit d9c2e87

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.
1 parent 2a52eb2 commit d9c2e87

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
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: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
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]
3442
"""
3543
# Authors
3644
# -------
@@ -132,17 +140,30 @@ def __init__(self, config, mpasTimeSeriesTask, controlConfig=None):
132140
plotTask.run_after(anomalyTask)
133141
self.add_subtask(plotTask)
134142

143+
self.cp = None
144+
self.rho = None
145+
146+
def setup_and_check(self):
147+
"""
148+
Store the specific heat and reference density of seawater for use
149+
in OHC calculations.
150+
"""
151+
super().setup_and_check()
152+
153+
self.cp = self.namelist.getfloat('config_specific_heat_sea_water')
154+
self.rho = self.namelist.getfloat('config_density0')
155+
135156
def _compute_ohc(self, ds):
136157
"""
137158
Compute the OHC time series.
138159
"""
139160
# for convenience, rename the variables to simpler, shorter names
140161
ds = ds.rename(self.variableDict)
141162

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')
163+
cp = self.cp
164+
assert cp is not None, "Specific heat 'cp' has not been set"
165+
rho = self.rho
166+
assert rho is not None, "Reference density 'rho' has not been set"
146167

147168
unitsScalefactor = 1e-22
148169

0 commit comments

Comments
 (0)