2017-08-01 43 views
0

鉴于rasterbrick与自然年(Jan-Dec),如何将其转换为水文年(从1st October and ending on the following 30th September开始)?与问题here类似(但不完全相同)。将自然年转换为水文年时间序列Rasterbrick R

的样本数据:

 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 

回答

0

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') 

编辑从您的意见,它听起来就像你真正想要的是分裂您rasterbrick水年,而不是在水文一年方面表示日期。

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) 
+0

这可能是非常有帮助的。你可以在我的'rasterbrick,s'上测试它吗? – code123

+0

我的确提供了上面的示例数据。当我到达我的电脑时,我会尝试它。但是,请尝试让我知道它是否适用于您。谢谢。 – code123

+0

我尝试了以下,但得到的数字,而不是日期:'s < - setZ(s,to_wateryear(日期))' – code123

相关问题