Message-ID: <1250988325.1664534.1455125231893.JavaMail.yahoo@mail.yahoo.com>
Date: 2016-02-10T17:27:11Z
From: Thiago V. dos Santos
Subject: Time index is lost after masking raster stack
Dear all,
I usually have to crop - and mask - multilayer raster objects (derived from netcdf files) using a polygon.
I noticed that all time (date) information of the raster objects are lost after the masking operation. Please see this example:
--------------------------
library(raster)
# Create the date sequence
idx <- seq(as.Date("2000/1/1"), as.Date("2000/12/31"), by = "day")
# Create raster stack and assign dates
r <- raster(ncol=20, nrow=20)
s <- stack(lapply(1:length(idx), function(x) setValues(r, runif(ncell(r)))))
s <- setZ(s, idx)
getZ(s) # everything looks good
# Create spatial objects for cropping
e <- extent(-80, -40, -50, -20)
p <- as(e, 'SpatialPolygons')
# Crop and mask
c <- crop(s, e)
getZ(c) # everything still looks good
m <- mask(c, p)
getZ(m) # this returns NULL
--------------------------
Of course it would be easy to manually assign the time index on the masked object - something like m <- setZ(m, getZ(c)) - but it would be great to have a "native" way of preserving it.
Am I doing something wrong with the mask function or is this something that could be addressed in the next release of raster?
Greetings,
-- Thiago V. dos Santos
PhD student
Land and Atmospheric Science
University of Minnesota