2

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!

1
  • There are several packages for working with NetCDF files. You might want to say which one you are using. Commented Jun 12, 2014 at 15:19

1 Answer 1

2

Here's how I would do this: create a netcdf file first with one of the variables, then add and fill the others in a loop.

library(ncdf)
#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="")

# values and length of dimensions
Longvector = -180:180
Latvector = -90:90
Datevector   = 1:10
x= length(Longvector)
y= length(Latvector)
z= length(Datevector)

# define dimensions
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(Datevector), unlim=TRUE)

# set missing value
missval  <- -9999

# create the file with first variable so dimensions are set
CWTvar1 <- var.def.ncdf(name=nam_cwt[1],"days",list(dimX,dimY,dimT), missval=missval)
ncfile <- create.ncdf("nctypes.nc",vars=CWTvar1)

# open newly created file for writing data
mync = open.ncdf(con='nctypes.nc', write=T)

# create some data for first variable
mydata = array(data=runif(n=x*y*z, min=0, max=10), dim=c(x,y,z))

# add data to ncdf file
put.var.ncdf(nc=mync, varid='N', vals=mydata)

# now add all other variables with the same dimensions
for (i in 2:length(nam_cwt)){

    # generate new data
    mydata = array(data=runif(n=x*y*z, min=0, max=10)*i, dim=c(x,y,z))  
    # create new variable
    CWTvar <- var.def.ncdf(name=nam_cwt[i],"days",list(dimX,dimY,dimT) ,missval=missval)
    # add new variable to existing file
    mync = var.add.ncdf(nc=mync, v=CWTvar)
    # add data to variable in file
    put.var.ncdf(nc=mync,varid=CWTvar$name,vals=mydata)


}

close.ncdf(mync)

# check file
newnc = open.ncdf(con='nctypes.nc')
par(mfrow=c(4,3))
for (i in 1:length(nam_cwt)){
    zz = get.var.ncdf(newnc, varid=nam_cwt[i])
    image(x=Longvector, y=Latvector, z=zz[,,1])
}
close.ncdf(newnc)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.