hdf5h5pypytables

Accessing hdf5 file datablock using tables rather than h5py


I had had some success reading hdf5 files generated from MSC/Nastran using both h5py and tables. Now I’m trying to do the same with a file generated by Simcenter/Nastran and I’m having trouble because the data block includes a colon in the names.

This Syntax works using h5py:

hf=h5py.File(‘solidbeam-3prtfem-sol101.hdf','r')
myElm=hf['model']['geom2:elem_data']['geom2_chexa']

Typically I would try the following for tables:

hft=tables.open_file(‘solidbeam-3prtfem-sol101.hdf','r')
myElmTable=hft/model/geom2:elem_data/geom2_chexa

The : character in geom2:elem does not work.

The following syntax does work and prints item names as follows:

for obj in hft:
   print obj

/model/geom2:elem_data/geom2_chexa (Table(40,)) 

Do you have any advice on how this data-block should be referenced in tables?

Any help will be greatly appreciated.


Solution

  • As @hpaulj noted, variable names can't include special characters (like -, :, +, etc). Your PyTables (aka tables) method to access objects is called "natural naming". PyTables has a .get_node() method to get around this limitation and will work with these object names. Note: when an object name has special characters, PyTables will issue a warning that you can't access that object with "natural naming" (as you already discovered). It's just a warning.

    Modified code for PyTables from above:

    hft = tables.open_file('solidbeam-3prtfem-sol101.hdf','r')
    myElmTable = hft.get_node('/model/geom2:elem_data/geom2_chexa')
    

    As an aside, I suggest using Python's context manager to open files. That way the file is closed automatically when you exit that code block. Example:

    import tables as tb
    with tb.open_file('solidbeam-3prtfem-sol101.hdf','r') as hft:
        myElmTable = hft.get_node('/model/geom2:elem_data/geom2_chexa')