Skip to content

Commit

Permalink
raqms: Add surf_only option
Browse files Browse the repository at this point in the history
  • Loading branch information
zmoon committed Nov 4, 2022
1 parent 43c1b9a commit 3e21763
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
19 changes: 14 additions & 5 deletions monetio/models/raqms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import xarray as xr


def open_dataset(fname):
def open_dataset(fname, *, surf_only=False):
"""Open a single dataset from RAQMS output. Currently expects netCDF file format.
Parameters
Expand All @@ -27,14 +27,12 @@ def open_dataset(fname):
)

ds = xr.open_dataset(names[0], drop_variables=["theta"])
ds = _fix_grid(ds)
ds = _fix_time(ds)
ds = _fix_pres(ds)
ds = _fix(ds, surf_only=surf_only)

return ds


def open_mfdataset(fname):
def open_mfdataset(fname, *, surf_only=False):
"""Open a multiple file dataset from RAQMS output.
Parameters
Expand All @@ -55,10 +53,21 @@ def open_mfdataset(fname):
)

ds = xr.open_mfdataset(names, concat_dim="time", drop_variables=["theta"], combine="nested")
ds = _fix(ds, surf_only=surf_only)

return ds


def _fix(ds, *, surf_only):
ds = _fix_grid(ds)
ds = _fix_time(ds)
ds = _fix_pres(ds)

if surf_only:
ds = ds.isel(z=0).expand_dims("z")

ds = ds.transpose("time", "z", "y", "x")

return ds


Expand Down
13 changes: 13 additions & 0 deletions tests/test_raqms.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def _test_ds(ds):
assert (ds["dp_pa"].mean(dim=("time", "y", "x")) > 1000).all()
assert 100000 > ds["surfpres_pa"].mean() > 95000

assert tuple(ds.o3vmr.dims) == ("time", "z", "y", "x")


def test_open_dataset():
ds = raqms.open_dataset(TEST_FP)
Expand All @@ -55,3 +57,14 @@ def test_open_dataset_bad():
def test_open_mfdataset_bad():
with pytest.raises(ValueError, match="^File format "):
raqms.open_mfdataset("asdf")


@pytest.mark.parametrize(
"fn",
["open_dataset", "open_mfdataset"],
)
def test_surf_only(fn):
ds = getattr(raqms, fn)(TEST_FP, surf_only=True)
assert set(ds.dims) == {"time", "z", "y", "x"}
assert tuple(ds.o3vmr.dims) == ("time", "z", "y", "x")
assert ds.sizes["z"] == 1

0 comments on commit 3e21763

Please sign in to comment.