66@author J. Chiang <jchiang@slac.stanford.edu>
77"""
88import warnings
9+ import numpy as np
910import astropy .io .fits as fits
1011from astropy .utils .exceptions import AstropyWarning , AstropyUserWarning
1112from lsst .eotest .fitsTools import fitsWriteto
@@ -227,7 +228,9 @@ def bias_image(self, amp, overscan=None, **kwargs):
227228 return my_image .Factory (my_image , deep = True )
228229 return self .bias_image_using_overscan (amp , overscan = overscan , ** kwargs )
229230
230- def bias_subtracted_image (self , amp , overscan = None , ** kwargs ):
231+ def bias_subtracted_image (self , amp , overscan = None ,
232+ max_pca_reduced_chisq = None ,
233+ ** kwargs ):
231234 """
232235 Subtract a bias image to correct for the offset. A bias correction is also
233236 applied if a base_frame is passed. The bias image with the offset values is
@@ -252,18 +255,24 @@ def bias_subtracted_image(self, amp, overscan=None, **kwargs):
252255 # Make a deep copy of the bias frame.
253256 bias = self .bias_frame [amp ].Factory (self .bias_frame [amp ], deep = True )
254257 # Subtract x-independent component using overscan.
255- bias -= \
256- self .bias_frame .bias_image_using_overscan (amp ,
257- overscan = overscan , ** kwargs )
258+ bias -= self .bias_frame .bias_image_using_overscan (
259+ amp , overscan = overscan , ** kwargs )
258260 # Subtract x-independent component of image for this amp
259261 # using overscan.
260262 my_image -= \
261263 self .bias_image_using_overscan (amp , overscan = overscan , ** kwargs )
262264 # Subtract structured, x-dependent part.
263265 my_image -= bias
264266 elif self .ccd_pcas is not None :
265- my_image .getImage ().array [:] \
266- -= self .ccd_pcas .pca_bias_correction (amp , my_image .getImage ().array )
267+ bias_model = self .ccd_pcas .pca_bias_correction (amp , my_image .getImage ().array )
268+ bias_image = afwImage .ImageF (np .array (bias_model , dtype = np .float32 ))
269+ bbox = self .amp_geom .serial_overscan
270+ chisq = self .ccd_pcas .bbox_chisq (my_image .getImage (), bias_image ,
271+ bbox )
272+ if (max_pca_reduced_chisq is not None and
273+ chisq / bbox .area > max_pca_reduced_chisq ):
274+ raise MaskedCCDBiasImageException
275+ my_image .getImage ().array [:] -= bias_model
267276 else :
268277 my_image -= self .bias_image (amp , overscan , ** kwargs )
269278
0 commit comments