diff --git a/specutils/io/default_loaders/sixdfgs_reader.py b/specutils/io/default_loaders/sixdfgs_reader.py new file mode 100644 index 000000000..8f131ca90 --- /dev/null +++ b/specutils/io/default_loaders/sixdfgs_reader.py @@ -0,0 +1,63 @@ +import astropy.io.fits as fits +from astropy.table import Table +from astropy.units import Quantity, Unit +from specutils.io.registers import data_loader +from specutils import Spectrum1D + +SIXDFGS_PRIMARY_HDU_KEYWORDS = ["OBSRA", "OBSDEC", "Z", "Z_HELIO", "QUALITY"] +COUNTS_PER_SECOND = Unit("counts/s", parse_strict="silent") + + +def identify_6dfgs_tabular_fits(origin, *args, **kwargs): + """ + Identify if the current file is a 6dFGS file (stored as a table) + """ + with fits.open(args[0]) as hdulist: + if len(hdulist) != 2: + return False + primary_hdu = hdulist[0] + if primary_hdu.header["NAXIS"] != 0: + return False + for keyword in SIXDFGS_PRIMARY_HDU_KEYWORDS: + if keyword not in primary_hdu.header: + return False + return True + + +@data_loader("6dFGS-tabular", + identifier=identify_6dfgs_tabular_fits,dtype=Spectrum1D, + extensions=["fit", "fits"]) +def sixdfgs_tabular_fits_loader(filename, **kwargs): + """ + Load the tabular variant of a 6dF Galaxy Survey (6dFGS) file. + + 6dFGS used the Six-degree Field instrument on the UK Schmidt Telescope + (UKST) at the Siding Spring Observatory (SSO) near Coonabarabran, + Australia. Further details can be found at http://www.6dfgs.net/, or + https://docs.datacentral.org.au/6dfgs/. Catalogues and spectra were + produced, with the spectra being provided as both fits tables and as fits + images. This loads the tabular variant of the spectra. Note that this does + not include uncertainties - uncertainties are only provided in the image + variants. + + Parameters + ---------- + file_name: str + The path to the FITS file + + Returns + ------- + data: Spectrum1D + The 6dF spectrum that is represented by the data in this table. + """ + with fits.open(filename) as hdulist: + header = hdulist[0].header + table = Table.read(hdulist) + flux = Quantity(table["FLUX"]) + wavelength = Quantity(table["WAVE"]) + + if flux.unit == COUNTS_PER_SECOND: + flux._unit = Unit("count/s") + meta = {"header": header} + + return Spectrum1D(flux=flux, spectral_axis=wavelength, meta=meta) diff --git a/specutils/tests/test_loaders.py b/specutils/tests/test_loaders.py index e19712e21..d546580e8 100644 --- a/specutils/tests/test_loaders.py +++ b/specutils/tests/test_loaders.py @@ -490,3 +490,11 @@ def test_subaru_pfs_loader(tmpdir): assert len(spec.flux) == len(spec.spectral_axis) > 10000 assert spec.spectral_axis.unit == u.nm assert spec.flux.unit == u.nJy + + +@remote_access([{'id': '3733958', 'filename': '1D-c0022498-344732.fits'}]) +def test_spectrum1d_6dfgs_tabular(remote_data_path): + spec = Spectrum1D.read(remote_data_path) + + assert spec.spectral_axis.unit == u.Unit("Angstrom") + assert spec.flux.unit == u.Unit("count/s")