From 27d92c98e192076402c324c39caaac9d67d2a03e Mon Sep 17 00:00:00 2001 From: Timothy Brandt Date: Wed, 26 Jun 2024 14:04:03 -0400 Subject: [PATCH 01/18] Added support for reuse of aperture correction objects in multiple integration data --- jwst/extract_1d/extract.py | 68 +++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/jwst/extract_1d/extract.py b/jwst/extract_1d/extract.py index 32442c8f83..ff64a602c6 100644 --- a/jwst/extract_1d/extract.py +++ b/jwst/extract_1d/extract.py @@ -3736,6 +3736,13 @@ def create_extraction(extract_ref_dict, use_source_posn = False log.info(f"Setting use_source_posn to False for source type {source_type}") + # Turn off use_source_posn if working on non-primary NRS fixed slits + if is_multiple_slits: + if exp_type == 'NRS_FIXEDSLIT' and slitname != slit.meta.instrument.fixed_slit: + use_source_posn = False + log.info("Can only compute source location for primary NIRSpec slit,") + log.info("so setting use_source_posn to False") + if photom_has_been_run: pixel_solid_angle = meta_source.meta.photometry.pixelarea_steradians if pixel_solid_angle is None: @@ -3783,6 +3790,8 @@ def create_extraction(extract_ref_dict, log.info(f"Beginning loop over {shape[0]} integrations ...") integrations = range(shape[0]) + ra_last = dec_last = wl_last = None + for integ in integrations: try: ra, dec, wavelength, temp_flux, f_var_poisson, f_var_rnoise, \ @@ -3896,27 +3905,46 @@ def create_extraction(extract_ref_dict, else: wl = wavelength.min() - if isinstance(input_model, datamodels.ImageModel): - apcorr = select_apcorr(input_model)( - input_model, - apcorr_ref_model.apcorr_table, - apcorr_ref_model.sizeunit, - location=(ra, dec, wl) - ) - else: - match_kwargs = {'location': (ra, dec, wl)} - if exp_type in ['NRS_FIXEDSLIT', 'NRS_BRIGHTOBJ']: - match_kwargs['slit'] = slitname - - apcorr = select_apcorr(input_model)( - input_model, - apcorr_ref_model.apcorr_table, - apcorr_ref_model.sizeunit, - slit_name=slitname, - **match_kwargs - ) - apcorr.apply(spec.spec_table) + # See whether we can reuse the previous aperture correction + # object. If so, just apply the pre-computed correction to + # save a ton of time. + if ra == ra_last and dec == dec_last and wl == wl_last and apcorr.tabulated_correction is not None: + # re-use the last aperture correction + apcorr.apply_tabulated_correction(spec.spec_table) + else: + if isinstance(input_model, datamodels.ImageModel): + apcorr = select_apcorr(input_model)( + input_model, + apcorr_ref_model.apcorr_table, + apcorr_ref_model.sizeunit, + location=(ra, dec, wl) + ) + else: + match_kwargs = {'location': (ra, dec, wl)} + if exp_type in ['NRS_FIXEDSLIT', 'NRS_BRIGHTOBJ']: + match_kwargs['slit'] = slitname + + apcorr = select_apcorr(input_model)( + input_model, + apcorr_ref_model.apcorr_table, + apcorr_ref_model.sizeunit, + slit_name=slitname, + **match_kwargs + ) + # Attempt to tabulate the aperture correction for later use. + # If this fails, fall back on the old method. + try: + apcorr.tabulate_correction(spec.spec_table) + print(apcorr.tabulated_correction) + except: + apcorr.apply(spec.spec_table) + + # Save previous ra, dec, wavelength in case we can reuse + # the aperture correction object. + ra_last = ra + dec_last = dec + wl_last = wl output_model.spec.append(spec) if log_increment > 0 and (integ + 1) % log_increment == 0: From cf9ed07a46dc21d0b10848f88f50d13df14f76c1 Mon Sep 17 00:00:00 2001 From: Timothy Brandt Date: Wed, 26 Jun 2024 14:04:42 -0400 Subject: [PATCH 02/18] Added methods to enable the reuse of aperature corrections --- jwst/extract_1d/apply_apcorr.py | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/jwst/extract_1d/apply_apcorr.py b/jwst/extract_1d/apply_apcorr.py index d39ba45235..79ae9a9b39 100644 --- a/jwst/extract_1d/apply_apcorr.py +++ b/jwst/extract_1d/apply_apcorr.py @@ -73,6 +73,7 @@ def __init__(self, input_model: DataModel, apcorr_table: fits.FITS_rec, sizeunit self.reference = self._reduce_reftable() self._convert_size_units() self.apcorr_func = self.approximate() + self.tabulated_correction = None def _convert_size_units(self): """If the SIZE or Radius column is in units of arcseconds, convert to pixels.""" @@ -227,6 +228,51 @@ def _approx_func(wavelength: float, size: float, pixel_phase: float) -> RectBiva def measure_phase(self): # Future method in determining pixel phase pass + def tabulate_correction(self, spec_table: fits.FITS_rec): + """Tabulate the interpolated aperture correction value. This will save time when applying it later, especially if it is to be applied to multiple integrations. Modifies self.tabulated_correction. + + Parameters + ---------- + spec_table : `~fits.FITS_rec` + Table of aperture corrections values from apcorr reference file. + + """ + + coefs = [] + for row in spec_table: + try: + correction = self.apcorr_func(row['wavelength'], row['npixels'], self.phase) + except ValueError: + correction = None # Some input wavelengths might not be supported (especially at the ends of the range) + + if correction: + coefs += [correction.item()] + else: + coefs += [1] + + self.tabulated_correction = np.asarray(coefs) + + def apply_tabulated_correction(self, spec_table: fits.FITS_rec): + """Apply self.tabulated_correction values to source-related extraction results in-place. + + Parameters + ---------- + spec_table : `~fits.FITS_rec` + Table of aperture corrections values from apcorr reference file. + + """ + + if self.tabulated_correction is None: + raise ValueError("Cannot call apply_tabulated_correction without first calling tabulate_correction") + + flux_cols_to_correct = ('flux', 'flux_error', 'surf_bright', 'sb_error') + var_cols_to_correct = ('flux_var_poisson', 'flux_var_rnoise', 'flux_var_flat', + 'sb_var_poisson', 'sb_var_rnoise', 'sb_var_flat') + for col in flux_cols_to_correct: + spec_table[col] *= self.tabulated_correction + for col in var_cols_to_correct: + spec_table[col] *= self.tabulated_correction**2 + def apply(self, spec_table: fits.FITS_rec): """Apply interpolated aperture correction value to source-related extraction results in-place. From ff0b1eaa4e3291140dbfce8996d9e9f3ca269241 Mon Sep 17 00:00:00 2001 From: Timothy Brandt Date: Wed, 26 Jun 2024 14:09:00 -0400 Subject: [PATCH 03/18] Added notes for changes to the aperture correction --- CHANGES.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 4f51f1ce17..3a773e9ec7 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -133,6 +133,13 @@ extract_1d all slits containing point sources are now handled consistently, whether they are marked primary or not. [#8467] +- Added functionality to the phase-based aperture correction object to support + reuse of aperture correction objects across multiple integrations [#] + +- Changed extract.py to attempt to tabulate and reuse an aperture correction + object in integrations after the first one. This can save a very large + amount of time in BOTS reductions [#] + extract_2d ---------- From 0913a1c64a9f980e21ae109ea37ad3e3bd5ca9d6 Mon Sep 17 00:00:00 2001 From: Timothy Brandt Date: Wed, 26 Jun 2024 14:13:57 -0400 Subject: [PATCH 04/18] Updated github issue number --- CHANGES.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 3a773e9ec7..9451eea497 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -134,11 +134,11 @@ extract_1d whether they are marked primary or not. [#8467] - Added functionality to the phase-based aperture correction object to support - reuse of aperture correction objects across multiple integrations [#] + reuse of aperture correction objects across multiple integrations [#8609] - Changed extract.py to attempt to tabulate and reuse an aperture correction object in integrations after the first one. This can save a very large - amount of time in BOTS reductions [#] + amount of time in BOTS reductions [#8609] extract_2d ---------- From 5df4b46fe06ad849522453f739ac06c6d2a034fa Mon Sep 17 00:00:00 2001 From: Timothy Brandt Date: Wed, 26 Jun 2024 14:28:27 -0400 Subject: [PATCH 05/18] Fixed bugs in the calling of the tabulated aperture correction --- jwst/extract_1d/extract.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/jwst/extract_1d/extract.py b/jwst/extract_1d/extract.py index ff64a602c6..6e75dcd237 100644 --- a/jwst/extract_1d/extract.py +++ b/jwst/extract_1d/extract.py @@ -3790,7 +3790,7 @@ def create_extraction(extract_ref_dict, log.info(f"Beginning loop over {shape[0]} integrations ...") integrations = range(shape[0]) - ra_last = dec_last = wl_last = None + ra_last = dec_last = wl_last = apcorr = None for integ in integrations: try: @@ -3905,10 +3905,16 @@ def create_extraction(extract_ref_dict, else: wl = wavelength.min() + apcorr_available = False + if apcorr is not None: + if hasattr(apcorr, 'tabulated_correction'): + if apcorr.tabulated_correction is not None: + apcorr_available = True + # See whether we can reuse the previous aperture correction # object. If so, just apply the pre-computed correction to # save a ton of time. - if ra == ra_last and dec == dec_last and wl == wl_last and apcorr.tabulated_correction is not None: + if ra == ra_last and dec == dec_last and wl == wl_last and apcorr_available: # re-use the last aperture correction apcorr.apply_tabulated_correction(spec.spec_table) @@ -3936,7 +3942,7 @@ def create_extraction(extract_ref_dict, # If this fails, fall back on the old method. try: apcorr.tabulate_correction(spec.spec_table) - print(apcorr.tabulated_correction) + apcorr.apply_tabulated_correction(spec.spec_table) except: apcorr.apply(spec.spec_table) From fb50e418ec867d3c6c528ab08e9be1591a361db1 Mon Sep 17 00:00:00 2001 From: Timothy Brandt Date: Wed, 26 Jun 2024 14:33:25 -0400 Subject: [PATCH 06/18] Fixed bare except statement --- jwst/extract_1d/extract.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jwst/extract_1d/extract.py b/jwst/extract_1d/extract.py index 6e75dcd237..1e4db226f0 100644 --- a/jwst/extract_1d/extract.py +++ b/jwst/extract_1d/extract.py @@ -3943,7 +3943,8 @@ def create_extraction(extract_ref_dict, try: apcorr.tabulate_correction(spec.spec_table) apcorr.apply_tabulated_correction(spec.spec_table) - except: + except AttributeError: + log.info("Falling back on old .apply method of aperture correction.") apcorr.apply(spec.spec_table) # Save previous ra, dec, wavelength in case we can reuse From 71fd2bd356750f00296a61e5dd4cf1067e6a8d75 Mon Sep 17 00:00:00 2001 From: Timothy Brandt Date: Wed, 26 Jun 2024 15:33:49 -0400 Subject: [PATCH 07/18] Removed code that had been deleted in #8467 --- jwst/extract_1d/extract.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/jwst/extract_1d/extract.py b/jwst/extract_1d/extract.py index 1e4db226f0..089de5be84 100644 --- a/jwst/extract_1d/extract.py +++ b/jwst/extract_1d/extract.py @@ -3736,12 +3736,6 @@ def create_extraction(extract_ref_dict, use_source_posn = False log.info(f"Setting use_source_posn to False for source type {source_type}") - # Turn off use_source_posn if working on non-primary NRS fixed slits - if is_multiple_slits: - if exp_type == 'NRS_FIXEDSLIT' and slitname != slit.meta.instrument.fixed_slit: - use_source_posn = False - log.info("Can only compute source location for primary NIRSpec slit,") - log.info("so setting use_source_posn to False") if photom_has_been_run: pixel_solid_angle = meta_source.meta.photometry.pixelarea_steradians From 04e575856514918a0e52764594d7a3ed7ffaca60 Mon Sep 17 00:00:00 2001 From: Timothy Brandt Date: Wed, 3 Jul 2024 21:16:04 -0400 Subject: [PATCH 08/18] Removed extra newline --- jwst/extract_1d/extract.py | 1 - 1 file changed, 1 deletion(-) diff --git a/jwst/extract_1d/extract.py b/jwst/extract_1d/extract.py index 089de5be84..1d3adcc114 100644 --- a/jwst/extract_1d/extract.py +++ b/jwst/extract_1d/extract.py @@ -3736,7 +3736,6 @@ def create_extraction(extract_ref_dict, use_source_posn = False log.info(f"Setting use_source_posn to False for source type {source_type}") - if photom_has_been_run: pixel_solid_angle = meta_source.meta.photometry.pixelarea_steradians if pixel_solid_angle is None: From 9fd1d7902bf4157cc9021995233d7b56491b01b9 Mon Sep 17 00:00:00 2001 From: Timothy Brandt Date: Wed, 3 Jul 2024 21:22:29 -0400 Subject: [PATCH 09/18] Added comments and log info for both tabulated and standalone aperture corrections. --- jwst/extract_1d/extract.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/jwst/extract_1d/extract.py b/jwst/extract_1d/extract.py index 1d3adcc114..b52476aca0 100644 --- a/jwst/extract_1d/extract.py +++ b/jwst/extract_1d/extract.py @@ -3898,6 +3898,9 @@ def create_extraction(extract_ref_dict, else: wl = wavelength.min() + # Determine whether we have a tabulated aperature correction + # available to save time. + apcorr_available = False if apcorr is not None: if hasattr(apcorr, 'tabulated_correction'): @@ -3936,8 +3939,9 @@ def create_extraction(extract_ref_dict, try: apcorr.tabulate_correction(spec.spec_table) apcorr.apply_tabulated_correction(spec.spec_table) + log.info("Tabulating aperture correction for use in multiple integrations.") except AttributeError: - log.info("Falling back on old .apply method of aperture correction.") + log.info("Computing aperture correction.") apcorr.apply(spec.spec_table) # Save previous ra, dec, wavelength in case we can reuse From 5b0ebb8c41b672428811b8b73b5c08c843265136 Mon Sep 17 00:00:00 2001 From: Timothy D Brandt Date: Fri, 5 Jul 2024 14:31:10 -0400 Subject: [PATCH 10/18] Update CHANGES.rst Co-authored-by: Melanie Clarke --- CHANGES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 1964c1e549..9c37eb5f56 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -149,7 +149,7 @@ extract_1d whether they are marked primary or not. [#8467] - Added functionality to the phase-based aperture correction object to support - reuse of aperture correction objects across multiple integrations [#8609] + reuse of aperture correction objects across multiple integrations. [#8609] - Changed extract.py to attempt to tabulate and reuse an aperture correction object in integrations after the first one. This can save a very large From 506816697ee08a17bf1eb77f9dba59ba5c74f5f0 Mon Sep 17 00:00:00 2001 From: Timothy D Brandt Date: Fri, 5 Jul 2024 14:31:20 -0400 Subject: [PATCH 11/18] Update CHANGES.rst Co-authored-by: Melanie Clarke --- CHANGES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 9c37eb5f56..af6893588c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -153,7 +153,7 @@ extract_1d - Changed extract.py to attempt to tabulate and reuse an aperture correction object in integrations after the first one. This can save a very large - amount of time in BOTS reductions [#8609] + amount of time in BOTS reductions. [#8609] extract_2d ---------- From 7d51a40f2ec161247e6e11ed86d96453216658ce Mon Sep 17 00:00:00 2001 From: Timothy D Brandt Date: Fri, 5 Jul 2024 14:34:05 -0400 Subject: [PATCH 12/18] Update jwst/extract_1d/extract.py Indenting saving ra, dec, wl to apply only where wavelength is defined Co-authored-by: Melanie Clarke --- jwst/extract_1d/extract.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/jwst/extract_1d/extract.py b/jwst/extract_1d/extract.py index b52476aca0..27464aad8c 100644 --- a/jwst/extract_1d/extract.py +++ b/jwst/extract_1d/extract.py @@ -3944,11 +3944,11 @@ def create_extraction(extract_ref_dict, log.info("Computing aperture correction.") apcorr.apply(spec.spec_table) - # Save previous ra, dec, wavelength in case we can reuse - # the aperture correction object. - ra_last = ra - dec_last = dec - wl_last = wl + # Save previous ra, dec, wavelength in case we can reuse + # the aperture correction object. + ra_last = ra + dec_last = dec + wl_last = wl output_model.spec.append(spec) if log_increment > 0 and (integ + 1) % log_increment == 0: From b93bb281e2d9723d4ebab55a6321c95e6492d959 Mon Sep 17 00:00:00 2001 From: Timothy D Brandt Date: Fri, 5 Jul 2024 14:34:27 -0400 Subject: [PATCH 13/18] Update jwst/extract_1d/extract.py Co-authored-by: Melanie Clarke --- jwst/extract_1d/extract.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jwst/extract_1d/extract.py b/jwst/extract_1d/extract.py index 27464aad8c..859b472106 100644 --- a/jwst/extract_1d/extract.py +++ b/jwst/extract_1d/extract.py @@ -3898,7 +3898,7 @@ def create_extraction(extract_ref_dict, else: wl = wavelength.min() - # Determine whether we have a tabulated aperature correction + # Determine whether we have a tabulated aperture correction # available to save time. apcorr_available = False From 45344ddc406a9bb2ab7903b0b395a36cce436df6 Mon Sep 17 00:00:00 2001 From: Timothy D Brandt Date: Fri, 5 Jul 2024 14:34:36 -0400 Subject: [PATCH 14/18] Update jwst/extract_1d/apply_apcorr.py Co-authored-by: Melanie Clarke --- jwst/extract_1d/apply_apcorr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jwst/extract_1d/apply_apcorr.py b/jwst/extract_1d/apply_apcorr.py index 79ae9a9b39..3661b5652a 100644 --- a/jwst/extract_1d/apply_apcorr.py +++ b/jwst/extract_1d/apply_apcorr.py @@ -267,7 +267,7 @@ def apply_tabulated_correction(self, spec_table: fits.FITS_rec): flux_cols_to_correct = ('flux', 'flux_error', 'surf_bright', 'sb_error') var_cols_to_correct = ('flux_var_poisson', 'flux_var_rnoise', 'flux_var_flat', - 'sb_var_poisson', 'sb_var_rnoise', 'sb_var_flat') + 'sb_var_poisson', 'sb_var_rnoise', 'sb_var_flat') for col in flux_cols_to_correct: spec_table[col] *= self.tabulated_correction for col in var_cols_to_correct: From 9674cf542f5658213a2b35169cab9f0c42710f66 Mon Sep 17 00:00:00 2001 From: Timothy Brandt Date: Fri, 5 Jul 2024 14:48:24 -0400 Subject: [PATCH 15/18] Splitting error message over two lines --- jwst/extract_1d/apply_apcorr.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jwst/extract_1d/apply_apcorr.py b/jwst/extract_1d/apply_apcorr.py index 3661b5652a..507f5ab895 100644 --- a/jwst/extract_1d/apply_apcorr.py +++ b/jwst/extract_1d/apply_apcorr.py @@ -263,7 +263,8 @@ def apply_tabulated_correction(self, spec_table: fits.FITS_rec): """ if self.tabulated_correction is None: - raise ValueError("Cannot call apply_tabulated_correction without first calling tabulate_correction") + raise ValueError("Cannot call apply_tabulated_correction without first " + "calling tabulate_correction") flux_cols_to_correct = ('flux', 'flux_error', 'surf_bright', 'sb_error') var_cols_to_correct = ('flux_var_poisson', 'flux_var_rnoise', 'flux_var_flat', From 47c9a7f50b7ae49657b2c3a138fdd84b18fa51d4 Mon Sep 17 00:00:00 2001 From: Timothy D Brandt Date: Fri, 5 Jul 2024 14:51:52 -0400 Subject: [PATCH 16/18] Update jwst/extract_1d/apply_apcorr.py Split docstring for tabulate_correction Co-authored-by: Melanie Clarke --- jwst/extract_1d/apply_apcorr.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/jwst/extract_1d/apply_apcorr.py b/jwst/extract_1d/apply_apcorr.py index 507f5ab895..1d6eeebe20 100644 --- a/jwst/extract_1d/apply_apcorr.py +++ b/jwst/extract_1d/apply_apcorr.py @@ -229,7 +229,10 @@ def measure_phase(self): # Future method in determining pixel phase pass def tabulate_correction(self, spec_table: fits.FITS_rec): - """Tabulate the interpolated aperture correction value. This will save time when applying it later, especially if it is to be applied to multiple integrations. Modifies self.tabulated_correction. + """Tabulate the interpolated aperture correction value. + + This will save time when applying it later, especially if it is to be applied to multiple integrations. + Modifies self.tabulated_correction. Parameters ---------- From fd85c8381354b218bd672751d55eb7f1d5dc0275 Mon Sep 17 00:00:00 2001 From: Timothy Brandt Date: Fri, 5 Jul 2024 15:52:00 -0400 Subject: [PATCH 17/18] Refactored tabulated aperture correction into apcorr.apply() --- jwst/extract_1d/extract.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jwst/extract_1d/extract.py b/jwst/extract_1d/extract.py index 859b472106..03b36b8968 100644 --- a/jwst/extract_1d/extract.py +++ b/jwst/extract_1d/extract.py @@ -3912,7 +3912,7 @@ def create_extraction(extract_ref_dict, # save a ton of time. if ra == ra_last and dec == dec_last and wl == wl_last and apcorr_available: # re-use the last aperture correction - apcorr.apply_tabulated_correction(spec.spec_table) + apcorr.apply(spec.spec_table, use_tabulated=True) else: if isinstance(input_model, datamodels.ImageModel): @@ -3938,7 +3938,7 @@ def create_extraction(extract_ref_dict, # If this fails, fall back on the old method. try: apcorr.tabulate_correction(spec.spec_table) - apcorr.apply_tabulated_correction(spec.spec_table) + apcorr.apply(spec.spec_table, use_tabulated=True) log.info("Tabulating aperture correction for use in multiple integrations.") except AttributeError: log.info("Computing aperture correction.") From 69a10bc7b6d0d625600cc9a647c63346835740a2 Mon Sep 17 00:00:00 2001 From: Timothy Brandt Date: Fri, 5 Jul 2024 15:52:10 -0400 Subject: [PATCH 18/18] Refactored tabulated aperture correction into apcorr.apply() --- jwst/extract_1d/apply_apcorr.py | 59 ++++++++++++++------------------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/jwst/extract_1d/apply_apcorr.py b/jwst/extract_1d/apply_apcorr.py index 1d6eeebe20..daf9f67588 100644 --- a/jwst/extract_1d/apply_apcorr.py +++ b/jwst/extract_1d/apply_apcorr.py @@ -255,52 +255,43 @@ def tabulate_correction(self, spec_table: fits.FITS_rec): self.tabulated_correction = np.asarray(coefs) - def apply_tabulated_correction(self, spec_table: fits.FITS_rec): - """Apply self.tabulated_correction values to source-related extraction results in-place. - - Parameters - ---------- - spec_table : `~fits.FITS_rec` - Table of aperture corrections values from apcorr reference file. - - """ - - if self.tabulated_correction is None: - raise ValueError("Cannot call apply_tabulated_correction without first " - "calling tabulate_correction") - - flux_cols_to_correct = ('flux', 'flux_error', 'surf_bright', 'sb_error') - var_cols_to_correct = ('flux_var_poisson', 'flux_var_rnoise', 'flux_var_flat', - 'sb_var_poisson', 'sb_var_rnoise', 'sb_var_flat') - for col in flux_cols_to_correct: - spec_table[col] *= self.tabulated_correction - for col in var_cols_to_correct: - spec_table[col] *= self.tabulated_correction**2 - - def apply(self, spec_table: fits.FITS_rec): + def apply(self, spec_table: fits.FITS_rec, use_tabulated=False): """Apply interpolated aperture correction value to source-related extraction results in-place. Parameters ---------- spec_table : `~fits.FITS_rec` Table of aperture corrections values from apcorr reference file. + use_tabulated : bool, Optional + Use self.tabulated_correction to perform the aperture correction? + Default False (recompute correction from scratch). """ flux_cols_to_correct = ('flux', 'flux_error', 'surf_bright', 'sb_error') var_cols_to_correct = ('flux_var_poisson', 'flux_var_rnoise', 'flux_var_flat', 'sb_var_poisson', 'sb_var_rnoise', 'sb_var_flat') + + if use_tabulated: + if self.tabulated_correction is None: + raise ValueError("Cannot call apply_tabulated_correction without first " + "calling tabulate_correction") - for row in spec_table: - try: - correction = self.apcorr_func(row['wavelength'], row['npixels'], self.phase) - except ValueError: - correction = None # Some input wavelengths might not be supported (especially at the ends of the range) - - if correction: - for col in flux_cols_to_correct: - row[col] *= correction.item() - for col in var_cols_to_correct: - row[col] *= correction.item() * correction.item() + for col in flux_cols_to_correct: + spec_table[col] *= self.tabulated_correction + for col in var_cols_to_correct: + spec_table[col] *= self.tabulated_correction**2 + else: + for row in spec_table: + try: + correction = self.apcorr_func(row['wavelength'], row['npixels'], self.phase) + except ValueError: + correction = None # Some input wavelengths might not be supported (especially at the ends of the range) + + if correction: + for col in flux_cols_to_correct: + row[col] *= correction.item() + for col in var_cols_to_correct: + row[col] *= correction.item() * correction.item() class ApCorrRadial(ApCorrBase):