在R中读取和使用FireModis HDF文件

时间:2019-01-03 14:34:04

标签: r gdal r-raster hdf

我正在尝试使用hdfeos信息中心(https://hdfeos.org/software/r.php)提供的说明读取Modis Fire HDF文件,这是代码:

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"

但是,出现此错误:

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

我不想翻译和编写其他格式的HDF,因为我希望在进行转换之前先进行合并和裁剪等操作:

r<-raster(d5)

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

阅读提供的link中的文档,您正在尝试创建5维数据集。

我最好的猜测是,您使用的是普通的GDAL(而不是此GEE?),这似乎是导致错误的原因(根据link):

  

使用RASTERXDIM,...,RASTER4DIM选项可以访问5维数据集,并且仅在GEE中可用。如果使用常规GDAL,则无法正确访问数据集。

但是,如果您只想读取和处理HDF文件,则效果很好:

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)