-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzeFits.py
392 lines (322 loc) · 9.83 KB
/
analyzeFits.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
"""
Analyzes and summarizes FITS file data
Use: Python analyzeFits.py <filename> <output directory>
"""
import os
import fitsio
import sys
import matplotlib
matplotlib.use('Agg') # Must be called before importing pyplot
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages # Save plots to PDF
import matplotlib.cm as cm
from pyPdf import PdfFileWriter, PdfFileReader# Merge PDF pages
import numpy as np
# For drawing paragraphs in PDF
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import letter #, landscape
import time
# Constants and parameters
tStep = 1.43
def getFitsData(fileHandle):
"""
Extracts FITS data for other functions
"""
def update_progress(progress):
barLength = 10 # Modify this to change the length of the progress bar
status = ""
if isinstance(progress, int):
progress = float(progress)
if not isinstance(progress, float):
progress = 0
status = "error: progress var must be float\r\n"
if progress < 0:
progress = 0
status = "Halt...\r\n"
if progress >= 1:
progress = 1
status = "Done...\r\n"
block = int(round(barLength*progress))
text = "\rPercent: [{0}] {1}% {2}".format( "#"*block + "-"*(barLength-block), progress*100, status)
sys.stdout.write(text)
sys.stdout.flush()
# Seperate data by tables
ccpwrs = [] # Table seperated ccpwrs
data = [] # Table seperated data
# Seperate data by time steps
nSteps = -1 # Number of time steps
delim = ['GBTSTATUS', 'AOSCRAM'] # Tables delimiting timesteps
rows = [] # Single data vector
times = [] # Times for rows
# Each element is an ETHITS table
# Columns: DETPOW, MEANPOW, COARCHAN, FINECHAN
# Parse data
for ind, table in enumerate(fileHandle):
try:
if 'ETHITS' in table.read_header()['EXTNAME']:
data.append(table.read())
for row in table:
times.append(tStep * nSteps)
if row[2] == 0:
rows.append(row[3])
else:
rows.append(row[2]*2**19 + row[3])
elif 'CCPWRS' in table.read_header()['EXTNAME']:
ccpwrs.append(table.read())
elif any(elem in table.read_header()['EXTNAME'] for elem in delim):
nSteps += 1
except:
pass
# Progress bar
if (ind+1)/float(len(fileHandle))*100%1 < .001:
update_progress((ind+1)/float(len(fileHandle)))
coarseID = fileHandle[1].read_header()['COARCHID']
ccpwrsPoints = [[row[0], row[1], tStep*ind] for ind, table in enumerate(ccpwrs) for row in table]
hitPoints = zip(rows, times)
return data, ccpwrs, hitPoints, ccpwrsPoints, nSteps, coarseID
def plotPDF(fileHandle, data, hitPoints, ccpwrsPoints, ccpwrs, coarseID):
"""
Prints plots to PDF
"""
def powerHist():
"""
Plots number of hits occured at each power level
"""
# Get DETPOW/MEANPOW
relpow = [row[0]/row[1] for table in data for row in table]
# Plot histogram
plt.figure()
plt.hist(relpow, bins = 1e3) # arbitrary bin amount
plt.title('Power Histogram')
plt.xlabel('Relative Power (DETPOW/MEANPOW)')
plt.ylabel('Number of Hits')
plt.autoscale(enable=True, axis='x', tight=True)
plt.yscale('log', nonposy='clip')
# plt.show(block = False)
def coarseHist():
"""
Plots the number of hits occured in each bin
"""
# Get COARSCHAN
coarse = [coarseID + row[2] for table in data for row in table]
# Plot histogram
plt.figure()
plt.hist(coarse, bins = 1e3) # arbitrary bin amount
plt.title('Coarse Bin Histogram')
plt.xlabel('Coarse Bin Number')
plt.ylabel('Number of Hits')
plt.autoscale(enable=True, axis='x', tight=True)
# plt.xlim(xmin=0)
plt.yscale('log', nonposy='clip')
# plt.show(block = False)
def coarseSpectrum():
"""
Plots power in each coarse channel (pole)
"""
# Get powers
xpol = [row[0] for table in ccpwrs for row in table]
ypol = [row[1] for table in ccpwrs for row in table]
# Plot histogram
plt.figure()
plt.plot(xpol[0:512], '-r', label = 'XPOL')
plt.plot(ypol[0:512], '-b', label = 'YPOL')
plt.title('Coarse Spectrum')
plt.legend(loc = 'center right')
plt.xlabel('Coarse Bin Number')
plt.autoscale(enable=True, axis='x', tight=True)
# plt.xlim([0,511])
plt.ylabel('Power')
plt.yscale('log')
# plt.show(block = False)
def waterfallHits():
"""
Plots the time at which a signal was received vs its frequency
"""
plt.figure()
plt.plot(*zip(*hitPoints), rasterized=True, linestyle='', color='black', marker='o', markersize=1)
plt.title('Waterfall Hits')
plt.xlabel('Fine Channel Number (Starting at channel ID)')
plt.ylabel('Time (s)')
plt.autoscale(enable=True, axis='x', tight=True)
#plt.xlim(xmin=0, xmax=max(x))
plt.ylim(ymin=0)
def waterfallCoarse():
"""
Plots CCPWRS over time. Color indicates Power.
"""
plt.figure()
plt.figure(figsize=(10,10))
plt.subplot(2,1,1)
xpol = [row[0] for table in ccpwrs for row in table]
imgX = np.array(xpol)
imgX = imgX.reshape(len(ccpwrs),512)
plt.imshow(imgX.astype(int), origin='lower', aspect='auto', cmap = cm.hot)
plt.title('X-Pole CCPWRS')
plt.ylabel('No. Time Steps (Time/Time Step)')
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
plt.xlabel('Coarse Channel ID')
plt.subplot(2,1,2)
ypol = [row[1] for table in ccpwrs for row in table]
imgY = np.array(ypol)
imgY = imgY.reshape(len(ccpwrs),512)
plt.imshow(imgY.astype(int), origin='lower', aspect='auto', cmap = cm.hot)
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
plt.title('Y-Pole CCPWRS')
plt.ylabel('No. Time Steps (Time/Time Step)')
plt.xlabel('Coarse Channel ID')
plt.subplots_adjust(hspace=0.4)
# Create PDF
pdf = PdfPages('%s%s_plots.pdf' %(outDir, filename))
# Generate plots and save to PDF
powerHist()
pdf.savefig()
plt.close()
coarseHist()
pdf.savefig()
plt.close()
waterfallHits()
pdf.savefig()
plt.close()
# coarseSpectrum()
if ccpwrs:
waterfallCoarse()
pdf.savefig()
plt.close()
# Close PDF and figures
pdf.close()
plt.close('all') # Just to be sure
def metaSummary(fileHandle, data, nSteps, hitPoints):
"""
Prints meta data to cover page as a summary of the FITS file
"""
def drawPage(meta):
"""
Creates cover page
"""
def coverPage(canvas, doc):
"""
Cover page format
"""
canvas.saveState()
canvas.setFont('Times-Bold',16)
canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-108, Title)
canvas.setFont('Times-Roman',9)
canvas.restoreState()
# PDF Parameters
PAGE_HEIGHT=defaultPageSize[1]; PAGE_WIDTH=defaultPageSize[0]
styles = getSampleStyleSheet()
Title = 'FITS Summary'
# Create cover page
doc = SimpleDocTemplate('%s%s_meta.pdf' %(outDir, filename))
content = [Spacer(1,2*inch)]
style = styles["Normal"]
for key in sorted(meta.keys()):
text = ("%s: %s \n" % (key, meta[key]))
p = Paragraph(text, style)
content.append(p)
doc.build(content, onFirstPage = coverPage)
def getMeta():
"""
Gather select meta data
"""
def getnHits():
"""
Add up NHITS key value in all tables
"""
nHits = len(hitPoints)
return nHits
def getDuration():
"""
Duration of file
"""
duration = str(round((tStep*nSteps)/60, 2)) + ' Minutes'
return duration
def getTime():
"""
Get time of first table
"""
time = fileHandle[0].read_header()['DATE']
return time
def getAvgHits():
"""
Get mean and median hit counts for entire file
For GBT: 16 means and medians (16 subands)
For AO: 14 means and medians (14 Beampols)
"""
# if 'GBTSTATUS' in fileHandle[1].read_header()['EXTNAME']:
# for i = range(len()
def getFileInfo():
"""
Gets fileinfo of file on disk
e.g. filesize on disk
"""
statInfo = os.stat(f)
fileSize = round(statInfo.st_size/(1024.0**2), 2) # Bytes to MB
fileSize = str(fileSize) + ' MB'
return fileSize
meta = {
'FILENAME': filename,
'NHITS': getnHits(),
'TIME': getTime(),
'DURATION': getDuration(),
'FILE SIZE': getFileInfo()
}
return meta
drawPage(getMeta())
def pdfMerge():
"""
Merges generated PDFs into one called <filename>_summary
Deletes the individual consituent PDFs
"""
def append_pdf(input, output):
"""
Combines PDF pages to be merged
"""
[output.addPage(input.getPage(page_num)) for page_num in range(input.numPages)]
# Merge PDFs
output = PdfFileWriter()
print outDir
append_pdf(PdfFileReader(file('%s%s_meta.pdf' %(outDir, filename), 'rb')), output)
append_pdf(PdfFileReader(file('%s%s_plots.pdf' %(outDir, filename), 'rb')), output)
outputFile = file('%s%s_summary.pdf' %(outDir, filename), 'wb')
output.write(outputFile)
outputFile.close()
# Delete PDFs
os.remove('%s%s_plots.pdf' %(outDir, filename))
os.remove('%s%s_meta.pdf' %(outDir, filename))
def main(filename):
"""
Summarizes and analyzes FITS file
"""
# Get data
print('Parsing Data')
fileHandle = fitsio.FITS(f) # File isn't opened until fileHandle is used
data, ccpwrs, hitPoints, ccpwrsPoints, nSteps, coarseID = getFitsData(fileHandle) # This opens fileHandle, will take a few minutes
# Print select meta data to PDF
print('Printing Metadata to PDF')
metaSummary(fileHandle, data, nSteps, hitPoints)
# Plot data to PDF
print('Plots printing, this may take a while')
plotPDF(fileHandle, data, hitPoints, ccpwrsPoints, ccpwrs, coarseID)
# Merge preceeding PDFs
print('Cleaning up...')
pdfMerge()
# End
fileHandle.close
print('Done')
if __name__ == "__main__":
start = time.time()
print('Starting timer')
f = sys.argv[1]
filename = f[f.rindex('/')+1:f.index('.fits')]
filepath = f[0:f.rindex('/')]
outDir = sys.argv[2]
if outDir[-1] != '/':
sys.exit('Exiting: Output directory must end with /')
elif os.path.isdir(outDir) == False:
sys.exit('Exiting: Output directory does not exist')
main(filename)
print('Total Time: %d Seconds' %(time.time()-start))