簡體   English   中英

在柵格立方體中與自定義 function 聚合

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

我有每日溫度的柵格數據,我想將它們聚合到每月第 95 個百分位數。 經過一些嘗試和研究,例如, terra::tappgdalcubes::aggregate_time的幫助頁面,我無法獲得每月的分位數。 有辦法嗎?

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

“不起作用”是什么意思? 您至少應該添加您看到的錯誤消息,或者解釋為什么結果不符合預期。

我認為它工作正常。 示例數據中我使用了兩個月,否則使用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