0

I am trying to set a reference time in a netcdf file with the command in python.

i have tried the following.

from cdo import *
cdo = Cdo()
cdo(input = "infile.nc", output = "outfile.nc", options = "2022-12-01,12:00:00")

Once i run this command all the dates in the file changes to 0001-01-01 instead of the one set in options.

i have also tried the following command.

cdo.setreftime("2022-12-01,12:00:00", input="infile.nc", output = "outfile.nc)

which gives the same result.

sinfon command

Grateful for any help.

sss
  • 3
  • 3
  • Command looks correct. Have you checked the command independently running it with CDO to ensure this is purely a problem with the Python CDO package? Potentially this is due to an incompatibility between cdo and CDO – Robert Wilson Dec 02 '22 at 18:01
  • Tried Cdo().setreftime("2022-12-01,12:00:00,hours", input = "infile.nc", output = "outfile.nc"), and instead of RefTime: 0001-01-01 00:00:00 it changed to Reftime: 2001-01-01 00:00:00 – sss Dec 03 '22 at 13:36
  • I asked if you tried this doing it from CDO on the command line to check something isn't going wrong with the Python package. Have you tried it on the command line? – Robert Wilson Dec 03 '22 at 13:42
  • Yes I did, it gave the same result. – sss Dec 03 '22 at 13:45
  • In that case you should raise the issue on the CDO website. Potentially this is a bug in CDO that they need to fix – Robert Wilson Dec 03 '22 at 14:24
  • You can change the timeunits using `netCDF4` library. I usually do it with that as with `from netCDF4 import Dataset,num2date,date2num` you get all the tools. If you want to change the units to "seconds since 2010-01-01 00:00:00", you do like this: `unitsout = "seconds since 2010-01-01 00:00:00"; ncout = Dataset(filein,'a');timevar=ncout.variables['time'];timein=num2date(timevar[:],timevar.units);timeout=date2num(timein,unitsout);timevar.setncattr('units',unitsout);timevar[:]=timeout;ncout.close()` CAUTION! Code overwrites the values in filein, therefore one can make first a copy of file. – msi_gerva Dec 04 '22 at 15:37
  • This also worked in environment where the cdo failed. – sss Dec 20 '22 at 06:52

1 Answers1

0

The correct command should look like

cdo.setreftime("2022-12-01,12:00:00,1hours", input=infile.nc, output=outfile.nc)

and

cdo -setreftime,"2022-12-01,12:00:00,1hours" infile.nc outfile.nc
sss
  • 3
  • 3