1

I was instructed to use xarray rather than netCDF4 with this code, however, when importing with xarray, time doesn't have units and calendar to input into the num2date function (3rd line). I tried giving the units and calendar manually, but it doesn't work either. Any ideas?

ncfile = netCDF4.MFDataset('cmems_*.nc', 'r', format='NETCDF4') 
time = ncfile.variables['time']
time_converted = netCDF4.num2date(time[:], time.units, time.calendar) ###year, month, day

hist_start=datetime.datetime(1993,1,1)
hist_end=datetime.datetime(1996,12,31)

lat = ncfile.variables['latitude'][:]
lon = ncfile.variables['longitude'][:]

ta_his = ncfile.variables['thetao'][:,:,:]

date_start = netCDF4.date2num(hist_start, time.units, time.calendar)
date_end = netCDF4.date2num(hist_end, time.units, time.calendar)

time_indices = np.array([(t.month in months)&(t.year in range(hist_start.year, hist_end.year+1)) for t in time_converted])
Francisco
  • 11
  • 2
  • instead of all of this, could you use [`xr.cftime_range`](https://docs.xarray.dev/en/stable/generated/xarray.cftime_range.html)? given start, stop, freq, and calendar you can define a CF-calendar compliant date range object which can be assigned to an xarray coordinate – Michael Delgado Sep 26 '22 at 23:44

0 Answers0