Skip to content

Commit ba3b04a

Browse files
xylarcbegeman
authored andcommitted
Update the init test case
Store the `inactive_top_comp_subdir` so we know what files to compare with during validation. Use `ds.load()` when modifying `minLevelCell` in the initial state to prevent issues with reading from and writing to the same file. Crop inactive top cells after writing out the modified initial state (because we now use the modified `minLevelCell`). Set the default to `inactive_top_cells = 0` in a new `init.cfg` instead of the config file for the QU240 mesh.
1 parent 98061df commit ba3b04a

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

compass/ocean/tests/global_ocean/init/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ class Init(TestCase):
2424
init_subdir : str
2525
The subdirectory within the test group for all test cases with this
2626
initial condition
27+
28+
inactive_top_comp_subdir : str
29+
If ``with_inactive_top_cells == True``, the subdirectory equivalent to
30+
``init_subdir`` for test cases without inactive top cells for use for
31+
validation between tests with and without inactive top cells
2732
"""
2833
def __init__(self, test_group, mesh, initial_condition,
2934
with_inactive_top_cells=False):
@@ -46,6 +51,9 @@ def __init__(self, test_group, mesh, initial_condition,
4651
ic_dir = initial_condition
4752
init_subdir = os.path.join(mesh_name, ic_dir)
4853
if with_inactive_top_cells:
54+
# Add the name of the subdir without inactive top cells whether or
55+
# not is has or will be run
56+
self.inactive_top_comp_subdir = init_subdir
4957
init_subdir = os.path.join(init_subdir, inactive_top)
5058
self.init_subdir = init_subdir
5159
subdir = os.path.join(self.init_subdir, name)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Options related to the vertical grid
2+
[vertical_grid]
3+
4+
# no inactive top cells by default
5+
inactive_top_cells = 0

compass/ocean/tests/global_ocean/init/initial_state.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import os
22
from importlib.resources import contents
3+
34
import xarray
5+
from mpas_tools.io import write_netcdf
46

57
from compass.model import run_model
8+
from compass.ocean.inactive_top_cells import remove_inactive_top_cells_output
69
from compass.ocean.plot import plot_initial_state, plot_vertical_grid
710
from compass.ocean.tests.global_ocean.metadata import (
811
add_mesh_and_init_metadata,
912
)
10-
from compass.ocean.inactive_top_cells import remove_inactive_top_cells_output
1113
from compass.ocean.vertical.grid_1d import generate_1d_grid, write_1d_grid
1214
from compass.step import Step
1315

14-
from mpas_tools.io import write_netcdf
15-
1616

1717
class InitialState(Step):
1818
"""
@@ -27,7 +27,7 @@ class InitialState(Step):
2727
initial_condition : {'WOA23', 'PHC', 'EN4_1900'}
2828
The initial condition dataset to use
2929
"""
30-
def __init__(self, test_case, mesh, initial_condition,
30+
def __init__(self, test_case, mesh, initial_condition,
3131
with_inactive_top_cells):
3232
"""
3333
Create the step
@@ -188,7 +188,7 @@ def run(self):
188188

189189
update_pio = config.getboolean('global_ocean', 'init_update_pio')
190190
run_model(self, update_pio=update_pio)
191-
191+
192192
if self.with_inactive_top_cells:
193193

194194
logger.info(" * Updating minLevelCell for inactive top cells")
@@ -197,26 +197,32 @@ def run(self):
197197
out_filename = in_filename
198198

199199
with xarray.open_dataset(in_filename) as ds:
200+
ds.load()
200201

201202
# keep the data set with Time for output
202203
ds_out = ds
203204

204205
ds = ds.isel(Time=0)
205206

206-
if ('minLevelCell' in ds):
207-
if config.has_option('vertical_grid',
208-
'inactive_top_cells'):
209-
offset = config.getint('vertical_grid',
210-
'inactive_top_cells')
211-
minLevelCell = ds.minLevelCell+offset
212-
ds_out['minLevelCell'] = minLevelCell
207+
if config.has_option('vertical_grid', 'inactive_top_cells'):
208+
offset = config.getint('vertical_grid',
209+
'inactive_top_cells')
213210
else:
214-
logger.info(" - Streams missing for inactive top cells")
211+
offset = 0
215212

216-
remove_inactive_top_cells_output(in_filename,
217-
inactive_top_cells=offset)
213+
if 'minLevelCell' in ds:
214+
minLevelCell = ds.minLevelCell + offset
215+
ds_out['minLevelCell'] = minLevelCell
216+
else:
217+
logger.info(" - Variable minLevelCell, needed for "
218+
"inactive top cells, is missing from the "
219+
"initial condition")
218220

219221
write_netcdf(ds_out, out_filename)
222+
223+
remove_inactive_top_cells_output(
224+
in_filename=in_filename, out_filename='initial_state_crop.nc')
225+
220226
logger.info(" - Complete")
221227

222228
add_mesh_and_init_metadata(self.outputs, config,

0 commit comments

Comments
 (0)