-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsddsdata.py
executable file
·229 lines (209 loc) · 5.82 KB
/
sddsdata.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#TODO pages not supported
#TODO tables not supported
#TODO multi dimensional array in binary are not supported
# struct format
# >: big
# <: little
# |: machine
# x: pad byte (no data);
# c:char;
# b:signed byte;
# B:unsigned byte;
# h:short;
# H:unsigned short;
# i:int;
# I:unsigned int;
# l:long;
# L:unsigned long;
# f:float;
# d:double.
# s:string (array of char);
# p:pascal string (with count byte).
# P:an integer type that is wide enough to hold a pointer.
# q:long long;
# Q:unsigned long long
import StringIO
import gzip
import numpy as n
import struct
def iterheader(o):
c=1
while c:
c=o.read(1)
if c=='!':
while c not in '\n\r':
c=o.read(1)
elif c not in '\n\t\r':
yield c
def iterbinarydata(o):
c=1
while c:
c=o.read(1)
if c=='!':
while c not in '\n\r':
c=o.read(1)
if c in '\n\r': # possibly a bug
c=o.read(1)
else:
yield c
def readtoken(o):
buf=[]
for i in o:
buf.append(i)
if ''.join(buf[-4:])=='&end':
yield ''.join(buf)
buf=[]
def myreadline(o):
buf=o.readline()
while buf[0]=='!':
buf=o.readline()
return buf
def parseheader(l):
t,data=l.split(' ',1)
data=data.replace(' ','')
data=[d.split('=') for d in data.split(',')]
data.pop()
data=dict(data)
data['header']=t
return data
sddstypes={
'short' : 'i2',
'long' : 'i4',
'llong' : 'u8',
'string': 'S',
'float': 'f4',
'double': 'f8',
'boolean': 'i2',
}
def myarray(fh,typ,count,endian):
# print typ
typ=n.dtype(endian+sddstypes.get(typ,typ))
size=typ.itemsize*count
ss=fh.read(size)
if len(ss)==size:
# print typ,count,size,repr(ss[:16])
return n.fromstring(ss,dtype=typ,count=count)
else:
return None
# return s
def mystruct(fh,typ,count,endian):
typ='%s%d%s' % (endian,count,typ)
size=struct.calcsize(typ)
ss=fh.read(size)
if len(ss)==size:
return struct.unpack(typ,ss)
else:
return None
def mysplit(fh,count):
out=[]
while len(out)<count:
l=fh.readline()
out.extend(l.split())
return out
class sddsdata(object):
def __init__(self,filename,endian='little',full=True):
self.filename=filename
if filename.endswith('.gz'):
fh=gzip.open(filename)
else:
fh=file(filename)
try:
self.version=fh.readline()
fendian=fh.readline().split(' ')[1].split('-')[0]
assert fendian in ['big','little']
endian=fendian
except AssertionError:
print 'Warning sddsdata: forcing endianess to %s' % endian
fh.seek(0)
self.version=fh.readline()
except IndexError:
print 'Failed to open data file'
raise IndexError
self.endian={'little':'<','big':'>'}[endian]
# read headear
# print 'read header'
it=readtoken(iterheader(fh))
header=[]
for i in it:
header.append(parseheader(i))
if header[-1]['header']=='&data':
break
header2=[]; istable=True
for i in header:
if i['header']=='&column':
if istable==True:
header2.append({'header':'&table'})
header2[-1]['columns']=[i]
istable=False
else:
header2[-1]['columns'].append(i)
else:
header2.append(i)
self.header=header2
# print self.header
# read data
if full:
fh.read(1)
self.data=[]
if self.header[-1]['mode']=='ascii':
data={}
self.data.append(data)
for i in self.header:
if 'type' in i:
typ=i['type']
#print i, typ
typ=n.dtype(sddstypes.get(typ,typ))
if i['header']=='¶meter':
ss=myreadline(fh)
d=n.array(ss,typ)
i['value']=d
elif i['header']=='&array':
dims=map(int,myreadline(fh).split())
i['shape']=dims
cnt=reduce(lambda a,b:a*b,dims)
# ss=myreadline(fh)
# print dims, len(ss)
d=n.array(mysplit(fh,cnt),typ).reshape(dims)
data[i['name']]=d
elif self.header[-1]['mode']=='binary':
while 1:
row=myarray(fh,'long',1,self.endian)
if row is None:
break
data={}
self.data.append(data)
for i in self.header:
if 'type' in i:
typ=i['type']
if i['header']=='¶meter':
if typ=='string':
smax=0
subcount=myarray(fh,'long',1,self.endian)[0]
smax=subcount<smax and smax or subcount
d=mystruct(fh,'s',subcount,self.endian)[0]
else:
d=myarray(fh,typ,1,self.endian)
elif i['header']=='&array':
count=myarray(fh,'long',1,self.endian)[0]
if typ=='string':
d=[];smax=0
for r in range(count):
subcount=myarray(fh,'long',1,self.endian)[0]
smax=subcount<smax and smax or subcount
# d.append(myarray(fh,'>S1',subcount,self.endian)[0])
d.append(mystruct(fh,'s',subcount,self.endian)[0])
d=n.array(d,n.dtype('S%s'%smax))
else:
d=myarray(fh,typ,count,self.endian)
data[i['name']]=d
fh.close()
def __str__(self):
out=['%s: %s' % (self.filename,self.version)]
for i in self.header:
oo=[]
for n,k in i.items():
if n is not 'header':
oo.append('%s=%s' % (n,k))
out.append(i['header'][1:]+' '+', '.join(oo))
return '\n'.join(out)
__repr__=__str__