How to combine two for loops working with netcdf lists?

91 views Asked by At

I have problems combining multiple for loops. I will give an example with two of them, I would like to combine. If I know how to do it with two I will also be able to do it with multiple loops.

If anyone knows how to write this as lapply function that would also be nice.

require(ncdf4)

#### download files from this link to directory: (I just downloaded manually,two files are sufficient to answer the example)
#### ftp://rfdata:[email protected]/WFDEI/LWdown_daily_WFDEI/

setwd("C:/place_where_I_have_downloaded_my_files_from_link/")
temp = list.files(pattern="*.nc")                         #list imported netcdf files
list2env(
  lapply(setNames(temp, make.names(gsub("*.nc$", "", temp))), 
         nc_open), envir = .GlobalEnv) #import all parameters lists to global environment

#### first loop                               -                # select parameter out of netcdf files and combine into a List of 2
list_temp<-list()                                         #create empty list before loop
for (t in temp[1:2]){
  list_temp[t]<-list(data.frame(LWdown=ncvar_get(nc_open(t),"LWdown")[428,176,],xcoor=176,ycoor=428))
}
LW_bind<-do.call(rbind,list_temp)
rownames(LWdown_1to2)<-NULL

#### second loop                                                    # select parameter out of onenetcdf file per x-coordinate and combine into a List of 2
list_temp<-list()                                         #create empty list before loop
for (x in 176:177){
  list_temp[t]<-list(data.frame(LWdown=ncvar_get(nc_open(temp[1]),"LWdown")[428,x,],xcoor=x,ycoor=428))
}
LW_bind<-do.call(rbind,list_temp)
rownames(LWdown_1to2)<-NULL

How I tried to combine but didn't work:

#### combined loops
list_temp<-list()
for (t in temp[1:2]){for (x in 176:177){
  #ncin<-list()
  ncin<-nc_open(t)
  list_temp[x][t]<-list(data.frame(LWdown=ncvar_get(ncin,"LWdown")[428,x,],x=x,y=428))
}}
LWdown_1to2<-do.call(rbind,list_temp)
rownames(LWdown_1to2)<-NULL

I already solved my problem. See below. But I am still curious how one could solve the two for loops as described above, so I will leave the question open an unanswered.

Here is my solution:

require(arrayhelpers);require(stringr);require(plyr);require(ncdf4)
# store all files from ftp://rfdata:[email protected]/WFDEI/ in the following folder:
setwd("C:/folder")
temp = list.files(pattern="*.nc")           #list all the file names
param<-gsub("_\\S+","",temp,perl=T)         #extract parameter from file name

xcoord=seq(176,180,by=1)                    #The X-coordinates you are interested in
ycoord=seq(428,433,by=1)                    #The Y-coordinates you are interested in

list_var<-list()                         # make an empty list
for (t in 1:length(temp)){
temp_year<-str_sub(temp[],-9,-6)                                                                                #take string number last place minus 9 till last place minus 6 to extract the year from file name
temp_month<-str_sub(temp[],-5,-4)                                                                               #take string number last place minus 9 till last place minus 6 to extract the month from file name
temp_netcdf<-nc_open(temp[t])
temp_day<-rep(seq(1:length(ncvar_get(temp_netcdf),"day"))),length(xcoord)*length(ycoord))                   # make a string of day numbers the same length as amount of values
    dim.order<-sapply(temp_netcdf[["var"]][[param[t]]][["dim"]],function(x) x$name)                            # gives the name of each level of the array
    start <- c(lon = 428, lat = 176, tstep = 1)                                                                     # indicates the starting value of each variable
    count <- c(lon = 6, lat = 5, tstep = length(ncvar_get(temp_netcdf,"day")))                                 # indicates how many values of each variable have to be present starting from start
    tempstore<-ncvar_get(temp_netcdf, param[t], start = start[dim.order], count = count[dim.order])            # array with parameter values

df_temp<-array2df (tempstore, levels = list(lon=ycoord, lat = xcoord, day = NA), label.x = "value")           # convert array to dataframe
Add_date<-sort(as.Date(paste(temp_year[t],"-",temp_month[t],"-",temp_day,sep=""),"%Y-%m-%d"),decreasing=FALSE)  # make vector with the dates
list_var[t]<-list(data.frame(Add_date,df_temp,parameter=param[t]))                                         #add dates to data frame and store in a list of all output files
  ### nc_close(temp_netcdf)                                                                                           #close nc file to prevent data loss and errors
}
All_NetCDF_var_in1df<-do.call(rbind,list_var)  
0

There are 0 answers