来自ncdf4包的命令ncdim_def是为了鼓励自动创建与尺寸相关的坐标变量而构建的,这是一个非常好的实践。
但是,它只允许为此坐标变量创建“双精度”或“整数”精度。出于外部原因,我需要将坐标变量"time“写成一个浮点数。
为此,我使用以下结构,该结构包括从维度定义中单独创建坐标变量(即。使用ncdim_def文档中描述的选项create_dimvar = FALSE )
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)但是,这将返回以下错误:
"ncvar_put: warning: you asked to write 0 values, but the passed data array has 11 entries!"实际上,生成的netcdf显示(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" ;我想我需要在创作时强制使用无限“时间”维度的维度,但我不明白如何在ncdf4的框架中做到这一点。
发布于 2020-02-11 18:00:35
我遇到了同样的问题(需要将时间维度定义为浮点型而不是双精度型),并能够通过将时间变量定义为具有小时向量长度的常规非无限维度来解决这个问题
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)不确定这是否只在时间维度只有一个值的情况下有效...
https://stackoverflow.com/questions/54571391
复制相似问题