Hi,
I have data which begins with TEXT which is exactly 13 lines (variable number of characters) followed by BINARY DATA (exactly 262144 chars). It looks like:
BASELINE NUM: 258
MJD: 54270
SECONDS: 28321
CONFIG INDEX: 0
SOURCE INDEX: 2
FREQ INDEX: 0
POLARISATION PAIR: RR
PULSAR BIN: 0
FLAGGED: 0
WEIGHTS WRITTEN: 0
U (METRES): -18954.5
V (METRES): 99486.5
W (METRES): 54117.25
<F1><AA><E8><U+0084>^]^NDeR.<U+0085><B2><D4>B<D8>m<8B><C1><B3><91><E2>A<AA>f)
<C1>˛<91>Ad<DD> <C1>^L^^%AESC<91><U+037F>c<9E> A^F<BE>0]<8D>@H<F8>^M<C0>m<D8>]@@3<F7>^W@<FF>^Z<96>><B6><B0><E9>@g<AA><CE>?dl<86>?^\<ED><FE><BD><BF><82><88>@<E8>
This combination repeats for a few thousand times. What I need to do is read the text which contains important info, then the binary data (exactly 262144 bytes), and convert to floats. The conversion is easy, what is not is reading the TEXT, and the BiNARY DATA to the exact byte.
Here is what I tried to do:
openVis = open(testVis, 'rb')
readVis = openVis.read()
size_array = 32768#number of channels
totalTime = 60#seconds
timeSize = totalTime/2.0#seconds
fig = plt.figure()
ax = fig.add_subplot(111)
plt.ylabel('Visibility Amplitude')
plt.xlabel('Channels')
word = "BASELINE"
headerStart = readVis.find(word)
relTime = np.zeros(timeSize)
while headerStart >1:
#Reading Header Info
baselineIn = float(readVis[headerStart+20:headerStart+23])
if baselineIn == 258.0:
print 'Baseline = ',baselineIn
PoL = readVis[headerStart+162:headerStart+164]
if PoL == 'RR':
print 'Polarisation = ',PoL
time = float(readVis[headerStart+70:headerStart+75])
print 'Time = ',time
relTime = time - 28321.0
sourceIn = readVis[headerStart+118:headerStart+119]
#Get BINARY DATA, decode, then plot
Vis = np.zeros(size_array)
phase = np.zeros(size_array)
u = readVis[headerStart+317:headerStart+317+262144]
bit1 = 0
bit8 = 8
for i in range (size_array):
visPair = struct.unpack('ff',u[bit1:bit8])
Vis[i] = sqrt((visPair[0])**2+(visPair[1])**2)
phase[i] = atan(visPair[0]/visPair[1])*(180/pi)
bit1 = bit1+8
bit8 = bit8+8
lgVis = np.log10(Vis)
ax.plot(lgVis,'r+')
plt.savefig(PLOTS+PoL+str(relTime)+'.eps')
else:
print 'wrong polarisation'
else:
print 'pbbbbbbbbt'
headerStart = readVis.find(word,headerStart+1)
This code would loop through 60/2 data combination (1 combination = TEXT + BINARY DATA). What I tried, was to read the entire data set, find the word "BASELINE", count characters to the end of the TEXT, and then read the BINARY DATA. However this only works if the TEXT is exactly 317 chars long. The TEXT changes size (but remains 13 lines) by 2-3 chars throughout the data set. Is there any way to read the exact size of the TEXT i.e. the number of chars?
Any help would be greatly appreciated.
cheers,
h.