Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sensible error message for specutils loaders #1709

Merged
merged 5 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/releases/1.14.1dev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Dependency Changes
------------------

- Removes use of deprecated ``pkg_resources``.
- Require version ``>=1.12`` for ``specutils``.

Functionality/Performance Improvements and Additions
----------------------------------------------------
Expand All @@ -20,6 +21,9 @@ Functionality/Performance Improvements and Additions
spectrum have monotonically increasing wavelength data. Non-monotonic
wavelength data usually indicate a problem with the data reduction, but this
at least lets users ingest the spectrum.
- Add a sensible error message to the pypeit Spectrum1D loaders in the event a
user inadvertently tries to use Spectrum1D instead of SpectrumList for a
``spec1d`` file.

Instrument-specific Updates
---------------------------
Expand Down
42 changes: 39 additions & 3 deletions pypeit/specutils/pypeit_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- 2022-02-04: Initial Version (tbowers)
- 2022-09-16: Correct an import error and add module docstring (tbowers)
- 2023-03-09: Moved into the main pypeit repo and refactored (KBW)
- 2023-06-23: Added sensible error message for incorrect spec1d loading (tbowers)

"""

Expand Down Expand Up @@ -145,7 +146,8 @@ def identify_pypeit_onespec(origin, *args, **kwargs):
identifier=identify_pypeit_spec1d,
extensions=["fits"],
priority=10,
dtype=SpectrumList)
dtype=SpectrumList,
autogenerate_spectrumlist=False)
def pypeit_spec1d_loader(filename, extract=None, fluxed=True, strict=True, **kwargs):
"""
Load spectra from a PypeIt spec1d file into a SpectrumList.
Expand Down Expand Up @@ -182,7 +184,7 @@ def pypeit_spec1d_loader(filename, extract=None, fluxed=True, strict=True, **kwa
sobjs = specobjs.SpecObjs.from_fitsfile(filename, chk_version=False)
except PypeItError:
file_pypeit_version = astropy.io.fits.getval(filename, 'VERSPYP', 'PRIMARY')
msgs.error(f'Unable to ingest {filename} using pypeit.specobjs module from your version '
msgs.error(f'Unable to ingest {filename.name} using pypeit.specobjs module from your version '
f'of PypeIt ({__version__}). The version used to write the file is '
f'{file_pypeit_version}. If these are different, you may need to re-reduce '
'your data using your current PypeIt version or install the matching version '
Expand Down Expand Up @@ -241,7 +243,7 @@ def pypeit_onespec_loader(filename, grid=False, strict=True, **kwargs):
spec = onespec.OneSpec.from_file(filename)
except PypeItError:
file_pypeit_version = astropy.io.fits.getval(filename, 'VERSPYP', 'PRIMARY')
msgs.error(f'Unable to ingest {filename} using pypeit.specobjs module from your version '
msgs.error(f'Unable to ingest {filename.name} using pypeit.specobjs module from your version '
f'of PypeIt ({__version__}). The version used to write the file is '
f'{file_pypeit_version}. If these are different, you may need to re-reduce '
'your data using your current PypeIt version or install the matching version '
Expand Down Expand Up @@ -271,5 +273,39 @@ def pypeit_onespec_loader(filename, grid=False, strict=True, **kwargs):
velocity_convention="doppler_optical",
bin_specification="centers")

# Warning Function ===========================================================#
@data_loader('PypeIt spec1d nolist',
identifier=identify_pypeit_spec1d,
extensions=["fits"],
priority=10,
dtype=Spectrum1D,
autogenerate_spectrumlist=False)
def pypeit_spec1d_loader_nolist(filename, extract=None, fluxed=True, **kwargs):
"""
Sensible error message if a user tries to load spectra from a PypeIt spec1d
file into a Spectrum1D.

This is not allowed because spec1d files may contain mutliple spectra. This
function accepts all arguments as the SpectrumList version, but only outputs
a PypeIt Error with a sensible message.

This avoids receiving unhelpful error messages such as::

OSError: Could not identify column containing the wavelength, frequency or energy

Parameters
----------
filename : str
The path to the FITS file
extract : str, optional
The extraction used to produce the spectrum. Must be either None,
``'BOX'`` (for a boxcar extraction), or ``'OPT'`` for optimal
extraction. If None, the optimal extraction will be returned, if it
exists, otherwise the boxcar extraction will be returned.
fluxed : bool, optional
If True, return the flux-calibrated spectrum, if it exists. If the flux
calibration hasn't been performed or ``fluxed=False``, the spectrum is
returned in counts.
"""
msgs.error(f'The spec1d file {filename.name} cannot be ingested into a Spectrum1D object.'
f'{msgs.newline()}Please use the SpectrumList object for spec1d files.')
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ scripts =
scikit-image =
scikit-image
specutils =
specutils
specutils>=1.12
test =
pytest>=6.0.0
pytest-astropy
Expand All @@ -75,7 +75,7 @@ docs =
sphinx_rtd_theme==1.2.2
dev =
scikit-image
specutils
specutils>=1.12
pytest>=6.0.0
pytest-astropy
tox
Expand Down