2017-06-20 43 views
0

我有159個Ncdf4文件,每個文件中有56個合奏。我想從159個輸入文件中的每一箇中抽出合奏1。然後在一個文件中生成一個NCDF4文件,其中包含所有的合奏1。我的代碼如下。我的問題是隻有159的最後一個文件的數據寫入輸出文件。我想我錯過了一個嵌套循環,但不知道,我的嘗試失敗了。 RM(列表= LS()) 庫(ncdf.tools) 庫(ncdf4) 庫(ncdf4.helpers) 庫(RNetCDF)使用從159個獨立的NCDF4文件中提取的變量編寫單個NCDF4文件

setwd("D:/Rwork/Project") # set working folder 

#####Write NCDF4 files############################################# 
dir("D:/Rwork/Project/Test")->xlab # This is the directory where the file for analysising are 
filelist <- paste("Test/",dir("Test"),sep="") 


N <- length(filelist)  # Loop over the individual files 
for(j in 1:N){ 

File<-nc_open(filelist[j]) 
print(filelist[j]) 
Temperature<-ncvar_get(File,"t2m") 
Lat<-ncvar_get(File, "lat") 
Lon<-ncvar_get(File,"lon") 
Time<-ncvar_get(File,"time") 
EnsambleNo.<-ncvar_get(File,"ensemble_member") 
Temperature 
Ensamble1<-Temperature[,,1,] #The Ensamble wanted, 1 to 56 
Ensamble1<-round(Ensamble1,digits = 0) 

tunits < - 「,因爲1800-01-小時01 00:00:00"

的#define尺寸

################################################################## 
londim<-ncdim_def("Lon","degrees_east",as.double(Lon)) 
latdim<-ncdim_def("Lat", "degrees_north",as.double(Lat)) 
timedim<-ncdim_def("Time",tunits,as.double(Time)) 


#Define variables 
################################################################## 
fillvalue<-1e32 

dlname<-"tm2" 

tmp_def<-ncvar_def("Ensamble1","deg_K", list(londim,latdim,timedim),fillvalue,dlname,prec =  "double") 

ncfname<-("D:/Rwork/Project/TrialEnsamble/TrialEnsamble.nc") 
ncout<-nc_create(ncfname,list(tmp_def),force_v4=T) 

ncvar_put(ncout,tmp_def,Ensamble1,start=NA,count = NA)# Think I need a nested loop here 


ncatt_put(ncout,"Lon","axis","X") 
ncatt_put(ncout, "Lat", "axis", "Y") 
ncatt_put(ncout, "Time","axis", "T") 

title<-c(1:2) 
names(title)<-c("Ian","Gillespie") 
title<-as.data.frame(title) 

ncatt_put(ncout,0,"Make_NCDF4_File",1, prec="int") 
ncatt_put(ncout,0,"Maynooth_University",1,prec="short") 
ncatt_put(ncout,0,"AR000087828",1, prec="short") 
ncatt_put(ncout,0,"mickymouse",1, prec="short") 

history <- paste("P.J. Bartlein", date(), sep=", ") 
ncatt_put(ncout,0,"description","this is the script to write NCDF4 files") 



#Close file and write date to disk 
########################################################## 
nc_close(ncout) 

} 

回答

0

實測值最好是與所述第一3相同的大小,製備四種尺寸的空數組和名稱捫sions作爲從for循環生成的數組,並具有額外的第四維。 dir(「D:/ Rwork/Project/Test」) - > xlab#這是用於分析的文件的目錄爲 filelist < - paste(「Test /」,dir (「Test」),sep =「」)

output <- array(, dim=c(192,94,12,160))# need to change this to length(Lat), etc 

N <- length(filelist)  # Loop over the individual files 
for(j in 1:N){ 

    File<-nc_open(filelist[j]) 
    print(filelist[j]) 
    Temperature<-ncvar_get(File,"t2m") 
    Lat<-ncvar_get(File, "lat") 
    Lon<-ncvar_get(File,"lon") 
    Time<-ncvar_get(File,"time") 
    Year<-c(1851:2010) 
    EnsambleNo.<-ncvar_get(File,"ensemble_member") 
    Temperature 
    Lat<-round(Lat,digits = 2) 
    Lon<-round(Lon,digits = 2) 
    Ensamble1<-Temperature[,,1,] #The Ensamble wanted, 1 to 56 
    Ensamble1<-round(Ensamble1,digits = 1) 
    dimnames(Ensamble1)<-list(Lon,Lat,Time) 
    dimnames(output) <- list(Lon,Lat,Time,Year) 
    } 

    print(Ensamble1) 
相關問題