@@ -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+
465495def test_remove_leapdays ():
466496 """Test that leapday removal service removes leap days"""
467497 # Setup input data
0 commit comments