5

I have a HDF4 file I need to read with python. For this I use pyhdf. In most cases I am quite happy to use SD class to open the file:

import pyhdf.SD  as SD
hdf = SD.SD(hdfFile)

and then continue with

v1 = hdf.select('Data set 1')
v2 = hdf.select('Data set 2')

However I have several groups in the HDF file and some variables appear in more than one group having the same name:

In Group 1 I have Data set 3 and in Group 2 I have Data set 3 so my select command will only select one of then I guess (without me knowing which one?).

Is there a simple way of selecting (reading) Data set 3 from Group 1 and then from Group 2?

I have looked at the V and VS modules. I found an example script that will loop through all groups and subgroups etc. and find all variables (data sets). But I have now Idea of how to connect those variables to the parent, as for me to know into which group they belong.

red_tiger
  • 1,402
  • 3
  • 16
  • 32

1 Answers1

0

I think that pyhdf might not be the best choice for this particular task. Have you looked at PyNIO?

From the HDF section of their documentation:

PyNIO has a read-only ability to understand HDF Vgroups. When a variable that is part of a Vgroup is encountered, PyNIO appends a double underscore and the group number to the end of the variable name. This ensures that the variable will have a unique name, relative to variables that belong to other Vgroups. It also provides two additional attributes to the variable: hdf_group, whose value is the HDF string name of the group, and hdf_group_id, whose value is the same as the group number appended to the end of the variable name.

Heather QC
  • 680
  • 8
  • 11
  • Thanks for the link to PyNIO, I will have a look at that. From a first glance it looks quite promising. – red_tiger Oct 14 '15 at 08:43
  • I'll be interested to hear how it goes. I've used PyNIO extensively for HDF-EOS files but never plain HDF4. I assume the variable names will be similar though. Here's example code for reading in the file : http://stackoverflow.com/questions/27255869/read-specific-z-component-slice-of-3d-hdf-from-python/32786460#32786460 and then a print file_handle.variables will show you what you have to work with. Good luck! – Heather QC Oct 14 '15 at 16:17