I want to create a netcdf with 11 or more different variables. For that I was trying to write it within a loop, but it is not working. My code would be something like that:
#Defining names and dimensions
nam_cwt <- c("N","NE","E","SE","S","SW","W","NW","C","A","U") #number of variables
CWTvar <- paste("var_",nam_cwt,sep="")
data_cwt <- list()
mat_cwt <- array()
dimX <- dim.def.ncdf("longitude", "degrees_east", Longvector)
dimY <- dim.def.ncdf("latitude", "degrees_north", Latvector)
dimT <- dim.def.ncdf("time","days since 1961-01-01",1:length(my.date), unlim=TRUE)
missval <- -999
#Creating and filling the netcdf file
for (i in 1:length(nam_cwt)){
#Getting every matrix of elements
data_cwt <- lapply(cwt_out,function(x) x[[1]][[i]][[2]])
dmatrix <- unlist(data_cwt)
mat_cwt <- array(dmatrix,dim=c(144,length(my.date),25))
tmat_cwt <- aperm(mat_cwt,c(1,3,2))
CWTvar[[i]] <- var.def.ncdf(nam_cwt[i],"days",list(dimX,dimY,dimT), ,missval,longname=nam_cwt[i])
ncfile <- create.ncdf("nctypes.nc",CWTvar)
put.var.ncdf(ncfile,CWTvar[i],tmat_cwt)
}
The problem is that I am not sure if I should use var.add.ncdf (instead put.var.ncdf).. any idea about that??? How can I create and write the file within the loop??
Any help will be helpful!