我正在嘗試使用R編寫多個NCDF4文件。我有大量文件需要在分析中讀取並輸出到新的NCDF4文件。 我有下面的代碼來創建和寫入到一個單獨的NCDF4文件。此代碼適用於創建和寫入文件。 我需要一些代碼來替換ncfname < - 「ABC123.nc」並循環,從而爲每個分析的文件生成一個NCDF4新文件當我嘗試使用一個循環,就像在R中一樣,如果失敗。在NCDF4包希望看到「NC」的延伸。 我的代碼如下。任何人都可以在這方面的幫助?寫入多個NCDF4文件
#####Write NCDF4 files#############################################
###################################################################
tunits<-"days since 1600-01-01 00:00:00"
#Define dimensions
##################################################################
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<-"2 meter air temperature"
dlname2<-" 2 meter max air temperature"
tmp_def<-ncvar_def("Data1","deg_C",
list(londim,latdim,timedim),fillvalue,dlname,prec = "double")
tmp_def2<-ncvar_def("Data2","deg_C",
list(londim,latdim,timedim),fillvalue,dlname2,prec = "double")
#Create Ncdf4 file and put arrays
##################################################################
ncfname<-"ABC123.nc"
ncout<-nc_create(ncfname,list(tmp_def,tmp_def2),force_v4=T)
#Put variables
#################################################################
ncvar_put(ncout,tmp_def,Data2,start=NA,count = NA)
ncvar_put(ncout,tmp_def2,Data1, start= NA,count = NA)
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("jack","jill")
title<-as.data.frame(title)
################################################
# attributes # the 0 designates the attribute as global
###########################################################
ncatt_put(ncout,0,"Make_NCDF4_File",1, prec="int")
ncatt_put(ncout,0,"XXXXXX",1,prec="short")
ncatt_put(ncout,0,"AR000087828",1, prec="short")
ncatt_put(ncout,0,"description","this is the script to write NCDF4files")
#Close file and write date to disk
##########################################################
nc_close(ncout)
謝謝nawendt – ian
我會努力做到這一點,希望能回到你身邊證實成功 – ian
謝謝nawendt。這段代碼完美地工作。我只需要解決它爲什麼不循環。但那是一個不同的問題。伊恩 – ian