1

I am having trouble converting the time dimension in an xarray dataset (consisting of NetCDF files) from units of 'hours since 2000-01-01 00:00:00' to a datetime format (ex. 2000-01-01 00:00:00). The Xarray docs have a very similar example, but the recommended fix(shown below) did not work for my dataset.

I first start by utilizing xarray.open_mfdataset (with decode_times=False on) to load my dataset.
This operation is completed successfully.

dataset =  xr.open_mfdataset(file_list, decode_times=False, data_vars='minimal', coords='minimal', parallel=True)

print(dataset)

<xarray.Dataset> Dimensions: (time: 4139, depth: 40, lat: 63, lon: 79)

Coordinates:

  • time (time) float64 1.403e+05 1.403e+05 ... 1.556e+05
  • depth (depth) float64 0.0 2.0 4.0 6.0 ... 3e+03 4e+03 5e+03
  • lat (lat) float64 30.0 30.08 30.16 30.24 ... 34.8 34.88 34.96
  • lon (lon) float64 -81.28 -81.2 -81.12 ... -75.2 -75.12 -75.04
  • Data variables:
  • tau (time) float64 dask.array<chunksize=(1,), meta=np.ndarray>
  • water_u (time, depth, lat, lon) float32 dask.array<chunksize=(1, 40, 63, 79),....

print(dataset.time)

array([140268., 140271., 140274., ..., 155595., 155598., 155601.])

Coordinates:
  • time     (time) float64 1.403e+05 1.403e+05 ... 1.556e+05 1.556e+05Attributes:
    
  • long_name:    Valid Timeunits:        hours since 2000-01-01 00:00:00
    
  • time_origin:  2000-01-01 00:00:00
    
  • calendar:     gregorian
    
  • axis:          T
    
  • TNAVO_code:    13
    

The xarray docs have an example almost exactly like this, in which they prescribe the simple use of xarray.decode_cf() to manually convert the time dimension from 'hours since...' to a datetime object.

However when I use xr.decode_cf(dataset) , the following error is thrown:

ValueError: invalid reference date for time units: analysis
During handling of the above exception, another exception occurred:

ValueError: unable to decode time units 'hours since analysis' with 'the default calendar'. Try opening your dataset with decode_times=False or installing cftime if it is not installed.
During handling of the above exception, another exception occurred:

ValueError: Failed to decode variable 'tau': unable to decode time units 'hours since analysis' with 'the default calendar'. Try opening your dataset with decode_times=False or installing cftime if it is not installed.

Do I need to install the 'cftime' package to deal with this? Is there a way I can manually work around this in Xarray?

Thank you

EDIT:

I have found a solution here --> https://stackoverflow.com/a/45469858/17461323

While this solution does work, it is unclear to me why refeeding in the same information that exists within the NetCDF file works. it seems like a 'hacky' solution. Is this an xarray issue?

C. Love FSU Meteorology

Chris Love
  • 33
  • 4
  • I guess the problem is that the time coordinate specification in your data does not conform to the [CF standard](https://cfconventions.org/cf-conventions/cf-conventions.html#time-coordinate). You'll want `unit` instead of `long_name` to be `"hours since 2000-01-01 00:00:00"`. – FObersteiner Apr 20 '23 at 06:20

0 Answers0