Skip to content

Commit 98061df

Browse files
xylarcbegeman
authored andcommitted
Add mesh and out filename when cropping inactive top
Use `minLevelCell` instead of an argument to determine the number of inactive top levels.
1 parent c75a01b commit 98061df

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

compass/ocean/inactive_top_cells.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from mpas_tools.io import write_netcdf
55

66

7-
def remove_inactive_top_cells_output(in_filename, inactive_top_cells=1):
7+
def remove_inactive_top_cells_output(in_filename, out_filename=None,
8+
mesh_filename=None):
89
"""
910
Remove inactive top cells from the output netCDF file
1011
@@ -13,16 +14,32 @@ def remove_inactive_top_cells_output(in_filename, inactive_top_cells=1):
1314
in_filename : str
1415
Filename for the netCDF file to be cropped
1516
16-
inactive_top_cells : str, optional
17-
The number of inactive top cell layers. It should be equal to
18-
``config.inactive_top_cells``.
17+
out_filename : str, optional
18+
Filename for the netCDF file after cropping. Tbe default name is the
19+
original file name with ``_crop`` appended before the extension
20+
21+
mesh_filename : str, optional
22+
Filename for an MPAS mesh if not included in the file to be cropped
23+
1924
"""
2025
if not os.path.exists(in_filename):
21-
raise OSError('File {} does not exist.'.format(in_filename))
26+
raise OSError(f'File {in_filename} does not exist.')
2227

23-
out_filename = in_filename.split('.')[0] + '_crop.nc'
28+
if out_filename is None:
29+
basename, ext = os.path.splitext(in_filename)
30+
out_filename = f'{basename}_crop{ext}'
2431

2532
with xarray.open_dataset(in_filename) as ds_in:
26-
ds_out = ds_in.isel(nVertLevels=slice(1, None))
33+
if mesh_filename is None:
34+
ds_mesh = ds_in
35+
else:
36+
ds_mesh = xarray.open_dataset(mesh_filename)
37+
minLevelCell = ds_mesh.minLevelCell
38+
minval = minLevelCell.min().values
39+
maxval = minLevelCell.max().values
40+
if minval != maxval:
41+
raise ValueError('Expected minLevelCell to have a constant '
42+
'value for inactive top cell tests')
43+
ds_out = ds_in.isel(nVertLevels=slice(minval-1, None))
2744

2845
write_netcdf(ds_out, out_filename)

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,24 @@ def validate(self):
100100
filename1='initial_state/initial_state.nc')
101101

102102
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)
103+
# construct the work directory for the other test
104+
filename2 = os.path.join(self.base_work_dir, self.mpas_core.name,
105+
self.test_group.name,
106+
self.inactive_top_comp_subdir,
107+
'init/initial_state/initial_state.nc')
108+
if os.path.exists(filename2):
109+
variables = ['temperature', 'salinity', 'layerThickness',
110+
'normalVelocity']
111+
compare_variables(test_case=self, variables=variables,
112+
filename1='initial_state/initial_state_crop.nc'
113+
filename2=filename2,
114+
quiet=False, check_outputs=False,
115+
skip_if_step_not_run=False)
116+
117+
else:
118+
self.logger.warn('The version of "init" without inactive top '
119+
'cells was not run. Skipping\n'
120+
'validation.')
109121

110122
if self.mesh.with_ice_shelf_cavities:
111123
variables = ['ssh', 'landIcePressure']

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,21 @@ def validate(self):
7373
compare_variables(test_case=self, variables=variables,
7474
filename1=f'{step_subdir}/output.nc')
7575

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'
76+
if self.init.with_inactive_top_cells:
77+
# construct the work directory for the other test
78+
subdir = get_forward_subdir(self.init.inactive_top_comp_subdir,
79+
self.time_integrator, self.name)
80+
filename2 = os.path.join(self.base_work_dir, self.mpas_core.name,
81+
self.test_group.name, subdir,
82+
'forward/output.nc')
83+
if os.path.exists(filename2):
8484
compare_variables(test_case=self, variables=variables,
8585
filename1='forward/output_crop.nc',
8686
filename2=filename2)
87+
else:
88+
self.logger.warn('The version of "performance_test" without '
89+
'inactive top cells was not run.\n'
90+
'Skipping validation.')
8791

8892
if self.mesh.with_ice_shelf_cavities:
8993
variables = [

0 commit comments

Comments
 (0)