# How to subset temporal range of CRU TS 3.0 rasterstack in R?

I have downloaded open data of CRU TS 3.0 from the website https://data.ceda.ac.uk/badc/cru/data/cru_ts/cru_ts_3.00/data in which cru_ts_3_00.1901.2006.tmp.nc.gz was downloaded. I open this data in R through the following code

``````library(ncdf4)
getwd()
nc.tmp <- nc_open("cru_ts_3_00.1901.2006.tmp.nc")
print(nc.tmp)
tmp1 <- brick("cru_ts_3_00.1901.2006.tmp.nc", varname="tmp")
tmp1
plot(tmp1)

> tmp1
class      : RasterBrick
dimensions : 360, 720, 259200, 1272  (nrow, ncol, ncell, nlayers)
resolution : 0.5, 0.5  (x, y)
extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs
source     : C:/Users/XX/cru_ts_3_00.1901.2006.tmp.nc
names      : X372, X373, X374, X375, X376, X377, X378, X379, X380, X381, X382, X383, X384, X385, X386, ...
z-value    : 372, 1643 (min, max)
varname    : tmp
``````

Now, I would like to take a mean of rasters from year 1960 to 1990 (1960-1990). I can't seem to make sense out of the names in `tmp1` as it starts with X372, X373 .... and ending at X1643. So I am confused that which year(containing months) is 1960,1961,1962 etc?

106 years is exactly 12 * 106 = 1272 months, so that's the 1272. If it is in a sensible order (ie Jan 1901 to Dec 1901, then 1902, etc etc, a short formula can work out the numerical layer indices for a given year:

``````year.layers = function(year){
start = 1 + (year - 1901)*12
start:(start+11)
}
``````

Quick test, it should return numbers from 1 to 12 for 1901, and so on:

``````> year.layers(1901)
[1]  1  2  3  4  5  6  7  8  9 10 11 12
> year.layers(1902)
[1] 13 14 15 16 17 18 19 20 21 22 23 24
> year.layers(2006)
[1] 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
``````

You can slice your stack using the layer numbers instead of the names with the "X" in front:

``````tmp1[[year.layers(1990)]]
``````

to get one year.

A function to compute the layer numbers for a range of years isn't much more complex. Here's a quick hack that uses the previous function:

``````years.layers <-
function(y1, y2){
start = min(year.layers(y1))
end = max(year.layers(y2))
start:end
}
``````

Then you can do something like:

``````data60_90 = tmp1[[years.layers(1960,1990)]]
``````

You should not use `nc_open`. Just do

``````library(raster)
tmp1 <- brick("cru_ts_3_00.1901.2006.tmp.nc", varname="tmp")
``````

Now subset

``````start <- 12*(1960-1901) + 1
end <- start + 30 * 12 - 1
start
#[1] 709
end
#[1] 1068

tmp <- tmp1[[start:end]]
``````

But I would use `terra` like this (I am using a more recent version of the database):

``````library(terra)
f <- "cru_ts4.05.1901.2020.tmn.dat.nc"
r <- rast(f, "tmn")
crutime <- time(r)
i <- crutime > as.POSIXct("1960-01-01") & crutime < as.POSIXct("1990-01-01")
sum(i)
#[1] 360
x <- r[[i]]
x
#class       : SpatRaster
#dimensions  : 360, 720, 372  (nrow, ncol, nlyr)
#resolution  : 0.5, 0.5  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +datum=WGS84 +no_defs
#source      : cru_ts4.05.1901.2020.tmn.dat.nc:tmn
#varname     : tmn (near-surface temperature minimum)
#names       :         tmn_709,         tmn_710,         tmn_711,         tmn_712,         tmn_713,         tmn_714, ...
#unit        : degrees Celsius, degrees Celsius, degrees Celsius, degrees Celsius, degrees Celsius, degrees Celsius, ...
#time        : 1960-01-16 to 1989-12-16

``````

I notice that with this file, `raster` is also much clearer

``````library(raster)
b <- brick(f, var="tmn")
b
#class      : RasterBrick
#dimensions : 360, 720, 259200, 1440  (nrow, ncol, ncell, nlayers)
#resolution : 0.5, 0.5  (x, y)
#extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +no_defs