Skip to content

Commit

Permalink
Merge pull request #407 from hamogu/patchwcs
Browse files Browse the repository at this point in the history
Extend list of hacks for reading non-standard complied 1d WCS fits files
  • Loading branch information
nmearl committed May 2, 2019
2 parents 0cc8b6c + 886cd6b commit b873f2a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
17 changes: 16 additions & 1 deletion specutils/tests/test_loaders.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import shutil
import tempfile
Expand Down Expand Up @@ -36,7 +37,7 @@ def test_spectrum1d_GMOSfits(remote_data_path):


@remote_access([{'id': '1481190', 'filename': 'L5g_0355+11_Cruz09.fits'}])
def test_spectrumlist_GMOSfits(remote_data_path):
def test_spectrumlist_GMOSfits(remote_data_path, caplog):
with warnings.catch_warnings():
warnings.simplefilter('ignore', (VerifyWarning, UnitsWarning))
spectrum_list = SpectrumList.read(remote_data_path, format='wcs1d-fits')
Expand All @@ -46,6 +47,11 @@ def test_spectrumlist_GMOSfits(remote_data_path):
spec = spectrum_list[0]
assert len(spec.data) == 3020

logmsg = caplog.record_tuples[0]
assert logmsg[1] == logging.WARN
assert "Assuming the axis 0 labeled 'linear' is spectral" in logmsg[2]



@remote_access([{'id': '1481190', 'filename': 'L5g_0355+11_Cruz09.fits'}])
def test_specific_spec_axis_unit(remote_data_path):
Expand All @@ -57,6 +63,15 @@ def test_specific_spec_axis_unit(remote_data_path):

assert optical_spec.spectral_axis.unit == "Angstrom"

@remote_access([{'id': '2656720', 'filename': '_v1410ori_20181204_261_Forrest%20Sims.fit'}])
def test_ctypye_not_compliant(remote_data_path, caplog):
optical_spec = Spectrum1D.read(remote_data_path,
spectral_axis_unit="Angstrom",
format='wcs1d-fits')
logmsg = caplog.record_tuples[0]
assert logmsg[1] == logging.WARN
assert "Assuming the axis 0 labeled 'wavelength' is spectral" in logmsg[2]


def test_generic_ecsv_reader(tmpdir):
# Create a small data set
Expand Down
24 changes: 17 additions & 7 deletions specutils/wcs/adapters/fitswcs_adapter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import astropy.units as u
from astropy.wcs import (WCS, WCSSUB_CELESTIAL, WCSSUB_CUBEFACE,
WCSSUB_LATITUDE, WCSSUB_LONGITUDE, WCSSUB_SPECTRAL,
Expand All @@ -14,10 +15,18 @@
class FITSWCSAdapter(WCSAdapter):
"""
Adapter class that adds support for FITSWCS objects.
In the wild, fits WCS headers are often non-standard compliant, but
can be interpreted with little ambiguity (e.g. the CTYPE of the
wavelength axis is called "Wavelength" instead of the standard fits
"WAVE"). In some common cases, this class will thus read files that
are not fully compliant. In these cases, it prints a warning message.
"""
wrapped_class = WCS
axes = None

substitute_spec_axis_names = ['linear', 'wavelength']

def __init__(self, wcs):
super(FITSWCSAdapter, self).__init__(wcs)
self._spec_axis = None
Expand Down Expand Up @@ -71,17 +80,18 @@ def spec_axis(self):
"""
self._spec_axis = self.wcs.wcs.spec

if self._spec_axis < 0:
try:
idx = list(self.wcs.wcs.ctype).index('LINEAR')
except ValueError:
if (self._spec_axis < 0) and (self._wcs.wcs.spec) < 0:
ctypelist = [c.lower() for c in self.wcs.wcs.ctype]
for n in self.substitute_spec_axis_names:
if n in ctypelist:
self._spec_axis = ctypelist.index(n)
logging.warning("WCS has a non-standard spectral axis, 'ctype's might be incorrect. Assuming the axis {} labeled '{}' is spectral and proceeding.".format(self._spec_axis, n))
break
else:
raise InvalidSubimageSpecificationError(
"Cannot find a spectral axis in the provided WCS."
"Are your 'ctype's correct?")

if self._wcs.wcs.spec < 0:
self._spec_axis = idx

return self._spec_axis

@property
Expand Down

0 comments on commit b873f2a

Please sign in to comment.