I'm working with 1.460 HDF files (4 years daily data). I'm interested in obtaining MEAN AOD from all the files. With the following code I only obtain information from the last file, not the combination of all the files I'm working with and I'm not sure why this is not working. (I'm not interested in getting TIF files in the process, but I'm not sure if this is necessary to get what I want)
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)