Skip to content

Commit 83a2384

Browse files
committed
Add validation with cropped output: Init
1 parent 7c658f4 commit 83a2384

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
import xarray
3+
4+
from mpas_tools.io import write_netcdf
5+
6+
7+
def remove_inactive_top_cells_output(in_filename, inactive_top_cells=1):
8+
"""
9+
Remove inactive top cells from the output netCDF file
10+
11+
Parameters
12+
----------
13+
in_filename : str
14+
Filename for the netCDF file to be cropped
15+
16+
inactive_top_cells : str, optional
17+
The number of inactive top cell layers. It should be equal to
18+
``config.inactive_top_cells``.
19+
"""
20+
if not os.path.exists(in_filename):
21+
raise OSError('File {} does not exist.'.format(in_filename))
22+
23+
out_filename = in_filename.split('.')[0] + '_crop.nc'
24+
25+
with xarray.open_dataset(in_filename) as ds_in:
26+
ds_out = ds_in.isel(nVertLevels=slice(1,None))
27+
28+
write_netcdf(ds_out, out_filename)

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ def validate(self):
121121
compare_variables(test_case=self, variables=variables,
122122
filename1='initial_state/initial_state.nc')
123123

124+
if self.with_inactive_top_cells:
125+
variables = [
126+
'temperature', 'salinity', 'layerThickness', 'normalVelocity']
127+
compare_variables(test_case=self, variables=variables,
128+
filename1='initial_state/initial_state_crop.nc')
129+
124130
if self.mesh.with_ice_shelf_cavities:
125131
variables = ['ssh', 'landIcePressure']
126132
compare_variables(test_case=self, variables=variables,

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from compass.ocean.tests.global_ocean.metadata import \
44
add_mesh_and_init_metadata
55
from compass.model import run_model
6+
from compass.ocean.inactive_top_cells import remove_inactive_top_cells_output
67
from compass.ocean.vertical.grid_1d import generate_1d_grid, write_1d_grid
78
from compass.ocean.plot import plot_vertical_grid, plot_initial_state
89
from mpas_tools.io import write_netcdf
@@ -141,6 +142,9 @@ def __init__(self, test_case, mesh, initial_condition, with_bgc,
141142
for file in ['initial_state.nc', 'init_mode_forcing_data.nc']:
142143
self.add_output_file(filename=file)
143144

145+
if with_inactive_top_cells:
146+
self.add_output_file(filename='initial_state_crop.nc')
147+
144148
def setup(self):
145149
"""
146150
Set up the test case in the work directory, including downloading any
@@ -157,6 +161,7 @@ def run(self):
157161
Run this step of the testcase
158162
"""
159163
config = self.config
164+
logger = self.logger
160165
interfaces = generate_1d_grid(config=config)
161166

162167
write_1d_grid(interfaces=interfaces, out_filename='vertical_grid.nc')
@@ -167,7 +172,6 @@ def run(self):
167172

168173
if self.with_inactive_top_cells:
169174

170-
logger = self.logger
171175
logger.info(" * Updating minLevelCell for inactive top cells")
172176

173177
in_filename = 'initial_state.nc'
@@ -181,10 +185,14 @@ def run(self):
181185
ds = ds.isel(Time=0)
182186

183187
if ('minLevelCell' in ds):
184-
minLevelCell = ds.minLevelCell+1
188+
if config.has_option('vertical_grid', 'inactive_top_cells'):
189+
offset = config.getint('vertical_grid', 'inactive_top_cells')
190+
minLevelCell = ds.minLevelCell+offset
185191
ds_out['minLevelCell'] = minLevelCell
186192
else:
187193
logger.info(" - Streams missing for inactive top cells")
194+
195+
remove_inactive_top_cells_output(in_filename, inactive_top_cells=offset)
188196

189197
write_netcdf(ds_out, out_filename)
190198
logger.info(" - Complete")

0 commit comments

Comments
 (0)