2

I am trying to read a .hdf file into R using:

gdalUtils::get_subdatasets()

but I am left with this error:

ERROR 4: `/var/folders/24/9_h4zhq564g3zpvqp6jmbm0c0000gn/T//RtmpibWWKQ/MODIStsp/HDFs/MCD12Q1.A2014001.h18v04.006.2018146020544.hdf' not recognized as a supported file format.
gdalinfo failed - unable to open '/var/folders/24/9_h4zhq564g3zpvqp6jmbm0c0000gn/T//RtmpibWWKQ/MODIStsp/HDFs/MCD12Q1.A2014001.h18v04.006.2018146020544.hdf'.
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'x' in selecting a method for function 'as.data.frame': subscript out of bounds
In addition: Warning message:
In system(cmd, intern = TRUE) :
  running command '"/usr/local/Cellar/gdal/3.2.0/bin/gdalinfo" "/var/folders/24/9_h4zhq564g3zpvqp6jmbm0c0000gn/T//RtmpibWWKQ/MODIStsp/HDFs/MCD12Q1.A2014001.h18v04.006.2018146020544.hdf"' had status 1

I have had issues with gdal before, and was forced to download and use an older version of gdal to get the above function to work properly. In mac terminal, running

gdalinfo --version

returns

GDAL 2.1.3, released 2017/20/01

I assume there is a build problem here. When I run the following command in the R Console:

getGDALVersionInfo()

I am returned:

"GDAL 3.2.0, released 2020/10/26"

Is there a method to "unlink" gdal from R and then relink it to the older version noted in my Mac terminal?

There is a similar problem posted here, but the (unaccepted) answer is ambigious: "You need to uninstall gdalinfo on your Mac environment and use R gdalinfo and don't forget to restart your R after uninstall it"

1 Answer 1

0

You should be able to use another gdal installation by running gdalUtils::gdal_setInstallation --- but I have no experience with it.

Or you can work around it. Below I show how you might use terra to do so. It has some functionality to make it easy to deal with files with sub-datasets.

Example file

library(terra)
hdf4 <- system.file("external/test_modis.hdf", package="gdalUtils")

Read as a SpatDataSet

s <- sds(hdf4)
names(s)
#[1] "Fpar_1km"       "Lai_1km"        "FparLai_QC"     "FparExtra_QC"   "FparStdDev_1km" "LaiStdDev_1km" 

s
#class       : SpatDataSet 
#subdatasets : 6 
#dimensions  : 1200, 1200 (nrow, ncol)
#nlyr        : 1, 1, 1, 1, 1, 1 
#resolution  : 926.6254, 926.6254  (x, y)
#extent      : -20015109, -18903159, 0, 1111951  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +R=6371007.181 +units=m +no_defs 
#names       : Fpar_1km, Lai_1km, FparLai_QC, FparExtra_QC, FparStdDev_1km, LaiStdDev_1km 

To use a subdataset, you can extract it using its index or name

a <- s[1]
b <- s["Lai_1km"]
x <- a * b
 

Or you can read the file as a SpatRaster. That is only possible if all subdatasets have the same extent and resolution. As is the case here:

r <- rast(hdf4)
names(r)
#[1] "Fpar_1km"       "Lai_1km"        "FparLai_QC"     "FparExtra_QC"   "FparStdDev_1km" "LaiStdDev_1km" 

r
#class       : SpatRaster 
#dimensions  : 1200, 1200, 6  (nrow, ncol, nlyr)
#resolution  : 926.6254, 926.6254  (x, y)
#extent      : -20015109, -18903159, 0, 1111951  (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)   : test_modis.hdf:MOD_Grid_MOD15A2:Fpar_1km  
#              test_modis.hdf:MOD_Grid_MOD15A2:Lai_1km  
#              test_modis.hdf:MOD_Grid_MOD15A2:FparLai_QC  
#              ... and 3 more source(s)
#names       : Fpar_1km, Lai_1km, FparLai_QC, FparExtra_QC, FparStdDev_1km, LaiStdDev_1km 
 
4
  • The system.file call returns "", and using both sds() and rast() returns this error: GDAL Error 4: `/private/var/folders/24/9_h4zhq564g3zpvqp6jmbm0c0000gn/T/RtmpsjAkHH/MODIStsp/HDFs/MCD12Q1.A2014001.h18v04.006.2018146020544.hdf' not recognized as a supported file format. Nov 18, 2020 at 17:18
  • Then I would uninstall (all version of) gdal, and get the latest brew install gdal and try again. Nov 18, 2020 at 17:58
  • stackoverflow.com/questions/45598772/… This link explains the issue. Installing gdal by brew doesn't allow the get_subdatasets() call to work for mac. Nov 18, 2020 at 18:00
  • Ah, yes, I see that now when trying on mac. homebrew gdal does not have hdf support. Nov 18, 2020 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.