question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected behaviour of `climpred.utils.add_time_from_init_lead()`

See original GitHub issue

Describe 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()

# Paste the output here climpred.show_versions() here INSTALLED VERSIONS ------------------ commit: None python: 3.9.4 | packaged by conda-forge | (default, May 10 2021, 22:13:33) [GCC 9.3.0] python-bits: 64 OS: Linux OS-release: 4.18.0-305.19.1.el8.nci.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: None LOCALE: en_US.UTF-8

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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:14

github_iconTop GitHub Comments

1reaction
dougiesquirecommented, Dec 3, 2021

I started digging into xarray, must be somewhere here: https://github.com/pydata/xarray/blob/eea76733770be03e78a0834803291659136bca31/xarray/coding/frequencies.py#L149

do you mind if I post the issue?

Please go ahead

1reaction
aaronspringcommented, Dec 3, 2021

ok. the above should be an xarray issue, but there is still an unrelated issue in climpred. .utils.my_shift just needs the quartely_rule and nquarters or monthly_rule and nmonths and so far doesnt use nquarters afaik: https://github.com/pangeo-data/climpred/blob/7440d7ca1d75618f6e6582b9dded942038031731/climpred/utils.py#L546

Read more comments on GitHub >

github_iconTop Results From Across the Web

climpred: verification of weather and climate forecasts ...
In our past coding journey, collaborative coding, feedbacking issues and pull requests advanced our code and thinking about forecast verification more than we ......
Read more >
climpred/README.rst at main · pangeo-data/climpred - GitHub
In our past coding journey, collaborative coding, feedbacking issues and pull requests advanced our code and thinking about forecast verification more than we ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found