Skip to content

extract_region: preserve WCS for single regions, add velocity-region support #1263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

harry353
Copy link

@harry353 harry353 commented Aug 7, 2025

This update addresses issues #1261 and #1262, changing how the extract_region function behaves in two ways:

  • When preserve_wcs=True, the WCS is now kept for single-region extractions.
  • Velocity regions can now be extracted from spectra with a frequency axis (and the other way around).

It also includes a new test script to cover these cases.

Copy link
Contributor

@keflavich keflavich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My changes are minor - documentation and variable names. I'm basically approving, but consider my suggestions.

Comment on lines 15 to 16
except Exception:
pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a little overly aggressive to catch all exceptions here. Can we limit this to UnitConversionError?

@@ -128,6 +139,10 @@ def extract_region(spectrum, region, return_single_spectrum=False):
instead of multiple `~specutils.Spectrum` objects. The returned spectrum
will be a unique, concatenated, spectrum of all sub-regions.

preserve_wcs: `bool`
If True, the WCS will be adjusted and retained in the output spectrum(s).
If False (default), WCS will be dropped.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current False behavior does keep a WCS, it just turns it into a lookuptable WCS - so it doesn't exactly drop the WCS, it changes it.

Comment on lines 192 to 200
original_wcs = spectrum.wcs.deepcopy()

# Set CRPIX = 1.0 (FITS convention: reference pixel is 1-indexed)
original_wcs.wcs.crpix[0] = 1.0

# Set CRVAL to match the first spectral axis value in the sliced spectrum
original_wcs.wcs.crval[0] = sliced.spectral_axis[0].to_value(original_wcs.wcs.cunit[0])

sliced._wcs = original_wcs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe call this new_wcs instead, since you're modifying it? Functionally I think this looks fine, but I find the variable name a little confusing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the intent, though - on the first line, you're copying the wcs from the unsliced spectrum, so there it's the original, but after you modify it, it's not.

…se now actually drops WCS entirely, rename WCS variable
@harry353
Copy link
Author

harry353 commented Aug 8, 2025

Thanks for the quick feedback. Applied your suggestions in a new push.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants