I am trying to combine multiple netcdf files with multiple variables:
- 6 types of parameters
-36 years
- 12 months
-31 days
- 6 Y coordinates
- 5 X coordinates
Each netcdf file contains data for 1 month of a year and 1 parameter, there are thus 432 * 6 =2592 files.
How would I best combine this all in a dataframe? It would in the end have to generate something like this:
rowID Date year month day coord.X coord.Y par1 par2 par3 par4 par5 par6
1 1979-01-01 1979 01 01 176 428 3.2 0.005 233.5 0.1 12.2 4.4
..................... 402568 rows in between.................
402570 2014-12-31 2014 12 31 180 433 1.7 0.006 235.7 0.2 0.0 2.7
How would I best combine this? I already have been struggling with this quite some time...
Excuse me for not knowing how to be able to make this question reproducible.. but there are so many elements involved. This where I have my files from: ftp://rfdata:[email protected]/WFDEI/
This is what I have so far, I think this what they call a nested loop right?: I ussually just try and try and try and in the end it works.. but I find this a tough job. Any recommendations on first steps are welcome, please.
require(ncdf4)
directory<-c("C:/folder/") # general folder
parameter<-c("par1","par2","par3","par4","par5","par6") # names of 6 parameters
directory2<-c("_folder2/") # parameter specific folder
directory3<-c("name") # last part of folder name
years<-c("1979","otheryears","2014") # years which are also part of netcdf file name
months<-c("01","othermonths","12") # months which are also part of netcdf file name
x=c(176:180) # X-coordinates
y=c(428:433) # Y-coordinates
require(plyr)
for (p in parameter){
assign(paste0(p,"list"), list())
for (i in years){
for (j in months){
for (k in x){
for (l in y){
fileloc<-paste(directory,p,directory2,p,directory3,i,j,".nc",sep="") #location to open
ncin<-nc_open(fileloc)
assign(paste0(p))<-ncvar_get(ncin,p) # extract the desired parameter from the netcdf list "ncin" and store in vector with name of parameter
day<-ncvar_get(ncin,"day") # extract the day of month from the netcdf list "ncin"
par.coord<-paste(p,"[",y,",",x,",","]",sep="") #string with function to select coordinates
temp<-data.frame(i,j,day,p=par.coord) # store day and parameter in dataframe
temp<-cbind(date=as.Date(with(temp,paste(i,j,day,sep="-")),"%Y-%m-%d"),temp,Y=y,X=x) # Add date and coordinates to df
assign(paste0(p,"list"), list(temp) #store multiple data frames in a list.. I think?
}assign(paste0(p,"list"), do.call(rbind,data) # something to bind the dataframes by row in a list
}}}}
In order to extract these Netcdf files by extract and group all the Netcdf files in one dataframe by:
First I made sure all *.nc files were in one folder. Second I simplified multiple for loops into one, since variables as year, month and parameter were available from the file name:
The variables day, Xcoord and Y coord could be extracted as one in an array.