Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions conda_package/docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,3 @@ Visualization
:toctree: generated/

register_sci_viz_colormaps

Tests
=====

.. currentmodule:: mpas_tools.tests.test_cime_constants

.. autosummary::
:toctree: generated/

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

Some of the constants most likely to be useful in MPAS-Tools, COMPASS and other
related projects are:
Expand Down
4 changes: 2 additions & 2 deletions conda_package/recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ test:
- mesh_tools/mesh_conversion_tools/test/Arctic_Ocean.geojson
- mesh_tools/mesh_conversion_tools/test/mesh.QU.1920km.151026.nc
- mesh_tools/mesh_conversion_tools/test/land_mask_final.nc
- conda_package/mpas_tools/tests/*
- conda_package/tests/*
imports:
- mpas_tools
- mpas_tools.mesh.conversion
Expand All @@ -87,7 +87,7 @@ test:
- MpasMaskCreator.x mesh.nc arctic_mask.nc -f mesh_tools/mesh_conversion_tools/test/Arctic_Ocean.geojson
- planar_hex --nx=30 --ny=20 --dc=1000. --npx --npy --outFileName='nonperiodic_mesh_30x20_1km.nc'
- MpasCellCuller.x nonperiodic_mesh_30x20_1km.nc culled_nonperiodic_mesh_30x20_1km.nc
- python -m pytest conda_package/mpas_tools/tests
- python -m pytest conda_package/tests
- mark_horns_for_culling --help
- set_lat_lon_fields_in_planar_grid --help
- create_scrip_file_from_mpas_mesh --help
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from mpas_tools.cime.constants import constants
import requests

from mpas_tools.cime.constants import constants


def test_cime_constants(e3sm_tag='master'):
"""
Expand All @@ -14,7 +15,8 @@ def test_cime_constants(e3sm_tag='master'):

resp = requests.get(
f'https://raw.githubusercontent.com/E3SM-Project/E3SM/{e3sm_tag}/'
f'share/util/shr_const_mod.F90')
f'share/util/shr_const_mod.F90'
)

text = resp.text

Expand All @@ -32,10 +34,10 @@ def test_cime_constants(e3sm_tag='master'):
print(f'parsed: {constant} = {value}')
if constant in constants:
if isinstance(value, float):
print('verifying {}'.format(constant))
print(f'verifying {constant}')
assert value == constants[constant]
else:
print('skipping verification for {}'.format(constant))
print(f'skipping verification for {constant}')

found[constant] = True
else:
Expand All @@ -46,7 +48,7 @@ def test_cime_constants(e3sm_tag='master'):
all_found = True
for constant in found:
if not found[constant]:
print('{} was not found!'.format(constant))
print(f'{constant} was not found!')
all_found = False

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

key = line[start:end]
line = line[end+1:]
line = line[end + 1 :]

if '!' in line:
line, _ = line.split('!', 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
#!/usr/bin/env python

from mpas_tools.mesh.conversion import convert, cull, mask
from mpas_tools.io import write_netcdf
import matplotlib

from mpas_tools.io import write_netcdf
from mpas_tools.mesh.conversion import convert, cull, mask

from .util import get_test_data_file

matplotlib.use('Agg')
from geometric_features import read_feature_collection
import xarray
from geometric_features import read_feature_collection


def test_conversion():
dsMesh = xarray.open_dataset(
'mesh_tools/mesh_conversion_tools/test/mesh.QU.1920km.151026.nc')
get_test_data_file('mesh.QU.1920km.151026.nc')
)
dsMesh = convert(dsIn=dsMesh)
write_netcdf(dsMesh, 'mesh.nc')

dsMask = xarray.open_dataset(
'mesh_tools/mesh_conversion_tools/test/land_mask_final.nc')
dsMask = xarray.open_dataset(get_test_data_file('land_mask_final.nc'))
dsCulled = cull(dsIn=dsMesh, dsMask=dsMask)
write_netcdf(dsCulled, 'culled_mesh.nc')

fcMask = read_feature_collection(
'mesh_tools/mesh_conversion_tools/test/Arctic_Ocean.geojson')
get_test_data_file('Arctic_Ocean.geojson')
)
dsMask = mask(dsMesh=dsMesh, fcMask=fcMask)
write_netcdf(dsMask, 'antarctic_mask.nc')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#!/usr/bin/env python
import os

import numpy
import xarray
import os

from mpas_tools.io import write_netcdf
from mpas_tools.ocean.depth import compute_depth, compute_zmid, add_depth, \
add_zmid, write_time_varying_zmid
from mpas_tools.ocean.depth import (
add_depth,
add_zmid,
compute_depth,
compute_zmid,
write_time_varying_zmid,
)

from .util import get_test_data_file


def create_3d_mesh():
Expand All @@ -14,19 +22,25 @@ def create_3d_mesh():
dsMesh = xarray.open_dataset(outFileName)
else:
dsMesh = xarray.open_dataset(
'mesh_tools/mesh_conversion_tools/test/mesh.QU.1920km.151026.nc')
get_test_data_file('mesh.QU.1920km.151026.nc')
)
nCells = dsMesh.sizes['nCells']
nVertLevels = 10
zmax = 1000.
layerThickness = zmax/nVertLevels
dsMesh['refBottomDepth'] = \
('nVertLevels', numpy.linspace(layerThickness, zmax, nVertLevels))
dsMesh['maxLevelCell'] = \
('nCells', nVertLevels*numpy.ones(nCells, dtype=int))
dsMesh['bottomDepth'] = ('nCells', zmax*numpy.ones(nCells))
dsMesh['layerThickness'] = \
(('Time', 'nCells', 'nVertLevels'),
layerThickness*numpy.ones((1, nCells, nVertLevels)))
zmax = 1000.0
layerThickness = zmax / nVertLevels
dsMesh['refBottomDepth'] = (
'nVertLevels',
numpy.linspace(layerThickness, zmax, nVertLevels),
)
dsMesh['maxLevelCell'] = (
'nCells',
nVertLevels * numpy.ones(nCells, dtype=int),
)
dsMesh['bottomDepth'] = ('nCells', zmax * numpy.ones(nCells))
dsMesh['layerThickness'] = (
('Time', 'nCells', 'nVertLevels'),
layerThickness * numpy.ones((1, nCells, nVertLevels)),
)
write_netcdf(dsMesh, 'test_depth_mesh.nc')

return dsMesh
Expand All @@ -35,23 +49,30 @@ def create_3d_mesh():
def test_compute_depth():
dsMesh = create_3d_mesh()
depth, depth_bnds = compute_depth(dsMesh.refBottomDepth)
assert numpy.all(numpy.isclose(depth, numpy.linspace(50., 950., 10)))
assert numpy.all(numpy.isclose(depth_bnds[:, 0],
numpy.linspace(0., 900., 10)))
assert numpy.all(numpy.isclose(depth_bnds[:, 1],
numpy.linspace(100., 1000., 10)))
assert numpy.all(numpy.isclose(depth, numpy.linspace(50.0, 950.0, 10)))
assert numpy.all(
numpy.isclose(depth_bnds[:, 0], numpy.linspace(0.0, 900.0, 10))
)
assert numpy.all(
numpy.isclose(depth_bnds[:, 1], numpy.linspace(100.0, 1000.0, 10))
)


def test_compute_zmid():
dsMesh = create_3d_mesh()
zMid = compute_zmid(dsMesh.bottomDepth, dsMesh.maxLevelCell,
dsMesh.layerThickness, depth_dim='nVertLevels')
zMid = compute_zmid(
dsMesh.bottomDepth,
dsMesh.maxLevelCell,
dsMesh.layerThickness,
depth_dim='nVertLevels',
)

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

depth = zMid.isel(Time=0, nCells=0)
assert numpy.all(numpy.isclose(depth.values,
numpy.linspace(-50., -950., 10)))
assert numpy.all(
numpy.isclose(depth.values, numpy.linspace(-50.0, -950.0, 10))
)


def test_add_depth():
Expand All @@ -65,15 +86,18 @@ def test_add_depth():

# test adding depth coordinate once to the mesh and once to the input file,
# with the mesh passed in separately
for in_filename, coord_filename in [(mesh_filename, None),
('test_depth_in.nc', mesh_filename)]:
for in_filename, coord_filename in [
(mesh_filename, None),
('test_depth_in.nc', mesh_filename),
]:
add_depth(in_filename, out_filename, coordFileName=coord_filename)
dsOut = xarray.open_dataset(out_filename)
assert 'depth' in dsOut.dims

depth = dsOut.depth
assert numpy.all(numpy.isclose(depth.values,
numpy.linspace(50., 950., 10)))
assert numpy.all(
numpy.isclose(depth.values, numpy.linspace(50.0, 950.0, 10))
)


def test_add_zmid():
Expand All @@ -87,8 +111,10 @@ def test_add_zmid():

# test adding zMid once to the mesh and once to the input file, with the
# mesh passed in separately
for in_filename, coord_filename in [(mesh_filename, None),
('test_depth_in.nc', mesh_filename)]:
for in_filename, coord_filename in [
(mesh_filename, None),
('test_depth_in.nc', mesh_filename),
]:
add_zmid(in_filename, out_filename, coordFileName=coord_filename)
dsOut = xarray.open_dataset(out_filename)
assert 'depth' in dsOut.dims
Expand All @@ -97,54 +123,63 @@ def test_add_zmid():
assert zMid.dims == ('nCells', 'depth')

depth = zMid.isel(nCells=0)
assert numpy.all(numpy.isclose(depth.values,
numpy.linspace(-50., -950., 10)))
assert numpy.all(
numpy.isclose(depth.values, numpy.linspace(-50.0, -950.0, 10))
)


def test_write_time_varying_zmid():

dsMesh = create_3d_mesh()
nCells = dsMesh.sizes['nCells']
nVertLevels = dsMesh.sizes['nVertLevels']
mesh_filename = 'test_depth_mesh.nc'
in_filename = 'test_depth_in.nc'
out_filename = 'test_depth_out.nc'

layerThickness = 100.
layerThickness = 100.0

# test adding zMid once to the mesh and once to the input file, with the
# mesh passed in separately, each one without and once with a prefix
for coord_filename, prefix in [(None, ''),
(mesh_filename, ''),
(None, 'timeMonthly_avg_'),
(mesh_filename, 'timeMonthly_avg_')]:
for coord_filename, prefix in [
(None, ''),
(mesh_filename, ''),
(None, 'timeMonthly_avg_'),
(mesh_filename, 'timeMonthly_avg_'),
]:
print(coord_filename, prefix)

if coord_filename is None:
dsIn = dsMesh.drop_vars('layerThickness')
else:
dsIn = xarray.Dataset()
layerThicknessVar = '{}layerThickness'.format(prefix)
dsIn[layerThicknessVar] = \
(('Time', 'nCells', 'nVertLevels'),
layerThickness*numpy.ones((2, nCells, nVertLevels)))
dsIn['{}temperature'.format(prefix)] = \
xarray.ones_like(dsIn[layerThicknessVar])
layerThicknessVar = f'{prefix}layerThickness'
dsIn[layerThicknessVar] = (
('Time', 'nCells', 'nVertLevels'),
layerThickness * numpy.ones((2, nCells, nVertLevels)),
)
dsIn[f'{prefix}temperature'] = xarray.ones_like(
dsIn[layerThicknessVar]
)
write_netcdf(dsIn, in_filename)
dsIn.close()

write_time_varying_zmid(in_filename, out_filename,
coordFileName=coord_filename, prefix=prefix)
write_time_varying_zmid(
in_filename,
out_filename,
coordFileName=coord_filename,
prefix=prefix,
)

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

zMid = dsOut['{}zMid'.format(prefix)]
zMid = dsOut[f'{prefix}zMid']
assert zMid.dims == ('Time', 'nCells', 'depth')

depth = zMid.isel(Time=0, nCells=0)
assert numpy.all(numpy.isclose(depth.values,
numpy.linspace(-50., -950., 10)))
assert numpy.all(
numpy.isclose(depth.values, numpy.linspace(-50.0, -950.0, 10))
)
dsOut.close()


Expand Down
Loading
Loading