Skip to content

Commit 6dd54ad

Browse files
authored
Merge pull request #833 from dhomeier/physicaltypes-fix
Handle new `PhysicalType` class in `spectrum_from_column_mapping`
2 parents f9d4124 + fcf624b commit 6dd54ad

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

specutils/io/default_loaders/tabular_fits.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,12 @@ def tabular_fits_writer(spectrum, file_name, hdu=1, update_header=False, **kwarg
134134
wunit = u.Unit(kwargs.pop('wunit', spectrum.spectral_axis.unit))
135135
disp = spectrum.spectral_axis.to(wunit, equivalencies=u.spectral())
136136

137-
# Mapping of spectral_axis types to header TTYPE1
138-
dispname = wunit.physical_type
137+
# Mapping of spectral_axis types to header TTYPE1 (no "torque/work" types!)
138+
dispname = str(wunit.physical_type)
139139
if dispname == "length":
140140
dispname = "wavelength"
141+
elif "energy" in dispname:
142+
dispname = "energy"
141143

142144
# Add flux array and unit
143145
ftype = kwargs.pop('ftype', spectrum.flux.dtype)

specutils/io/parsing_utils.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,16 @@ def spectrum_from_column_mapping(table, column_mapping, wcs=None):
100100

101101
# Attempt to convert the table unit to the user-defined unit.
102102
log.debug("Attempting auto-convert of table unit '%s' to "
103-
"user-provided unit '%s'.", tab_unit, cm_unit)
103+
"user-provided unit '%s'.", tab_unit, cm_unit)
104104

105105
if not isinstance(cm_unit, u.Unit):
106106
cm_unit = u.Unit(cm_unit)
107-
if cm_unit.physical_type in ('length', 'frequency'):
107+
if cm_unit.physical_type in ('length', 'frequency', 'energy'):
108108
# Spectral axis column information
109109
kwarg_val = kwarg_val.to(cm_unit, equivalencies=u.spectral())
110-
elif 'spectral flux' in cm_unit.physical_type:
110+
elif 'spectral flux' in str(cm_unit.physical_type):
111111
# Flux/error column information
112-
kwarg_val = kwarg_val.to(
113-
cm_unit, equivalencies=u.spectral_density(1 * u.AA))
112+
kwarg_val = kwarg_val.to(cm_unit, equivalencies=u.spectral_density(1 * u.AA))
114113
elif tab_unit:
115114
# The user has provided no unit in the column mapping, so we
116115
# use the unit as defined in the table object.

specutils/tests/test_loaders.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,15 @@ def test_tabular_fits_writer(tmpdir, spectral_axis):
522522
spectrum.write(tmpfile, format='tabular-fits')
523523
spectrum.write(tmpfile, format='tabular-fits', overwrite=True)
524524

525-
cmap = {spectral_axis: ('spectral_axis', wlu[spectral_axis]),
525+
# Map to alternative set of units
526+
cmap = {spectral_axis: ('spectral_axis', 'micron'),
526527
'flux': ('flux', 'erg / (s cm**2 AA)'),
527528
'uncertainty': ('uncertainty', None)}
528529

529530
# Read it back again and check against the original
530531
spec = Spectrum1D.read(tmpfile, format='tabular-fits', column_mapping=cmap)
531532
assert spec.flux.unit == u.Unit('erg / (s cm**2 AA)')
532-
assert spec.spectral_axis.unit == spectrum.spectral_axis.unit
533+
assert spec.spectral_axis.unit == u.um
533534
assert quantity_allclose(spec.spectral_axis, spectrum.spectral_axis)
534535
assert quantity_allclose(spec.flux, spectrum.flux)
535536
assert quantity_allclose(spec.uncertainty.quantity,
@@ -565,14 +566,14 @@ def test_tabular_fits_multid(tmpdir, ndim, spectral_axis):
565566
assert quantity_allclose(spec.uncertainty.quantity,
566567
spectrum.uncertainty.quantity)
567568

568-
# Test again, using `column_mapping` to convert to different flux unit
569-
cmap = {spectral_axis: ('spectral_axis', wlu[spectral_axis]),
569+
# Test again, using `column_mapping` to convert to different spectral axis and flux units
570+
cmap = {spectral_axis: ('spectral_axis', 'THz'),
570571
'flux': ('flux', 'erg / (s cm**2 AA)'),
571572
'uncertainty': ('uncertainty', None)}
572573

573574
spec = Spectrum1D.read(tmpfile, format='tabular-fits', column_mapping=cmap)
574575
assert spec.flux.unit == u.Unit('erg / (s cm**2 AA)')
575-
assert spec.spectral_axis.unit == spectrum.spectral_axis.unit
576+
assert spec.spectral_axis.unit == u.THz
576577
assert quantity_allclose(spec.spectral_axis, spectrum.spectral_axis)
577578
assert quantity_allclose(spec.flux, spectrum.flux)
578579
assert quantity_allclose(spec.uncertainty.quantity,

0 commit comments

Comments
 (0)