/
bench_reduce.py
executable file
·143 lines (110 loc) · 4.22 KB
/
bench_reduce.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
#!/usr/bin/python
import sys
import os
import glob
glob = glob.glob
#Load the IRAF packages we'll need
try:
current_dir = os.getcwd()
if os.getlogin() == 'Arthur':
os.chdir('/Users/Arthur/Ureka/iraf/local')
from pyraf import iraf
os.chdir(current_dir)
iraf.imred(_doprint=0)
iraf.ccdred(_doprint=0)
except Exception as e:
print "Failure: could not find pyraf/iraf"
sys.exit(1)
def make_lists(raw_dir):
all_raw = glob('{}/*.fits'.format(raw_dir))
all_ot = ['{}_ot.fits'.format(os.path.basename(i).\
split('.fits')[0]) for i in all_raw]
comps = [i for i in all_ot if 'comp' in i]
darks = [i for i in all_ot if 'dark' in i]
flats = [i for i in all_ot if 'dflat' in i]
zero = [i for i in all_ot if 'zero' in i]
all_otz = [i for i in all_ot if 'zero' not in i]
outdict = {'all_raw':all_raw,
'all_ot':all_ot,
'all_otz':all_otz,
'comps':comps,
'darks':darks,
'zeros':zero,
'flats':flats}
return outdict
def write_list(inlist, name):
with open(name,'w') as f:
for i in inlist:
f.write('{}\n'.format(i))
return
def setup_dir(raw_dir):
list_dict = make_lists(raw_dir)
for k in list_dict.keys():
if k == 'flats':
continue
write_list(list_dict[k],'{}.lst'.format(k))
return list_dict
def cull_bias(biaslist,listname):
iraf.imstat('@{}'.format(listname))
bads = raw_input('Select any biasi you want to remove, q to continue\n')
while bads.lower() != 'q':
badlist = bads.split(',')
print 'badlist is {}'.format(badlist)
newlist = [i for i in biaslist if not any([j.strip() in i for j in badlist])]
write_list(newlist,listname)
iraf.imstat('@{}'.format(listname))
bads = raw_input('Select any biasi you want to remove, q to continue\n')
return
def detect_flats(directory):
flatlist = glob('{}/dflat*.fits'.format(directory))
explist = [os.path.basename(i).split('_')[1] for i in flatlist]
unique_exp = set(explist)
outlist = []
print 'detecting flats...'
for exp in unique_exp:
print '\tfound {}'.format(exp)
tmplist = [i for i in flatlist if exp in i]
tmpname = '{}/dflats_{}.lst'.format(directory,exp)
write_list(tmplist,tmpname)
outlist.append(tmpname)
return
def main(raw_dir):
if not os.path.exists('{}/step1.tmp'.format(os.getcwd())):
print 'Setting up directory'
list_dict = setup_dir(raw_dir)
print "OT'ing"
iraf.ccdproc('@all_raw.lst',output='@all_ot.lst',overscan=True,trim=True,
zerocor=False,darkcor=False,flatcor=False,illumco=False,
biassec='image',trimsec='image',zero='',interact=True,order=100)
cull_bias(list_dict['zeros'],'zeros.lst')
os.system('touch {}/step1.tmp'.format(os.getcwd()))
return
print 'Making master bias'
iraf.zerocombine('@zeros.lst',output='Zero.fits',combine='average',reject='crreject')
print 'Subtracting bias'
iraf.ccdproc('@all_otz.lst',overscan=False,trim=False,zerocor=True,zero='Zero.fits')
detect_flats(os.getcwd())
flats_to_make = glob('dflat*.lst')
for flat in flats_to_make:
name = 'dFlat_{}.fits'.format(flat.split('.lst')[0].split('_')[1])
print 'Making flat {}'.format(name)
iraf.flatcombine('@{}'.format(flat),output=name,combine='average',reject='crreject')
try:
print 'Making master dark'
iraf.darkcombine('@darks.lst',output='Dark.fits',combine='average',reject='crreject')
except Exception as e:
print '\t{}'.format(e)
print "\tNO DARK FOUND"
print 'Making master comp'
iraf.imcombine('@comps.lst','Comp.fits',combine='average',reject='crreject')
return
if __name__ == '__main__':
try:
if not os.path.exists(sys.argv[1]):
print 'The request was made but it was not good'
sys.exit(1)
except Exception as e:
print 'The request was made but it was not good'
sys.exit(1)
print "Let's do it {}".format(sys.argv[1])
main(sys.argv[1])