Question

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

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

created from 12 matrices with dim

73 (LON) x 36 (LAT)

Here is my code

#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
Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top