Project

General

Profile

Actions

AddingMetadata

Adding Metadata

This page is intended for those users who need to add or modify the VizSchema metadata in their data files.

Getting PyTables

The PyTables module for Python implements common HDF5 routines for adding, removing, and modifying HDF5 data files. This provides a fast and straight-forward way to edit the metadata on a data file.
PyTables depends on the NumPy package, so both must be downloaded before beginning:
NumPy
PyTables

Writing Scripts using PyTables

Your Python script must begin by importing the two packages above:

import tables
import numpy

Next you must open the particular data file to be edited. The first argument is the name of the file to open - this is obviously a good place to use a command-line argument. The second argument is the mode to open the file - 'a' is used to append data to an existing file. Avoid the use of 'w', which will replace an existing file with a blank file for writing.

h5file = tables.openFile(fileName, mode='a')

As an aside, all of the PyTables commands may be more safely used by wrapping in a try-catch block like so:

try:
  h5file = tables.openFile(fileName, mode='a')
except AttributeError, inst:
  print "Error opening file: ", inst
  print "Exiting program"
  sys.exit()

With the file opened, the next step is to retrieve the node to attach the attribute to. Nodes may be retrieved with the following code. (Note that nodes are referred to using absolute path names from the root of the file.)

mesh = h5file.getNode("/fluids/dc")

With the node retrieved, attributes may be accessed and changed using the _f_getAttr and _f_setAttr methods:

mesh._f_setAttr("vsType", "mesh")
mesh._f_setAttr("vsLowerBounds", numPy.array({0.0, 0.0, 0.0})

As a shorthand notation for the preceding calls, the attribute names may be used to directly access their values. The following code achieves the same results as the preceding block of code:

mesh.vsType = "mesh"
mesh.vsLowerBounds = numPy.array({0.0, 0.0, 0.0})

Finally, when all metadata changes have been made, the file must be closed again:

h5file.close()

Example

The following is a trimmed version of a script used to fix a metadata typo in a data file. In this case, the attribute "vsMesh" was accidentally spelled without the uppercase "M", and needs to be fixed before VizSchema will recognize it.

#!/usr/bin/env python
"""
Example script for adding VizSchema data.

"""

import sys, os, getopt
import re
import tables
import numpy

def usage():
  print "changeVsMesh.py -f <filename>"

def main():
# Get the file name from command line                                                                              
  try:
    opts, args = getopt.getopt(sys.argv[1:], "f:", ["file="])
  except getopt.GetoptError:
# print help information and exit:
    usage()
    sys.exit(2)

# Get the file name                                                                             
  fileName = None
  for o, a in opts:
    if o in ("-f", "--file"):
      fileName = a

  if fileName == None:
    usage()
    print "Unable to find filename in argument list: ",sys.argv
    sys.exit()

# Open the file                                                                                 
  try:
    h5file = tables.openFile(fileName, mode='a')
  except:
    print "Unable to open the file '" + fileName + "'."
    raise
  print "File '" + fileName + "' opened."

  #Fix up variable totalEnergyAbs
  print "Loading dataset totalEnergyAbs"                                                       
  dataSet = h5file.getNode("/Absorbed/totalEnergyAbs")
  print "Getting value of vsmesh"
  oldValue = dataSet._f_getAttr("vsmesh")
  print "Value is " + oldValue
  print "Removing vsmesh (lowercase m)"
  dataSet._f_delAttr("vsmesh")
  print "Adding vsMesh (uppercase m)"
  dataSet._f_setAttr("vsMesh", oldValue)

  print "Done, closing file"
  h5file.close()
  sys.exit()

if __name__ == "__main__":
  main()

Updated by Ted Sume about 5 years ago · 1 revisions