2017-08-01 72 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

相關問題