2

I am still working in the same project where I asked this question.

And I am getting by allright, but my processing times are too long, for each NetCDF file I am reading it, getting a stack form several of the time-slices, and then cropping each of those time slices within r, using a code like the one bellow:

library(raster)
library(ncdf4)
library(ncdf4.helpers)
library(rworldxtra)
data("countriesHigh")

The NETCdf files I have are for all of the world, but I am using only South America, for that I use countiresHigh from the rworldxtra package to subset it:

NONA <- countriesHigh[!is.na(countriesHigh@data$GEO3),]
## get shapefile of South America
SA <- NONA[NONA@data$GEO3 == "South America",]

Then using the following code I crop every layer that I need.

##Open conection to the layer
nc <- nc_open("C:/Users/mean_temperature-15000BP-10000BP.nc")

Now I start a loop

for(i in 1:10){
  message(paste("reading layer", i))
  # Read the stack for year i
  r <- stack("C:/Users/mean_temperaturemean_temperature-15000BP-10000BP.nc", varname = age[i])  
  #Change the extent to the correct one
  extent(r) <- c(-180,180,-90,90)
  #Crop it to South America
  r <- crop(r, SA)
  gc()
}

Problem 1: Can I crop the netcdf once before reading stacks to make this process faster?

I have looked here, here and here, and found no answer.

Problem 2: If I can crop it, how do I define the extent given that the NETCDF file contains only positive latitude and positive longitude

as stated in this question, the defined extent of the map is not the most usual, but I am basing my cropping in the extent of a shapefile that has the more typical c(-180,180,-90,90) extent

Cœur
  • 37,241
  • 25
  • 195
  • 267
Derek Corcoran
  • 3,930
  • 2
  • 25
  • 54
  • 1
    Maybe the [stars](https://github.com/r-spatial/stars) package might be helpful here? This [blog](https://www.r-spatial.org/r/2017/11/23/stars1.html) describes how to read NetCDF. – SeGa Aug 27 '18 at 14:24
  • Thanks @SeGa I will check it out, seems to be a good package – Derek Corcoran Aug 27 '18 at 15:47

0 Answers0