使用R(ncdf4)对netcdf中的无限制尺寸变量(时间)的坐标变量强制“浮动”精度

时间:2019-02-07 10:36:35

标签: r netcdf dimensions ncdf4

软件包toString中的命令ncdim_def旨在鼓励自动创建随尺寸变化的坐标变量,这是一种很好的做法。

但是,它仅允许为此坐标变量创建“双精度”或“整数”精度。由于外部原因,我需要将坐标变量“时间”写为浮点数。

为此,我使用以下结构,该结构包含与尺寸定义分开创建坐标变量(即,使用ncdf4文档中描述的选项create_dimvar = FALSE

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)

实际上,生成的netcdf显示(ncdump):

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

我想我需要在创建时强制使用无限的“时间”维度,但是我不知道如何在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" ; 的框架中做到这一点。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题(需要将时间维度设置为float而不是double),并且能够通过将时间变量定义为具有小时矢量长度的常规非无限维度来解决此问题

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)

不确定这是否仅在时间维度只有一个值的情况下有效...