astropy / specutils

An Astropy coordinated package for astronomical spectroscopy. Maintainers: @rosteen @keflavich @eteq
http://specutils.readthedocs.io/en/latest/
170 stars 127 forks source link

Spline interpolation no longer works for binning on velocity grid #1186

Closed StuartLittlefair closed 3 weeks ago

StuartLittlefair commented 1 month ago

Due to changes added in https://github.com/astropy/specutils/pull/1121, the SplineInterpolatedResampler is no longer suitable for binning a spectrum onto a spectral axis which is uniform in velocity space. Prior to this, the following code would work (e.g works in 1.12.0 but not in 1.17.0):

rest_wavelength = 656.2 * u.nm

# make a fake spectrum
wavelengths = np.linspace(640, 672, 1000) * u.nm
flux = np.random.normal(1.0, 0.1, 1000) * u.mJy
spec1d = Spectrum1D(spectral_axis=wavelengths, velocity_convention="optical", flux=flux)
spec1d.spectral_axis.doppler_rest = rest_wavelength

velocities = np.linspace(-1000, 1000, 500) * u.km/u.s
velocity_grid = SpectralAxis(
        velocities, doppler_rest=rest_wavelength, doppler_convention="optical"
)
resample_fn = SplineInterpolatedResampler()
velocity_binned = resample_fn(spec1d, velocity_grid)

This now fails because the line below

https://github.com/astropy/specutils/blob/a859dd6c9b540b6f651a3b0d882be6b418aecde7/specutils/manipulation/resample.py#L465

assumes that the bin edges and the fin_spec_axis are both in wavelength units. The only resampler that works for this use case is the LinearInterpolatedResampler, but this is not accurate enough for velocity binning.

rosteen commented 1 month ago

Thanks for pinpointing what broke this - should be a simple fix, I'll get it in before the next release (hopefully in the next week or so).

StuartLittlefair commented 1 month ago

Amazing service @rosteen , thanks!

rosteen commented 4 weeks ago

My simple fix ran into an error in astropy, still trying to figure out if that's a problem with my fix or a bug in astropy.

StuartLittlefair commented 4 weeks ago

Oops! Let me know if I can help in any way.