The easiest way to do this is by passing off all the actual work to python.
This requires that you have a python environment that has pyhdf
installed. Honestly, the bulk of the work is going to be actually installing pyhdf
, and I'm afraid that I can't help you there.
Assuming that you do have a python environment with pyhdf
installed, the rest of this answer is for you.
What's going to happen is we're going to ask pyhdf
for the results using ExternalEvaluate
. If you've used python before the code is actually very simple.
First, we need to set up an ExternalEvaluate session:
session =
StartExternalSession[<|"System" -> "Python", "Version" -> "3.7.1",
"Executable" -> "/usr/local/bin/python3.7"|>]
Here I'm linking the session to my local python install that has pyhdf
installed. If you got pyhdf
using conda
or similar, you'll want to find what the right path is for your executable.
Now that we have a session, we set up the pyhdf
library:
ExternalEvaluate[session, "from pyhdf.SD import SD, SDC"]
And now we can for instance read information about the datasets in the file. This returns a native Wolfram Language association.
ExternalEvaluate[session, "hdf.datasets()"]
Now, let's use the Land/SeaMask
dataset as an example, and read its attributes:
ExternalEvaluate[session, "hdf.select('Land/SeaMask').attributes()"]
<|"valid_range" -> {0, 7}, "_FillValue" -> 221|>
Fantastic. Well, there's your question answered, really!
We can make this a nice function like so:
readHDFDatasetAttributes[file_, dataset_] :=
ExternalEvaluate[<|"System" -> "Python", "Version" -> "3.7.1",
"Executable" -> "/usr/local/bin/python3.7"|>, StringTemplate["
from pyhdf.SD import SD, SDC
hdf = SD('`file`', SDC.READ)
hdf.select('`dataset`').attributes()
"][<|"file" -> file, "dataset" -> dataset|>]]
A final note: reading the dataset from python and reading it natively result in exactly the same data, which I find pleasing:
ExternalEvaluate[session, "hdf.select('Land/SeaMask')[:,:]"] ==
Import[hdffile, {"Datasets", "Land/SeaMask"}]
True
ExternalEvaluate
, Python, andpyhdf
(akapython-hdf4
) to make this happen... Be warned, though,pyhdf
can be hell to install. For anybody interested, a large number of HDF4 files are available here. To be clear, I think you're trying to access the attributes of the HDF dataset, right? $\endgroup$h4toh5
: support.hdfgroup.org/products/hdf5_tools/h4toh5/index.html) and then useImport
. $\endgroup$