time and geographical subset of netcdf raster stack or raster brick using R

After b <- brick('sst.day.mean.2016.v2.nc'), we can type b to see information of the raster brick.

b
# class       : RasterBrick 
# dimensions  : 720, 1440, 1036800, 366  (nrow, ncol, ncell, nlayers)
# resolution  : 0.25, 0.25  (x, y)
# extent      : 0, 360, -90, 90  (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
# data source : C:\Users\basaw\Downloads\sst.day.mean.2016.v2.nc 
# names       : X2016.01.01, X2016.01.02, X2016.01.03, X2016.01.04, X2016.01.05, X2016.01.06, X2016.01.07, X2016.01.08, X2016.01.09, X2016.01.10, X2016.01.11, X2016.01.12, X2016.01.13, X2016.01.14, X2016.01.15, ... 
# Date        : 2016-01-01, 2016-12-31 (min, max)
# varname     : sst 

Notice that the Date slot has information from 2016-01-01 to 2016-12-31, which means the Z values already has date information and we can use that to subset the raster brick.

We can use the getZ function to access the values stored in the Z values. Type getZ(b) we can see a series of dates.

head(getZ(b))
# [1] "2016-01-01" "2016-01-02" "2016-01-03" "2016-01-04" "2016-01-05" "2016-01-06"

class(getZ(b))
# [1] "Date"

We can thus use the following code to subset the raster brick.

b2 <- b[[which(getZ(b) >= as.Date("2016-09-01") & getZ(b) <= as.Date("2016-10-15"))]]

We can then crop the image based on the code you provided.

b3 <- crop(b2, extent(144, 146, 14, 16))

To calculate the average, just use the mean function.

b4 <- mean(b3, na.rm = TRUE)

Finally, we can plot the average.

plot(b4)

enter image description here


Not in R, but just to point out that the subsetting and averaging task is easy to do in CDO from the command line:

cdo timmean -sellonlatbox,lon1,lon2,lat1,lat2 -seldate,date1,date2 in.nc out.nc

where the lon1,lon2 etc define the lon-lat area to cut out and date1,date2 are the date bounds.

You can then read the resultant file into R for plotting, or take a quick look with ncview.