1
2 """
3 Read and write HDF5 files.
4
5 HDF5 is a data model, library, and file format for storing and managing data. It supports
6 an unlimited variety of datatypes, and is designed for flexible and efficient I/O and for
7 high volume and complex data. HDF5 is portable and is extensible, allowing applications
8 to evolve in their use of HDF5. The HDF5 Technology suite includes tools and applications
9 for managing, manipulating, viewing, and analyzing data in the HDF5 format.
10 http://alfven.org/wp/hdf5-for-python/
11 """
12
13 import os
14 import h5py
15 import logging
16 from ivs.aux import loggers
17
18 logger = logging.getLogger("IO.HDF5")
19 logger.addHandler(loggers.NullHandler())
20
21
22
24 """
25 Read the filestructure of a hdf5 file to a dictionary.
26
27 @param filename: the name of the hdf5 file to read
28 @type filename: str
29 @return: dictionary with read filestructure
30 @rtype: dict
31 """
32
33 if not os.path.isfile(filename):
34 logger.error('The file you try to read does not exist!')
35 raise IOError
36
37 def read_rec(hdf):
38 """ recusively read the hdf5 file """
39 res = {}
40 for name,grp in hdf.items():
41
42 if hasattr(grp, 'items'):
43
44 res[name] = read_rec(grp)
45 else:
46
47 res[name] = grp.value
48
49
50 for name, atr in hdf.attrs.iteritems():
51 res[name] = atr
52
53 return res
54
55 hdf = h5py.File(filename, 'r')
56 result = read_rec(hdf)
57 hdf.close()
58
59 return result
60
61
62
63
64
65 -def write_dict(data, filename, update=True, attr_types=[]):
66 """
67 Write the content of a dictionary to a hdf5 file. The dictionary can contain other
68 nested dictionaries, this file stucture will be maintained in the saved hdf5 file.
69
70 Pay attention to the fact that the data type of lists might change when writing to
71 hdf5. Lists are stored as numpy arrays, thus all items in a list are converted to
72 the same type: ['bla', 1, 24.5] will become ['bla', '1', '24.5']. Upt till now there
73 is nothing in place to check this, or correct it when reading a hdf5 file.
74
75 @param data: the dictionary to write to file
76 @type data: dict
77 @param filename: the name of the hdf5 file to write to
78 @type filename: str
79 @param update: True if you want to update an existing file, False to overwrite
80 @type update: bool
81 @param attr_types: the data types that you want to save as an attribute instead of
82 a dataset. (standard everything is saved as dataset.)
83 @type attr_types: List of types
84 """
85
86 if not update and os.path.isfile(filename):
87 os.remove(filename)
88
89 def save_rec(data, hdf):
90 """ recusively save a dictionary """
91 for key in data.keys():
92
93 if type(data[key]) == dict:
94
95 if not key in hdf:
96 hdf.create_group(key)
97 save_rec(data[key], hdf[key])
98
99 elif type(data[key]) in attr_types:
100
101 hdf.attrs[key] = data[key]
102
103 else:
104
105 if key in hdf:
106 del hdf[key]
107 hdf.create_dataset(key, data=data[key])
108
109 hdf = h5py.File(filename)
110 save_rec(data, hdf)
111 hdf.close()
112
113