Search code examples
rgdalr-rasterhdf

Reading and working with FireModis HDF files in R


I`m trying to read a Modis Fire HDF file using the instructions provided by the hdfeos information center (https://hdfeos.org/software/r.php), this is the code:

sds<-get_subdatasets("modis_file")
#Modis_fire/MCD64A1.A2000306.h12v11.006.2017012010432.hdf"
d5<-readGDAL(sds[1],options=c("RASTERXDIM=4","RASTERYDIM=3","RASTERBDIM=2","RASTER4DIM=1","RASTER5DIM=0"))
#"HDF4_EOS:EOS_GRID:/Modis_fire/MCD64A1.A2000306.h12v11.006.2017012010432.hdf:MOD_Grid_Monthly_500m_DB_BA:Burn Date"

However, I get this error:

option 0: RASTERXDIM=4
option 1: RASTERYDIM=3
option 2: RASTERBDIM=2
option 3: RASTER4DIM=1
option 4: RASTER5DIM=0
Error in .local(.Object, ...) :

I don´t want to translate and write the HDF in other formats because I expect to do some operations like merge and crop before writing it converting:

r<-raster(d5)

Any suggestion?


Solution

  • Reading the documentation in the link provided, you're trying to create a 5-dimensional dataset.

    And my best guess is, that you're using normal GDAL (as opposed to this GEE?) which seems to be the cause for your error (according to the link):

    The RASTERXDIM, ..., RASTER4DIM options allow you to access 5-dimensional dataset and they are available only in GEE. If you use regular GDAL, you cannot access the dataset correctly.

    But if you just want to read and process the HDF files, this works just fine:

    library(MODIS)
    
    ## Note: I'm using the MODIS package to download the HDF file (not necessary if you have it on disk.
    
    # the hdf variable will be the path to the file
    hdf <- getHdf(HdfName = 'MCD64A1.A2000306.h12v11.006.2017012010432.hdf',forceDownload=T)
    
    # print the subdatasets
    
    gdalUtils::get_subdatasets(hdf)
    
    # [1] "HDF4_EOS:EOS_GRID:/tmp/Rtmp9QhqAG/MODIS_ARC/MODIS/MCD64A1.006/2000.11.01/MCD64A1.A2000306.h12v11.006.2017012010432.hdf:MOD_Grid_Monthly_500m_DB_BA:Burn Date"            
    # [2] "HDF4_EOS:EOS_GRID:/tmp/Rtmp9QhqAG/MODIS_ARC/MODIS/MCD64A1.006/2000.11.01/MCD64A1.A2000306.h12v11.006.2017012010432.hdf:MOD_Grid_Monthly_500m_DB_BA:Burn Date Uncertainty"
    # [3] "HDF4_EOS:EOS_GRID:/tmp/Rtmp9QhqAG/MODIS_ARC/MODIS/MCD64A1.006/2000.11.01/MCD64A1.A2000306.h12v11.006.2017012010432.hdf:MOD_Grid_Monthly_500m_DB_BA:QA"                   
    # [4] "HDF4_EOS:EOS_GRID:/tmp/Rtmp9QhqAG/MODIS_ARC/MODIS/MCD64A1.006/2000.11.01/MCD64A1.A2000306.h12v11.006.2017012010432.hdf:MOD_Grid_Monthly_500m_DB_BA:First Day"            
    # [5] "HDF4_EOS:EOS_GRID:/tmp/Rtmp9QhqAG/MODIS_ARC/MODIS/MCD64A1.006/2000.11.01/MCD64A1.A2000306.h12v11.006.2017012010432.hdf:MOD_Grid_Monthly_500m_DB_BA:Last Day"   
    
    sds <- gdalUtils::get_subdatasets(hdf)
    
    r <- raster(sds[1])
    
    # check raster output
    r
    
    # class       : RasterLayer 
    # dimensions  : 2400, 2400, 5760000  (nrow, ncol, ncell)
    # resolution  : 463.3127, 463.3127  (x, y)
    # extent      : -6671703, -5559753, -3335852, -2223901  (xmin, xmax, ymin, ymax)
    # coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs 
    # data source : HDF4_EOS:EOS_GRID:/tmp/Rtmp9QhqAG/MODIS_ARC/MODIS/MCD64A1.006/2000.11.01/MCD64A1.A2000306.h12v11.006.2017012010432.hdf:MOD_Grid_Monthly_500m_DB_BA:Burn Date 
    # names       : MCD64A1.A2000306.h12v11.006.2017012010432.hdf.MOD_Grid_Monthly_500m_DB_BA.Burn_Date 
    # values      : -32768, 32767  (min, max)