Unexpected behaviour of `climpred.utils.add_time_from_init_lead()`
See original GitHub issueDescribe the bug
The function climpred.utils.add_time_from_init_lead
(which adds the “valid_time” coordinate) seems to return unusual values when the lead frequency is “yearly” and the init frequency is higher than annual (e.g. 6 monthly). The “valid_time” coordinate is used heavily by climpred internals so this causes strange behaviour/bugs further downstream.
Code Sample
# Hindcasts initialised every 6 months with yearly lead
init = xr.cftime_range(start="2000-01-01", end="2002-01-01", freq="6MS")
lead = range(0, 5)
data = np.random.random((len(init), len(lead)))
hind = xr.DataArray(data, coords=[init, lead], dims=["init", "lead"], name="var")
hind["lead"].attrs["units"] = "years"
# Add "valid_time" coordinate using `climpred.utils.add_time_from_init_lead()`
hind = climpred.utils.add_time_from_init_lead(hind)
print(hind["valid_time"])
Returns
<xarray.DataArray 'valid_time' (lead: 4, init: 5)>
array([[cftime.DatetimeGregorian(2000, 10, 1, 0, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2000, 10, 1, 0, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2001, 10, 1, 0, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2001, 10, 1, 0, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2002, 10, 1, 0, 0, 0, 0, has_year_zero=False)],
[cftime.DatetimeGregorian(2000, 10, 1, 0, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2000, 10, 1, 0, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2001, 10, 1, 0, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2001, 10, 1, 0, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2002, 10, 1, 0, 0, 0, 0, has_year_zero=False)],
[cftime.DatetimeGregorian(2001, 10, 1, 0, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2001, 10, 1, 0, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2002, 10, 1, 0, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2002, 10, 1, 0, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2003, 10, 1, 0, 0, 0, 0, has_year_zero=False)],
[cftime.DatetimeGregorian(2002, 10, 1, 0, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2002, 10, 1, 0, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2003, 10, 1, 0, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2003, 10, 1, 0, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2004, 10, 1, 0, 0, 0, 0, has_year_zero=False)]],
dtype=object)
Coordinates:
* init (init) object 2000-01-01 00:00:00 ... 2002-01-01 00:00:00
* lead (lead) int64 0 1 2 3
valid_time (lead, init) object 2000-10-01 00:00:00 ... 2004-10-01 00:00:00
Attributes:
long_name: validity time
standard_name: time
description: time for which the forecast is valid
calculate: init + lead
You can see that the "valid_time"s at the first two leads are identical and that all “valid_times” are at month 10, regardless of initialisation month (which alternates between 1 and 7).
Expected behavior "valid_time"s should correspond to L years after the initialisation date, where L is the lead.
Output of climpred.show_versions()
climpred: 2.1.7.dev10+gb8bf61b xarray: 0.20.1 pandas: 1.3.4 numpy: 1.21.4 scipy: 1.6.3 cftime: 1.5.0 netcdf4: None nc_time_axis: 1.4.0 matplotlib: 3.4.2 cf_xarray: 0.6.1 xclim: 0.31.0 dask: 2021.11.2 distributed: 2021.11.2 setuptools: 49.6.0.post20210108 pip: 21.1.2 conda: 4.11.0 IPython: 7.24.0 sphinx: None
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:14
Top GitHub Comments
Please go ahead
ok. the above should be an xarray issue, but there is still an unrelated issue in climpred.
.utils.my_shift
just needs thequartely_rule
andnquarters
ormonthly_rule
andnmonths
and so far doesnt usenquarters
afaik: https://github.com/pangeo-data/climpred/blob/7440d7ca1d75618f6e6582b9dded942038031731/climpred/utils.py#L546