time_raster | R Documentation |
Time raster
time_raster(x, dates = NULL)
x |
one of |
dates |
An object with dates, one of object of class
|
This function creates TimeRaster-class
, a subclass of
stack in the raster package. It adds an xts
time series
(using only the index).
You can create a TimeRaster-class
in a variety of ways:
TimeRaster(stack, dates)
- Raster/Brick stack and date vector
TimeRaster(filelist, dates)
- List of files and date vector
TimeRaster(stack, xtsobj)
- Raster/Brick stack and xts object
TimeRaster(filelist, xtsobj)
- List of files and xts object
Where stack
is an already loaded Raster stack or filelist is a list of files.
A simple example to get a list of files from all geotifs in a directory would be
files <- list.files(path= "/data/subdir", pattern=".tif$", all.files=T, full.names=T)
dates
is a list of dates of the same length as nlayers(stack)
. A simple
creation of a datelist for the 365 days in 2014 is as.Date("2014-01-01")+0:364
If an xts
object is already made you can pass that in, but only index(xtsobj)
is used
The dates
object need not be sequentially ordered although that would be an
intuitive way to store the data - however, the dates
object must be in the same
order as the files or stack layers
Once created, you can use all raster methods including plot, show, arithemetic, etc
The layer subscripting as tr[[3]]
works. However, the main feature is that the
layer subscripting has been greatly extended. If the [[]]
index is a string then
time subscripting is invoked. The overall syntax for time subscripting is
[daterange][aggregation][cycling]
, where:
daterange - startdate [TO enddate]
- as with xts, dates can be higher level.
So even if the data is daily an index of "2010" can be used for all days in 2010
or "2010-10" for all days in October
aggregation -
UPTO WEEKS|MONTHS|QUARTERS|YEARS [BY SUM|MIN|MAX|MEAN|SD|COUNT][KEEPNA]
-
this will summarize a data set to a more aggregate time level default is to
use sum with NA's omitted
cycling - ACROSS MONTH|YEAR [BY SUM|MIN|MAX|MEAN|MEDIAN|VAR|STD|COUNT][KEEPNA]
-
this allows one to take say monthly averages across multiple years so if one
starts with monthly date from Jan 2010 to Dec 2015 ACROSS YEAR would give
stack with 12 layers (JAN-DEC) with each layer aggregated or averaged by month
across the 6 years
A full complex time subscript for daily data from 1980-2015 would be:
tr[["2000 TO 2009 UPTO MONTH ACROSS YEAR BY MEAN"]]
. This would subscript the
data to one decade, aggregate daily up to monthly data then average all 10 January
data points together giving a stack with 12 layers for the 12 months each month
being an average across the years 2000-2009
Two additional methods are supported to ensure that when a vector is extracted
across the layers it returns an xts
object:
get_ts(tr, xcoord, ycoord)
- returns an xts
object at (xcoord, ycoord)
in the raster
cellStats(tr, func, ...)
- returns an xts
object that has been
summed/averaged, etc (depending on func) across the layers
An object of class TimeRaster-class
## Not run: zip <- system.file("examples", "prismrain.zip", package = "timeraster") dir <- paste0(tempdir(), "/prismrain") dir <- "prismrain" dir.create(dir) unzip(zip, exdir = dir) files <- list.files(dir, full.names = TRUE, pattern = ".tif$", all.files = TRUE) # Create raster stack from file paths rf <- raster::stack(files) # Create a time series object library("xts") ts <- xts(1:365, as.Date("2014-01-01") + 0:364) # Create TimeRaster object ## Using new() res <- new("TimeRaster", rf, ts = ts) ## Use convenience function time_raster() ### With list of files as input - & xts object res <- time_raster(files, ts) ### With RasterStack class as input - & xts object res <- time_raster(rf, ts) ### With RasterStack class as input - & Date class object res <- time_raster(rf, as.Date("2014-01-01") + 0:364) # plot data plot(res[["2014-10-01 TO 2014-10-03"]]) plot(res[["UPTO MONTHS"]]) # Get data at a lat/long location get_ts(res, -67, 45) # Get cell stats cellStats(res, "mean") ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.