-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocess_flat_git_v1.py
512 lines (368 loc) · 13.2 KB
/
preprocess_flat_git_v1.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# Import required libraries
#--------------------------------------------------------------------------------#
# This script will be used for creating a master-flat frame in each filter
# preprocess_flat_git_v1.py
#--------------------------------------------------------------------------------#
# User can change this
cwd = '/home/anirban.dutta/SN2022erq_Reduction/flat/u-flat/'
OBJECT_NAME='SN2022erq'
RA_object = '18:33:25.360'
DEC_object = '+44:05:11.65'
Standard_Name = 'PG'
#--------------------------------------------------------------------------------#
import os
import re
import sys
import glob
import shutil
import numpy as np
from pyraf import iraf
from astropy.io import fits
#--------------------------------------------------------------------------------#
Software_Code = 'preprocess_v1'
#--------------------------------------------------------------------------------#
# Global Variables
filters=['u', 'g', 'r', 'i', 'z']
# GIT New CCD specifications
read_noise=12.0
gain=1.04
data_max=55000
# HCT new CCD specifications
read_noise_new = 5.75
gain_new = 0.28
data_max_new = 700000
#image header keyword
Date='DATE-OBS'
Filter='FILTER'
OBJECT='OBJECT'
CCDSECTION = 'CCDSEC'
RA = 'RA'
DEC = 'DEC'
# Working Directory
DIR_PHOT = 'For_Phot'
print('The current working directory is:\n %s' % cwd)
os.chdir(cwd)
# Functions for file handling
# function for removing files: argument: file_name
def remove_file(file_name):
try:
os.remove(file_name)
except OSError:
pass
# Function for removing files having similar names: argument: common_text
def remove_similar_files(common_text):
for residual_file in glob.glob(common_text):
remove_file(residual_file)
for text in ['*b_flat*', 'list_*']:
remove_similar_files(common_text=text)
# Function to group similar files: arguments: text_list, common_text, exceptions
def group_similar_files(text_list, common_text, exceptions=''):
list_files=glob.glob(common_text)
if exceptions !='':
list_files = filter(lambda x: not re.search(exceptions, x), list_files)
list_files.sort()
if len(text_list) !=0:
with open(text_list, 'w') as f:
for file_name in list_files:
f.write(file_name + '\n')
return list_files
def text_list_to_python_list(text_list):
if os.path.exists(text_list):
with open(text_list, 'r+') as f:
python_list=f.read().split()
return python_list
def python_list_to_text_list(python_list, text_list):
with open(text_list, 'w') as f:
for element in python_list:
f.write(str(element)+'\n')
def list_lists_to_list(list_lists, text_list):
list_name=[]
for file_name in list_lists:
with open(file_name, 'r') as f:
file_list=f.read().split()
for element in file_list:
list_name.append(element)
python_list_to_text_list(list_name, text_list)
return list_name
class color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
def display_text(text):
print('#'+'-'*(10+len(text))+'#')
print('#'+('-'*5) + str(text) +('-'*5)+'#')
print('#'+'-'*(10+len(text))+'#')
# Load IRAF Packages:
iraf.noao(_doprint=0)
iraf.imred(_doprint=0)
iraf.ccdred(_doprint=0)
iraf.crutil(_doprint=0)
iraf.images(_doprint=0)
iraf.ccdred.instrument='ccddb$kpno/direct.dat'
def edit_header(textlist_files, object_name):
list_files = text_list_to_python_list(textlist_files)
for file_name in list_files:
hdulist = fits.open(file_name, mode = 'update')
file_header = hdulist[0].header
OBJECT = file_header['OBJECT']
#RA = file_header['RA']
#DEC = file_header['DEC']
list_keywords = ['OBJECT', 'CCDSEC', 'RA', 'DEC']
dict_header = {'OBJECT': OBJECT, 'CCDSEC': CCDSECTION, 'RA': RA,
'DEC': DEC}
for keyword in list_keywords:
if keyword in file_header.keys():
file_header.remove(keyword, remove_all = True)
list_update_keywords = ['OBJECT', 'RA', 'DEC', 'RN', 'GAIN']
dict_update_header = {'OBJECT': object_name, 'RA': RA_object,
'DEC': DEC_object, 'RN': read_noise,
'GAIN': gain}
for keyword in list_update_keywords:
if keyword in file_header.keys():
file_header.remove(keyword, remove_all = True)
file_header.append(card=(keyword, dict_update_header[keyword]))
hdulist.flush()
hdulist.close()
def zero_combine(textlist_bias, master_bias, rd_noise, ccd_gain):
'''
About: combining bias frames
textlist_bias: list of bias as a text file.
master_bias: name of output master bias file (.fits)
rd_noise: provide readnoise. check ccd_property script for finding readnoise and gain.
ccd_gain: provide ccd_gain. check ccd_property script for finding readnoise and gain.
'''
task=iraf.noao.imred.ccdred.zerocombine
task.unlearn()
task.combine='median'
task.reject='ccdclip'
task.ccdtype=''
task.process='no'
task.delete='no'
task.clobber='no'
task.scale='none'
task.statsec=''
task.nlow=0
task.nhigh=1
task.nkeep=1
task.mclip='yes'
task.lsigma=3.
task.hsigma=3.
task.snoise=0
task.pclip=-0.5
task.blank=0
task.mode='ql'
#remove_file(master_bias)
task(input='@'+ textlist_bias, output = master_bias, rdnoise=rd_noise,
gain=ccd_gain)
def bias_subtract(textlist_tbs, master_bias='mbias.fits', prefix_str='b_'):
'''
About: Subtract the master bias from the frames (flat+object)
textlist_tbs: Text list of files to be bias subtracted.
master_bias: master bias file (.fits)
prefix_str: append a 'b_' to the files to denote bias subtraction.
'''
task=iraf.noao.imred.ccdred.ccdproc
task.unlearn()
task.ccdtype=''
task.max_cac=0
task.noproc='no'
task.fixpix='no'
task.oversca='no'
task.trim='no'
task.zerocor='yes'
task.darkcor='no'
task.flatcor='no'
task.illumco='no'
task.fringec='no'
task.readcor='no'
task.scancor='no'
task.readaxi='line'
task.fixfile=''
task.biassec=''
task.trimsec=''
task.zero=master_bias
task.dark=''
task.flat=''
task.fringe=''
task.minrepl=1
task.scantyp='shortscan'
task.nscan=1
task.interac='no'
task.functio='legendre'
task.order=1
task.sample='*'
task.naverag=1
task.niterat=1
task.low_rej=3
task.high_re=3
task.grow=0
task.mode='ql'
output_filename=prefix_str+'//'+'@'+textlist_tbs
remove_file(output_filename)
task(images='@'+textlist_tbs, output=output_filename)
def image_statistics(ctext, text_list='list_bsflat'):
textlist_bsflat='list_bsflat'
group_similar_files(textlist_bsflat, common_text=ctext)
task=iraf.images.imutil.imstatistics
task.unlearn()
task.lower='INDEF'
task.upper='INDEF'
task.nclip=0
task.lsigma=3
task.usigma=3
task.binwidt=0.1
task.cache='no'
task.mode='ql'
task(format='no', images='@'+textlist_bsflat, field='mean',
Stdout=text_list+'_mean')
task(format='yes', images='@'+textlist_bsflat,
field='image,npix,mean,midpt,stddev,min,max', Stdout=text_list+'_stat')
list_mean=text_list_to_python_list(text_list+'_mean')
#print list_mean
return list_mean
def flat_normalize(ctext, textlist_mean='list_bsflat', prefix_str='n'):
'''
About: Normalize the flat frames by their mean values.
Calls imstatistics function.
ctext: common text to group bias subtracted files.
textlist_mean: mean value if frames in a text file.
prefix_str: append a prefix to bias subtracted flat frames to denote normalized flat.
'''
list_bsflat=group_similar_files('', common_text=ctext)
#print list_bsflat
#print len(list_bsflat)
list_bsflat_mean=image_statistics(ctext, textlist_mean)
task=iraf.images.imutil.imarith
task.unlearn()
print('FILENAME'+' '+'MEAN_VALUE')
for index in range(0, len(list_bsflat)):
print list_bsflat[index],' ',list_bsflat_mean[index]
output_filename=prefix_str+list_bsflat[index]
remove_file(output_filename)
task(operand1=list_bsflat[index], op='/',
operand2=float(list_bsflat_mean[index]), result=output_filename)
def flat_combine(ctext, rd_noise, ccd_gain, filter_band):
'''
About: Combine flats in each band
ctext: common text to group flats in each filter.
rd_noise: provide readnoise. check ccd_property script for finding readnoise and gain.
ccd_gain: provide ccd_gain. check ccd_property script for finding readnoise and gain.
'''
textlist_nbflat='list_nbflat'
group_similar_files(textlist_nbflat, common_text=ctext)
task=iraf.noao.imred.ccdred.flatcombine
task.unlearn()
task.combine='median'
task.reject='ccdclip'
task.ccdtype=''
task.process='no'
task.subsets='no'
task.delete='no'
task.clobber='no'
task.scale='mode'
task.statsec=''
task.nlow=1
task.nhigh=1
task.nkeep=1
task.mclip='yes'
task.lsigma=3
task.hsigma=3
task.snoise=0
task.pclip=-0.5
task.blank=1
task.mode='ql'
output_filename='mflat'+filter_band+'.fits'
remove_file(output_filename)
task(input='@'+textlist_nbflat, output=output_filename, rdnoise=rd_noise, gain=ccd_gain)
def flat_correction(ctext, master_flat, exception='flat', prefix_str='f'):
list_bsobject=group_similar_files('',common_text=ctext, exceptions=exception)
print list_bsobject
task=iraf.images.imutil.imarith
task.unlearn()
for image in list_bsobject:
output_filename=prefix_str+image
remove_file(output_filename)
task(operand1=image, op='/', operand2=master_flat, result=output_filename)
def crmedian(ctext, prefix_str='c'):
list_cosmic=group_similar_files('', common_text=ctext)
task=iraf.noao.imred.crutil.crmedian
task.unlearn()
task.crmask=''
task.median=''
task.sigma=''
task.residua=''
task.var0=0
task.var1=0
task.var2=0
task.lsigma=10
task.hsigma=3
task.ncmed=5
task.nlmed=5
task.ncsig=25
task.nlsig=25
task.mode='ql'
for image in list_cosmic:
output_filename=prefix_str+image
remove_file(output_filename)
task(input=image, output=output_filename)
def astroscrappy(file_list, gain, rd_noise, data_max, sig_clip=5.5):
cosmics_no = []
mask_list = []
for i in range(len(file_list)):
hdu = fits.open(file_list[i])[0]
data = hdu.data
header = hdu.header
object_ = header['OBJECT']
crmask, cleanarray = cr.detect_cosmics(data, sigclip = sig_clip, gain = gain,
readnoise = rd_noise, satlevel = data_max, sepmed=False,
cleantype='medmask', fsmode='median')
print('Number of affected pixels is %d for file %s'%(np.sum(crmask),
file_list[i]))
cosmics_no.append(np.sum(crmask))
proc_cr = cleanarray/gain
clean_hdu = fits.PrimaryHDU(proc_cr)
clean_hdu.header = header
clean_hdu.writeto('c'+file_list[i], overwrite = True)
mask_list.append(crmask)
return [cosmics_no, mask_list]
#--------------------------------------------------------------------------------#
# Do you want to remove files from previous run
remove_prev_files=True
if remove_prev_files:
for text in ['list_*', 'masterflat*']:
remove_similar_files(common_text=text)
#--------------------------------------------------------------------------------#
# List of flat frames
list_flat = group_similar_files('list_flat', '*flat*.fits')
# List flats in same filters
for band in filters:
group_similar_files('list_'+band, '*flat_'+band+'*.fits')
# Edit header
edit_header('list_flat', 'FLAT')
display_text('BIAS FRAME IS BEING SUBTRACTED FROM ALL THE IMAGE FRAME (flat)')
# subtract the bias frames
bias_subtract(textlist_tbs='list_flat')
display_text('NORMALIZATION OF THE FLATS ARE BEING PERFORMED')
display_text('Printing the mean values of the flat frames')
# Normalize the flat frames
flat_normalize(ctext='b_*flat*.fits')
display_text('COMBINING FLATS IN EACH FILTER TO GET A MASTER FLAT')
# Flaw in code !! Modify !!
# Combine flats in each filter
for band in filters:
if [os.path.isfile(f) for f in glob.glob('nb_*'+band+'*.fits')]:
flat_combine('nb_*'+band+'*.fits', rd_noise=read_noise, ccd_gain=gain, filter_band=band)
else:
print('**************')
print('NO FLATS IN %s FILTER TO COMBINE' %band)
print('**************')
display_text('MASTER FLAT FRAME is CREATED in EACH FILTER. CONGRATS')
#--------------------------------------------------------------------------------#
#------------------------------------THE END-------------------------------------#