@@ -110,71 +110,73 @@ def process(self, data):
110110 - Reverse-calibrate the 2D background, using the correction arrays calculated above.
111111 - Subtract the background from the input slit data.
112112 """
113- with datamodels .open (data ) as data_model :
114- # If some type of background processing had already been done. Abort.
115- # UNLESS forcing is enacted.
116- if not self .force_subtract and "COMPLETE" in [
117- data_model .meta .cal_step .bkg_subtract ,
118- data_model .meta .cal_step .master_background ,
119- ]:
120- log .info ("Background subtraction has already occurred. Skipping." )
121- result = data_model .copy ()
122- record_step_status (result , "master_background" , success = False )
123- return result
124-
125- if self .user_background :
126- log .info (
127- "Calculating master background from "
128- f"user-supplied background { self .user_background } "
129- )
130- user_background = datamodels .open (self .user_background )
113+ output_model = self .prepare_output (data )
114+
115+ # If some type of background processing had already been done. Abort.
116+ # UNLESS forcing is enacted.
117+ if not self .force_subtract and "COMPLETE" in [
118+ output_model .meta .cal_step .bkg_subtract ,
119+ output_model .meta .cal_step .master_background ,
120+ ]:
121+ log .info ("Background subtraction has already occurred. Skipping." )
122+ record_step_status (output_model , "master_background" , success = False )
123+ return output_model
124+
125+ bkg_x1d_spectra = None
126+ if self .user_background :
127+ log .info (
128+ "Calculating master background from "
129+ f"user-supplied background { self .user_background } "
130+ )
131+ with datamodels .open (self .user_background ) as user_background :
131132 master_background , mb_multislit , bkg_x1d_spectra = self ._calc_master_background (
132- data_model , user_background
133+ output_model , user_background
133134 )
134- elif self .use_correction_pars :
135- log .info ("Using pre-calculated correction parameters." )
136- master_background = self .correction_pars ["masterbkg_1d" ]
137- mb_multislit = self .correction_pars ["masterbkg_2d" ]
138- else :
139- num_bkg , num_src = self ._classify_slits (data_model )
140- if num_bkg == 0 :
141- log .warning (
142- "No background slits available for creating master background. Skipping"
143- )
144- result = data_model .copy ()
145- record_step_status (result , "master_background" , False )
146- return result
147- elif num_src == 0 :
148- log .warning ("No source slits for applying master background. Skipping" )
149- result = data_model .copy ()
150- record_step_status (result , "master_background" , False )
151- return result
152-
153- log .info ("Calculating master background" )
154- master_background , mb_multislit , bkg_x1d_spectra = self ._calc_master_background (
155- data_model , sigma_clip = self .sigma_clip , median_kernel = self .median_kernel
135+ elif self .use_correction_pars :
136+ log .info ("Using pre-calculated correction parameters." )
137+ master_background = self .correction_pars ["masterbkg_1d" ]
138+ mb_multislit = self .correction_pars ["masterbkg_2d" ]
139+ else :
140+ num_bkg , num_src = self ._classify_slits (output_model )
141+ if num_bkg == 0 :
142+ log .warning (
143+ "No background slits available for creating master background. Skipping"
156144 )
157-
158- # Check that a master background was actually determined.
159- if master_background is None :
160- log .info ("No master background could be calculated. Skipping." )
161- result = data_model .copy ()
162- record_step_status (result , "master_background" , False )
163- return result
164-
165- # Now apply the de-calibrated background to the original science
166- result = nirspec_utils .apply_master_background (
167- data_model , mb_multislit , inverse = self .inverse
145+ record_step_status (output_model , "master_background" , False )
146+ return output_model
147+ elif num_src == 0 :
148+ log .warning ("No source slits for applying master background. Skipping" )
149+ record_step_status (output_model , "master_background" , False )
150+ return output_model
151+
152+ log .info ("Calculating master background" )
153+ master_background , mb_multislit , bkg_x1d_spectra = self ._calc_master_background (
154+ output_model , sigma_clip = self .sigma_clip , median_kernel = self .median_kernel
168155 )
169156
170- # Mark as completed and setup return data
171- record_step_status (result , "master_background" , True )
172- self .correction_pars = {"masterbkg_1d" : master_background , "masterbkg_2d" : mb_multislit }
173- if self .save_background :
174- self .save_model (master_background , suffix = "masterbg1d" , force = True )
175- self .save_model (mb_multislit , suffix = "masterbg2d" , force = True )
176- if bkg_x1d_spectra is not None :
177- self .save_model (bkg_x1d_spectra , suffix = "bkgx1d" , force = True )
157+ # Check that a master background was actually determined.
158+ if master_background is None :
159+ log .info ("No master background could be calculated. Skipping." )
160+ record_step_status (output_model , "master_background" , False )
161+ return output_model
162+
163+ # Now apply the de-calibrated background to the original science
164+ result = nirspec_utils .apply_master_background (
165+ output_model , mb_multislit , inverse = self .inverse
166+ )
167+
168+ # Mark as completed and setup return data
169+ record_step_status (result , "master_background" , True )
170+ self .correction_pars = {"masterbkg_1d" : master_background , "masterbkg_2d" : mb_multislit }
171+ if self .save_background :
172+ self .save_model (master_background , suffix = "masterbg1d" , force = True )
173+ self .save_model (mb_multislit , suffix = "masterbg2d" , force = True )
174+ if bkg_x1d_spectra is not None :
175+ self .save_model (bkg_x1d_spectra , suffix = "bkgx1d" , force = True )
176+
177+ # Close intermediate models that are not held in correction_pars
178+ if bkg_x1d_spectra is not None :
179+ bkg_x1d_spectra .close ()
178180
179181 return result
180182
@@ -277,21 +279,25 @@ def _calc_master_background(self, data, user_background=None, sigma_clip=3, medi
277279 # Any parameters that need be changed below are ignored.
278280 self .set_pars_from_parent ()
279281
282+ # Copy the data before processing so that intermediate calibrations
283+ # don't get applied to the output model
284+ pre_calibrated = data .copy ()
285+
280286 # First pass: just do the calibration to determine the correction
281287 # arrays. However, force all slits to be processed as extended sources.
282288 self .pathloss .source_type = "EXTENDED"
283289 self .barshadow .source_type = "EXTENDED"
284290 self .photom .source_type = "EXTENDED"
285291
286- pre_calibrated = self .flat_field .run (data )
292+ pre_calibrated = self .flat_field .run (pre_calibrated )
287293 pre_calibrated = self .pathloss .run (pre_calibrated )
288294 pre_calibrated = self .barshadow .run (pre_calibrated )
289295 pre_calibrated = self .photom .run (pre_calibrated )
290296
291297 # Create the 1D, fully calibrated master background.
292298 if user_background :
293299 log .debug (f"User background provided { user_background } " )
294- master_background = user_background
300+ master_background = user_background . copy ()
295301 bkg_x1d_spectra = None
296302 else :
297303 log .info ("Creating MOS master background from background slitlets" )
0 commit comments