[英]Force 'float' precision for the coordinate variable of an unlimited dimension variable (time) in netcdf using R(ncdf4)
該命令ncdim_def
從包ncdf4
是建立鼓勵自動創建坐標變量與維度,一個非常好的做法去的。
但是,它只允許為此坐標變量創建“雙精度”或“整數”精度。 由於外部原因,我需要將坐標變量“時間”寫為浮點數。
為此,我使用以下結構,該結構包括與維度定義分開創建坐標變量(即,使用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
的框架中ncdf4
這ncdf4
。
我遇到了同樣的問題(需要時間維度為 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)
不確定這是否僅在時間維度只有一個值時才有效......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.