[R-sig-Geo] Subsetting Raster Time Series

Dutrieux, Loic loic.dutrieux at wur.nl
Tue Aug 26 13:54:22 CEST 2014


Hi Srinivas,

You can access the time dimension of these raster object using the getZ and setZ functions; both from the raster package (avoid when possible accessing objects' slots by using @). Following that it is mostly vector arithmetic, which can be achieved in several ways.

Using the example in the help of getZ.

library(raster)
# Create a rasterStack object with time written to z dimension.
r <- raster(ncol=10, nrow=10)
s <- stack(lapply(1:3, function(x) setValues(r, runif(ncell(r)))))
s <- setZ(s, as.Date('2000-1-1') + 0:2)
s

# Extract time vector
time <- getZ(s)
# You can get the indices which fit your subset criteria using which() and inject them into raster::subset()
id <- which(time < as.Date('2000-1-3'))
subset(s, id)

# Nested
subset(s, which(getZ(s) < as.Date('2000-1-3')))

Best regards,

--
Loïc Dutrieux
Laboratory of Geo-Information sciences and remote sensing
Wageningen University
The Netherlands




-----Original Message-----
From: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of Srinivas V
Sent: Tuesday, August 26, 2014 11:56
To: r-sig-geo at r-project.org
Subject: [R-sig-Geo] Subsetting Raster Time Series

Hi,

I would like to subset the CRU dataset to a particular time period
(1980-2013) is there an option to do this within package raster? I can manually specifiy the layers to drop, but I would like to drop them based on a time period. I'm doing this to ensure two datasets are of the same time period.

I would appreciate any advice on dealing with this issue. Thanks!

library(raster)
library(rgdal)
library(ncdf)
library(zoo)

temp<-brick("/media/data/data_cru/tmp/cru_ts3.22.1901.2013.tmp.dat.nc")
temp
temp at z

output
+++++
 > temp
class       : RasterBrick
dimensions  : 360, 720, 259200, 1356  (nrow, ncol, ncell, nlayers) resolution  : 0.5, 0.5  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 data source : /media/data/data_cru/tmp/cru_ts3.22.1901.2013.tmp.dat.nc
names       : X1901.01.16, X1901.02.15, X1901.03.16, X1901.04.16, 
X1901.05.16, X1901.06.16, X1901.07.16, X1901.08.16, X1901.09.16, X1901.10.16, X1901.11.16, X1901.12.16, X1902.01.16, X1902.02.15, X1902.03.16, ...
Date        : 1901-01-16, 2013-12-16 (min, max)
varname     : tmp

 > temp at z
$Date
  [1] "1901-01-16" "1901-02-15" "1901-03-16" "1901-04-16" "1901-05-16" 
"1901-06-16" "1901-07-16" "1901-08-16" "1901-09-16" "1901-10-16"

[1351] "2013-07-16" "2013-08-16" "2013-09-16" "2013-10-16" "2013-11-16" 
"2013-12-16"



-- 

Srinivas Vaidyanathan
Senior Research Fellow
Foundation for Ecological Research, Advocacy & Learning
Web: www.feralindia.org


	[[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list