如何获取光栅砖年度最大网格单元值的日期? [英] How to obtain Dates of annual maximum gridcell values of rasterbrick?

查看:7
本文介绍了如何获取光栅砖年度最大网格单元值的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能得到两个栅格,分别给出每个网格单元每年的最大值,并给出最大值出现的日期.下面是可重现的示例,其中包含我已实施的一些步骤.

How can i get two rasters that gives the maximum value for each grid cells per year and also gives the dates on which that maximum value has occured. Below is the reproducible example with some steps i have implemented.

library(raster)

# Create a raster

r1 <- raster(nrow=10, ncol=7)
r <- stack(setValues(r1, runif(ncell(r1))),
           setValues(r1, runif(70 ,0.6,0.9)),
           setValues(r1, runif(70 ,0.2,0.4)),
           setValues(r1, runif(70 ,1,2)),
           setValues(r1, runif(70 ,0.5,1.0)),
           setValues(r1, runif(70 ,0.3,0.9)),
           setValues(r1, runif(70 ,1,2)))
r

# Make Dates. This is random, i have about 24000 values.

Dates<-data.frame(Date=c("2000-01-02","2000-01-03","2000-02-03",
           "2001-09-02","2001-09-03","2001-10-01",
           "2001-10-02"))

Date_val<-as.Date(Dates$Date,format="%Y-%m-%d")
Date_val

r.dt<-setZ(r,Date_val)

# Get indices to make annual maxima value for each grid cells

indices <- format(as.Date(getZ(r.dt), format = "%Y-%b-%d"), format = "%Y")

# Implement stackApply to get maximum value each year.

rmax<-stackApply(r.dt,indices = indices,fun=max,na.rm=T)
plot(rmax)

推荐答案

你可以做

wmax <- stackApply(r.dt, indices = indices, fun=function(i,...) which.max(i))

获取引用日期向量的索引

to get the indices that refer to the Date vector

要获取(整数)日期表示,即自 1970 年 1 月 1 日以来的天数,您可以为每一年做这样的事情

To get (integer) date representations, i.e., the number of days since 1970-01-01, you could do something like this, for each year

m2000 = data.frame(from=1:3, to=as.integer(Date_val)[1:3])
x <- subs(wmax[[1]], m2000)

这篇关于如何获取光栅砖年度最大网格单元值的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆