From 85effbc1cb43a7c8b1d7aa2d0102043f6d42c643 Mon Sep 17 00:00:00 2001 From: "Timothy P. Ellsworth Bowers" Date: Fri, 23 Jun 2023 11:35:22 -0700 Subject: [PATCH 1/3] Add sensible error message When users try to ingest a `spec1d` file into a Spectrum1D object, the current scheme ends badly with an obscure error message like: ``` OSError: Could not identify column containing the wavelength, frequency or energy ``` Because PypeIt `spec1d` files may have multiple extensions, by convention we load these into SpectrumList objects in specutils, even if there is only a single spectrum. This commit catches the case when a user tries to load a `spec1d` file into a Spectrum1D object. Previously, such an attempt ended up with specutils loading in the `spec1d` file as a tabular FITS format, then didn't know what to do about the spectral axis (leading to the above obscure error). This commit specifically includes a loader for `spec1d` files into Spectrum1D objects, but emits a sensible error message in this case rather than trying to actually load the file. As it happens, in order to properly do this, I needed to make an adjustment in specutils itself. A PR has been submitted (astropy/specutils#1068), but this commit should not be considered as a PypeIt PR until the specutils change has incorporated into an official release. At that time, the specutils optional dependency will need to have a minimum version specified. So... this commit will quietly sit in a branch until specutils makes its move. modified: pypeit/specutils/pypeit_loaders.py --- pypeit/specutils/pypeit_loaders.py | 42 +++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/pypeit/specutils/pypeit_loaders.py b/pypeit/specutils/pypeit_loaders.py index b7cd629c99..e051ef81b3 100644 --- a/pypeit/specutils/pypeit_loaders.py +++ b/pypeit/specutils/pypeit_loaders.py @@ -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) """ @@ -80,7 +81,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, **kwargs): """ Load spectra from a PypeIt spec1d file into a SpectrumList. @@ -109,7 +111,7 @@ def pypeit_spec1d_loader(filename, extract=None, fluxed=True, **kwargs): 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 ' @@ -160,7 +162,7 @@ def pypeit_onespec_loader(filename, grid=False, **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 ' @@ -188,5 +190,39 @@ def pypeit_onespec_loader(filename, grid=False, **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.') From a9cc26ac5d2dfc202ef3ff3104eba44f76b06592 Mon Sep 17 00:00:00 2001 From: "Timothy P. Ellsworth Bowers" Date: Mon, 28 Aug 2023 12:00:56 -0700 Subject: [PATCH 2/3] Update specutils version to accomodate this PR modified: setup.cfg --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index bbb6f42214..ca496fe4d9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -59,7 +59,7 @@ scripts = scikit-image = scikit-image specutils = - specutils + specutils>=1.12 test = pytest>=6.0.0 pytest-astropy From 795230d6609e483241de8e3b81ca544ae77d8d60 Mon Sep 17 00:00:00 2001 From: "Timothy P. Ellsworth Bowers" Date: Thu, 19 Oct 2023 22:52:54 -0700 Subject: [PATCH 3/3] Update release notes modified: doc/releases/1.14.1dev.rst modified: setup.cfg --- doc/releases/1.14.1dev.rst | 4 ++++ setup.cfg | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/doc/releases/1.14.1dev.rst b/doc/releases/1.14.1dev.rst index efce08cc5a..8f3c998841 100644 --- a/doc/releases/1.14.1dev.rst +++ b/doc/releases/1.14.1dev.rst @@ -6,6 +6,7 @@ Dependency Changes ------------------ - Removes use of deprecated ``pkg_resources``. +- Require version ``>=1.12`` for ``specutils``. Functionality/Performance Improvements and Additions ---------------------------------------------------- @@ -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 --------------------------- diff --git a/setup.cfg b/setup.cfg index d7e0b56625..e46e893c82 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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