Anne Sophie
Anne Sophie

Reputation: 187

Average a dimension of a chunk (xarray)

Let X be a variable I have stored in multiple files. In each files, X has dimension of (time, depth, latitude, longitude). I wish to do a yearly average, and each file store a year of data.

To be efficient, I open the data with X = xarray.open_mfdataset('path'+'year_*_X.nc').

I do not want to average all the time axis, i.e.: X.mean(dim='time').

I observe that X store 'chunks' since it gives the 'chunksize', so maybe it can be used to average the time dimension of each chunks?

In [1] : X

Out[1] : <xarray.DataArray 'ty_trans_submeso' (time: 240, st_ocean: 50, yu_ocean: 200, xt_ocean: 360)> dask.array<shape=(240, 50, 200, 360), dtype=float32, chunksize=(12, 50, 200, 360)>

Upvotes: 2

Views: 860

Answers (1)

bwc
bwc

Reputation: 1115

X.groupby('time.year').mean(dim='time') should do the trick as long as time is a datetime64 object.

Upvotes: 3

Related Questions