Vaughn Smith
Vaughn Smith

Reputation: 23

How to extract time from rasterbrick?

This is probably an easy issue, but I'm having trouble extracting the dates from a rasterbrick that looks like this:

    > ndviStack
    class       : RasterBrick 
    dimensions  : 315, 317, 99855, 221  (nrow, ncol, ncell, nlayers)
    resolution  : 30, 30  (x, y)
    extent      : 232611.4, 242121.4, 2298929, 2308379  (xmin, xmax, 
                  ymin, ymax)
    coord. ref. : +proj=utm +zone=16 +datum=WGS84 +units=m +no_defs 
                  +ellps=WGS84 +towgs84=0,0,0 
    data source : /Users/vaughnsmith/Desktop/CR_Y_Data/Yucatan/area1/data/ndvi_stack_00_15.grd 
    names       : LE70200462000007, LE70200462000055, LE70200462000119, 
                  LE70200462000183, LE70200462000279, LE70200462000295, 
                  LE70200462000311, LE70200462001041, LE70200462001073, 
                  LE70200462001105, LE70200462001169, LE70200462001185, 
                  LE70200462001201, LE70200462001249, LE70200462001281, ... 

    min values  :             2077,             -758,            -1310,              664,             4413,             2121,             1294,             2312,             1138,             -639,             2433,             3138,              991,              150,             1384, ... 
    max values  :            10000,             8087,            20000,             8729,             8989,            20000,             9277,            20000,             7806,             8489,             8839,            20000,             9345,             9281,             9700, ... 
    time        : 2000-01-07, 2015-12-26 (min, max)

The last row, time, is what I need. I thought it might be as easy as the names(ndviStack) function to return names, but that's not the case. Could someone please assist? Thank you.

Upvotes: 1

Views: 1180

Answers (2)

Rodrigo Lustosa
Rodrigo Lustosa

Reputation: 76

A bit late, but you can get your dates using the getZ function (and set dates using setZ, as shown here). @loki answer is a nice path, but the date already is in your rasterbrick as an character (in your time row) and is that what getZ extract. Reproducible example:

ras <- raster(matrix(1:20,4,5))
my_brick <- brick(ras,ras,ras)
my_brick <- setZ(my_brick,c("2000-01-07", "2000-01-10","2015-12-26"))

Which prints as:

> my_brick
class      : RasterBrick 
dimensions : 4, 5, 20, 3  (nrow, ncol, ncell, nlayers)
resolution : 0.2, 0.25  (x, y)
extent     : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : memory
names      : layer.1, layer.2, layer.3 
min values :       1,       1,       1 
max values :      20,      20,      20 
time       : 2000-01-07, 2000-01-10, 2015-12-26 

And finally, you can get your dates with:

> getZ(my_brick)
[1] "2000-01-07" "2000-01-10" "2015-12-26"

It will be in a character class. You can coerce it to date with as.Date (as shown by @loki):

as.Date(getZ(my_brick))

Upvotes: 1

loki
loki

Reputation: 10350

We can achieve this from the layer names which you can acquire with names(ndviStack).
First some names to get a reproducible example:

nams <- c("LE70200462000007", "LE70200462000055", "LE70200462000119", 
"LE70200462000183", "LE70200462000279", "LE70200462000295", 
"LE70200462000311", "LE70200462001041", "LE70200462001073", 
"LE70200462001105", "LE70200462001169", "LE70200462001185")

Using as.POSIXct

nams <- substr(nams, nchar(nams)-6, nchar(nams))
datesfromnames <- as.POSIXct(nams, format = "%Y%j", tz = "UTC")
# "2000-01-07 UTC" "2000-02-24 UTC" "2000-04-28 UTC" "2000-07-01 UTC" "2000-10-05 UTC" 
# "2000-10-21 UTC" "2000-11-06 UTC" "2001-02-10 UTC" "2001-03-14 UTC" "2001-04-15 UTC" 
# "2001-06-18 UTC" "2001-07-04 UTC"

Using as.Date

First, we divide the names into years and days.

nams <- substr(nams, nchar(nams)-6, nchar(nams))
years <- substr(nams, 1, 4)
days <- substr(nams, 5, 7)

Then, we create a data.frame

dates <- data.frame(years, days)
dates
#    years days
# 1   2000  007
# 2   2000  055
# 3   2000  119
# 4   2000  183
# 5   2000  279
# 6   2000  295
# 7   2000  311
# 8   2001  041
# 9   2001  073
# 10  2001  105
# 11  2001  169
# 12  2001  185

They hold now the years and the number of number of the day in the year. These variables can be used to generate Date objects.

dates$dates <- apply(dates, 1, 
                     function(x){as.Date(as.numeric(x[2]), origin = paste0(x[1], "-01-01"))})
as.Date(dates$dates-1, origin = '1970-01-01')

#    years days      dates
# 1   2000  007 2000-01-07
# 2   2000  055 2000-02-24
# 3   2000  119 2000-04-28
# 4   2000  183 2000-07-01
# 5   2000  279 2000-10-05
# 6   2000  295 2000-10-21
# 7   2000  311 2000-11-06
# 8   2001  041 2001-02-10
# 9   2001  073 2001-03-14
# 10  2001  105 2001-04-15
# 11  2001  169 2001-06-18
# 12  2001  185 2001-07-04

Upvotes: 1

Related Questions