-
Notifications
You must be signed in to change notification settings - Fork 0
/
HDF4File.py
179 lines (140 loc) · 5.84 KB
/
HDF4File.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python
# encoding: utf-8
"""
HDF4File.py
Created by Geoff Cureton <[email protected]> on 2012-09-25.
Copyright (c) 2012-2013 University of Wisconsin Regents. All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http:https://www.gnu.org/licenses/>.
"""
file_Date = '$Date$'
file_Revision = '$Revision$'
file_Author = '$Author$'
file_HeadURL = '$HeadURL$'
file_Id = '$Id$'
__author__ = 'Geoff Cureton <[email protected]>'
__version__ = '$Id$'
__docformat__ = 'Epytext'
import string
import pyhdf.HDF as HDF
import pyhdf.V as V
import pyhdf.VS as VS
import pyhdf.SD as SD
# every module should have a LOG object
import logging
#sourcename= file_Id.split(" ")
sourcename=__name__
LOG = logging.getLogger(sourcename[1])
class HDF4File():
"""
Common interface for (reading) HDF4 files.
"""
HDFobjTag = {
'dataset': HDF.HC.DFTAG_NDG,
'vdata': HDF.HC.DFTAG_VH,
'vgroup': HDF.HC.DFTAG_VG
}
"""
Initialise the HDF4 class object for this file, exposing the
SD file object, and attribute and dataset dictionaries.
"""
def __init__(self,filename):
self.filename = filename
self.fileObj = SD.SD(self.filename, SD.SDC.READ)
self.attrDict = self.fileObj.attributes()
self.datasetDict = self.fileObj.datasets()
"""
Perform a direct read of a dataset and return it.
"""
def read_data(self, varName):
return self.fileObj.select(varName).get()
"""
Quick fix for large datasets that we don't want to use 'get' with right away.
"""
def get_dataset_obj(self, varName):
return self.fileObj.select(varName)
"""
Read the named attribute from the supplied object.
"""
def read_attribute(self, obj, attrName):
inAttrs = obj.attributes()
if attrName in inAttrs:
return inAttrs[attrName]
else:
return None
# Small wrapper for getSDS
def get_dataset(self, varName):
return self._getSDS(varName).get()
# Module for specifying the whole vgroup path to an SDS
# in a UNIX-style path name. Returns the SDS.
#
# Once trusted, this should probably be rolled in with readData
# so that only one needs to be used!
def _getSDS(self, varPath):
# Open HDF file in readonly mode.
hdf = HDF.HDF(self.filename)
# Initialize the SD, V and VS interfaces on the file.
#sd = SD.SD(self.filename)
sd = self.fileObj
vs = hdf.vstart()
v = hdf.vgstart()
vars = string.split(varPath, "/")
LOG.debug("vars : %r" %(vars))
if len(vars) > 0:
LOG.debug("Specifying '%s' as root group." % vars[0])
vgroup = v.attach(vars[0])
for i in range(0, len(vars)):
varname = vars[i]
LOG.debug("Traversing level %s with varname '%s'"%(str(i),varname))
members = vgroup.tagrefs()
LOG.debug("%s has members: %r" % (varname,members))
for tag, ref in members:
LOG.debug("\ttag,ref = (%r,%r)" %(tag,ref))
# Vdata tag
if tag == HDF.HC.DFTAG_VH:
LOG.debug("\tWe have a vdata tag (DFTAG_VH)")
vd = vs.attach(ref)
nrecs, intmode, fields, size, name = vd.inquire()
LOG.debug("\tvdata: %s %s,%s" % (name,tag,ref))
LOG.debug("\tfields: %s" %(fields))
LOG.debug("\tnrecs: %d" %(nrecs))
vd.detach()
# SDS tag
elif tag == HDF.HC.DFTAG_NDG:
LOG.debug("\tWe have a dataset tag (DFTAG_NDG)")
sds = sd.select(sd.reftoindex(ref))
name, rank, dims, type, nattrs = sds.info()
LOG.debug("\tWe have dataset %s" %(name))
if name == varname:
# Check that if we reach our desired name, that it is the
# last one (and hence likely to be a dataset rather than a group).
# TODO: Make this more understandable and robust.
if i != (len(vars)-1):
LOG.warn("Only at '%s' and reached SDS! Returning '%s'\n" %(varname,name))
return sds
else:
sds.endaccess()
# VG tag
elif tag == HDF.HC.DFTAG_VG:
LOG.debug("\tWe have a vgroup tag (DFTAG_VG)")
vg0 = v.attach(ref)
if vg0._name == varname:
vgroup.detach()
vgroup = vg0
# Unhandled tag
else:
LOG.warn("unhandled (tag,ref): %r,%r" % (tag,ref))
# Terminate V, VS and SD interfaces.
v.end()
vs.end()
sd.end()
# Close HDF file.
hdf.close()