terra
terra copied to clipboard
Reading multiple MODIS LST files (MOD11A2 product) in HDF format with `rast()` fails, file does not exist
Messing around with some MODIS LST data (MOD11A2 acquired via USGS EarthExpolorer to be precise) in order to test tapp()
, I spend some time now understandig why my workflow fails and it seems like rast()
would not accept more than two HDF-EOS files before something breaks:
library(terra)
#> terra 1.7.71
# MOD11A2, tile h18v03, year 2023 (=46 files)
fnames <- list.files(pattern = "hdf$")
head(fnames, 3)
#> [1] "MOD11A2.A2023001.h18v03.061.2023015004633.hdf"
#> [2] "MOD11A2.A2023009.h18v03.061.2023020002349.hdf"
#> [3] "MOD11A2.A2023017.h18v03.061.2023027004006.hdf"
# 1 file = 12 layers, looking ok
r1 <- rast(fnames[1])
r1 * 1
#> class : SpatRaster
#> dimensions : 1200, 1200, 12 (nrow, ncol, nlyr)
#> resolution : 926.6254, 926.6254 (x, y)
#> extent : 0, 1111951, 5559753, 6671703 (xmin, xmax, ymin, ymax)
#> coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +R=6371007.181 +units=m +no_defs
#> source(s) : memory
#> varname : MOD11A2.A2023001.h18v03.061.2023015004633
#> names : LST_Day_1km, QC_Day, Day_v~_time, Day_v~_angl, LST_N~t_1km, QC_Night, ...
#> min values : 242.40, 2, 9.7, -65, 247.56, 2, ...
#> max values : 284.94, 177, 12.6, 65, 282.54, 177, ...
# 2 files = 24 layers, still ok
r2 <- rast(fnames[1:2])
r2 * 1
#> class : SpatRaster
#> dimensions : 1200, 1200, 24 (nrow, ncol, nlyr)
#> resolution : 926.6254, 926.6254 (x, y)
#> extent : 0, 1111951, 5559753, 6671703 (xmin, xmax, ymin, ymax)
#> coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +R=6371007.181 +units=m +no_defs
#> source(s) : memory
#> names : LST_Day_1km, QC_Day, Day_v~_time, Day_v~_angl, LST_N~t_1km, QC_Night, ...
#> min values : 242.40, 2, 9.7, -65, 247.56, 2, ...
#> max values : 284.94, 177, 12.6, 65, 282.54, 177, ...
# all of a sudden, file not found?
r3 <- rast(fnames[1:3])
r3 * 1
#> Error: [*] file does not exist: HDF4_EOS:EOS_GRID:"MOD11A2.A2023017.h18v03.061.2023027004006.hdf":MODIS_Grid_8Day_1km_LST:Emis_31
# this worked before, now it does not
r1 * 1
#> Error: [*] file does not exist: HDF4_EOS:EOS_GRID:"MOD11A2.A2023001.h18v03.061.2023015004633.hdf":MODIS_Grid_8Day_1km_LST:LST_Day_1km
Obviously, you would need this data to reproduce the issue, I guess... But I also don't want to violate any guidelines by just uploading this here.
Do you have access to Earth Explorer (any other tile would work too, probably)? Or is there any other way I can hand over these files?
sessionInfo()
#> R version 4.3.3 (2024-02-29 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19045)