【问题标题】:Error when loading HDF files into R将 HDF 文件加载到 R 时出错
【发布时间】:2015-06-04 11:26:20
【问题描述】:

我正在尝试在 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.

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

【问题讨论】:

  • 你确定这是 hdf5 而不是 hdf4? NASA 的很多产品仍在 hdf4 中
  • 如果是 hdf4,你怎么把它输入到 R 中?
  • 似乎还没有一种简单的方法 - rgdal 看起来是最常用的方法,但您必须事先进行大量计算机设置才能使其正常工作。 link1, link2

标签: r hdf


【解决方案1】:

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

它们是 hdf4,我找到了一个简单的解决方案,可以在 R 中处理 hdf4 文件,而无需使用 gdalUtils 包中的 gdal_translate 重新编译 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),看起来您只需更改文件名并选择您想要的子集,它应该适合您。这个答案的原始帖子在这里:

Reading hdf files into R and converting them to geoTIFF rasters

【讨论】:

    猜你喜欢
    • 2017-10-05
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多