From 2ddef6aea000cafa84a7e65a586904f96b719832 Mon Sep 17 00:00:00 2001 From: Michele De La Pena Date: Tue, 14 Jan 2025 14:34:26 -0500 Subject: [PATCH 1/3] Updates to Photutils functionality. The original updates to replace deprecated functionality did not produce results at least as good as the results generated by the previous Photutils functionality. These changes have been shown to yield better agreement with the previous results for the limited number of datasets tested. --- drizzlepac/haputils/astrometric_utils.py | 25 ++++++++++-------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/drizzlepac/haputils/astrometric_utils.py b/drizzlepac/haputils/astrometric_utils.py index 1aedd3dc2..d2c6b9f6a 100644 --- a/drizzlepac/haputils/astrometric_utils.py +++ b/drizzlepac/haputils/astrometric_utils.py @@ -43,7 +43,7 @@ sigma_clipped_stats, SigmaClip) from astropy.visualization import SqrtStretch from astropy.visualization.mpl_normalize import ImageNormalize -from astropy.modeling.fitting import LevMarLSQFitter +from astropy.modeling.fitting import LMLSQFitter from astropy.time import Time from astropy.utils.decorators import deprecated @@ -836,7 +836,7 @@ def build_auto_kernel(imgarr, whtarr, fwhm=3.0, threshold=None, source_box=7, return (kernel, kernel_psf), kernel_fwhm -def find_fwhm(psf, default_fwhm): +def find_fwhm(psf, default_fwhm, log_level=logutil.logging.INFO): """Determine FWHM for auto-kernel PSF This function iteratively fits a Gaussian model to the extracted PSF @@ -858,22 +858,19 @@ def find_fwhm(psf, default_fwhm): Value of the computed Gaussian FWHM for the PSF """ + fwhm = 0.0 + # Default 1.0 * default_fwhm (default_fwhm is detector-dependent) aperture_radius = 1.5 * default_fwhm - source_group = SourceGrouper(min_separation=8) mmm_bkg = MMMBackground() - # LocalBackground: Inner and outer radius of circular annulus in pixels - base = int(math.ceil(aperture_radius)) - local_bkg = LocalBackground(base + 1, base + 3, mmm_bkg) iraffind = DAOStarFinder(threshold=2.5 * mmm_bkg(psf), fwhm=default_fwhm) - fitter = LevMarLSQFitter() + # LevMarLSQFitter is in disfavor and will be deprecated + fitter = LMLSQFitter() sigma_psf = gaussian_fwhm_to_sigma * default_fwhm gaussian_prf = IntegratedGaussianPRF(sigma=sigma_psf) gaussian_prf.sigma.fixed = False try: itr_phot_obj = IterativePSFPhotometry(finder=iraffind, - grouper=source_group, - localbkg_estimator=local_bkg, psf_model=gaussian_prf, aperture_radius=aperture_radius, fitter=fitter, @@ -881,20 +878,18 @@ def find_fwhm(psf, default_fwhm): maxiters=2) phot_results = itr_phot_obj(psf) - except Exception: - log.error("The find_fwhm() failed due to problem with fitting.") + except Exception as x_cept: + log.warn(f"The find_fwhm() failed due to problem with fitting. Trying again. Exception: {x_cept}") return None # Check the phot_results table was generated successfully if isinstance(phot_results, (type(None))): + log.warn("The PHOT_RESULTS table was not generated successfully. Trying again.") return None # Check the table actually has rows if len(phot_results['flux_fit']) == 0: - return None - - # Check the 'flags' column has at least one row with a flag value of zero - if (phot_results['flags'] == 0).sum() == 0: + log.warn("The PHOT_RESULTS table has no rows. Trying again.") return None # Insure none of the fluxes determined by photutils is np.nan From eda8f64a5c51dd89931b91721734558a2ea604bc Mon Sep 17 00:00:00 2001 From: Michele De La Pena Date: Fri, 17 Jan 2025 17:44:51 -0500 Subject: [PATCH 2/3] Added/revised informational messages. --- CHANGELOG.rst | 3 +++ drizzlepac/haputils/astrometric_utils.py | 2 ++ drizzlepac/haputils/product.py | 10 +++------- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 8ab86803d..34cbdda84 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -22,6 +22,9 @@ number of the code change for that issue. These PRs can be viewed at: ================== - Added new header keywords and match requirements for relative fitting. [#1860] + +- Further changes/tuning to improve the alignment results after deprecated Photutils + functionality was upgraded. [#nnnn] 3.9.0 (16-Dec-2024) diff --git a/drizzlepac/haputils/astrometric_utils.py b/drizzlepac/haputils/astrometric_utils.py index d2c6b9f6a..8bb3abd93 100644 --- a/drizzlepac/haputils/astrometric_utils.py +++ b/drizzlepac/haputils/astrometric_utils.py @@ -833,6 +833,8 @@ def build_auto_kernel(imgarr, whtarr, fwhm=3.0, threshold=None, source_box=7, kernel_fwhm = fwhm kernel = build_gaussian_kernel(fwhm, npixels=source_box) + log.info(f"Finished build_auto_kernel") + return (kernel, kernel_psf), kernel_fwhm diff --git a/drizzlepac/haputils/product.py b/drizzlepac/haputils/product.py index 33a1a7534..5610a68fd 100755 --- a/drizzlepac/haputils/product.py +++ b/drizzlepac/haputils/product.py @@ -495,21 +495,17 @@ def align_to_gaia( headerlet_filenames=headerlet_filenames, fit_label=fit_label ) else: - log.warning("No satisfactory fit found for any catalog.") + log.warning("No satisfactory fit found for any catalog. No correction to absolute astrometric frame applied.\n") raise ValueError except Exception: # Report a problem with the alignment if fit_label.upper().strip() == "SVM": log.warning( - "EXCEPTION encountered in align_to_gaia for the FilteredProduct.\n" - ) + "EXCEPTION encountered in align_to_gaia for the FilterProduct. Proceeding with previous best solution.\n") else: log.warning( - "EXCEPTION encountered in align_to_gaia for the SkyCellProduct.\n" - ) - log.warning("No correction to absolute astrometric frame applied.\n") - log.warning("Proceeding with previous best solution.\n") + "EXCEPTION encountered in align_to_gaia for the SkyCellProduct. Proceeding with previous best solution.\n") # Only write out the traceback if in "debug" mode since not being able to # align the data to an absolute astrometric frame is not actually a failure. From 6cd3577a353e09e45bb105dfe0717261b1e2ae38 Mon Sep 17 00:00:00 2001 From: Michele De La Pena Date: Tue, 21 Jan 2025 10:23:18 -0500 Subject: [PATCH 3/3] Further updates to accommodate the replacement Photutils functionality. --- drizzlepac/haputils/astrometric_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drizzlepac/haputils/astrometric_utils.py b/drizzlepac/haputils/astrometric_utils.py index 8bb3abd93..4690791fa 100644 --- a/drizzlepac/haputils/astrometric_utils.py +++ b/drizzlepac/haputils/astrometric_utils.py @@ -833,7 +833,7 @@ def build_auto_kernel(imgarr, whtarr, fwhm=3.0, threshold=None, source_box=7, kernel_fwhm = fwhm kernel = build_gaussian_kernel(fwhm, npixels=source_box) - log.info(f"Finished build_auto_kernel") + log.info(f"Finished build_auto_kernel") return (kernel, kernel_psf), kernel_fwhm