创建 NetCDF R [英] Create NetCDF R

查看:75
本文介绍了创建 NetCDF R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用尺寸为 3d 矩阵 m3d 编写 NetCDF 文件

I am trying to write a NetCDF file with a 3d matrix m3d with dimensions

73 (LON) x 36 (LAT) x 12 (TIME)

从 12 个矩阵创建,带暗

created from 12 matrices with dim

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)

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

Anyway, I get as output this error

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

推荐答案

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

IMHO your problem is a wrong Longvector and a wrong count argument in put.var.ncdf, e.q. this works for me (please note: I changed the matrix generation in your for loop):

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)

这篇关于创建 NetCDF R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆