coecms / xmhw

Xarray version of Marine Heatwaves code by Eric Olivier
https://xmhw.readthedocs.io/en/latest/
Apache License 2.0
21 stars 10 forks source link

Handling cftime objects #50

Open florianboergel opened 1 year ago

florianboergel commented 1 year ago

I am currently using this package to analyze SST data from 950 to 1800. Hence, I need to use cftime objects. Without fixing possible errors with the calendar I came across a problem with the function add_doy().

I'd simply propose a new branch called cftime first that removes

        not_leap_year = ~t.dt.is_leap_year
        doy = doy_original + (not_leap_year & march_or_later)

and adds

        not_leap_year = xr.apply_ufunc(cftime.is_leap_year,
                                       t.dt.year,
                                       input_core_dims=[[]],
                                       output_core_dims=[[]],
                                       kwargs={'calendar': "standard",
                                               "has_year_zero":None},
                                       dask="allowed",
                                       vectorize=True)
        doy = doy_original + (~not_leap_year & march_or_later)

One could also just catch the type of the DatetimeIndex and choose between those two methods. The problem with cftime objects is that they do not have the function is_leap_year. cftime.is_leap_year only takes integers, therefore I needed to use apply_ufunc.

paolap commented 1 year ago

What SST data are you using? For testing purposes

florianboergel commented 1 year ago

It's a regional uncoupled ocean simulation using the atmospheric forcing of Schimanke and Meier (2012) https://cp.copernicus.org/articles/8/1419/2012/.

I will upload a small SST dataset today for testing.

paolap commented 1 year ago

I would probably prefer to detect the type of the DatetimeIndex and then choose between the 2 approaches based on that. As there's so much more that can go wrong with the apply_ufunc() approach, simply in term of changes to xarray and/or potentially the arguments passed not working for different examples. Time axis are always causing pain

florianboergel commented 1 year ago

I forgot that it was my turn to upload some data. Sorry for that. Find temperature data for the Baltic Sea for 1000 - 1009 in the link below.

DOI