[AstroPy] Recovering and interpolating contents of a Spectrum1D spectrum

Peter Dzwig pdzwig at summaventures.com
Sun Jun 14 09:52:51 EDT 2020


All,

that looks to be the solution.

When I went to the specutils webpages - as Derek kindly suggested - and
did (for example)


import numpy as np
import astropy.units as u
from specutils import Spectrum1D
from specutils.manipulation import FluxConservingResampler
input_spectra = Spectrum1D(
    flux=np.array([1, 3, 7, 6, 20]) * u.mJy,
    spectral_axis=np.array([2, 4, 12, 16, 20]) * u.nm)
resample_grid = [1, 5, 9, 13, 14, 17, 21, 22, 23]  *u.nm
fluxc_resample = FluxConservingResampler()
output_spectrum1D = fluxc_resample(input_spectra, resample_grid)


(which is their demonstration case) I get:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-3-a44cc2071722> in <module>
      2 import astropy.units as u
      3 from specutils import Spectrum1D
----> 4 from specutils.manipulation import FluxConservingResampler
      5 input_spectra = Spectrum1D(
      6     flux=np.array([1, 3, 7, 6, 20]) * u.mJy,

ImportError: cannot import name 'FluxConservingResampler'


similarly for the other resamplers: SplineInterpolatedResampler and
LinearInterpolatedResampler.

Is there a specutils issue?

Peter


On 13/06/2020 14:13, Derek Homeier wrote:
> Hi Peter,
>>
>> I have to work with a Spectrum1D object, spectrum, which I have
>> "inherited" (think of it as legacy-ware):
>>
>>  spectrum = Spectrum1D(spectral_axis= wavelength, flux=field_strength)
>>
>> I need to recover the wavelengths and field_strength to standard numpy
>> arrays because I need to interpolate between the points in the available
>> (probably less than first-class) dataset.  It would be great if I could
>> do the interpolation within Spectrum1D  but as far as I can see
>> specutils doesn't provide a tool to do so.
>>
>> Hence, I have to extract the data - and subsequently create a new
>> Spectrum1D object containing the interpolated data.
>>
>> I know what the units are already, so am not concerned about preserving
>> units while extracting the datapoints.
> 
> spectral_axis and flux are Quantity objects, and can be accessed as arrays via
> spectrum.spectral_axis.value, spectrum.flux.value
> 
> However as subclasses of ndarray many numpy operations are also directly supported on quantities, e.g.
> 
> flux_intp =  np.interp(wl_array * spectrum.spectral_axis.unit, spectrum.spectral_axis, spectrum.flux)
> 
> should work as well (preserving units!).
> 
> That said specutils.manipulation provides also some of its own resampling/rebinning methods
> offering different choices for flux conservation etc. :)
> 
> https://specutils.readthedocs.io/en/latest/manipulation.html#resampling
> 
> HTH
> 					Derek
> 

-- 

Dr. Peter Dzwig


More information about the AstroPy mailing list