2014-06-12 3 views
0

11 개 이상의 다른 변수로 netcdf를 만들고 싶습니다. 루프 내에서 작성하려고했지만 작동하지 않습니다. 내 코드는 그런 일이 될 것입니다 :R에서 다중 변수를 사용하여 netcdf를 만듭니다.

#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) 


} 

문제는 내가 var.add.ncdf (대신 put.var.ncdf) .. 그것에 대해 어떤 생각을 사용하는 경우는 잘 모르겠습니다이다 ??? 루프 내에서 파일을 만들고 쓰려면 어떻게합니까 ??

도움이 될 것입니다.

+0

NetCDF 파일 작업을위한 여러 패키지가 있습니다. 어떤 것을 사용하고 있는지 말할 수 있습니다. – rrs

답변

0

다음은이 작업을 수행하는 방법입니다. 먼저 변수 중 하나와 함께 netcdf 파일을 만든 다음 다른 루프를 추가하고 채 웁니다.

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) 
관련 문제