Removing leap day from leap years in xarray dataset

Question:

I have Netcdf file loaded in an xarray dataset and I want to make daily climatologies without the leap day that is, without 29th Feb included in it. I’m trying the Dataset.drop method by the syntax is not so intuitive for me. Here is the Dataset

print(ds)
>><xarray.Dataset>
Dimensions:        (lat: 1, lev: 1, lon: 720, time: 27133)
Coordinates:
* lon            (lon) float32 -180.0 -179.5 -179.0 ... 178.5 179.0 179.5
* lev            (lev) float32 1.0
* time           (time) datetime64[ns] 2000-01-02T18:00:00 ... 2018-07-30
Dimensions without coordinates: lat
Data variables:
Var1              (time, lev, lon) float32 ...
Var2              (time, lat, lon) float64 ...
Var3              (time, lat, lon) float64 ...

I tried

ds_N_R.drop(['Var1', 'Var2', 'Var3'], time='2000-02-29')
>>TypeError: drop() got an unexpected keyword argument 'time'
##another approach
ds_N_R.sel(time='2000-02-29').drop(['Var1', 'Var2', 'Var3'])
## gives not the result I intended
<xarray.Dataset>
Dimensions:  (lev: 1, lon: 720, time: 4)
Coordinates:
* lon      (lon) float32 -180.0 -179.5 -179.0 -178.5 ... 178.5 179.0 179.5
* lev      (lev) float32 1.0
* time     (time) datetime64[ns] 2000-02-29 ... 2000-02-29T18:00:00
Data variables:
*empty*

How do I proceed here? It would be great to know if there is a direct method through which I can calculate daily climatologies considering only 365 days of a year but I would also like to know how to remove data from a particular time step when required.

Asked By: Light_B

||

Answers:

The right way to use drop() here would be:
ds_N_R.drop([np.datetime64('2000-02-29')], dim='time')

But I think this could actually be more cleanly done with an indexing operation, e.g.,
ds_N_R.sel(time=~((ds_N_R.time.dt.month == 2) & (ds_N_R.time.dt.day == 29)))

Answered By: shoyer

You can convert your calendar to a non_leap one using xarray’s convert_calendar. That is ds_N_R.convert_calendar('noleap').

Per the xarray documentation (https://docs.xarray.dev/en/stable/generated/xarray.Dataset.convert_calendar.html): "If the source and target calendars are either no_leap, all_leap or a standard type, only the type of the time array is modified. When converting to a leap year from a non-leap year, the 29th of February is removed from the array."

Answered By: user39360
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.