I'm trying to take an array and resample it with a custom function. From this post: Apply function along time dimension of XArray
def special_mean(x, drop_min=False):
s = np.sum(x)
n = len(x)
if drop_min:
s = s - x.min()
n -= 1
return s/n
is an example sample_mean.
I have a dataset that is:
<xarray.Dataset>
Dimensions: (lat: 100, lon: 130, time: 7305)
Coordinates:
* lon (lon) float32 -99.375 -99.291664 -99.208336 ... -88.708336 -88.625
* lat (lat) float32 49.78038 49.696426 49.61247 ... 41.552795 41.46884
lev float32 1.0
* time (time) datetime64[ns] 2040-01-01 2040-01-02 ... 2059-12-31
Data variables:
tmin (time, lat, lon) float32 dask.array<chunksize=(366, 100, 130), meta=np.ndarray>
tmax (time, lat, lon) float32 dask.array<chunksize=(366, 100, 130), meta=np.ndarray>
prec (time, lat, lon) float32 dask.array<chunksize=(366, 100, 130), meta=np.ndarray>
relh (time, lat, lon) float32 dask.array<chunksize=(366, 100, 130), meta=np.ndarray>
wspd (time, lat, lon) float32 dask.array<chunksize=(366, 100, 130), meta=np.ndarray>
rads (time, lat, lon) float32 dask.array<chunksize=(366, 100, 130), meta=np.ndarray>
Attributes:
history: Fri Jun 14 10:32:22 2019: ncatted -a _FillValue,,o,d,9e+20 IBIS...
And then I apply a resample that is:
data.resample(time='1MS').map(special_mean)
<xarray.Dataset>
Dimensions: (time: 240)
Coordinates:
* time (time) datetime64[ns] 2040-01-01 2040-02-01 ... 2059-12-01
lev float32 1.0
Data variables:
tmin (time) float32 dask.array<chunksize=(1,), meta=np.ndarray>
tmax (time) float32 dask.array<chunksize=(1,), meta=np.ndarray>
prec (time) float32 dask.array<chunksize=(1,), meta=np.ndarray>
relh (time) float32 dask.array<chunksize=(1,), meta=np.ndarray>
wspd (time) float32 dask.array<chunksize=(1,), meta=np.ndarray>
rads (time) float32 dask.array<chunksize=(1,), meta=np.ndarray>
How do I do this function such that I can retain the 'lon' and 'lat' coordinates like when doing
data.resample(time='1MS').mean()