创建NetCDF R.

时间:2014-02-13 14:46:22

标签: r netcdf

我正在尝试使用尺寸为

的3d矩阵m3d编写NetCDF文件
73 (LON) x 36 (LAT) x 12 (TIME)

使用暗淡的12个矩阵创建

73 (LON) x 36 (LAT)

这是我的代码

#Setting dimensions

space <- 5
Longvector = seq(-180, 180, by = space)
Latvector = seq(-90, 90, by = space)
dimMATR <- 73
dimMATC <- 36

dimX <- dim.def.ncdf("Long", "degrees", Longvector)
dimY <- dim.def.ncdf("LAT", "degrees", Latvector)
dimT <- dim.def.ncdf("Time", "days", 1:12, unlim = FALSE)

#Create 3d Matrix

m3d <- array(0, dim = c(dimMATR,dimMATC,12))
for (i in 1:12){
  m <- as.matrix(do.call(rbind,myfilesContent[i]))
  m3d[,,i]<-t(m)
  remove(m)
}

#Create NetCDF

mv <- -9999 # missing value to use
L <- prod(dimMATR,dimMATC,12)
var3d <- var.def.ncdf( "monthlyav_sst", "units", list(dimX,dimY,dimT), mv,prec="double")
nc <- create.ncdf( "monthlyav_sst.nc", var3d)

put.var.ncdf(nc, var3d, m3d, start = c(1, 1, 1),  count = c(1, 1, L))
close.ncdf(nc)

无论如何,我得到输出这个错误

Error in R_nc_put_vara_double: NetCDF: Start+count exceeds dimension bound
Error in put.var.ncdf(nc, var3d, m3d, start = c(1, 1, 1), count = c(1,  : 
  C function R_nc_put_var_double returned error

1 个答案:

答案 0 :(得分:1)

恕我直言,Longvector中您的问题是错误的count和错误的put.var.ncdf参数,e.q。这对我有用(请注意:我在for循环中更改了矩阵生成):

library("ncdf")

# Setting dimensions
space <- 5
Longvector = seq(-180, 180, by = space)
Latvector = seq(-90, 90, by = space)
dimMATR <- length(Longvector)
dimMATC <- length(Latvector)

dimX <- dim.def.ncdf("Long", "degrees", Longvector)
dimY <- dim.def.ncdf("LAT", "degrees", Latvector)
dimT <- dim.def.ncdf("Time", "days", 1:12, unlim = FALSE)

# Create 3d Matrix
m3d <- array(0, dim = c(dimMATR,dimMATC,12))
for (i in 1:12){
  m <- matrix(sample(dimMATR*dimMATC), dimMATR, dimMATC)
  m3d[,,i]<-t(m)
  remove(m)
}

# Create NetCDF
mv <- -9999 # missing value to use
var3d <- var.def.ncdf( "monthlyav_sst", "units", list(dimX,dimY,dimT), mv,prec="double")

nc <- create.ncdf( "monthlyav_sst.nc", var3d)
put.var.ncdf(nc, var3d, m3d, start=c(1, 1, 1), count=dim(m3d))
close(nc)