将自然年转换为水文年系列Rasterbrick R.

时间:2017-08-01 23:03:34

标签: r maps time-series raster

给定rasterbrick具有自然年份(Jan-Dec),如何将其转换为具有水文年代(从1st October and ending on the following 30th September开始)?与问题here类似的(但不完全相同)。

示例数据:

     nl <- 768
     s <- brick(nrows = 510,  ncols = 1068,
                   xmn = -180, xmx = 180, ymn = -90, ymx = 90,
                   crs = "+proj=longlat +datum=WGS84",
                   nl = nl,values=TRUE)
        dates <- seq(as.Date("1950-01-01"), as.Date("2013-12-31"), by = "month")
        s <- setZ(s, dates)
vals <- 1:ncell(s)
s[]=vals

1 个答案:

答案 0 :(得分:1)

我前一段时间wrote a function

to_wateryear <- function(thedate){
  thedate = as.Date(thedate)
  year = as.integer(substr(thedate, 1, 4))
  cutoff =  cutoff = as.Date(paste(year, '-10-01', sep=''))
  return(ifelse(thedate < cutoff, 
           year*1000 + thedate - as.Date(paste(year - 1, '-09-30', sep='')),
           (year + 1)*1000 + thedate - as.Date(paste(year, '-09-30', sep=''))))
}

示例:

to_wateryear(as.Date(Sys.time()))
to_wateryear('2013-10-01')
# leap year behavior
to_wateryear('2012-09-30')
to_wateryear('2013-09-30')
to_wateryear('2012-02-29')
to_wateryear('2013-03-31')

编辑根据您的评论,您实际想要的是按照水年拆分您的栅格砖,而不是用水文年代表日期。

library(raster)
nl <- 768
s <- brick(nrows = 510,  ncols = 1068, 
  xmn = -180, xmx = 180, ymn = -90, ymx = 90,
  crs = "+proj=longlat +datum=WGS84",
  nl = nl,values=TRUE)

dates <- seq(as.Date("1950-01-01"), as.Date("2013-12-31"), by = "month")
# use water year
s <- setZ(s, to_wateryear(dates))
vals <- 1:ncell(s)
s[]=vals

# split by water year
hyears = unique(getZ(s) %/% 1000)
res = vector("list", length(hyears))
for(i in 1:length(hyears))
  res[[i]] = s[[which(getZ(s) %/% 1000 == hyears[i])]]
names(res) = paste0("HY", hyears)