-
Notifications
You must be signed in to change notification settings - Fork 0
/
V2PM_measure_fluxes.py
314 lines (241 loc) · 11.9 KB
/
V2PM_measure_fluxes.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
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 6 15:36:15 2015
@author: bnorris
This takes the fits files from the Xenics camera and puts the measured waveguide
fluxes into appropriate variables.
"""
import numpy as np
import matplotlib.pyplot as plt
import pyfits as pf
import pdb
from scipy.io.idl import readsav
from circles import circles
from numpy.polynomial.polynomial import polyval
global p1Orp2, nWgsOP, nWgsIN, nBLs, nPists, segmentOrder, showPlots, nonLinCoeffs
p1Orp2 = 1 #Which side of the baseline to piston? (1 or 2)
nWgsOP = 120
nWgsIN = 8
nBLs = 28
nPists = 61
wavelength = 1.55 #microns
showPlots = True
saveFigs = False
nonLinCoeffsFile = 'coeffs_detailed_6.idlvar'
segmentOrder = np.array([23, 25, 27, 9, 29, 36, 31, 33])
outFile = 'extractedFluxes_ipbc_20140930_3_xtr2-nonlin6'
############################################################################################################################################################
# LAB DATA
############################################################################################################################################################
#dataDir='./dragonfly20140930data/'
dataDir='/Volumes/mojito2/snert/dragonfly/labtests/20140930/data_fits/'
darkFrame="ipbc_20140930_3_dark.mat.fits"
singleChannels=['ipbc_20140930_3_SingleWGScan_WG_23.mat.fits','ipbc_20140930_3_SingleWGScan_WG_25.mat.fits',
'ipbc_20140930_3_SingleWGScan_WG_27.mat.fits','ipbc_20140930_3_SingleWGScan_WG_9.mat.fits',
'ipbc_20140930_3_SingleWGScan_WG_29.mat.fits','ipbc_20140930_3_SingleWGScan_WG_36.mat.fits',
'ipbc_20140930_3_SingleWGScan_WG_31.mat.fits','ipbc_20140930_3_SingleWGScan_WG_33.mat.fits']
baselineChannels1=[
'ipbc_20140930_3_BaselineScan_WGs_23_25_p1.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_23_27_p1.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_23_9_p1.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_23_29_p1.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_23_36_p1.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_23_31_p1.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_23_33_p1.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_25_27_p1.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_25_9_p1.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_25_29_p1.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_25_36_p1.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_25_31_p1.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_25_33_p1.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_27_9_p1.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_27_29_p1.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_27_36_p1.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_27_31_p1.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_27_33_p1.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_9_29_p1.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_9_36_p1.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_9_31_p1.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_9_33_p1.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_29_36_p1.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_29_31_p1.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_29_33_p1.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_36_31_p1.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_36_33_p1.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_31_33_p1.mat.fits']
baselineChannels2=[
'ipbc_20140930_3_BaselineScan_WGs_23_25_p2.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_23_27_p2.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_23_9_p2.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_23_29_p2.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_23_36_p2.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_23_31_p2.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_23_33_p2.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_25_27_p2.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_25_9_p2.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_25_29_p2.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_25_36_p2.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_25_31_p2.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_25_33_p2.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_27_9_p2.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_27_29_p2.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_27_36_p2.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_27_31_p2.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_27_33_p2.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_9_29_p2.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_9_36_p2.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_9_31_p2.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_9_33_p2.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_29_36_p2.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_29_31_p2.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_29_33_p2.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_36_31_p2.mat.fits',
'ipbc_20140930_3_BaselineScan_WGs_36_33_p2.mat.fits', 'ipbc_20140930_3_BaselineScan_WGs_31_33_p2.mat.fits']
allOnScans=["ipbc_20140930_3b_AllOnScan_WG_23.mat.fits","ipbc_20140930_3b_AllOnScan_WG_25.mat.fits",
"ipbc_20140930_3b_AllOnScan_WG_27.mat.fits","ipbc_20140930_3b_AllOnScan_WG_9.mat.fits",
"ipbc_20140930_3b_AllOnScan_WG_29.mat.fits","ipbc_20140930_3b_AllOnScan_WG_36.mat.fits",
"ipbc_20140930_3b_AllOnScan_WG_31.mat.fits","ipbc_20140930_3b_AllOnScan_WG_33.mat.fits"
]
#==============================================================================
# Dark Frame, etc.
#==============================================================================
dark=pf.open(dataDir+darkFrame)
dark = dark['PRIMARY'].data
dark=np.average(dark,axis=0)
nonLinCoeffs = readsav(nonLinCoeffsFile).coeffs
if showPlots:
plt.figure(1)
plt.pause(5)
#==============================================================================
# Function to extract fluxes from a given frame
#==============================================================================
# TODO - Rigth now you have to manually put in the following numbers. Make
# an interactive GUI for this.
def fluxesFromIm(image, xstart=86., xend=540., ycoord=234., radius=1.5):
ycents = np.concatenate((np.repeat(ycoord+1,50),np.repeat(ycoord,70)))
xcents = np.linspace(xstart, xend, nWgsOP)
xpx=range(len(image[0,:]))
ypx=range(len(image[:,0]))
xinds,yinds=np.meshgrid(xpx,ypx)
#pdb.set_trace()
image = np.squeeze(polyval(image, nonLinCoeffs)) #Non-linear correction
if False: #showPlots: <- This is too slow.
#if showPlots: #<- This is too slow.
plt.figure(2)
#plt.clf()
plt.imshow(image, interpolation='nearest')
plt.plot(xcents,ycents,'+r',linewidth=1)
circles(xcents, ycents, radius, facecolor='none',edgecolor='r')
plt.pause(0.01)
fluxes = np.zeros(nWgsOP)
for wg in range(nWgsOP):
circleInds = np.sqrt( (xinds-xcents[wg])**2 + (yinds-ycents[wg])**2 ) < radius
fluxes[wg]=np.average(image[circleInds])
return fluxes
#==============================================================================
# Extract Single Waveguide Scans
#==============================================================================
wgInInds = range(nWgsIN)
singleWGscan_allChipOuts = np.zeros((nPists,nWgsOP,nWgsIN))
print '######## Doing Single Waveguide Scans ########'
for i in wgInInds:
wg2pist = i
print 'Pistoning waveguide %d' % i
filename = dataDir+singleChannels[i]
hdulist=pf.open(filename)
imCube=hdulist['PRIMARY'].data
for pist in range(nPists):
print 'Piston step %d of %d' % (pist+1, nPists)
im=imCube[pist,:,:] - dark
fluxes = fluxesFromIm(im)
singleWGscan_allChipOuts[pist,:,i] = fluxes
if showPlots:
plt.figure(1)
plt.clf()
im2show = singleWGscan_allChipOuts[:,:,i].T
plt.imshow(im2show, interpolation='nearest')
plt.colorbar(orientation='vertical')
plt.pause(0.01)
if saveFigs:
fname="fig_ExtractSingleWGScan_%d.pdf" % i
plt.savefig(fname)
singleWGscan_allChipOuts_avgd = np.average(singleWGscan_allChipOuts, axis=0)
#==============================================================================
# Extract Baseline Scans
#==============================================================================
bl2wg = np.array([ [23,25 ],
[23,27],
[23,9 ],
[23,29],
[23,36 ],
[23,31],
[23,33 ],
[25,27],
[25,9 ],
[25,29],
[25,36 ],
[25,31],
[25,33 ],
[27,9],
[27,29 ],
[27,36],
[27,31 ],
[27,33],
[9,29 ],
[9,36],
[9,31 ],
[9,33],
[29,36 ],
[29,31],
[29,33 ],
[36,31],
[36,33 ],
[31,33] ])
blInds = range(nBLs)
BLscan_allChipOuts = np.zeros((nPists,nWgsOP,nBLs))
BLscan_pistVals = np.zeros((nPists,nBLs))
print '######## Doing Baseline Scans ########'
for i in blInds:
wg2pist = bl2wg[i, p1Orp2-1]
print 'Baseline %d, pistoning waveguide %d' % (i, wg2pist)
if p1Orp2 == 1:
filename = dataDir+baselineChannels1[i]
else:
filename = dataDir+baselineChannels2[i]
hdulist=pf.open(filename)
imCube=hdulist['PRIMARY'].data
# Put pistvals in radians. NB *2 is because OPL = 2*MEMS_piston
BLscan_pistVals[:,i] = (imCube[:,0,0]*2. / wavelength) * 2.*np.pi
print BLscan_pistVals[:,i]
for pist in range(nPists):
print 'Piston step %d of %d' % (pist+1, nPists)
im=imCube[pist,:,:] - dark
fluxes = fluxesFromIm(im)
BLscan_allChipOuts[pist,:,i] = fluxes
if showPlots:
plt.figure(1)
plt.clf()
im2show = BLscan_allChipOuts[:,:,i].T
plt.imshow(im2show, interpolation='nearest')
plt.colorbar(orientation='vertical')
plt.pause(0.01)
if saveFigs:
fname="fig_ExtractBLScan_%d.pdf" % i
plt.savefig(fname)
#==============================================================================
# Extract AllOn Scans (for later testing)
#==============================================================================
wgInInds = range(nWgsIN)
allOnWGscan_allChipOuts = np.zeros((nPists,nWgsOP,nWgsIN))
print '######## Doing AllOn Scans ########'
for i in wgInInds:
wg2pist = i
print 'Pistoning waveguide %d' % i
filename = dataDir+allOnScans[i]
hdulist=pf.open(filename)
imCube=hdulist['PRIMARY'].data
for pist in range(nPists):
print 'Piston step %d of %d' % (pist+1, nPists)
im=imCube[pist,:,:] - dark
fluxes = fluxesFromIm(im)
allOnWGscan_allChipOuts[pist,:,i] = fluxes
if showPlots:
plt.figure(1)
plt.clf()
im2show = allOnWGscan_allChipOuts[:,:,i].T
plt.imshow(im2show, interpolation='nearest')
plt.colorbar(orientation='vertical')
plt.pause(0.01)
if saveFigs:
fname="fig_ExtractAllOnScan_%d.pdf" % i
plt.savefig(fname)
#==============================================================================
# Save everything
#==============================================================================
#FIXME
np.savez(outFile, singleWGscan_allChipOuts_avgd=singleWGscan_allChipOuts_avgd,
singleWGscan_allChipOuts=singleWGscan_allChipOuts,
BLscan_allChipOuts=BLscan_allChipOuts,
BLscan_pistVals=BLscan_pistVals,
allOnWGscan_allChipOuts=allOnWGscan_allChipOuts)
#==============================================================================
# To restore, you'd do:
#file='extractedFluxes_ipbc_20140930_3_xtr1.npz'
#npzfile=np.load(file)
#singleWGscan_allChipOuts=npzfile['singleWGscan_allChipOuts']
#etc.
#npzfile.files will tell you what variables are in there (by name)
#==============================================================================