I am using R to loop through thousands of NetCDF files (downscaled climate data), and while I am able to do it using R, the code is too slow. I am looking for a way to speed it up and to maintain functionality.
What I need is a program or platform where I can (1) import netcdf data, (2) import a polygon layer, (3) get the mean for the polygon layer, and (4) export output to csv file. The following is a general code workflow:
shape <- readOGR(dsn,"shape") ## read in shapefile
files <- dir("in directory", recursive=TRUE, full.names=TRUE, pattern="\\.nc$") ### get file paths to netCDF files
out <-NULL
for (i in 1:length(files)){
brick.tmp <- files[i]
for (j in 1:nlayers(brick.tmp)){
val = extract(brick.tmp[[j]],shape, fun = mean)
out <- rbind(out,val)
}
}
I would prefer to stay in the R environment, but I'm not sure how to speed up the code.
I have attempted to use several programs and have looked through the list here: https://www.unidata.ucar.edu/software/netcdf/docs/software.html
However, I thought I would leverage everyone's collective knowledge and see if I can narrow down the potential choices.