From 5ab1bba8b75cfb10e45f69a8b49204408f5eadfb Mon Sep 17 00:00:00 2001 From: duytnguyendtn Date: Wed, 13 Oct 2021 18:19:00 -0400 Subject: [PATCH 1/3] First Pass WCS extract region bound fix --- specutils/manipulation/extract_spectral_region.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/specutils/manipulation/extract_spectral_region.py b/specutils/manipulation/extract_spectral_region.py index bae78c118..eb98877cd 100644 --- a/specutils/manipulation/extract_spectral_region.py +++ b/specutils/manipulation/extract_spectral_region.py @@ -163,8 +163,12 @@ def extract_region(spectrum, region, return_single_spectrum=False): if left_index > right_index: left_index, right_index = right_index, left_index - - extracted_spectrum.append(spectrum[..., left_index:right_index]) + + # Slice spectrum to calculated indices + extracted_spectra = spectrum[..., left_index:right_index] + # Set the WCS wavelength reference value to new lower bound of spectrum + extracted_spectra.wcs.wcs.crval[0] = min(extracted_spectra.wavelength).to(u.Unit(extracted_spectra.wcs.wcs.cunit[0])).value + extracted_spectrum.append(extracted_spectra) # If there is only one subregion in the region then we will # just return a spectrum. From 8d7433b00ce54445bac276fd709dae5459c8f259 Mon Sep 17 00:00:00 2001 From: duytnguyendtn Date: Fri, 15 Oct 2021 16:56:10 -0400 Subject: [PATCH 2/3] Typecheck WCS trim only for Astropy WCS --- specutils/manipulation/extract_spectral_region.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/specutils/manipulation/extract_spectral_region.py b/specutils/manipulation/extract_spectral_region.py index eb98877cd..b85b0cd76 100644 --- a/specutils/manipulation/extract_spectral_region.py +++ b/specutils/manipulation/extract_spectral_region.py @@ -5,6 +5,7 @@ import numpy as np from astropy import units as u +from astropy.wcs.wcs import WCS from ..spectra import Spectrum1D, SpectralRegion __all__ = ['extract_region', 'extract_bounding_spectral_region', 'spectral_slab'] @@ -166,8 +167,10 @@ def extract_region(spectrum, region, return_single_spectrum=False): # Slice spectrum to calculated indices extracted_spectra = spectrum[..., left_index:right_index] - # Set the WCS wavelength reference value to new lower bound of spectrum - extracted_spectra.wcs.wcs.crval[0] = min(extracted_spectra.wavelength).to(u.Unit(extracted_spectra.wcs.wcs.cunit[0])).value + + # Set the Astropy WCS wavelength reference value to new lower bound of spectrum + if type(extracted_spectra.wcs) is WCS: + extracted_spectra.wcs.wcs.crval[0] = min(extracted_spectra.wavelength).to(u.Unit(extracted_spectra.wcs.wcs.cunit[0])).value extracted_spectrum.append(extracted_spectra) # If there is only one subregion in the region then we will From c071fb0bf1983cb6f27effbf3a1802c24680f45e Mon Sep 17 00:00:00 2001 From: duytnguyendtn Date: Fri, 15 Oct 2021 16:59:13 -0400 Subject: [PATCH 3/3] Codestyle --- specutils/manipulation/extract_spectral_region.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/specutils/manipulation/extract_spectral_region.py b/specutils/manipulation/extract_spectral_region.py index b85b0cd76..6123d8795 100644 --- a/specutils/manipulation/extract_spectral_region.py +++ b/specutils/manipulation/extract_spectral_region.py @@ -164,13 +164,16 @@ def extract_region(spectrum, region, return_single_spectrum=False): if left_index > right_index: left_index, right_index = right_index, left_index - + # Slice spectrum to calculated indices extracted_spectra = spectrum[..., left_index:right_index] - + # Set the Astropy WCS wavelength reference value to new lower bound of spectrum if type(extracted_spectra.wcs) is WCS: - extracted_spectra.wcs.wcs.crval[0] = min(extracted_spectra.wavelength).to(u.Unit(extracted_spectra.wcs.wcs.cunit[0])).value + extracted_spectra.wcs.wcs.crval[0] = min(extracted_spectra.wavelength + ).to( + u.Unit(extracted_spectra.wcs.wcs.cunit[0]) # noqa + ).value extracted_spectrum.append(extracted_spectra) # If there is only one subregion in the region then we will