Skip to content

Commit

Permalink
fix calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
juliettelavoie committed Mar 11, 2024
1 parent 3e60f6d commit b94bfc2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
34 changes: 13 additions & 21 deletions tests/test_biasadjust.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import dask
import numpy as np
import pytest
import xarray as xr
Expand Down Expand Up @@ -108,17 +107,15 @@ def test_errors(self):

class TestAdjust:
dref = timeseries(
dask.array.from_array(
np.ones(365 * 3)
), # dask array b/c xclim bug with sdba_encode_cf
np.ones((365 * 3) + 1), # leap year
variable="tas",
start="2001-01-01",
freq="D",
as_dataset=True,
)

dsim = timeseries(
np.concatenate([np.ones(365 * 3) * 2, np.ones(365 * 3) * 4]),
np.concatenate([np.ones(365 * 3) * 2, np.ones((365 * 3) + 1) * 4]),
variable="tas",
start="2001-01-01",
freq="D",
Expand Down Expand Up @@ -150,15 +147,18 @@ def test_basic(
self.dsim,
periods=periods,
to_level=to_level,
# xclim_adjust_args={'detrend': {
# 'LoessDetrend': {'f': 0.2, 'niter': 1, 'd': 0,
# 'weights': 'tricube'}}},
bias_adjust_institution=bias_adjust_institution,
bias_adjust_project=bias_adjust_project,
# moving_yearly_window={'window': 2, 'step': 1},
)
assert out.attrs["cat:processing_level"] == to_level or "biasadjusted"
assert out.attrs["cat:variable"] == ("tas",)
assert out.attrs["cat:id"] == "fake_id"
assert (
out["tas"].attrs["bias_adjustment"]
== "DetrendedQuantileMapping(group=Grouper("
"name='time.dayofyear', window=31), kind='+'"
").adjust(sim, )"
)
assert xc.core.calendar.get_calendar(out) == "noleap"

if bias_adjust_institution is not None:
Expand All @@ -172,7 +172,9 @@ def test_basic(
if periods == ["2001", "2006"]:
np.testing.assert_array_equal(
out["tas"].values,
np.concatenate([np.ones(365 * 3) * 1, np.ones(365 * 3) * 3]),
np.concatenate(
[np.ones(365 * 3) * 1, np.ones(365 * 3) * 3]
), # -1 for leap year
)
else: # periods==[['2001','2001'], ['2006','2006']]
np.testing.assert_array_equal(
Expand Down Expand Up @@ -244,9 +246,7 @@ def test_xclim_vs_xscen(
): # should give the same results using xscen and xclim
dref = (
timeseries(
dask.array.from_array(
np.random.randint(0, high=10, size=(365 * 3) + 1)
),
np.random.randint(0, high=10, size=(365 * 3) + 1),
variable="pr",
start="2001-01-01",
freq="D",
Expand Down Expand Up @@ -276,7 +276,6 @@ def test_xclim_vs_xscen(
var="pr",
period=["2001", "2003"],
adapt_freq={"thresh": "1 mm d-1"},
# jitter_under={"thresh": "0.01 mm d-1"},
xclim_train_args={"kind": "*", "nquantiles": 50},
)

Expand Down Expand Up @@ -307,13 +306,6 @@ def test_xclim_vs_xscen(
dsim.sel(time=slice("2001", "2006")), "noleap"
)

# drefx["pr"] = xc.sdba.processing.jitter_under_thresh(
# drefx["pr"], thresh="0.01 mm d-1"
# )
# dhistx["pr"] = xc.sdba.processing.jitter_under_thresh(
# dhistx["pr"], thresh="0.01 mm d-1"
# )

dhist_ad, pth, dP0 = xc.sdba.processing.adapt_freq(
drefx["pr"], dhistx["pr"], group=group, thresh="1 mm d-1"
)
Expand Down
2 changes: 0 additions & 2 deletions xscen/biasadjust.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,6 @@ def adjust(

dscen = _add_preprocessing_attr(dscen, dtrain.attrs["train_params"])
dscen = xr.Dataset(data_vars={var: dscen}, attrs=dsim.attrs)
# TODO: History, attrs, etc. (TODO kept from previous version of `biasadjust`)
# TODO: Check for variables to add (grid_mapping, etc.) (TODO kept from previous version of `biasadjust`)
dscen.attrs["cat:processing_level"] = to_level
dscen.attrs["cat:variable"] = parse_from_ds(dscen, ["variable"])["variable"]
if bias_adjust_institution is not None:
Expand Down

0 comments on commit b94bfc2

Please sign in to comment.