@@ -352,6 +352,7 @@ def run(self, calExps, calBkgs, skyFrames, camera, backgroundToPhotometricRatioH
352352 detectors = []
353353 masks = []
354354 skyCorrs = []
355+ bgModel1Indices = []
355356 if not self .config .doApplyFlatBackgroundRatio :
356357 backgroundToPhotometricRatioHandles = [None ] * len (calExps )
357358 for calExpHandle , calBkg , backgroundToPhotometricRatioHandle in zip (
@@ -366,6 +367,7 @@ def run(self, calExps, calBkgs, skyFrames, camera, backgroundToPhotometricRatioH
366367 _ = self ._restoreOriginalBackgroundRefineMask (calExp , calBkg )
367368 masks .append (calExp .mask )
368369 skyCorrs .append (calBkg ) # Contains only the inverted original background elements at this stage
370+ bgModel1Indices .append (len (calBkg )) # Index of the original background element
369371
370372 # Make a background model for the image, using bgModel1 configs
371373 bgModel1Detector = FocalPlaneBackground .fromCamera (self .config .bgModel1 , camera )
@@ -383,7 +385,6 @@ def run(self, calExps, calBkgs, skyFrames, camera, backgroundToPhotometricRatioH
383385 self ._validateBgModel ("bgModel1" , bgModel1 , self .config .bgModel1 )
384386
385387 # Update skyCorrs with new bgModel1 background elements
386- bgModel1Index = len (skyCorrs [0 ]._backgrounds ) # used to remove bgModel1 if undoBgModel1 is True
387388 for detector , skyCorr in zip (detectors , skyCorrs ):
388389 with warnings .catch_warnings ():
389390 warnings .filterwarnings ("ignore" , "invalid value encountered" )
@@ -399,7 +400,7 @@ def run(self, calExps, calBkgs, skyFrames, camera, backgroundToPhotometricRatioH
399400
400401 # Remove the initial background model (bgModel1) from every skyCorr
401402 if self .config .undoBgModel1 :
402- for skyCorr in skyCorrs :
403+ for skyCorr , bgModel1Index in zip ( skyCorrs , bgModel1Indices ) :
403404 skyCorr ._backgrounds .pop (bgModel1Index )
404405 self .log .info (
405406 "Initial background models (bgModel1s) have been removed from all skyCorr background lists" ,
@@ -438,8 +439,8 @@ def run(self, calExps, calBkgs, skyFrames, camera, backgroundToPhotometricRatioH
438439 # Make camera-level mosaics of bg subtracted calexps and subtracted bgs
439440 calExpsBinned = []
440441 calBkgsBinned = []
441- for calExpHandle , mask , skyCorr , backgroundToPhotometricRatioHandle in zip (
442- calExps , masks , skyCorrs , backgroundToPhotometricRatioHandles
442+ for calExpHandle , mask , skyCorr , backgroundToPhotometricRatioHandle , bgModel1Index in zip (
443+ calExps , masks , skyCorrs , backgroundToPhotometricRatioHandles , bgModel1Indices
443444 ):
444445 calExp = self ._getCalExp (calExpHandle , mask , skyCorr , backgroundToPhotometricRatioHandle )
445446
0 commit comments