-1

I have to work with an Astropy Spectrum1D array:

    spectrum = Spectrum1D(spectral_axis= wavelength, flux = field_strength)

I need to recover the wavelengths and field_strength to standard numpy arrays so that I can manipulate them (this has to be done outside "spectrum" in numpy for a variety of reasons). I am not concerned about the units as I know what they are already.

How do I do it?

TIA

PeterD
  • 29
  • 5

2 Answers2

1

From https://docs.astropy.org/en/stable/units/#getting-started:

You can get the unit and value from a Quantity using the unit and value members

spectrum.flux.value
spectrum.spectral_axis.value

or wavelength.value and field_strength.value from the arguments.

Stef
  • 28,728
  • 2
  • 24
  • 52
1

Thanks @Stef

Derek Homeier offered the comments below on another forum.

I am posting it here for the sake of others who may come after...It extends what @Steff said, but it also points out what is possible under numpy and what Spectrum1D has as internal capabilities.

"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 "

My thanks to both Derek and @Steff for all your help.

PeterD
  • 29
  • 5
  • As an after note: It has been pointed out that there is a problem calling the samplers in specutils. A bug has been flagged, hopefully matters will have been resolved by the time that you need this. – PeterD Jun 15 '20 at 11:57