Skip to content

Commit 9d0fe85

Browse files
authored
Merge pull request #636 from xylar/update-unit-tests
Move unit tests out of `mpas_tools`
2 parents c3d49ec + 1ef9529 commit 9d0fe85

12 files changed

Lines changed: 315 additions & 202 deletions

conda_package/docs/api.rst

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -392,13 +392,3 @@ Visualization
392392
:toctree: generated/
393393

394394
register_sci_viz_colormaps
395-
396-
Tests
397-
=====
398-
399-
.. currentmodule:: mpas_tools.tests.test_cime_constants
400-
401-
.. autosummary::
402-
:toctree: generated/
403-
404-
test_cime_constants

conda_package/docs/cime.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ sync with `CIME <https://github.com/ESMCI/cime>`_, which provides infrastructure
88
and utilities for Earth System Models such at E3SM. Currently, we sync only
99
those constants given numerical values in CIME, not those that are derivied
1010
from other constants. Constants are checked against their values on CIME's
11-
master branch during tests of the conda build. See
12-
:py:func:`mpas_tools.tests.test_cime_constants.test_cime_constants`.
11+
master branch during tests of the conda build.
1312

1413
Some of the constants most likely to be useful in MPAS-Tools, COMPASS and other
1514
related projects are:

conda_package/recipe/meta.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ test:
6565
- mesh_tools/mesh_conversion_tools/test/Arctic_Ocean.geojson
6666
- mesh_tools/mesh_conversion_tools/test/mesh.QU.1920km.151026.nc
6767
- mesh_tools/mesh_conversion_tools/test/land_mask_final.nc
68-
- conda_package/mpas_tools/tests/*
68+
- conda_package/tests/*
6969
imports:
7070
- mpas_tools
7171
- mpas_tools.mesh.conversion
@@ -87,7 +87,7 @@ test:
8787
- MpasMaskCreator.x mesh.nc arctic_mask.nc -f mesh_tools/mesh_conversion_tools/test/Arctic_Ocean.geojson
8888
- planar_hex --nx=30 --ny=20 --dc=1000. --npx --npy --outFileName='nonperiodic_mesh_30x20_1km.nc'
8989
- MpasCellCuller.x nonperiodic_mesh_30x20_1km.nc culled_nonperiodic_mesh_30x20_1km.nc
90-
- python -m pytest conda_package/mpas_tools/tests
90+
- python -m pytest conda_package/tests
9191
- mark_horns_for_culling --help
9292
- set_lat_lon_fields_in_planar_grid --help
9393
- create_scrip_file_from_mpas_mesh --help
File renamed without changes.

conda_package/mpas_tools/tests/test_cime_constants.py renamed to conda_package/tests/test_cime_constants.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from mpas_tools.cime.constants import constants
21
import requests
32

3+
from mpas_tools.cime.constants import constants
4+
45

56
def test_cime_constants(e3sm_tag='master'):
67
"""
@@ -14,7 +15,8 @@ def test_cime_constants(e3sm_tag='master'):
1415

1516
resp = requests.get(
1617
f'https://raw.githubusercontent.com/E3SM-Project/E3SM/{e3sm_tag}/'
17-
f'share/util/shr_const_mod.F90')
18+
f'share/util/shr_const_mod.F90'
19+
)
1820

1921
text = resp.text
2022

@@ -32,10 +34,10 @@ def test_cime_constants(e3sm_tag='master'):
3234
print(f'parsed: {constant} = {value}')
3335
if constant in constants:
3436
if isinstance(value, float):
35-
print('verifying {}'.format(constant))
37+
print(f'verifying {constant}')
3638
assert value == constants[constant]
3739
else:
38-
print('skipping verification for {}'.format(constant))
40+
print(f'skipping verification for {constant}')
3941

4042
found[constant] = True
4143
else:
@@ -46,7 +48,7 @@ def test_cime_constants(e3sm_tag='master'):
4648
all_found = True
4749
for constant in found:
4850
if not found[constant]:
49-
print('{} was not found!'.format(constant))
51+
print(f'{constant} was not found!')
5052
all_found = False
5153

5254
assert all_found
@@ -60,7 +62,7 @@ def _parse_value(line):
6062
end = line.find('=')
6163

6264
key = line[start:end]
63-
line = line[end+1:]
65+
line = line[end + 1 :]
6466

6567
if '!' in line:
6668
line, _ = line.split('!', 1)

conda_package/mpas_tools/tests/test_conversion.py renamed to conda_package/tests/test_conversion.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
#!/usr/bin/env python
22

3-
from mpas_tools.mesh.conversion import convert, cull, mask
4-
from mpas_tools.io import write_netcdf
53
import matplotlib
4+
5+
from mpas_tools.io import write_netcdf
6+
from mpas_tools.mesh.conversion import convert, cull, mask
7+
8+
from .util import get_test_data_file
9+
610
matplotlib.use('Agg')
7-
from geometric_features import read_feature_collection
811
import xarray
12+
from geometric_features import read_feature_collection
913

1014

1115
def test_conversion():
1216
dsMesh = xarray.open_dataset(
13-
'mesh_tools/mesh_conversion_tools/test/mesh.QU.1920km.151026.nc')
17+
get_test_data_file('mesh.QU.1920km.151026.nc')
18+
)
1419
dsMesh = convert(dsIn=dsMesh)
1520
write_netcdf(dsMesh, 'mesh.nc')
1621

17-
dsMask = xarray.open_dataset(
18-
'mesh_tools/mesh_conversion_tools/test/land_mask_final.nc')
22+
dsMask = xarray.open_dataset(get_test_data_file('land_mask_final.nc'))
1923
dsCulled = cull(dsIn=dsMesh, dsMask=dsMask)
2024
write_netcdf(dsCulled, 'culled_mesh.nc')
2125

2226
fcMask = read_feature_collection(
23-
'mesh_tools/mesh_conversion_tools/test/Arctic_Ocean.geojson')
27+
get_test_data_file('Arctic_Ocean.geojson')
28+
)
2429
dsMask = mask(dsMesh=dsMesh, fcMask=fcMask)
2530
write_netcdf(dsMask, 'antarctic_mask.nc')
2631

conda_package/mpas_tools/tests/test_depth.py renamed to conda_package/tests/test_depth.py

Lines changed: 83 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
#!/usr/bin/env python
2+
import os
3+
24
import numpy
35
import xarray
4-
import os
56

67
from mpas_tools.io import write_netcdf
7-
from mpas_tools.ocean.depth import compute_depth, compute_zmid, add_depth, \
8-
add_zmid, write_time_varying_zmid
8+
from mpas_tools.ocean.depth import (
9+
add_depth,
10+
add_zmid,
11+
compute_depth,
12+
compute_zmid,
13+
write_time_varying_zmid,
14+
)
15+
16+
from .util import get_test_data_file
917

1018

1119
def create_3d_mesh():
@@ -14,19 +22,25 @@ def create_3d_mesh():
1422
dsMesh = xarray.open_dataset(outFileName)
1523
else:
1624
dsMesh = xarray.open_dataset(
17-
'mesh_tools/mesh_conversion_tools/test/mesh.QU.1920km.151026.nc')
25+
get_test_data_file('mesh.QU.1920km.151026.nc')
26+
)
1827
nCells = dsMesh.sizes['nCells']
1928
nVertLevels = 10
20-
zmax = 1000.
21-
layerThickness = zmax/nVertLevels
22-
dsMesh['refBottomDepth'] = \
23-
('nVertLevels', numpy.linspace(layerThickness, zmax, nVertLevels))
24-
dsMesh['maxLevelCell'] = \
25-
('nCells', nVertLevels*numpy.ones(nCells, dtype=int))
26-
dsMesh['bottomDepth'] = ('nCells', zmax*numpy.ones(nCells))
27-
dsMesh['layerThickness'] = \
28-
(('Time', 'nCells', 'nVertLevels'),
29-
layerThickness*numpy.ones((1, nCells, nVertLevels)))
29+
zmax = 1000.0
30+
layerThickness = zmax / nVertLevels
31+
dsMesh['refBottomDepth'] = (
32+
'nVertLevels',
33+
numpy.linspace(layerThickness, zmax, nVertLevels),
34+
)
35+
dsMesh['maxLevelCell'] = (
36+
'nCells',
37+
nVertLevels * numpy.ones(nCells, dtype=int),
38+
)
39+
dsMesh['bottomDepth'] = ('nCells', zmax * numpy.ones(nCells))
40+
dsMesh['layerThickness'] = (
41+
('Time', 'nCells', 'nVertLevels'),
42+
layerThickness * numpy.ones((1, nCells, nVertLevels)),
43+
)
3044
write_netcdf(dsMesh, 'test_depth_mesh.nc')
3145

3246
return dsMesh
@@ -35,23 +49,30 @@ def create_3d_mesh():
3549
def test_compute_depth():
3650
dsMesh = create_3d_mesh()
3751
depth, depth_bnds = compute_depth(dsMesh.refBottomDepth)
38-
assert numpy.all(numpy.isclose(depth, numpy.linspace(50., 950., 10)))
39-
assert numpy.all(numpy.isclose(depth_bnds[:, 0],
40-
numpy.linspace(0., 900., 10)))
41-
assert numpy.all(numpy.isclose(depth_bnds[:, 1],
42-
numpy.linspace(100., 1000., 10)))
52+
assert numpy.all(numpy.isclose(depth, numpy.linspace(50.0, 950.0, 10)))
53+
assert numpy.all(
54+
numpy.isclose(depth_bnds[:, 0], numpy.linspace(0.0, 900.0, 10))
55+
)
56+
assert numpy.all(
57+
numpy.isclose(depth_bnds[:, 1], numpy.linspace(100.0, 1000.0, 10))
58+
)
4359

4460

4561
def test_compute_zmid():
4662
dsMesh = create_3d_mesh()
47-
zMid = compute_zmid(dsMesh.bottomDepth, dsMesh.maxLevelCell,
48-
dsMesh.layerThickness, depth_dim='nVertLevels')
63+
zMid = compute_zmid(
64+
dsMesh.bottomDepth,
65+
dsMesh.maxLevelCell,
66+
dsMesh.layerThickness,
67+
depth_dim='nVertLevels',
68+
)
4969

5070
assert zMid.dims == ('Time', 'nCells', 'nVertLevels')
5171

5272
depth = zMid.isel(Time=0, nCells=0)
53-
assert numpy.all(numpy.isclose(depth.values,
54-
numpy.linspace(-50., -950., 10)))
73+
assert numpy.all(
74+
numpy.isclose(depth.values, numpy.linspace(-50.0, -950.0, 10))
75+
)
5576

5677

5778
def test_add_depth():
@@ -65,15 +86,18 @@ def test_add_depth():
6586

6687
# test adding depth coordinate once to the mesh and once to the input file,
6788
# with the mesh passed in separately
68-
for in_filename, coord_filename in [(mesh_filename, None),
69-
('test_depth_in.nc', mesh_filename)]:
89+
for in_filename, coord_filename in [
90+
(mesh_filename, None),
91+
('test_depth_in.nc', mesh_filename),
92+
]:
7093
add_depth(in_filename, out_filename, coordFileName=coord_filename)
7194
dsOut = xarray.open_dataset(out_filename)
7295
assert 'depth' in dsOut.dims
7396

7497
depth = dsOut.depth
75-
assert numpy.all(numpy.isclose(depth.values,
76-
numpy.linspace(50., 950., 10)))
98+
assert numpy.all(
99+
numpy.isclose(depth.values, numpy.linspace(50.0, 950.0, 10))
100+
)
77101

78102

79103
def test_add_zmid():
@@ -87,8 +111,10 @@ def test_add_zmid():
87111

88112
# test adding zMid once to the mesh and once to the input file, with the
89113
# mesh passed in separately
90-
for in_filename, coord_filename in [(mesh_filename, None),
91-
('test_depth_in.nc', mesh_filename)]:
114+
for in_filename, coord_filename in [
115+
(mesh_filename, None),
116+
('test_depth_in.nc', mesh_filename),
117+
]:
92118
add_zmid(in_filename, out_filename, coordFileName=coord_filename)
93119
dsOut = xarray.open_dataset(out_filename)
94120
assert 'depth' in dsOut.dims
@@ -97,54 +123,63 @@ def test_add_zmid():
97123
assert zMid.dims == ('nCells', 'depth')
98124

99125
depth = zMid.isel(nCells=0)
100-
assert numpy.all(numpy.isclose(depth.values,
101-
numpy.linspace(-50., -950., 10)))
126+
assert numpy.all(
127+
numpy.isclose(depth.values, numpy.linspace(-50.0, -950.0, 10))
128+
)
102129

103130

104131
def test_write_time_varying_zmid():
105-
106132
dsMesh = create_3d_mesh()
107133
nCells = dsMesh.sizes['nCells']
108134
nVertLevels = dsMesh.sizes['nVertLevels']
109135
mesh_filename = 'test_depth_mesh.nc'
110136
in_filename = 'test_depth_in.nc'
111137
out_filename = 'test_depth_out.nc'
112138

113-
layerThickness = 100.
139+
layerThickness = 100.0
114140

115141
# test adding zMid once to the mesh and once to the input file, with the
116142
# mesh passed in separately, each one without and once with a prefix
117-
for coord_filename, prefix in [(None, ''),
118-
(mesh_filename, ''),
119-
(None, 'timeMonthly_avg_'),
120-
(mesh_filename, 'timeMonthly_avg_')]:
143+
for coord_filename, prefix in [
144+
(None, ''),
145+
(mesh_filename, ''),
146+
(None, 'timeMonthly_avg_'),
147+
(mesh_filename, 'timeMonthly_avg_'),
148+
]:
121149
print(coord_filename, prefix)
122150

123151
if coord_filename is None:
124152
dsIn = dsMesh.drop_vars('layerThickness')
125153
else:
126154
dsIn = xarray.Dataset()
127-
layerThicknessVar = '{}layerThickness'.format(prefix)
128-
dsIn[layerThicknessVar] = \
129-
(('Time', 'nCells', 'nVertLevels'),
130-
layerThickness*numpy.ones((2, nCells, nVertLevels)))
131-
dsIn['{}temperature'.format(prefix)] = \
132-
xarray.ones_like(dsIn[layerThicknessVar])
155+
layerThicknessVar = f'{prefix}layerThickness'
156+
dsIn[layerThicknessVar] = (
157+
('Time', 'nCells', 'nVertLevels'),
158+
layerThickness * numpy.ones((2, nCells, nVertLevels)),
159+
)
160+
dsIn[f'{prefix}temperature'] = xarray.ones_like(
161+
dsIn[layerThicknessVar]
162+
)
133163
write_netcdf(dsIn, in_filename)
134164
dsIn.close()
135165

136-
write_time_varying_zmid(in_filename, out_filename,
137-
coordFileName=coord_filename, prefix=prefix)
166+
write_time_varying_zmid(
167+
in_filename,
168+
out_filename,
169+
coordFileName=coord_filename,
170+
prefix=prefix,
171+
)
138172

139173
dsOut = xarray.open_dataset(out_filename)
140174
assert 'depth' in dsOut.dims
141175

142-
zMid = dsOut['{}zMid'.format(prefix)]
176+
zMid = dsOut[f'{prefix}zMid']
143177
assert zMid.dims == ('Time', 'nCells', 'depth')
144178

145179
depth = zMid.isel(Time=0, nCells=0)
146-
assert numpy.all(numpy.isclose(depth.values,
147-
numpy.linspace(-50., -950., 10)))
180+
assert numpy.all(
181+
numpy.isclose(depth.values, numpy.linspace(-50.0, -950.0, 10))
182+
)
148183
dsOut.close()
149184

150185

0 commit comments

Comments
 (0)