0

I am trying to use HDF data of Chlorophyll levels in R, I have the package rhdf5 installed and running yet when I try and load my HDF data in I get lines of errors.

Here is the code I am using

library("rhdf5")
library(maps)

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

The error it produces is

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.

I have looked through google and have found other people with this problem but no idea how to fix it.

Marc in the box
  • 11,769
  • 4
  • 47
  • 97
  • Are you sure this is hdf5 and not hdf4? A lot of NASA products are still in hdf4 – Marc in the box Jun 04 '15 at 12:08
  • If it is hdf4 how would you get it into R? – Rory Graham Jun 08 '15 at 12:02
  • There doesn't seem to be an easy way yet - `rgdal` looks to be theway most use, but you have to do a lot of computer set up beforehand to get it to work properly. [link1](http://gis.stackexchange.com/questions/86282/processing-hdf4-data-in-r), [link2](https://everydropr.wordpress.com/2011/11/06/how-to-processing-hdf4-data-using-r/) – Marc in the box Jun 08 '15 at 13:27

1 Answers1

1

I'm not sure if you've solved your problem yet or even figured out if your files are hdf4 or hdf? I was having similar problems and downloaded HDFView from here to quickly check whether my files were hdf4 or 5.

They were hdf4 and I found an easy solution for working with hdf4 files in R without having to mess about with recompiling gdal or similar using gdal_translate from the gdalUtils package. This is the code I eventually used to get my hdf files to work:

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])
}

It doesn't read them into R so there's no way to manipulate them before converting them so its worth finding the smallest geographic extent possible for your hdf files so you're not waiting ages.

For your data (assuming its hdf4) it looks like you could just change the filename and select what subset you want and it should work for you. The original post for this answer is here:

Reading hdf files into R and converting them to geoTIFF rasters

Community
  • 1
  • 1
James
  • 1,164
  • 2
  • 15
  • 36