我想使用 R 中的 raster 包将每日数据(35 年)聚合到月份,然后计算季节平均值(我知道如何使用 CDO)。以下是我的代码,它输出了所有年份的四个季节平均值(140 层)。如何循环输出仅包含四个季节的 4 个图层?感谢您的帮助。
dailydata <- brick ("dailyrain.nc")
dates <- seq(as.Date("1981-01-01"), as.Date("2015-12-31"), by="day")
months <- format(dates, "%Y-%m")
Aggregate2Monthly <- function(x) {
agg <- aggregate(x, by=list(months), sum)
return(agg$x)
}
mothlydata <- calc(dailydata, Aggregate2Monthly)
mondates <- seq(as.Date("1981-01-01"), as.Date("2015-12-31"), by="month")
years <- format(mondates, "%Y")
seasons.def=c(1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4)
years.seasons <- paste(years, seasons.def, sep="-")
nyears <- years[!duplicated(years)]
nseas <- seasons.def[!duplicated(seasons.def)]
Aggregate2Seasons <- function(x) {
agg <- aggregate(x, by=list(years.seasons), mean)
return(agg$x)
}
seasonsdata <- calc(mothlydata, Aggregate2Seasons)