I opened multiple netcdfs (each one corresponds to an hour 0-30hr) with xr.open_mfdataset('*.nc'). Now I have an extra dimension (time). I am considering one of my variables(u,v,w). I want to average u (time: 30 z: 200 y: 100 x: 100) over 24 hours instead of the whole time period I have. How can I do that?
CodePudding user response:
To select the first 24 observations along the time dim, you can use .isel
, e.g.:
ds24 = ds.isel(time=range(24))
Now, you can average over the time dim:
ds24.mean(dim='time')
CodePudding user response:
Made some simple test case:
#!/usr/bin/env ipython
# ---------------------
import numpy as np
import xarray as xr
import datetime
# ---------------------
# let us make test serie:
N = 30;
datesout = np.array([datetime.datetime(2022,10,23,0) datetime.timedelta(seconds = nn*3600) for nn in range(N)])
serieout = np.array([10 nn for nn in range(N) ]);
# let us save netcdf:
dsout = xr.Dataset(data_vars=dict(someserie=(["time"], serieout)),coords=dict(time=datesout),attrs=dict(description="Test data."))
dsout.to_netcdf('test.nc')
# ---------------------
# let us make daily average:
with xr.open_dataset('test.nc') as ncin:
# let us calculate daily means:
dfmeanout = ncin.groupby("time.day").mean()
# -----------------------------------------
# keep only the first day:
dfmeanout = dfmeanout.isel(day=0)
# -----------------------------------------
print('Got value: ',dfmeanout.values)
# --------------------------------------------
# simple test:
expected_mean = np.sum(np.array([vv 10 for vv in range(24)]))/24
print('Correct mean is : ',expected_mean)
# ---------------------------------------------