Search code examples
netcdfpython-xarray

xarray error when decoding netcdf data with time units of "years since"


I have a netcdf file that someone passed me that uses "years since DATE":

double time(time) ;
    time:standard_name = "time" ;
    time:long_name = "time" ;
    time:calendar = "proleptic_gregorian" ;
    time:axis = "T" ;
    time:units = "years since 2000-1-1 00:00:00" ;

When I attempt to open this with xarray I get the error:

ValueError: unable to decode time units 'years since 2000-1-1 00:00:00' with calendar 'proleptic_gregorian'. Try opening your dataset with decode_times=False.

I can open when decode_times=False but then I can't slice the times. I found that changing the units to "days since" solved the error, but of course messes up the time axis (I simply overwrote the units with

ncatted -O -a units,time,m,c,"days since 2000-1-1 00:00:00" ./test.nc

as a test, but didn't update the actual time variable values)

Is there a trick to open netcdf data files with units "years since" in xarray?


Solution

  • I believe this is down to xarray not being able to parse times properly all of the time, even for CF-compliant files. I don't think it should be criticized too much for this, as it is not a trivial thing to be able to parse everything.

    However, there is a solution in my nctoolkit package. It has a built in to_xarray method. By default it used xarray to decode times. However, for cases when xarray cannot decode times, you can use CDO to do it:

    import nctoolkit as nc
    data = nc.open_data("infile.nc")
    ds = data.to_xarray(cdo_times = True)
    

    From experience CDO is capable of decoding the times for more or less anything, so this will probably solve your problem.