-
Notifications
You must be signed in to change notification settings - Fork 0
/
makemastercals.py
180 lines (129 loc) · 4.4 KB
/
makemastercals.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
#!/usr/bin/env python
import os
import sys
import pyfits
import numpy
import logging
import glob
import warnings
import sdss2fits
import reduce_sdsspt
import config
sys.path.append(config.qr_dir)
import podi_imcombine
def make_master_bias(filelist):
datablocks = []
for fn in filelist:
valid_hdu, extras = reduce_sdsspt.reduce_sdss(
fn,
overscan=True,
trim=True,
subtract_bias=False,
correct_flat=False,
)
if (valid_hdu is None):
continue
# valid_hdu.info()
datablocks.append(valid_hdu['SCI'].data)
# print datablocks
masterbias = podi_imcombine.imcombine_data(
datas=datablocks,
operation='sigmaclipmean'
)
masterbias_hdu = pyfits.HDUList([
pyfits.PrimaryHDU(),
pyfits.ImageHDU(
data=masterbias,
name='SCI',
)])
return masterbias_hdu
def make_master_flat(filelist, bias_hdu, write_norm_flat=False,
good_flux=None):
if (good_flux is None):
good_flux=[10000,45000]
logger = logging.getLogger("MakeMasterFlat")
datablocks = []
for fn in filelist:
valid_hdu, extras = reduce_sdsspt.reduce_sdss(
fn=fn,
overscan=True, trim=True,
subtract_bias=True,
bias_hdu=bias_hdu,
correct_flat=False,
)
if (valid_hdu is None):
continue
# normalize flatfield using the central 50%
flatraw = valid_hdu['SCI'].data
norm_area = flatraw[
int(0.25*flatraw.shape[0]):int(0.75*flatraw.shape[0]),
int(0.25*flatraw.shape[1]):int(0.75*flatraw.shape[1])]
norm_intensity = numpy.median(norm_area)
logger.debug("%s - mean flux: %.1f" % (fn, norm_intensity))
if (norm_intensity < good_flux[0]):
logger.warning("Excluding flat %s from mastercal, flux (%7.1f) to LOW" % (
fn, norm_intensity
))
continue
elif (norm_intensity > good_flux[1]):
logger.warning("Excluding flat %s from mastercal, flux (%7.1f) to HIGH" % (
fn, norm_intensity
))
continue
flat_norm = flatraw / norm_intensity
flat_norm[flat_norm < 0.1] = numpy.NaN
if (write_norm_flat):
pyfits.PrimaryHDU(data=flat_norm).writeto(fn[:-4]+".norm.fits", clobber=True)
datablocks.append(flat_norm)
masterflat = podi_imcombine.imcombine_data(
datas=datablocks,
operation='sigmaclipmean'
)
masterflat_hdu = pyfits.HDUList([
pyfits.PrimaryHDU(),
pyfits.ImageHDU(
data=masterflat,
name='SCI',
)])
return masterflat_hdu
def make_mastercals_from_filelist(filelist, cals_dir):
#
# Select all bias frames from file list
#
bias_list = []
flat_list = {'u': [], 'g': [], 'r': [], 'i': [], 'z': [], 'unknown': []}
for filename in filelist:
hdulist = sdss2fits.open_sdss_fits(filename)
if (hdulist is None):
continue
# with warnings.catch_warnings():
# warnings.simplefilter("ignore")
# hdulist = pyfits.open(filename)
hdr = hdulist[0].header
filtername = hdr['FILTER']
obstype = hdr['FLAVOR']
if (obstype.lower() == 'bias'):
bias_list.append(filename)
elif (obstype.lower() == 'flat'):
flat_list[filtername].append(filename)
if (len(bias_list) > 0):
print "\n\nBIAS:\n--%s" % ("\n--".join(bias_list))
bias_hdu = make_master_bias(bias_list)
bias_out = "%s/masterbias.fits" % (cals_dir)
bias_hdu.writeto(bias_out, clobber=True)
for filtername in flat_list:
filelist = flat_list[filtername]
print "\n\nFLATS for %s:\n--%s" % (filtername, "\n--".join(filelist))
if (len(filelist) <= 0):
print "No files found!"
continue
masterflat_hdu = make_master_flat(filelist, bias_hdu=bias_hdu)
flat_out = "%s/masterflat_%s.fits" % (cals_dir, filtername)
print "Writing master-flat to %s" % (flat_out)
masterflat_hdu.writeto(flat_out, clobber=True)
if __name__ == "__main__":
dirname = sys.argv[1]
filelist = glob.glob("%s/*.fit" % (dirname))
print filelist
cals_dir = sys.argv[2]
make_mastercals_from_filelist(filelist, cals_dir)