简体   繁体   English

在栅格立方体中与自定义 function 聚合

[英]Aggregate with custom function over groups in raster cube

I have raster data on daily temperatures, and I want to aggregate them to monthly 95th percentile.我有每日温度的栅格数据,我想将它们聚合到每月第 95 个百分位数。 After some trying and research eg, help pages of terra::tapp or gdalcubes::aggregate_time I was not able to get monthly quantiles.经过一些尝试和研究,例如, terra::tappgdalcubes::aggregate_time的帮助页面,我无法获得每月的分位数。 Is there a way how to do that?有办法吗?

library(terra)
r <- rast( system.file("ex/elev.tif", package="terra") )
rast<-c(r,r,r,r)
time(rast)<-as.POSIXct(c("2023-01-16","2023-01-17","2023-01-18","2023-01-19"),tz="UTC")

# works as expected
rast2 <- tapp(rast, "months", max ) 

# does not work
rast2 <- tapp(rast, "months", fun = function(i) quantile(i, probs = 0.95, na.rm = T))

What do you mean with "does not work". “不起作用”是什么意思? You should at least add the error message you see, or explain why the results are not as expected.您至少应该添加您看到的错误消息,或者解释为什么结果不符合预期。

I think it works fine.我认为它工作正常。 In the example data I use two months, otherwise it makes no sense to use tapp , you could just use app if there is only one month.示例数据中我使用了两个月,否则使用tapp没有任何意义,如果只有一个月,你可以使用app

library(terra)
#terra 1.6.53
r <- rast( system.file("ex/elev.tif", package="terra") )
rast <- rep(r, 6) * 1:6
time(rast)<-as.POSIXct(c("2023-01-16","2023-01-17","2023-01-18","2023-02-17","2023-02-18","2023-02-19"),tz="UTC")

x <- tapp(rast, "months", max) 
y <- tapp(rast, "months", fun = function(i) quantile(i, probs = 0.95, na.rm = T))
y
#class       : SpatRaster 
#dimensions  : 90, 95, 2  (nrow, ncol, nlyr)
#resolution  : 0.008333333, 0.008333333  (x, y)
#extent      : 5.741667, 6.533333, 49.44167, 50.19167  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#source(s)   : memory
#names       :    m_1,    m_2 
#min values  :  408.9,  831.9 
#max values  : 1586.3, 3227.3 
#time (mnts) : Jan to Feb 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM