1

I have a netCDF file in Python in which the unit of the time variable is days_since_Jan11900 which cannot be read by the xarray package. It throws the error

unable to decode time units 'days_since_Jan11900' with 'the default calendar'. Try opening your dataset with decode_times=False or installing cftime if it is not installed.

I do not want to use decode_times but simply change it into days_since_0. It is days since 1/1/0000.

How can I change the units in the netCDF to something read-able?

<class 'netCDF4._netCDF4.Variable'>
float64 days(time)
    units: days_since_Jan11900
    long_name: calendar_days
unlimited dimensions: 
current shape = (87600,)
filling on, default _FillValue of 9.969209968386869e+36 used
Thomas
  • 441
  • 3
  • 16
  • I also had an issue with xarray, the answer here may also help: https://stackoverflow.com/questions/64624632/change-time-axis-units-from-years-since-to-days-since-in-netcdf-file – ClimateUnboxed Nov 01 '20 at 15:52

2 Answers2

0

If you are on Unix, you could try changing the calendar using CDO. The following should fix your problem:

cdo -L  -setreftime,0001-01-01  -settaxis,0001-01-01,0:00:00,1day  -setcalendar,standard infile.nc outfile.nc 

I have set the time to year 1, as I don't think there is a year 0 in any valid calendar.

Robert Wilson
  • 3,192
  • 11
  • 19
0

I solved it myself in Python, as I had asked for, and not via cdo. Here is how I did, it is quite easy:

ds['days'] = ds.days - 730966
ds.days.attrs['units'] = 'days since 1860-01-01'
ds.to_netcdf(path + 'name_of_file.nc')

The 730966 is my starting value in the array of days, therefore I subtracted that and changed my unit to an adequate unit, in my case days since 1860-01-01.

Thomas
  • 441
  • 3
  • 16