Coverage for larch/io/hdf5group.py: 22%
46 statements
« prev ^ index » next coverage.py v7.6.0, created at 2024-10-16 21:04 +0000
« prev ^ index » next coverage.py v7.6.0, created at 2024-10-16 21:04 +0000
1#!/usr/bin/env python
2"""
3 Larch hdf5group() function
4"""
6import h5py
7import numpy
8from functools import partial
9from larch.utils import fixName
10from larch import Group as larchGroup
11import scipy.io.netcdf
13def netcdf_group(fname, **kws):
14 """open a NetCDF file and map the variables in it to larch groups
15 g = netcdf_group('tmp.nc')
16 """
17 finp = scipy.io.netcdf.netcdf_file(fname, mode='r')
18 group = larchGroup()
19 for k, v in finp.variables.items():
20 setattr(group, k, v.data)
21 finp.close()
22 return group
24def netcdf_file(fname, mode='r'):
25 """open and return a raw NetCDF file, equvialent to
26 scipy.io.netcdf.netcdf_file(fname, mode=mode)
27 """
28 return scipy.io.netcdf.netcdf_file(fname, mode=mode)
30def h5file(fname, mode='r'):
31 """open and return a raw HDF5 file, equvialent to
32 import h5py
33 h5py.File(fname, mode)
34 """
35 return h5py.File(fname, mode)
37def h5group(fname, mode='r+'):
38 """open an HDF5 file, and map to larch groups
39 g = h5group('myfile.h5')
41 Arguments
42 ------
43 mode string for file access mode ('r', 'w', etc)
44 default mode is 'r+' for read-write access.
46 Notes:
47 ------
48 1. The raw file handle will be held in the 'h5_file' group member.
49 2. Attributes of groups and datasets are generally placed in
50 'itemname_attrs'.
51 """
52 fh = h5py.File(fname, mode)
54 def add_component(key, val, top):
55 parents = [fixName(w, allow_dot=False) for w in key.split('/')]
56 current = parents.pop()
57 for word in parents:
58 if not hasattr(top, word):
59 setattr(top, word, larchGroup())
60 top = getattr(top, word)
61 tname = top.__name__
62 if isinstance(val, h5py.Group):
63 setattr(top, current, larchGroup(name="%s/%s" % (tname, current)))
64 if len(val.attrs) > 0:
65 getattr(top, current)._attrs = dict(val.attrs)
66 else:
67 dat = fh.get(key)
68 try:
69 if dat.dtype.type == numpy.string_:
70 if len(dat) == 1:
71 dat = dat[()]
72 else:
73 dat = list(dat)
74 except (ValueError, TypeError):
75 pass
76 setattr(top, current, dat)
77 if len(val.attrs) > 0:
78 setattr(top, "%s_attrs" % current, dict(val.attrs))
79 top = larchGroup(name=fname)
80 top.h5_file = fh
81 fh.visititems(partial(add_component, top=top))
82 return top