2017-08-01 4 views
0

自然年(Jan-Dec)のrasterbrickがある場合、どのように水文学年に変換することができますか(から始まる)? hereの質問に類似していますが、正確ではありません。自然年を水文年の時系列に変換する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

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

EDITあなたのコメントから、それはあなたが実際にはかなり水文年の面で日付を表すよりも、水の年でスプリットあなた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

これは非常に役に立ちます。私の「ラスターブリック」でテストできますか? – code123

+0

上記のサンプルデータを提供しました。私は自分のコンピュータに着いたらそれを試してみる。しかし、それはあなたのために動作する場合、plsしようと私に知らせてください。ありがとう。 – code123

+0

私は以下を試しましたが、日付の代わりに数字がありました: 's < - setZ(s、to_wateryear(dates))' – code123

関連する問題