Hayden
Hayden

Reputation: 107

Modifying variable attribute of netcdf file

I'm trying to modify the calendar type of a time variable in a netcdf file to change from GREGORIAN to gregorian as I think it is causing problems when I try to access the time variables within a later analysis.

        double Time(Time) ;
                Time:long_name = "Time" ;
                Time:units = "days since 1979-01-01 00:00:00" ;
                Time:cartesian_axis = "T" ;
                Time:calendar_type = "GREGORIAN" ;
                Time:calendar = "GREGORIAN" ;
                Time:bounds = "Time_bounds" ;
                Time:_ChunkSizes = 1 ;

to

        double Time(Time) ;
                Time:long_name = "Time" ;
                Time:units = "days since 1979-01-01 00:00:00" ;
                Time:cartesian_axis = "T" ;
                Time:calendar_type = "gregorian" ;
                Time:calendar = "gregorian" ;
                Time:bounds = "Time_bounds" ;
                Time:_ChunkSizes = 1 ;

I have tried to use the nco function nccat but I can't seem to get the syntax correct. I tried:

ncatted -a 'calendar,time,o,c,"gregorian"' Ocean_v_1994_01.nc out.nc

Upvotes: 3

Views: 3360

Answers (3)

ClimateUnboxed
ClimateUnboxed

Reputation: 8107

I think you could also sort this by using the cdo setcalendar command:

 cdo setcalendar,proleptic_gregorian in.nc out.nc

Upvotes: 1

Charlie Zender
Charlie Zender

Reputation: 6352

The single quotes you placed around the ncatted argument causes the double quotes to become literals which is not what you want. There are no literals, spaces, or special characters in your argument, so just drop all quotes:

ncatted -a calendar,time,o,c,gregorian Ocean_v_1994_01.nc out.nc

Upvotes: 4

Hayden
Hayden

Reputation: 107

I found a way to do this using R and the ncdf4 package. The solution was:

library(ncdf4)
mydata <- nc_open("Ocean_v_1994_01.nc", write = TRUE)

ncatt_put(mydata, "Time", 'calendar', attval = 'gregorian', prec = 'text')
ncatt_put(mydata, "Time", 'calendar_type', attval = 'gregorian', prec = 'text')

# check result 
ncatt_get(mydata, "Time")

nc_sync(mydata)
nc_close(mydata)

Upvotes: 2

Related Questions