这可能是一个简单的问题,但我无法从栅格砖中提取日期,如下所示:
> 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)
最后一行,时间,是我需要的。我认为它可能像名称(ndviStack)函数一样容易返回名称,但事实并非如此。有人可以帮忙吗?谢谢。
答案 0 :(得分:1)
我们可以使用names(ndviStack)
获取的图层名称来实现此目的
首先是一些名称,以获得可重现的例子:
nams <- c("LE70200462000007", "LE70200462000055", "LE70200462000119",
"LE70200462000183", "LE70200462000279", "LE70200462000295",
"LE70200462000311", "LE70200462001041", "LE70200462001073",
"LE70200462001105", "LE70200462001169", "LE70200462001185")
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"
as.Date
首先,我们将名称分为年和日。
nams <- substr(nams, nchar(nams)-6, nchar(nams))
years <- substr(nams, 1, 4)
days <- substr(nams, 5, 7)
然后,我们创建一个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
他们现在掌握了一年中的年份和数量。这些变量可用于生成Date
个对象。
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