@@ -66,9 +66,10 @@ def subtract_wfss_bkg(
6666 # i.e. in regions we can use as background.
6767 if got_catalog :
6868 bkg_mask = _mask_from_source_cat (input_model , wl_range_name , mmag_extract )
69- if not _sufficient_background_pixels (input_model .dq , bkg_mask ):
69+ if not _sufficient_background_pixels (input_model .dq , bkg_mask , bkg_ref . data ):
7070 log .warning ("Not enough background pixels to work with." )
7171 log .warning ("Step will be SKIPPED." )
72+ bkg_ref .close ()
7273 return None
7374 else :
7475 bkg_mask = np .ones (input_model .data .shape , dtype = bool )
@@ -86,6 +87,15 @@ def subtract_wfss_bkg(
8687 bkg = bkg_ref .data .copy ()
8788 factor , _ = rescaler (sci , bkg , var , mask = ~ bkg_mask )
8889
90+ # check for bad value of factor
91+ if not np .isfinite (factor ):
92+ log .warning (
93+ "Could not determine a finite scaling factor between reference background and data."
94+ " Step will be SKIPPED."
95+ )
96+ bkg_ref .close ()
97+ return None
98+
8999 # extract the derived factor and apply it to the unmasked, non-outlier-rejected data
90100 subtract_this = factor * bkg_ref .data
91101 result = input_model .copy ()
@@ -226,18 +236,40 @@ def _compute_rms_residual(self, sci_sub):
226236 return np .sqrt (np .nanmean (sci_sub_profile ** 2 ))
227237
228238
229- def _sufficient_background_pixels (dq_array , bkg_mask , min_pixels = 100 ):
230- """Count number of good pixels for background use.
239+ def _sufficient_background_pixels (dq_array , bkg_mask , bkg , min_pixfrac = 0.05 ):
240+ """
241+ Count number of good pixels for background use.
231242
232243 Check DQ flags of pixels selected for bkg use - XOR the DQ values with
233244 the DO_NOT_USE flag to flip the DO_NOT_USE bit. Then count the number
234245 of pixels that AND with the DO_NOT_USE flag, i.e. initially did not have
235246 the DO_NOT_USE bit set.
247+
248+ Parameters
249+ ----------
250+ dq_array : ndarray
251+ Subarray input DQ array
252+
253+ bkg_mask : ndarray
254+ Boolean background mask. True where background is GOOD.
255+
256+ bkg : ndarray
257+ Background data array
258+
259+ min_pixfrac : float, optional
260+ Minimum fraction of good pixels required for background use.
261+ Default is 0.05 (5%).
262+
263+ Returns
264+ -------
265+ int or array of int
266+ The number of good pixels for background use.
236267 """
237- return np .count_nonzero ((dq_array [bkg_mask ]
238- ^ pixel ['DO_NOT_USE' ])
239- & pixel ['DO_NOT_USE' ]
240- ) > min_pixels
268+ good_bkg = bkg != 0
269+ good_mask = np .logical_and (bkg_mask , good_bkg )
270+ n_good = np .count_nonzero ((dq_array [good_mask ] ^ pixel ["DO_NOT_USE" ]) & pixel ["DO_NOT_USE" ])
271+ min_pixels = int (min_pixfrac * dq_array .size )
272+ return n_good > min_pixels
241273
242274
243275def _mask_from_source_cat (input_model , wl_range_name , mmag_extract = None ):
0 commit comments