specutils icon indicating copy to clipboard operation
specutils copied to clipboard

Trouble reading and writing spectra with convenience functions read and write.

Open jkeohane opened this issue 2 years ago • 1 comments

I am using specutils and I am having a problem doing a simple thing. I made a spectrum, call it "spec" and I saved it to a fits file using:

spec.write(fname+'.fits', format='tabular-fits', overwrite=True)

Then I read the exact same file using: spec_test = Spectrum1D.read(fname+'.fits', format='tabular-fits')

However, this does not work. Any ideas?

I get the following errors, see below:

spec.write(fname+'.fits', format='tabular-fits', overwrite=True)

spec_test = Spectrum1D.read(fname+'.fits', format='tabular-fits') Traceback (most recent call last):

Cell In[941], line 1 spec_test = Spectrum1D.read(fname+'.fits', format='tabular-fits')

File ~/anaconda3/lib/python3.10/site-packages/astropy/nddata/mixins/ndio.py:59 in call return self.registry.read(self._cls, *args, **kwargs)

File ~/anaconda3/lib/python3.10/site-packages/astropy/io/registry/core.py:218 in read data = reader(*args, **kwargs)

File ~/anaconda3/lib/python3.10/site-packages/specutils/io/default_loaders/tabular_fits.py:87 in tabular_fits_loader return generic_spectrum_from_table(tab, wcs=wcs, **kwargs)

File ~/anaconda3/lib/python3.10/site-packages/specutils/io/parsing_utils.py:258 in generic_spectrum_from_table raise IOError("Could not identify column containing the flux")

OSError: Could not identify column containing the flux

jkeohane avatar Jul 21 '23 18:07 jkeohane

Thanks for reaching out, sorry for the delayed reply. I just tested this with a dummy spectrum (see below) and can't reproduce your error - can you provide some more detail (or the code) on how you created spec? And can you confirm that you're working with an up-to-date version of specutils?

The code I tested with is:

import astropy.units as u
from specutils import Spectrum1D

f = [10,15,20,18] * u.Jy
sa = [3000,3010,3020,3030] * u.AA
spec = Spectrum1D(f, spectral_axis=sa)
spec.write('test_tabular.fits', format='tabular-fits', overwrite=True)
spec_test = Spectrum1D.read('test_tabular.fits', format='tabular-fits')
spec_test

   <Spectrum1D(flux=<Quantity [10., 15., 20., 18.] Jy>, spectral_axis=<SpectralAxis [3000., 3010., 3020., 3030.] Angstrom>)>

rosteen avatar Sep 29 '23 18:09 rosteen