0
votes

The command ncdim_def from package ncdf4 is built to encourage the automatic creation of the coordinate variable that goes with the dimension, a very good practice.

However, it only allows to create "double" or "integer" precision for this coordinate variable. For external reasons, I need write the coordinate variable "time" as a float.

To do so, I use the following structure which consists in creating the coordinate variable separately from the dimension definition ( ie. using the option create_dimvar = FALSE described in the doc of ncdim_def)

timevalue= seq(0.5,10.5)
VAR1value= seq(10.2,20.2)

# define time dim, but without the time var
timedim <- ncdim_def( name   = 'time'  ,
                  units  = '', 
                  vals   = seq(length(timevalue)),
                  unlim  = TRUE,
                  create_dimvar = FALSE)

# define time coordinate variable  
timevar <- ncvar_def(name  = 'time',
                 units = 'days since 1950-01-01 00:00:00',
                 dim = list(timedim), 
                 longname = 'time', 
                 prec = "float")

# define another variable
var1var<- ncvar_def(name = 'VAR1',
                units    = 'unit1',
                dim      = list(timedim),
                missval  = -9999.0, 
                longname = 'VAR1 long name')

defVar<-list(timevar,var1var)

# creating ncfile (removing any previous one for repeated attempt)
ncfname='test.nc'
if (file.exists(ncfname)) file.remove(ncfname)
ncout   <- nc_create(ncfname,defVar,force_v4=T, verbose = T)

# writing the values
ncvar_put(ncout,timevar,timevalue)
ncvar_put(ncout,var1var,VAR1value)

nc_close(ncout)

However, this returns the error :

"ncvar_put: warning: you asked to write 0 values, but the passed data array has 11 entries!"

Indeed, the resulting netcdf shows (ncdump) :

dimensions:
    time = UNLIMITED ; // (0 currently)
variables:
    float time(time) ;
        time:units = "days since 1950-01-01 00:00:00" ;
    float VAR1(time) ;
        VAR1:units = "unit1" ;
        VAR1:_FillValue = -9999.f ;
        VAR1:long_name = "VAR1 long name" ;

I guess I need to force the dimension of the unlimited 'time' dimension upon creation, but i don't understand how to this in the frame of ncdf4.

1

1 Answers

0
votes

I ran into the same issue (needing the time dimension as float instead of double) and was able to solve it by defining the time variable as a regular non-unlimited dimension with the length of my hours vector

hours <- c("595377")
t <- ncdim_def("time", "", 1:length(hours), create_dimvar = FALSE)
timevar <- ncvar_def(name = "time",
                     units = 'hours since 1950-01-01 00:00:00',
                     dim = list(t),
                     longname = "time",
                     prec = "float")
variables <- list(timevar)
ncnew <- nc_create("TEST.nc", variables )
ncvar_put(ncnew, timevar, hours, start=c(1), count=c(length(hours)))
nc_close(ncnew)

Not sure though if this only works if the time dimension only has one value...