Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将自然年转换为水文年时间序列Rasterr_R_Maps_Time Series_Raster - Fatal编程技术网

将自然年转换为水文年时间序列Rasterr

将自然年转换为水文年时间序列Rasterr,r,maps,time-series,raster,R,Maps,Time Series,Raster,给定一个具有自然年(Jan-Dec)的rasterbrick,如何将其转换为水文年(从10月1日开始,到9月30日结束)?与问题相似(但不完全相同) 样本数据: nl <- 768 s <- brick(nrows = 510, ncols = 1068, xmn = -180, xmx = 180, ymn = -90, ymx = 90, crs = "+proj=longlat +

给定一个具有自然年(
Jan-Dec
)的
rasterbrick
,如何将其转换为水文年(从10月1日开始,到9月30日结束)?与问题相似(但不完全相同)

样本数据:

     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
nlI刚才对此表示感谢

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=''))))
}
编辑根据您的评论,听起来您真正想要的是按水年分割您的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)
库(光栅)

nl这可能会很有帮助。你能在我的rasterbrick上测试它吗?我确实提供了上面的示例数据。我到电脑前会试试的。但是,请尝试让我知道它是否适合你。谢谢。我尝试了以下方法,但得到的是数字而不是日期:
s是的。。。您希望如何表示水年份的日期?也许您应该添加一个您期望的输出示例。类似这样的示例,但在rasterbrick
https://gist.github.com/cvitolo/e2ec4887f376e3b7d912
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)