Skip to content

Commit 0ad3449

Browse files
authored
Merge pull request #94 from dgergel/add_precip_unit_conversion
Add precip unit conversion
2 parents 87e2ca6 + 1a66fb3 commit 0ad3449

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

HISTORY.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ History
66
0.X.X (XXXX-XX-XX)
77
------------------
88
* Add ``include-quantiles`` flag to ``apply_qdm`` to allow for including quantile information in bias corrected output. (PR #95, @dgergel)
9+
* Add precipitation unit conversion to ``standardize_gcm``. (PR #94, @dgergel)
910
* Add ``astype`` argument to ``regrid``. (PR #92, @brews)
1011
* Make ``dodola`` container's default CMD. (PR #90, @brews)
1112
* Add ``dask-kubernetes``, ``distributed`` to container environment. (PR #90, @brews)

dodola/core.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,20 @@ def standardize_gcm(ds, leapday_removal=True):
307307
)
308308

309309
# Cleanup time.
310+
311+
# if variable is precip, need to update units to mm day-1
312+
if "pr" in ds_cleaned.variables:
313+
# units should be kg/m2/s in CMIP6 output
314+
if ds_cleaned["pr"].units == "kg m-2 s-1":
315+
# convert to mm/day
316+
mmday_conversion = 24 * 60 * 60
317+
ds_cleaned["pr"] = ds_cleaned["pr"] * mmday_conversion
318+
# update units attribute
319+
ds_cleaned["pr"].attrs["units"] = "mm day-1"
320+
else:
321+
# we want this to fail, as pr units are something we don't expect
322+
raise ValueError("check units: pr units attribute is not kg m-2 s-1")
323+
310324
if leapday_removal:
311325
# if calendar is just integers, xclim cannot understand it
312326
if ds.time.dtype == "int64":

dodola/tests/test_services.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def _datafactory(x, start_time="1950-01-01", variable_name="fakevariable"):
4444
return out
4545

4646

47-
def _gcmfactory(x, start_time="1950-01-01"):
47+
def _gcmfactory(x, gcm_variable="fakevariable", start_time="1950-01-01"):
4848
"""Populate xr.Dataset with synthetic GCM data for testing
4949
that includes extra dimensions and leap days to be removed.
5050
"""
@@ -58,7 +58,7 @@ def _gcmfactory(x, start_time="1950-01-01"):
5858

5959
out = xr.Dataset(
6060
{
61-
"fakevariable": (
61+
gcm_variable: (
6262
["time", "lon", "lat", "member_id"],
6363
x[:, np.newaxis, np.newaxis, np.newaxis],
6464
)
@@ -74,6 +74,7 @@ def _gcmfactory(x, start_time="1950-01-01"):
7474
"time_bnds": (["time", "bnds"], np.ones((len(x), 2))),
7575
},
7676
)
77+
7778
return out
7879

7980

@@ -462,6 +463,35 @@ def test_clean_cmip6():
462463
assert "time_bnds" not in ds_cleaned.coords
463464

464465

466+
@pytest.mark.parametrize(
467+
"gcm_variable", [pytest.param("tasmax"), pytest.param("tasmin"), pytest.param("pr")]
468+
)
469+
def test_cmip6_precip_unitconversion(gcm_variable):
470+
"""Tests that precip units are converted in CMIP6 cleanup if variable is precip"""
471+
# Setup input data
472+
n = 1500 # need over four years of daily data
473+
ts = np.sin(np.linspace(-10 * np.pi, 10 * np.pi, n)) * 0.5
474+
ds_gcm = _gcmfactory(ts, gcm_variable=gcm_variable, start_time="1950-01-01")
475+
476+
if gcm_variable == "pr":
477+
# assign units to typical GCM pr units so they can be cleaned
478+
ds_gcm["pr"].attrs["units"] = "kg m-2 s-1"
479+
480+
in_url = "memory://test_clean_cmip6/an/input/path.zarr"
481+
out_url = "memory://test_clean_cmip6/an/output/path.zarr"
482+
repository.write(in_url, ds_gcm)
483+
484+
clean_cmip6(in_url, out_url, leapday_removal=True)
485+
ds_cleaned = repository.read(out_url)
486+
487+
assert "height" not in ds_cleaned.coords
488+
assert "member_id" not in ds_cleaned.coords
489+
assert "time_bnds" not in ds_cleaned.coords
490+
491+
if "pr" in ds_cleaned.variables:
492+
assert ds_cleaned["pr"].units == "mm day-1"
493+
494+
465495
def test_remove_leapdays():
466496
"""Test that leapday removal service removes leap days"""
467497
# Setup input data

0 commit comments

Comments
 (0)