The xarray supplies the groupby function which we can use to calculate the anomaly of the climate data. For example, the anomaly of the monthly weather data can be calculated accroding to http://xarray.pydata.org/en/stable/examples/weather-data.html:
climatology = ds.groupby("time.month").mean("time")
anomalies = ds.groupby("time.month") - climatology
However, when we want to calculate the anomaly of the daily data, we need to consider the Feb 29th in the leap year. If we use the grammar mentioned above, the example is given below:
import pandas as pd
import numpy as np
date = pd.date_range('20110101','20161231',freq='D')
data = np.random.rand(len(date))
da = xr.DataArray(data,dims=['date'],coords=dict(date=date))
da_group = da.groupby('date.dayofyear')
This method divides the DataArray according to the dayofyear in the date. But how do we do when we want to groupby according to the 'month' and the 'day' of the date, for example, Month=4 and Day=14 of every year (It's worth to mention that the dayofyears of 2011-04-10 and 2012-04-10 are different).
I have tried da_group = da.groupby(['date.month','date.day']), but it seems wrong with the error `group` must be an xarray.DataArray or the name of an xarray variable or dimension.Received ['date.month', 'date.day'] instead..
So how do we groupby according to both the month and the day of the date? Mang thanks!