time.Rd
Get or set the time of the layers of a SpatRaster. Time can be stored as POSIXlt
(date and time, with a resolution of seconds, and a time zone), Date
, "months", "years", or "yearmonths".
# S4 method for SpatRaster
time(x, format="")
# S4 method for SpatRaster
time(x, tstep="")<-value
# S4 method for SpatRaster
timeInfo(x)
SpatRaster or SpatRasterDataset
One of "", "seconds" (POSIXlt), "days" (Date), "yearmonths" (decimal years), "years", "months". If "", the returned format is (based on) the format that was used to set the time
Date
, POSIXt
, yearmon
(defined in package zoo), or numeric
One of "years", "months", "yearmonths". Used when value
is numeric. Ignored when value
is of type Date
, POSIXt
, or yearmon
time
: POSIXlt, Date, or numeric
timeInfo
: data.frame
s <- rast(system.file("ex/logo.tif", package="terra"))
# Date"
d <- as.Date("2001-05-04") + 0:2
time(s) <- d
time(s)
#> [1] "2001-05-04" "2001-05-05" "2001-05-06"
# POSIX (date/time with a resolution of seconds)
time(s) <- as.POSIXlt(d)
time(s)
#> [1] "2001-05-04 UTC" "2001-05-05 UTC" "2001-05-06 UTC"
# with time zone
time(s) <- as.POSIXlt(Sys.time(), "America/New_York") + 0:2
time(s)
#> [1] "2023-05-25 05:46:15 EDT" "2023-05-25 05:46:16 EDT"
#> [3] "2023-05-25 05:46:17 EDT"
timeInfo(s)
#> time step zone
#> 1 TRUE seconds America/New_York
# years
time(s, tstep="years") <- 2000 + 0:2
s
#> class : SpatRaster
#> dimensions : 77, 101, 3 (nrow, ncol, nlyr)
#> resolution : 1, 1 (x, y)
#> extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax)
#> coord. ref. : Cartesian (Meter)
#> source : logo.tif
#> colors RGB : 1, 2, 3
#> names : red, green, blue
#> min values : 0, 0, 0
#> max values : 255, 255, 255
#> time (years): 2000 to 2002
time(s, tstep="months") <- 1:3
s
#> class : SpatRaster
#> dimensions : 77, 101, 3 (nrow, ncol, nlyr)
#> resolution : 1, 1 (x, y)
#> extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax)
#> coord. ref. : Cartesian (Meter)
#> source : logo.tif
#> colors RGB : 1, 2, 3
#> names : red, green, blue
#> min values : 0, 0, 0
#> max values : 255, 255, 255
#> time (mnts) : Jan to Mar