Skip to content

Commit c75a01b

Browse files
committed
Add validation with cropped output
1 parent 95e0ca2 commit c75a01b

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ def validate(self):
9999
compare_variables(test_case=self, variables=variables,
100100
filename1='initial_state/initial_state.nc')
101101

102+
if self.with_inactive_top_cells:
103+
variables = [
104+
'temperature', 'salinity', 'layerThickness', 'normalVelocity']
105+
compare_variables(test_case=self, variables=variables,
106+
filename1='initial_state/initial_state_crop.nc',
107+
filename2='initial_state/initial_state_comp.nc',
108+
quiet=False, check_outputs=False)
109+
102110
if self.mesh.with_ice_shelf_cavities:
103111
variables = ['ssh', 'landIcePressure']
104112
compare_variables(test_case=self, variables=variables,

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from compass.ocean.tests.global_ocean.metadata import (
88
add_mesh_and_init_metadata,
99
)
10+
from compass.ocean.inactive_top_cells import remove_inactive_top_cells_output
1011
from compass.ocean.vertical.grid_1d import generate_1d_grid, write_1d_grid
1112
from compass.step import Step
1213

@@ -139,6 +140,9 @@ def __init__(self, test_case, mesh, initial_condition,
139140
'graph.info']:
140141
self.add_output_file(filename=file)
141142

143+
if with_inactive_top_cells:
144+
self.add_output_file(filename='initial_state_crop.nc')
145+
142146
def setup(self):
143147
"""
144148
Get resources at setup from config options
@@ -175,6 +179,7 @@ def run(self):
175179
config.set('vertical_grid', 'vert_levels', f'{vert_levels + 1}',
176180
comment='the number of vertical levels + 1')
177181
config.set('vertical_grid', 'inactive_top_cells', '1')
182+
logger = self.logger
178183
interfaces = generate_1d_grid(config=config)
179184

180185
write_1d_grid(interfaces=interfaces, out_filename='vertical_grid.nc')
@@ -186,7 +191,6 @@ def run(self):
186191

187192
if self.with_inactive_top_cells:
188193

189-
logger = self.logger
190194
logger.info(" * Updating minLevelCell for inactive top cells")
191195

192196
in_filename = 'initial_state.nc'
@@ -200,11 +204,18 @@ def run(self):
200204
ds = ds.isel(Time=0)
201205

202206
if ('minLevelCell' in ds):
203-
minLevelCell = ds.minLevelCell+1
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
204212
ds_out['minLevelCell'] = minLevelCell
205213
else:
206214
logger.info(" - Streams missing for inactive top cells")
207215

216+
remove_inactive_top_cells_output(in_filename,
217+
inactive_top_cells=offset)
218+
208219
write_netcdf(ds_out, out_filename)
209220
logger.info(" - Complete")
210221

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import os
12
from compass.ocean.tests.global_ocean.forward import (
23
ForwardStep,
34
ForwardTestCase,
45
)
56
from compass.validate import compare_timers, compare_variables
7+
from compass.ocean.inactive_top_cells import remove_inactive_top_cells_output
68

79

810
class PerformanceTest(ForwardTestCase):
@@ -71,6 +73,18 @@ def validate(self):
7173
compare_variables(test_case=self, variables=variables,
7274
filename1=f'{step_subdir}/output.nc')
7375

76+
if self.config.has_option('vertical_grid', 'inactive_top_cells'):
77+
offset = self.config.getint('vertical_grid', 'inactive_top_cells')
78+
if offset > 0:
79+
remove_inactive_top_cells_output('forward/output.nc',
80+
inactive_top_cells=offset)
81+
filename2=None
82+
if os.path.exists('forward/output_comp.nc'):
83+
filename2='forward/output_comp.nc'
84+
compare_variables(test_case=self, variables=variables,
85+
filename1='forward/output_crop.nc',
86+
filename2=filename2)
87+
7488
if self.mesh.with_ice_shelf_cavities:
7589
variables = [
7690
'ssh', 'landIcePressure', 'landIceDraft',

0 commit comments

Comments
 (0)