Skip to content

How do I apply shift_spectrum_to(radial_velocity=...) to a SpectrumCollection? #1318

@hamogu

Description

@hamogu

Spectrum.shift_spectrum_to(...) updates the wavelength axis in place, but I cannot find the equivalent for a SpectrumCollection:

from astropy.io import fits
from astropy import units as u
filename = 'https://data.sdss.org/sas/dr16/sdss/spectro/redux/26/spectra/1323/spec-1323-52797-0012.fits'
# The spectrum is in the second HDU of this file.
with fits.open(filename) as f:
    specdata = f[1].data
from specutils import Spectrum
lamb = 10**specdata['loglam'] * u.AA
flux = specdata['flux'] * 10**-17 * u.Unit('erg cm-2 s-1 AA-1')
spec = Spectrum(spectral_axis=lamb, flux=flux)
spec_collection = SpectrumCollection.from_spectra([spec])

for s in spec_collection:
    print(s.wavelength)
    s.shift_spectrum_to(radial_velocity=-2700.5 * u.km / u.s)
    print(s.wavelength)
print(spec_collection[0].wavelength)

This gives me the following output:

[3815.0483 3815.926  3816.806  ... 9202.379  9204.495  9206.613 ] Angstrom
[3780.8362 3781.706  3782.578  ... 9119.8545 9121.952  9124.051 ] Angstrom
[3815.0483 3815.926  3816.806  ... 9202.379  9204.495  9206.613 ] Angstrom

Note how the wavelength for s is updated, but once I leave the loop, it's gone and spec_collection[0] leaves me with the old data.

It seems that iterating over a SpectrumCollection doesn't give me the spectra that are actually in it, but copies of them?
I'm not sure if that's a bug or a feature. If that's intended, I'm happy to do a PR to highlight that in the documentation. Looking at the existing documentation, there are sentences like "SpectrumCollection objects can be treated just like Spectrum objects;" which doesn't sound like "You can update one of them in place, but in the other you always need to make a new object".

In fact, I think it would be useful to give SpectrumCollection its own shift_spectrum_to method. At least the way I use it, the collections are often spectra from the same observation (e.g. different echelle orders) so they naturally need the same heliocentric correction and the same redshift. Even when the collection contains spectra from different observations, they are typically related in some way, e.g. they are spectra from the same object taken at different times (same radial velocity correction) or for different stars in e.g. a cluster, observed with multiple fibers in the same field (same observation time and almost the same pointing, thus the same heliocentric correction).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions