我正在處理 1.460 HDF 檔案(4 年的每日資料)。我有興趣從所有檔案中獲取 MEAN AOD。使用以下代碼,我只從最后一個檔案中獲取資訊,而不是我正在使用的所有檔案的組合,我不確定為什么這不起作用。(我對在此程序中獲取 TIF 檔案不感興趣,但我不確定這是否有必要獲得我想要的內容)
read_hdf = function(file, n) {
sds = get_subdatasets(file)
f = tempfile(fileext = ".tif")
gdal_translate(sds[1], f)
brick(f)
}
files <- dir(pattern = ".hdf")
for(f in files) {
sds = get_subdatasets(f)
Optical_Depth_047 = read_hdf(f, grep("grid1km:Optical_Depth_047", sds))
names(Optical_Depth_047) = paste0("Optical_Depth_047.", letters[1:nlayers(Optical_Depth_047)])
r = stack(Optical_Depth_047)
}
meanAOD <- stackApply(r, indices = rep(1,nlayers(r)), fun = "mean", na.rm = T)
uj5u.com熱心網友回復:
歡迎來到 Stack Overflow M. Fernanda Valle Seijo!為了將來參考,請嘗試發布可重現的示例。您可以在此處查看它們的指南。這些可以幫助回答您問題的人更好地診斷問題。
我認為您的代碼走在正確的軌道上,但看起來您只獲得最后一項的原因是您stack()在回圈中包含了該函式。這將r在回圈的每次迭代中覆寫。此外,您可能需要先設定Optical_Depth_047為一個串列,然后將回圈的每次迭代保存為該串列的一個元素。嘗試像這樣運行你的代碼:
read_hdf = function(file, n) {
sds = get_subdatasets(file)
f = tempfile(fileext = ".tif")
gdal_translate(sds[1], f)
brick(f)
}
files <- dir(pattern = ".hdf")
Optical_Depth_047<-list()
for(f in files) {
sds = get_subdatasets(f)
Optical_Depth_047[[f]] = read_hdf(f, grep("grid1km:Optical_Depth_047", sds))
names(Optical_Depth_047)[f] = paste0("Optical_Depth_047.", letters[f])
}
r = stack(Optical_Depth_047)
meanAOD <- stackApply(r, indices = rep(1,nlayers(r)), fun = "mean", na.rm = T)
uj5u.com熱心網友回復:
與terra您可以采取一些快捷鍵,你可以直接讀取HDF檔案。你可以做
library(terra)
r <- rast(f, "grid1km:Optical_Depth_047")
也許你所追求的可以像這樣簡單:
x <- rast(files, "grid1km:Optical_Depth_047")
m <- mean(x)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/394824.html
上一篇:如何從目錄加載jpeg圖片
