AddingMetadata » History » Version 1
Ted Sume, 11/13/2019 03:51 PM
1 | 1 | Ted Sume | # AddingMetadata |
---|---|---|---|
2 | |||
3 | # Adding Metadata |
||
4 | |||
5 | This page is intended for those users who need to add or modify the VizSchema metadata in their data files. |
||
6 | |||
7 | **Getting PyTables** |
||
8 | |||
9 | 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. |
||
10 | PyTables depends on the NumPy package, so both must be downloaded before beginning: |
||
11 | [NumPy](http://numpy.scipy.org) |
||
12 | [PyTables](http://www.pytables.org) |
||
13 | |||
14 | **Writing Scripts using PyTables** |
||
15 | |||
16 | Your Python script must begin by importing the two packages above: |
||
17 | ``` |
||
18 | import tables |
||
19 | import numpy |
||
20 | ``` |
||
21 | 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. |
||
22 | ``` |
||
23 | h5file = tables.openFile(fileName, mode='a') |
||
24 | ``` |
||
25 | As an aside, all of the PyTables commands may be more safely used by wrapping in a try-catch block like so: |
||
26 | ``` |
||
27 | try: |
||
28 | h5file = tables.openFile(fileName, mode='a') |
||
29 | except AttributeError, inst: |
||
30 | print "Error opening file: ", inst |
||
31 | print "Exiting program" |
||
32 | sys.exit() |
||
33 | ``` |
||
34 | 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.) |
||
35 | ``` |
||
36 | mesh = h5file.getNode("/fluids/dc") |
||
37 | ``` |
||
38 | With the node retrieved, attributes may be accessed and changed using the _f_getAttr and _f_setAttr methods: |
||
39 | ``` |
||
40 | mesh._f_setAttr("vsType", "mesh") |
||
41 | mesh._f_setAttr("vsLowerBounds", numPy.array({0.0, 0.0, 0.0}) |
||
42 | ``` |
||
43 | 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: |
||
44 | ``` |
||
45 | mesh.vsType = "mesh" |
||
46 | mesh.vsLowerBounds = numPy.array({0.0, 0.0, 0.0}) |
||
47 | ``` |
||
48 | Finally, when all metadata changes have been made, the file must be closed again: |
||
49 | ``` |
||
50 | h5file.close() |
||
51 | ``` |
||
52 | **Example** |
||
53 | |||
54 | 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. |
||
55 | ``` |
||
56 | #!/usr/bin/env python |
||
57 | """ |
||
58 | Example script for adding VizSchema data. |
||
59 | |||
60 | """ |
||
61 | |||
62 | import sys, os, getopt |
||
63 | import re |
||
64 | import tables |
||
65 | import numpy |
||
66 | |||
67 | def usage(): |
||
68 | print "changeVsMesh.py -f <filename>" |
||
69 | |||
70 | def main(): |
||
71 | # Get the file name from command line |
||
72 | try: |
||
73 | opts, args = getopt.getopt(sys.argv[1:], "f:", ["file="]) |
||
74 | except getopt.GetoptError: |
||
75 | # print help information and exit: |
||
76 | usage() |
||
77 | sys.exit(2) |
||
78 | |||
79 | # Get the file name |
||
80 | fileName = None |
||
81 | for o, a in opts: |
||
82 | if o in ("-f", "--file"): |
||
83 | fileName = a |
||
84 | |||
85 | if fileName == None: |
||
86 | usage() |
||
87 | print "Unable to find filename in argument list: ",sys.argv |
||
88 | sys.exit() |
||
89 | |||
90 | # Open the file |
||
91 | try: |
||
92 | h5file = tables.openFile(fileName, mode='a') |
||
93 | except: |
||
94 | print "Unable to open the file '" + fileName + "'." |
||
95 | raise |
||
96 | print "File '" + fileName + "' opened." |
||
97 | |||
98 | #Fix up variable totalEnergyAbs |
||
99 | print "Loading dataset totalEnergyAbs" |
||
100 | dataSet = h5file.getNode("/Absorbed/totalEnergyAbs") |
||
101 | print "Getting value of vsmesh" |
||
102 | oldValue = dataSet._f_getAttr("vsmesh") |
||
103 | print "Value is " + oldValue |
||
104 | print "Removing vsmesh (lowercase m)" |
||
105 | dataSet._f_delAttr("vsmesh") |
||
106 | print "Adding vsMesh (uppercase m)" |
||
107 | dataSet._f_setAttr("vsMesh", oldValue) |
||
108 | |||
109 | print "Done, closing file" |
||
110 | h5file.close() |
||
111 | sys.exit() |
||
112 | |||
113 | if __name__ == "__main__": |
||
114 | main() |
||
115 | ``` |