1

I have climate data in big netCDF file which covers 60 years time span climate data observation (please take a look into this link). However, I want to subset this climate data for only recent 20 years while discarding the time period that I don't want to use. I used raster package to read netCDF file in R, but to get a smaller chunk of raster grid from original netCDF file is unknown to me.

goal:

I want to subset or get a smaller chunk of raster grid (only 20 years' period climate data observation) from big netCDF file (it covers 60 year's climate data).

Is there any efficient fast way to get this done in R?

  • Subset by time first, then crop – mdsumner Jul 6 '18 at 10:26
3

Key components were already addressed by AF7 so this may not add much value, but I would suggest raster::brick() to enable getZ() function (as brick() keeps @z slot).

(I downloaded 0.44deg_tg dataset (tg_0.44deg_rot_v17.0.nc)... not sure if this is you are after).

# Libraries {raster} and {ncdf4}
library(raster)
library(ncdf4)
# read into R by raster::brick()
(tg <- brick("C:\\tg_0.44deg_rot_v17.0.nc"))
str(tg)

# Find the record of start and end dates
tg_date <- getZ(tg)            # extract Date information
grep("1998-01-01", tg_date)    # record [17533] corresponds to "1998-01-01"
grep("2017-12-31", tg_date)    # as above [24837] - end date

# Subset by date range. Then recover Date field, which was dropped during subsetting
# tg_20y is tg variable for 20 years
tg_20y <- subset(tg, 17533:24837)          # subset 1998-2017
tg_20y@z$Date <- tg@z$Date[17533:24837]    # put $Date back into @z slot
  • 2
    You can get tricky with the date classes eg., to query specific months you can use: grep( paste(c("May',"June"), collapse = "|") , months(tg_date)) or equivalently: which( months(tg_date) %in% c("May","June")) and even just subset the object directly: tg.sub <- tg[[which( months(tg_date) %in% c("May","June"))]] – Jeffrey Evans Jul 5 '18 at 16:30
  • @Jerry Thanks. Just a tiny dash, but I'm glad if it helped. – Kazuhito Jul 6 '18 at 12:31
  • @JeffreyEvans Thanks for the tip, it works like a charm and gives stylish file. I haven't gone further yet but it would be very useful to analyze changes across these years. – Kazuhito Jul 6 '18 at 12:38
4

You should take a look at the manual of the raster package and the package vignette, which cover this topic. You want open the whole file using stack() or brick(), then subset to the layers you want using the [[ operator, like this:

library(raster)
filename = "some_file.nc"
s = stack(filename, varname="varname")
s # take a look
s_subset = s[[layer_numbers]] 

If you need to subset to specific timestamps easily, you can refer to this question.

As for efficiency and speed, this is as fast as it can be. However, how fast it is really depends on the chunking of the netCDF file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.