-
Notifications
You must be signed in to change notification settings - Fork 1
/
getipc.py
executable file
·400 lines (338 loc) · 16.1 KB
/
getipc.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
393
394
395
396
397
398
399
#!/usr/bin/env python
#This procedure determines a 5x5 IPC kernel image based on spread of charge from hot pixels
#Includes the NIRISS void map to calculate a different IPC kernel within the void region
#Uses noise and bad pixel files output by makebpmreadnoise.py
#Outputs 5x5 images for each amp in the void and outside and a 5x5x2048x2048 array of IPC across the full detector
#Also outputs a normalized 3x3x2048x2048 array of IPC across the full detector as that is what is required for the JWST pipeline
import numpy as np
import re
import os
import optparse
import shlex, subprocess, signal
from astropy.io import fits
from astropy.stats import sigma_clip
from astropy.table import Table, Column
import natsort
from copy import deepcopy
import time
import stsci.imagestats as imagestats
from photutils import CircularAnnulus
from photutils import aperture_photometry
import scipy.ndimage
#Set to enforce IPC symmetry rather than use each of the 25 pixels independently
makesymm=True
#NIRISS Detector
#noise files are in units of ADU so need gain to convert to electrons
gain=1.62
#define reference file locations
refdir='/home/user/reffiles'
darkrefdir='/home/user/darkresults'
longbpmfile=darkrefdir+'jwst_niriss_com_bpm_long.fits'
shortbpmfile=darkrefdir+'jwst_niriss_com_bpm_short.fits'
typebpmdir=darkrefdir+'badpixeltypes/'
typebpmfile=typebpmdir+'jwst_niriss_com_bpm_type'
hotfile=typebpmfile+'_hot.fits'
badflatfile=typebpmfile+'_unrelflat.fits'
donotuselongfile=typebpmfile+'_donotuse_long.fits'
refpixfile=typebpmfile+'_refpixel.fits'
voidmaskfile=refdir+'volk_voidmask10.fits'
ipcfile=refdir+'jwst_niriss_com_ipc.fits'
ipc4d=np.zeros((5,5,2048,2048))
#location of dark noise files
darknoisedir='/home/user/darkresults/noise/'
#Load dark noise data and multiply by gain to get in e-
slopenoisefile=darknoisedir+'gdqsigmadarkzero.fits'
hdulist=fits.open(slopenoisefile)
header=hdulist[0].header
slopenoise=hdulist[0].data*gain
darkcurrfile=darknoisedir+'gdqmediandark.fits'
hdulist=fits.open(darkcurrfile)
darkcurr=hdulist[0].data*gain
cdsnoisefile=darknoisedir+'gdqmediancds1fstd.fits'
hdulist=fits.open(cdsnoisefile)
cdsnoise=hdulist[0].data*gain
#load hot pixel array
hdulist=fits.open(hotfile)
hotflagged=hdulist[0].data
#load bad flat to exclude pixels bordering on ref pixels
hdulist=fits.open(badflatfile)
badflatflagged=hdulist[0].data
#load bad pixel file and refpixel file to exclude bad and ref pixels from median DC
hdulist=fits.open(donotuselongfile)
donotuselongflagged=hdulist[0].data
hdulist=fits.open(refpixfile)
refpixflagged=hdulist[0].data
#load voidmask
hdulist=fits.open(voidmaskfile)
voidmask=hdulist[0].data
#create an empty array for bad pixel mask and donotuse mask
arrshape=(5,5)
ipc=np.zeros(arrshape)
##Dark images section##
#slopenoiseactive=slopenoise[4:2044,4:2044]
#imstatslope = imagestats.ImageStats(slopenoiseactive,fields="npix,min,max,median,mean,stddev",binwidth=0.1,nclip=3)
#imstatslope.printStats()
#cdsnoiseactive=cdsnoise[4:2044,4:2044]
#imstatcds = imagestats.ImageStats(cdsnoiseactive,fields="npix,min,max,median,mean,stddev",binwidth=0.1,nclip=3)
#imstatcds.printStats()
#darkcurractive=darkcurr[4:2044,4:2044]
imstatdark = imagestats.ImageStats(darkcurr,fields="npix,min,max,median,mean,stddev",binwidth=0.1,nclip=3)
imstatdark.printStats()
#Get hot pixels not in void - need to add void mask sel below
w=(np.where((voidmask!=1)&(hotflagged==1)&(badflatflagged!=1)&(darkcurr>1.0)&(darkcurr<100.0)))
print (darkcurr[w].size)
yhotnotvoid=w[0]
xhotnotvoid=w[1]
#print (yhotnotvoid,xhotnotvoid)
numhotnotvoid=yhotnotvoid.size
#Get hot pixels in void - repeat above block
w=(np.where((voidmask==1)&(hotflagged==1)&(badflatflagged!=1)&(darkcurr>1.0)&(darkcurr<100.0)))
print (darkcurr[w].size)
yhotinvoid=w[0]
xhotinvoid=w[1]
#print (yhotinvoid,xhotinvoid)
numhotinvoid=yhotinvoid.size
#Use per amplifier - exclude pixels near amp edges
amplifier=np.array(['4','3','2','1','all'])
colstart=np.array([7,514,1028,1538,7])
colstop=np.array([510,1020,1534,2041,2041])
for j in range(5):
ampmask=np.zeros((2048,2048),'uint16')
ampmask[j*512:(1+j)*512,:]=1
#Get stats in amplifier section for medians in out and of voids
darkcurrsection=darkcurr[colstart[j]:colstop[j],:]
badflatflaggedsection=badflatflagged[colstart[j]:colstop[j],:]
donotuselongflaggedsection=donotuselongflagged[colstart[j]:colstop[j],:]
refpixflaggedsection=refpixflagged[colstart[j]:colstop[j],:]
voidmasksection=voidmask[colstart[j]:colstop[j],:]
#=====================================================================================================
#Firstly work on out of void region
w=(np.where((voidmasksection!=1)&(badflatflaggedsection!=1)&(donotuselongflaggedsection!=1)&(refpixflaggedsection!=1)))
clippeddarkcurrsection=sigma_clip(darkcurrsection[w],sigma=3,maxiters=5)
mediandarkcurrnotvoid=np.ma.median(clippeddarkcurrsection)
#mediandarkcurrnotvoid=np.median(darkcurrsection[w])
print (amplifier[j],mediandarkcurrnotvoid)
#In void - not for amp A
if j>0:
w=(np.where((voidmasksection==1)&(badflatflaggedsection!=1)&(donotuselongflaggedsection!=1)&(refpixflaggedsection!=1)))
mediandarkcurrinvoid=np.median(darkcurrsection[w])
print (amplifier[j],mediandarkcurrinvoid)
#only use pixels >3 away from ref pixels and with no hot (>1 e/s) neighbours
fiveby3d=[]
for k in range(numhotnotvoid):
if ((yhotnotvoid[k]>colstart[j])and(yhotnotvoid[k]<colstop[j])and(xhotnotvoid[k]>6)and(xhotnotvoid[k]<2041)):
fivebydc=darkcurr[yhotnotvoid[k]-3:yhotnotvoid[k]+4,xhotnotvoid[k]-3:xhotnotvoid[k]+4]
numhotincutout=fivebydc[np.where(fivebydc>1.0)].size
if numhotincutout<2:
fiveby=fivebydc-mediandarkcurrnotvoid
fiveby=fiveby/np.sum(fiveby)
#if doing all amps together flip if in amps 4 or 2
if ((j==4) and (((yhotnotvoid[k]>colstart[0]) and (yhotnotvoid[k]<colstop[0]))or((yhotnotvoid[k]>colstart[2]) and (yhotnotvoid[k]<colstop[2])))):
fiveby=np.flip(fiveby,axis=0)
if len(fiveby3d)==0:
fiveby3d=fiveby
else:
fiveby3d=np.dstack((fiveby3d,fiveby))
clippedfiveby3d=sigma_clip(fiveby3d,sigma=3,maxiters=5,axis=2)
clippedfiveby3dmean=np.ma.mean(clippedfiveby3d,axis=2)
#print ('clippedfiveby3dmean',clippedfiveby3dmean)
clippedfiveby3dmedian=np.ma.median(clippedfiveby3d,axis=2)
#print ('clippedfiveby3dmedian',clippedfiveby3dmedian)
header['NAXIS'] = 2
border=np.concatenate((np.ravel(clippedfiveby3dmedian[0,:]),np.ravel(clippedfiveby3dmedian[-1,:]),
np.ravel(clippedfiveby3dmedian[1:6,0]),np.ravel(clippedfiveby3dmedian[1:6,-1])))
normclippedfiveby3dmedian=clippedfiveby3dmedian.data-np.mean(border)
normclippedfiveby3dmedian=normclippedfiveby3dmedian/np.sum(normclippedfiveby3dmedian)
print (fiveby3d.shape,np.mean(border))
#trim & enforce symmetry for some pixels
normclippedfiveby3dmedian=normclippedfiveby3dmedian[1:6,1:6]
mask1=np.zeros((normclippedfiveby3dmedian.shape),'uint16')
mask1[2,1]=1
mask1[2,3]=1
mask2=np.zeros((normclippedfiveby3dmedian.shape),'uint16')
mask2[1,1]=1
mask2[1,3]=1
mask2[3,1]=1
mask2[3,3]=1
mask3=np.zeros((normclippedfiveby3dmedian.shape),'uint16')
mask3[0,2]=1
mask3[4,2]=1
mask3[2,0]=1
mask3[2,4]=1
mask4=np.zeros((normclippedfiveby3dmedian.shape),'uint16')
mask4[0,1]=1
mask4[0,3]=1
mask4[1,0]=1
mask4[1,4]=1
mask4[3,0]=1
mask4[3,4]=1
mask4[4,1]=1
mask4[4,3]=1
mask5=np.zeros((normclippedfiveby3dmedian.shape),'uint16')
mask5[0,0]=1
mask5[0,4]=1
mask5[4,0]=1
mask5[4,4]=1
#set option of enforced symmetry
if makesymm==True:
w=np.where(mask1==1)
normclippedfiveby3dmedian[w]=np.mean(normclippedfiveby3dmedian[w])
w=np.where(mask2==1)
normclippedfiveby3dmedian[w]=np.mean(normclippedfiveby3dmedian[w])
w=np.where(mask3==1)
normclippedfiveby3dmedian[w]=np.mean(normclippedfiveby3dmedian[w])
w=np.where(mask4==1)
normclippedfiveby3dmedian[w]=np.mean(normclippedfiveby3dmedian[w])
w=np.where(mask5==1)
normclippedfiveby3dmedian[w]=np.mean(normclippedfiveby3dmedian[w])
filemedian=os.path.join(refdir,'ipc5by5median_amp%s_notvoid.fits' % (amplifier[j]))
fits.writeto(filemedian,normclippedfiveby3dmedian,header,overwrite=True)
print (normclippedfiveby3dmedian[2,1])
if j<4:
#put section in 4D IPC file
w=np.where((voidmask!=1)&(ampmask==1))
normclippedfiveby3dmedianexpand=np.expand_dims(normclippedfiveby3dmedian,axis=2)
normclippedfiveby3dmedianexpand=np.expand_dims(normclippedfiveby3dmedianexpand,axis=3)
normclippedfiveby3dmedianexpand=np.repeat(normclippedfiveby3dmedianexpand,2048,axis=2)
normclippedfiveby3dmedianexpand=np.repeat(normclippedfiveby3dmedianexpand,2048,axis=3)
ipc4d[:,:,w[0],w[1]]=normclippedfiveby3dmedianexpand[:,:,w[0],w[1]]
#=====================================================================================================
#then repeat this section for void - not for amp A and for amp D too few hot pixels so use inverted amp C
if j==1 or j==2 or j==4:
fiveby3d=[]
for k in range(numhotinvoid):
if ((yhotinvoid[k]>colstart[j])and(yhotinvoid[k]<colstop[j])and(xhotinvoid[k]>6)and(xhotinvoid[k]<2041)):
fivebydc=darkcurr[yhotinvoid[k]-3:yhotinvoid[k]+4,xhotinvoid[k]-3:xhotinvoid[k]+4]
numhotincutout=fivebydc[np.where(fivebydc>1.0)].size
if numhotincutout<2:
fiveby=fivebydc-mediandarkcurrinvoid
fiveby=fiveby/np.sum(fiveby)
#if doing all amps together flip if in amps 4 or 2
if ((j==4) and (((yhotnotvoid[k]>colstart[0]) and (yhotnotvoid[k]<colstop[0]))or((yhotnotvoid[k]>colstart[2]) and (yhotnotvoid[k]<colstop[2])))):
fiveby=np.flip(fiveby,axis=0)
#print (fiveby.shape)
if len(fiveby3d)==0:
fiveby3d=fiveby
else:
fiveby3d=np.dstack((fiveby3d,fiveby))
clippedfiveby3d=sigma_clip(fiveby3d,sigma=3,maxiters=5,axis=2)
clippedfiveby3dmean=np.ma.mean(clippedfiveby3d,axis=2)
#print ('clippedfiveby3dmean',clippedfiveby3dmean)
clippedfiveby3dmedian=np.ma.median(clippedfiveby3d,axis=2)
#print ('clippedfiveby3dmedian',clippedfiveby3dmedian)
header['NAXIS'] = 2
border=np.concatenate((np.ravel(clippedfiveby3dmedian[0,:]),np.ravel(clippedfiveby3dmedian[-1,:]),
np.ravel(clippedfiveby3dmedian[1:6,0]),np.ravel(clippedfiveby3dmedian[1:6,-1])))
normclippedfiveby3dmedian=clippedfiveby3dmedian.data-np.mean(border)
normclippedfiveby3dmedian=normclippedfiveby3dmedian/np.sum(normclippedfiveby3dmedian)
#trim & enforce symmetry for some pixels
normclippedfiveby3dmedian=normclippedfiveby3dmedian[1:6,1:6]
mask1=np.zeros((normclippedfiveby3dmedian.shape),'uint16')
mask1[2,1]=1
mask1[2,3]=1
mask2=np.zeros((normclippedfiveby3dmedian.shape),'uint16')
mask2[1,1]=1
mask2[1,3]=1
mask2[3,1]=1
mask2[3,3]=1
mask3=np.zeros((normclippedfiveby3dmedian.shape),'uint16')
mask3[0,2]=1
mask3[4,2]=1
mask3[2,0]=1
mask3[2,4]=1
mask4=np.zeros((normclippedfiveby3dmedian.shape),'uint16')
mask4[0,1]=1
mask4[0,3]=1
mask4[1,0]=1
mask4[1,4]=1
mask4[3,0]=1
mask4[3,4]=1
mask4[4,1]=1
mask4[4,3]=1
mask5=np.zeros((normclippedfiveby3dmedian.shape),'uint16')
mask5[0,0]=1
mask5[0,4]=1
mask5[4,0]=1
mask5[4,4]=1
#set option of enforced symmetry
if makesymm==True:
w=np.where(mask1==1)
normclippedfiveby3dmedian[w]=np.mean(normclippedfiveby3dmedian[w])
w=np.where(mask2==1)
normclippedfiveby3dmedian[w]=np.mean(normclippedfiveby3dmedian[w])
w=np.where(mask3==1)
normclippedfiveby3dmedian[w]=np.mean(normclippedfiveby3dmedian[w])
w=np.where(mask4==1)
normclippedfiveby3dmedian[w]=np.mean(normclippedfiveby3dmedian[w])
w=np.where(mask5==1)
normclippedfiveby3dmedian[w]=np.mean(normclippedfiveby3dmedian[w])
normclippedfiveby3dmedianinvoid=deepcopy(normclippedfiveby3dmedian)
filemedian=os.path.join(refdir,'ipc5by5median_amp%s_invoid.fits' % (amplifier[j]))
fits.writeto(filemedian,normclippedfiveby3dmedian,header,overwrite=True)
print (normclippedfiveby3dmedian[2,1])
print (fiveby3d.shape,np.mean(border))
#expand shape of array
normclippedfiveby3dmedianinvoidexpand=np.expand_dims(normclippedfiveby3dmedianinvoid,axis=2)
normclippedfiveby3dmedianinvoidexpand=np.expand_dims(normclippedfiveby3dmedianinvoidexpand,axis=3)
normclippedfiveby3dmedianinvoidexpand=np.repeat(normclippedfiveby3dmedianinvoidexpand,2048,axis=2)
normclippedfiveby3dmedianinvoidexpand=np.repeat(normclippedfiveby3dmedianinvoidexpand,2048,axis=3)
if j==3:
normclippedfiveby3dmedianinvoid=np.flip(normclippedfiveby3dmedianinvoid,axis=0)
filemedian=os.path.join(refdir,'ipc5by5median_amp%s_invoid_symm.fits' % (amplifier[j]))
fits.writeto(filemedian,normclippedfiveby3dmedianinvoid,header,overwrite=True)
#expand shape of array
normclippedfiveby3dmedianinvoidexpand=np.expand_dims(normclippedfiveby3dmedianinvoid,axis=2)
normclippedfiveby3dmedianinvoidexpand=np.expand_dims(normclippedfiveby3dmedianinvoidexpand,axis=3)
normclippedfiveby3dmedianinvoidexpand=np.repeat(normclippedfiveby3dmedianinvoidexpand,2048,axis=2)
normclippedfiveby3dmedianinvoidexpand=np.repeat(normclippedfiveby3dmedianinvoidexpand,2048,axis=3)
if j>0 and j<4:
#put section in 4D IPC file
w=np.where((voidmask==1)&(ampmask==1))
print (ipc4d[:,:,w[0],w[1]].shape,normclippedfiveby3dmedianinvoidexpand[:,:,w[0],w[1]].shape)
ipc4d[:,:,w[0],w[1]]=normclippedfiveby3dmedianinvoidexpand[:,:,w[0],w[1]]
#set all reference pixels to 1 in centre and zero elsewhere
w=np.where(refpixflagged==1)
print (refpixflagged[w].size)
ipc4d[:,:,w[0],w[1]]=0
ipc4d[2,2,w[0],w[1]]=1
#output 4D IPC convolution reference file with 5x5
header['NAXIS'] = 4
fits.writeto(ipcfile,ipc4d,header,overwrite=True)
#make a 4D IPC convolution reference file with 3x3 for the official reference file - will need to renormalize
ipc4d_3by3=ipc4d[1:4,1:4,:,:]
print (ipc4d_3by3.shape)
#Do per amplifier
amplifier=np.array(['4','3','2','1'])
colstart=np.array([4,512,1024,1536])
colstop=np.array([512,1024,1536,2044])
for j in range(4):
ampmask=np.zeros((2048,2048),'uint16')
ampmask[j*512:(1+j)*512,:]=1
#first do void region (only if not amp 4)
if j>0:
w=np.where((voidmask==1)&(ampmask==1)&(refpixflagged==0))
print (voidmask[w].size,w[0][0],w[1][0])
scale = np.sum(ipc4d_3by3[:,:,w[0][0],w[1][0]])
print (j, scale)
print (w[0][0],w[1][0])
print (ipc4d_3by3[:,:,w[0][0],w[1][0]])
ipc4d_3by3[:,:,w[0],w[1]] = ipc4d_3by3[:,:,w[0],w[1]] / scale
print (np.sum(ipc4d_3by3[:,:,w[0][0],w[1][0]]))
#then do out of void region
w=np.where((voidmask==0)&(ampmask==1)&(refpixflagged==0))
print (voidmask[w].size,w[0][0],w[1][0])
scale = np.sum(ipc4d_3by3[:,:,w[0][0],w[1][0]])
print (j, scale)
print (w[0][0],w[1][0])
print (ipc4d_3by3[:,:,w[0][0],w[1][0]])
ipc4d_3by3[:,:,w[0],w[1]] = ipc4d_3by3[:,:,w[0],w[1]] / scale
print (np.sum(ipc4d_3by3[:,:,w[0][0],w[1][0]]))
#set all reference pixels to 1 in centre and zero elsewhere
w=np.where(refpixflagged==1)
print (refpixflagged[w].size)
ipc4d_3by3[:,:,w[0],w[1]]=0
ipc4d_3by3[1,1,w[0],w[1]]=1
#output 4D IPC convolution reference file with 3x3
header['NAXIS'] = 4
ipcfile_3by3 = ipcfile.replace('.fits','_3x3.fits')
fits.writeto(ipcfile_3by3,ipc4d_3by3,header,overwrite=True)