From 5c3d6c796e119a59d30bc402d8f4681aceaf3f67 Mon Sep 17 00:00:00 2001 From: "Timothy P. Ellsworth Bowers" Date: Thu, 21 Sep 2023 09:53:21 -0700 Subject: [PATCH 1/4] Add clearer error messages (from User Slack) Following up on a question in the User Slack #gmos channel, this adds clearer error messages in the case when the wavelength solution method is "full_template" but no `reid_arxiv` was specified. modified: pypeit/core/wavecal/autoid.py modified: pypeit/core/wavecal/waveio.py --- pypeit/core/wavecal/autoid.py | 4 ++++ pypeit/core/wavecal/waveio.py | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pypeit/core/wavecal/autoid.py b/pypeit/core/wavecal/autoid.py index 76b0edd3cf..b21aa3204f 100644 --- a/pypeit/core/wavecal/autoid.py +++ b/pypeit/core/wavecal/autoid.py @@ -1046,6 +1046,10 @@ def full_template(spec, lamps, par, ok_mask, det, binspectral, nsnippet=2, slit_ # Load template if template_dict is None: + # Error checking + if par['reid_arxiv'] is None: + msgs.error('WavelengthSolutionPar parameter `reid_arxiv` not ' + 'specified for "full_template" method.') temp_wv, temp_spec, temp_bin = waveio.load_template( par['reid_arxiv'], det, wvrng=par['wvrng_arxiv']) else: diff --git a/pypeit/core/wavecal/waveio.py b/pypeit/core/wavecal/waveio.py index e765ebe90a..9da6e0d1af 100644 --- a/pypeit/core/wavecal/waveio.py +++ b/pypeit/core/wavecal/waveio.py @@ -50,7 +50,7 @@ def load_wavelength_calibration(filename: pathlib.Path) -> dict: return wv_calib -def load_template(arxiv_file, det, wvrng=None): +def load_template(arxiv_file:str, det:int, wvrng:list=None)->tuple[np.ndarray,np.ndarray,int]: """ Load a full template file from disk @@ -73,6 +73,9 @@ def load_template(arxiv_file, det, wvrng=None): binning of the template arc spectrum """ + if not isinstance(arxiv_file, (str, pathlib.Path)): + msgs.error(f"Incorrect or nonexistant arxiv file specified: {arxiv_file}") + # Path already included? if pathlib.Path(arxiv_file).name == arxiv_file: calibfile, _ = data.get_reid_arxiv_filepath(arxiv_file) From b083fc532eacf0b37e493c4bde2d5719a7ab2611 Mon Sep 17 00:00:00 2001 From: "Timothy P. Ellsworth Bowers" Date: Mon, 30 Oct 2023 09:31:07 -0700 Subject: [PATCH 2/4] Improve error message for reid_arxiv file not exist As commented in the Users Slack: https://pypeit-users.slack.com/archives/CST2Q4QPN/p1694827540917799 This commit adds checking to see if the input is empty before calling `pathlib.Path()`, which is what was causing the errors reported. modified: pypeit/core/wavecal/waveio.py --- pypeit/core/wavecal/waveio.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pypeit/core/wavecal/waveio.py b/pypeit/core/wavecal/waveio.py index 9da6e0d1af..3b437e4c60 100644 --- a/pypeit/core/wavecal/waveio.py +++ b/pypeit/core/wavecal/waveio.py @@ -73,7 +73,7 @@ def load_template(arxiv_file:str, det:int, wvrng:list=None)->tuple[np.ndarray,np binning of the template arc spectrum """ - if not isinstance(arxiv_file, (str, pathlib.Path)): + if not isinstance(arxiv_file, (str, pathlib.Path)) or arxiv_file is None or arxiv_file == "": msgs.error(f"Incorrect or nonexistant arxiv file specified: {arxiv_file}") # Path already included? From 5e925752dd56536f0afcfda1757d4b0d86a89c25 Mon Sep 17 00:00:00 2001 From: "Timothy P. Ellsworth Bowers" Date: Wed, 1 Nov 2023 10:01:29 -0700 Subject: [PATCH 3/4] PR Comments modified: pypeit/core/wavecal/waveio.py modified: pypeit/data/utils.py --- pypeit/core/wavecal/waveio.py | 12 ++---------- pypeit/data/utils.py | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/pypeit/core/wavecal/waveio.py b/pypeit/core/wavecal/waveio.py index 3b437e4c60..5760d72907 100644 --- a/pypeit/core/wavecal/waveio.py +++ b/pypeit/core/wavecal/waveio.py @@ -73,16 +73,8 @@ def load_template(arxiv_file:str, det:int, wvrng:list=None)->tuple[np.ndarray,np binning of the template arc spectrum """ - if not isinstance(arxiv_file, (str, pathlib.Path)) or arxiv_file is None or arxiv_file == "": - msgs.error(f"Incorrect or nonexistant arxiv file specified: {arxiv_file}") - - # Path already included? - if pathlib.Path(arxiv_file).name == arxiv_file: - calibfile, _ = data.get_reid_arxiv_filepath(arxiv_file) - else: - calibfile = pathlib.Path(arxiv_file) - # Read me - tbl = astropy.table.Table.read(calibfile, format='fits') + calibfile, fmt = data.get_reid_arxiv_filepath(arxiv_file) + tbl = astropy.table.Table.read(calibfile, format=fmt) # Parse on detector? if 'det' in tbl.keys(): idx = np.where(tbl['det'].data & 2**det)[0] diff --git a/pypeit/data/utils.py b/pypeit/data/utils.py index 263eb52872..41c32c7c53 100644 --- a/pypeit/data/utils.py +++ b/pypeit/data/utils.py @@ -211,18 +211,30 @@ def get_reid_arxiv_filepath(arxiv_file: str) -> tuple[pathlib.Path, str]: version of PypeIt). Args: - arxiv_file (str): + arxiv_file (:obj:`str`): The base filename of the ``reid_arxiv`` file to be located Returns: tuple: The full path and whether the path is in the cache: - * reid_path (:obj:`pathlib.Path`): The full path to the ``reid_arxiv`` file + * reid_path (:obj:`~pathlib.Path`): The full path to the ``reid_arxiv`` file * arxiv_fmt (:obj:`str`): The extension of the ``reid_arxiv`` file (format) """ - # Full path within the package data structure: + # First check that what is passed in works + if not isinstance(arxiv_file, (str, pathlib.Path)): + msgs.error(f"Incorrect or non-existent arxiv file specified: {arxiv_file}") + if isinstance(arxiv_file, pathlib.Path): + arxiv_file = str(arxiv_file) + + # Check if the `arxiv_file` already comes with a full path + if (reid_path := pathlib.Path(arxiv_file)).name != arxiv_file: + # Check existence, return it with format + if not reid_path.is_file(): + msgs.error(f"Incorrect or non-existent arxiv file specified: {reid_path}") + return reid_path, reid_path.suffix.replace('.','').lower() + + # Else, full path within the package data structure: reid_path = Paths.reid_arxiv / arxiv_file - arxiv_fmt = arxiv_file.split(".")[-1].lower() # Check if the file does NOT exist in the package directory # NOTE: This should be the case for all but from-source installations @@ -235,7 +247,7 @@ def get_reid_arxiv_filepath(arxiv_file: str) -> tuple[pathlib.Path, str]: reid_path = fetch_remote_file(arxiv_file, "arc_lines/reid_arxiv") # Return the path to the `reid_arxiv` file, and the file format - return reid_path, arxiv_fmt + return reid_path, reid_path.suffix.replace('.','').lower() def get_skisim_filepath(skisim_file: str) -> pathlib.Path: From 23b369f1c09e0ecb689d8be80833b626258ffb6d Mon Sep 17 00:00:00 2001 From: "Timothy P. Ellsworth Bowers" Date: Wed, 1 Nov 2023 10:19:32 -0700 Subject: [PATCH 4/4] Format of cached files modified: pypeit/data/utils.py --- pypeit/data/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pypeit/data/utils.py b/pypeit/data/utils.py index 41c32c7c53..cba62b0153 100644 --- a/pypeit/data/utils.py +++ b/pypeit/data/utils.py @@ -247,7 +247,7 @@ def get_reid_arxiv_filepath(arxiv_file: str) -> tuple[pathlib.Path, str]: reid_path = fetch_remote_file(arxiv_file, "arc_lines/reid_arxiv") # Return the path to the `reid_arxiv` file, and the file format - return reid_path, reid_path.suffix.replace('.','').lower() + return reid_path, arxiv_file.split('.')[-1].lower() def get_skisim_filepath(skisim_file: str) -> pathlib.Path: