-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigproc3.py
319 lines (247 loc) · 9.74 KB
/
sigproc3.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python
"""
Sigproc utilities
See here for format description: http://sigproc.sourceforge.net/sigproc.pdf
Copyright (C) Keith Bannister 2011
Converted to Python3 by Harry Qiu 2019
"""
import struct
import os
import numpy as np
import warnings
global _verbose
_verbose = False
def _debug(msg):
global _verbose
if _verbose:
print(msg)
HEADER_LENGTH=1024*10
INT_PARAMS=('telescope_id','machine_id','data_type','barycentric','pulsarcentric','nbits','nsamples', 'nchans', 'nifs')
DOUBLE_PARAMS = ('az_start','za_start','src_raj','src_dej','tstart','tsamp','fch1','foff','fchannel','refdm','period')
STRING_PARAMS = ('rawdatafile','source_name')
FREQ_PARAMS = ('FREQUENCY_START','FREQUENCY_END')
DATA_TYPES = ("UNKNOWN", "filterbank", "time series")
TELESCOPE_IDS = ("fake data", "Arecibo", "Ooty")
MACHINE_IDS = ("FAKE", "PSPM", "WAPP", "OOTY")
INT_FORMAT = 'i'
STRING_FORMAT = 's'
DOUBLE_FORMAT = 'd'
def sigproc_sex2deg(x):
'''
Computes a decimal number in degrees from a rediculous sigproc number in the form
ddmmss.s
>>> print '%0.5f' % sigproc_sex2deg(123456.789)
12.58244
>>> print '%0.5f' % sigproc_sex2deg(-123456.789)
-12.58244
>>> sigproc_sex2deg(0)
0.0
'''
sign = 1.
if x < 0:
sign = -1.
x = abs(x)
dd = float(int(x/10000))
mm = float(int((x - dd*10000)/100))
ss = x - dd*10000 - mm*100
dout = sign*(dd + mm/60.0 + ss/3600.0)
return dout
def unpack(hdr, param, struct_format):
idx = hdr.find(param)
if idx < 0:
#warnings.warn('Could not find parameter {}'.format(param))
return None
idx += len(param) # idx is thte start of the string
size = struct.calcsize(struct_format)
bits = hdr[idx:idx+size]
value = struct.unpack(struct_format, bits)[0]
#print 'param', param, 'size', size, 'part', bits, 'value', value
return value
def unpack_str(hdr, param):
idx = hdr.find(param)
if idx < 0:
#warnings.warn('Could not find parameter {}'.format(param))
return None
idx += len(param) # idx is thte start of the string
# The size of the string is given in the next field, then the value of the string
count = struct.unpack('i', hdr[idx:idx+4])[0]
end_idx = idx+4+count
value = hdr[idx+4:end_idx]
#print 'param', param, 'start', idx, 'end', end_idx, 'value', value
return value
def write_str(f, s):
n = struct.pack('i', len(s))
f.write(n)
f.write(s.encode("utf8"))
def write(f, v, struct_format):
try:
f.write(struct.pack(struct_format, v))
except:
print('Could not write value %s to %s with format %s' %( v, f, struct_format))
raise
class SigprocFile(object):
def __init__(self, filename, mode='r', header=None):
self.filename = filename
self.fin = open(self.filename, mode)
if header is not None:
self._write_header(header)
self.header = header
else:
self._read_header()
if 'src_raj' in self.header:
self.src_raj_deg = sigproc_sex2deg(self.header['src_raj'])*15.0
if 'src_dej' in self.header:
self.src_dej_deg = sigproc_sex2deg(self.header['src_dej'])
def _write_header(self, header):
f = self.fin
f.seek(0)
write_str(f, 'HEADER_START')
for k, v in header.items():
if v is None:
continue
if k in STRING_PARAMS:
write_str(f, k)
write_str(f, v)
elif k in INT_PARAMS:
write_str(f, k)
write(f, v, INT_FORMAT)
elif k in DOUBLE_PARAMS:
write_str(f, k)
write(f, v, DOUBLE_FORMAT)
else:
print('Cannot write header', k)
write_str(f, 'HEADER_END')
self.data_start_idx = f.tell()
def _read_header(self):
fin = self.fin
fin.seek(0)
hdr = fin.read(HEADER_LENGTH)
start_idx = hdr.find("HEADER_START")
end_idx = hdr.find("HEADER_END")
hdr = hdr[start_idx:end_idx]
self.data_start_idx = end_idx + len("HEADER_END")
self.seek_data()
header = {}
self.header = header
self.hdr = hdr
for p in STRING_PARAMS:
header[p] = unpack_str(hdr, p)
for p in INT_PARAMS:
header[p] = unpack(hdr, p, INT_FORMAT)
for p in DOUBLE_PARAMS:
header[p] = unpack(hdr, p, DOUBLE_FORMAT)
for k,v in self.header.items():
setattr(self, k, v)
self.file_size_bytes = os.path.getsize(self.filename)
self.header_size_bytes = self.data_start_idx
self.data_size_bytes = self.file_size_bytes - self.header_size_bytes
assert self.nifs > 0, 'Invalid nifs {}'.format(self.nifs)
assert self.nchans > 0, 'Invalid nchans {}'.format(self.nchans)
assert self.nbits > 0, 'Invalid nbits {}'.format(self.nbits)
self.bytes_per_element = self.nifs * self.nchans * self.nbits/8
self.file_size_elements = self.data_size_bytes / self.bytes_per_element
if self.nsamples is None:
self.nsamples = self.file_size_elements
self.observation_duration = self.nsamples * self.tsamp
def seek_data(self, offset_bytes=0):
self.fin.seek(self.data_start_idx + offset_bytes)
def seek_sample(self, sampnum):
self.fin.seek(self.data_start_idx + sampnum*self.nifs*self.nchans*self.nbits/8)
def get_num_elements(self):
nelements = self.nifs * self.nchans * self.nsamples
return nelements
def arr_index(self, sampindex, chanindex=0, ifindex=0):
if chanindex < 0 or chanindex >= self.nchans:
raise ValueError('Invalid channel index')
if ifindex < 0 or ifindex >= self.nifs:
raise ValueError('Invalid IF index')
if sampindex < 0 or sampindex >= self.nsamples:
raise ValueError('Invalid sample index')
arridx = sampindex * self.nifs * self.nchans + ifindex * self.nchans + chanindex
return arridx
def sky_freq(self, chanidx):
"""REturns the sky frequency in MHz"""
if chanidx < 0 or chanidx >= self.nchans:
raise ValueError('Invalid channel index')
freq = self.fch1 + chanidx * self.foff
return freq
def get_data_type(self):
if self.data_type < len(DATA_TYPES):
return DATA_TYPES[self.data_type]
else:
return None
def get_data(self, time_slice, chanindex=0, ifindex=0):
if self.nifs != 1:
raise NotImplementedError("Can't handle Nif > 1")
if self.nbits == 8:
dtype = np.uint8
samps_per_element = 1
elif self.nbits == 32:
dtype = np.float32
samps_per_element = 1
elif self.nbits == 2:
dtype = np.uint8
samps_per_element = 4
else:
raise NotImplementedError("Can't handle nbits: %d" % self.nbits)
if time_slice.step is not None:
raise NotImplementedError("Can only handle contiguous slices")
if time_slice.start is None:
time_start = 0
else:
time_start = time_slice.start
if time_slice.stop is None:
time_end = self.nsamples
else:
time_end = time_slice.stop
num_samples = (time_end - time_start)
num_elements = num_samples*self.nchans
num_dtypes = num_elements / samps_per_element
if num_samples < 0:
raise ValueError("cant do negative number of samples")
byte_start = self.arr_index(time_start, chanindex, ifindex) * self.nbits / 8
self.seek_data(byte_start)
""" TODO: If nbits < 8 will need to read an extra byte here"""
num_bytes = num_samples * self.nbits / 8
if num_bytes + self.data_start_idx > self.file_size_bytes:
raise ValueError('Requested data off the end of the file. File size: %d. num_bytes: %d. data_start_idx: %d' % (self.file_size_bytes, num_bytes, self.data_start_idx))
data = np.fromfile(self.fin, dtype=dtype, count=num_dtypes)
assert len(data) == num_dtypes, "Didn't get count dtypes %d" % num_dtypes
if self.nbits == 2:
data2 = np.zeros(num_elements*samps_per_element, dtype=np.int8)
print('samp', num_samples, 'nelem', num_elements, 'ndtypes', num_dtypes, len(data), len(data2))
data2[0::4] = (data & 0b00000011)*2 - 3
data2[1::4] = (data & 0b00001100)/2 - 3
data2[2::4] = (data & 0b00110000)/4 - 3
data2[3::4] = (data & 0b11001100)/8 - 3
data = data2
data.shape = (num_samples, self.nchans)
return data
def __getitem__(self, slice_list):
return self.get_data(slice_list, 0, 0)
def print_header(self):
for k,v, in self.header.items():
if isinstance(v, float):
print('{}:{:0.15f}'.format(k,v))
else:
print(k, ':', v)
def _main():
from optparse import OptionParser
parser = OptionParser()
parser.set_usage('%prog [options] args')
parser.set_description('Script description')
parser.add_option('-v', '--verbose', dest='verbose', action='store_true', help='Be verbose [Default %default]')
parser.set_defaults(verbose=False)
(values, args) = parser.parse_args()
global _verbose
_verbose = bool(values.verbose)
for filename in args:
fin = SigprocFile(filename)
fin.print_header()
print('RADEC DEG', fin.src_raj_deg, fin.src_dej_deg)
print("Header size", fin.header_size_bytes)
print("Data size", fin.data_size_bytes)
print("Number of elements", fin.file_size_elements)
print('Duration (seconds)', fin.observation_duration)
if __name__ == '__main__':
_main()