0

我正在尝试在 R 中使用叶绿素水平的 HDF 数据,我已经rhdf5安装并运行了包,但是当我尝试在其中加载我的 HDF 数据时,我得到了错误行。

这是我正在使用的代码

library("rhdf5")
library(maps)

June_data<-h5ls('./Data/June Chloro level.hdf')
June_data

它产生的错误是

HDF5-DIAG: Error detected in HDF5 (1.8.7) thread 0:
#000: H5F.c line 1522 in H5Fopen(): unable to open file
major: File accessability
minor: Unable to open file
#001: H5F.c line 1313 in H5F_open(): unable to read superblock
major: File accessability
minor: Read failed
#002: H5Fsuper.c line 334 in H5F_super_read(): unable to find file signature
major: File accessability
minor: Not an HDF5 file
#003: H5Fsuper.c line 155 in H5F_locate_signature(): unable to find a valid file signature
major: Low-level I/O
minor: Unable to initialize object
HDF5: unable to open file
Error in h5checktypeOrOpenLoc(file, readonly = TRUE) : 
Error in h5checktypeOrOpenLoc(). File './Data/June Chloro level.hdf' is not a valid HDF5 file.

我浏览了谷歌,发现其他人有这个问题,但不知道如何解决它。

4

1 回答 1

1

我不确定您是否已经解决了您的问题,甚至还不确定您的文件是 hdf4 还是 hdf?我遇到了类似的问题并从这里下载了 HDFView以快速检查我的文件是 hdf4 还是 5。

它们是 hdf4,我找到了一个简单的解决方案,可以在 R 中使用 hdf4 文件,而不必gdal_translategdalUtils包中重新编译 gdal 或类似的东西。这是我最终用来让我的 hdf 文件工作的代码:

library(gdalUtils)

# Provides detailed data on hdf4 files but takes ages

gdalinfo("MOD17A3H.A2000001.h21v09.006.2015141183401.hdf")

# Tells me what subdatasets are within my hdf4 MODIS files and makes them into a list

sds <- get_subdatasets("MOD17A3H.A2000001.h21v09.006.2015141183401.hdf")
sds

[1] "HDF4_EOS:EOS_GRID:MOD17A3H.A2000001.h21v09.006.2015141183401.hdf:MOD_Grid_MOD17A3H:Npp_500m"   
[2] "HDF4_EOS:EOS_GRID:MOD17A3H.A2000001.h21v09.006.2015141183401.hdf:MOD_Grid_MOD17A3H:Npp_QC_500m"

# I'm only interested in the first subdataset and I can use gdal_translate to convert it to a .tif

gdal_translate(sds[1], dst_dataset = "NPP2000.tif")

# Load and plot the new .tif

rast <- raster("NPP2000.tif")
plot(rast)

# If you have lots of files then you can make a loop to do all this for you

files <- dir(pattern = ".hdf")
files

 [1] "MOD17A3H.A2000001.h21v09.006.2015141183401.hdf" "MOD17A3H.A2001001.h21v09.006.2015148124025.hdf"
 [3] "MOD17A3H.A2002001.h21v09.006.2015153182349.hdf" "MOD17A3H.A2003001.h21v09.006.2015166203852.hdf"
 [5] "MOD17A3H.A2004001.h21v09.006.2015099031743.hdf" "MOD17A3H.A2005001.h21v09.006.2015113012334.hdf"
 [7] "MOD17A3H.A2006001.h21v09.006.2015125163852.hdf" "MOD17A3H.A2007001.h21v09.006.2015169164508.hdf"
 [9] "MOD17A3H.A2008001.h21v09.006.2015186104744.hdf" "MOD17A3H.A2009001.h21v09.006.2015198113503.hdf"
[11] "MOD17A3H.A2010001.h21v09.006.2015216071137.hdf" "MOD17A3H.A2011001.h21v09.006.2015230092603.hdf"
[13] "MOD17A3H.A2012001.h21v09.006.2015254070417.hdf" "MOD17A3H.A2013001.h21v09.006.2015272075433.hdf"
[15] "MOD17A3H.A2014001.h21v09.006.2015295062210.hdf"

filename <- substr(files,11,14)
filename <- paste0("NPP", filename, ".tif")
filename

[1] "NPP2000.tif" "NPP2001.tif" "NPP2002.tif" "NPP2003.tif" "NPP2004.tif" "NPP2005.tif" "NPP2006.tif" "NPP2007.tif" "NPP2008.tif"
[10] "NPP2009.tif" "NPP2010.tif" "NPP2011.tif" "NPP2012.tif" "NPP2013.tif" "NPP2014.tif"

i <- 1

for (i in 1:15){
  sds <- get_subdatasets(files[i])
  gdal_translate(sds[1], dst_dataset = filename[i])
}

它不会将它们读入 R,因此在转换它们之前无法对其进行操作,因此值得为您的 hdf 文件找到最小的地理范围,这样您就不必等待很长时间。

对于您的数据(假设它的 hdf4),看起来您可以更改文件名并选择您想要的子集,它应该适合您。这个答案的原始帖子在这里:

将 hdf 文件读入 R 并将它们转换为 geoTIFF 栅格

于 2016-04-22T13:57:41.280 回答