I'm using terra to import an hdf file from the MODIS instrument. I filter for the NDVI variable and I want to export as a tif. I'm getting errors when I do this.
You can download a raster yourself with this sample URL: https://data.lpdaac.earthdatacloud.nasa.gov/lp-prod-protected/MOD13Q1.061/MOD13Q1.A2017049.h12v04.061.2021267000402/MOD13Q1.A2017049.h12v04.061.2021267000402.hdf
Here's the code:
path = "/Users/sample/MODIS"
omi <- list.files(path, pattern = "MOD",full.names = TRUE)
for (i in omi){
print(i)
fname = paste(substr(i,86,126),'.tif',sep="")
r1 <- terra::rast(i) ## select the variable name
r2 = r1$`"250m 16 days NDVI"`
writeRaster(r2, fname)
}
Here's the error:
Error: [writeRaster] too few values for writing: 0 < 23040000
In addition: Warning messages:
1: GDreadfield() failed for block. (GDAL error 1)
2: MOD13Q1.A2016353.h12v04.061.2021363152532.hdf":MODIS_Grid_16DAY_250m_500m_VI:"250m 16 days NDVI", band 1: IReadBlock failed at X offset 0, Y offset 0: GDreadfield() failed for block. (GDAL error 1)
Where's the error here?
I would suspect a corrupted
.hdffile on your end, below the example with linked above file:Created on 2024-03-26 with reprex v2.1.0