Specutils is an `Astropy`_ affiliated package with the goal of providing a shared set of Python representations of astronomical spectra and basic tools to operate on these spectra. The effort is also meant to be a “hub”, helping to unite the Python astronomical spectroscopy community around shared effort, much as `Astropy`_ is meant to for the wider astronomy Python ecosystem.
The Spectrum1D class is one of the core classes of the specutils package. You can import it like this:
>>> from specutils import Spectrum1D
To instantiate it you can define a wave and a flux:
>>> wave = np.arange(6000, 9000) * u.Angstrom >>> flux = np.random.random(3000) * u.Unit('W m-2 angstrom-1 sr-1')
and then call the from_array method:
>>> spec1d = Spectrum1D.from_array(wave, flux) >>> spec1d.wavelength <Quantity [ 6000., 6001., 6002.,..., 8997., 8998., 8999.] Angstrom> >>> spec1d.flux <Quantity [ 0.75639906, 0.23677036, 0.08408417,..., 0.82740303, 0.38345114, 0.77815595] W / (Angstrom m2 sr)>
Or you can read a Spectrum from a .fits file with the read_fits method:
>>> from specutils.io import read_fits >>> myspec = read_fits.read_fits_spectrum1d('myfile.fits')
It supports the types of FITS formats listed in this page.
Note: A list of spectra is returned whenever the input file is a multispec file.
Writing spectra to .fits files works in the same way with the write_fits method:
>>> from specutils.io import write_fits >>> write_fits.write(myspec, 'mynewfile.fits')
Note: write_fits.write deciphers the type of object passed and writes spectra to the given file in FITS format.
Reading a Spectrum from a FITS file with no specified units in the header will give the following warning:
>>> myspec = read_fits.read_fits_spectrum1d('specutils/io/tests/files/UVES.fits') UserWarning: Initializing a Spectrum1D WCS with units set to `None` is not recommended
the Spectrum1D.dispersion will be an array:
>>> myspec.dispersion array([ 3732.05623192, 3732.0858853 , 3732.11553869, ..., 4999.67906915, 4999.70872253, 4999.73837591])
and thus the Spectrum1D’s wavelength, energy and frequency will not be available. In order to be convertible, the dispersion must be an astropy Quantity, which will happen if the FITS header has specified the units or if you specify them manually like this:
>>> myspec = read_fits.read_fits_spectrum1d('specutils/io/tests/files/UVES.fits', dispersion_unit='angstrom') >>> myspec.dispersion <Quantity [ 3732.05623192, 3732.0858853 , 3732.11553869,..., 4999.67906915, 4999.70872253, 4999.73837591] Angstrom> >>> myspec.wavelength <Quantity [ 3732.05623192, 3732.0858853 , 3732.11553869,..., 4999.67906915, 4999.70872253, 4999.73837591] Angstrom> >>> myspec.energy <Quantity [ 5.32265743e-19, 5.32261514e-19, 5.32257285e-19,..., 3.97314639e-19, 3.97312282e-19, 3.97309926e-19] J> >>> myspec.frequency <Quantity [ 8.03290303e+14, 8.03283920e+14, 8.03277538e+14,..., 5.99623404e+14, 5.99619847e+14, 5.99616291e+14] Hz>
You can easily make a plot of the Spectrum using matplotlib in ipython with the –pylab flag and calling:
>>> import pylab as pl >>> pl.plot(myspec.wavelength, myspec.flux)