From 3f273391564fb56c6bca63318eee7fdc8ea6b840 Mon Sep 17 00:00:00 2001 From: rcooke Date: Thu, 2 Nov 2023 12:55:35 +0000 Subject: [PATCH 1/4] fix error --- pypeit/core/gui/skysub_regions.py | 39 +++++++++++++++++++++++-------- pypeit/core/skysub.py | 9 +++---- pypeit/scripts/skysub_regions.py | 8 ++++++- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/pypeit/core/gui/skysub_regions.py b/pypeit/core/gui/skysub_regions.py index 26417eaa94..be326301da 100644 --- a/pypeit/core/gui/skysub_regions.py +++ b/pypeit/core/gui/skysub_regions.py @@ -571,22 +571,41 @@ def operations(self, key, axisID): self.replot() def get_result(self): - """Save a mask containing the skysub regions, and print information + """Generate a master calibration file containing a mask of the skysub regions, and print information for what the user should include in their .pypeit file + + Returns + ------- + msskyreg : :class:`SkyRegions`, None + Returns an instance of the :class:`SkyRegions` class. If None is returned, + the user has requested to not use the updates. """ # Only do this if the user wishes to save the result + msskyreg = None if self._use_updates: # Generate the mask inmask = skysub.generate_mask(self.pypeline, self._skyreg, self.slits, self.slits_left, self.slits_right) - # Save the mask - outfil = self._outname - if os.path.exists(self._outname) and not self._overwrite: - outfil = 'temp.fits' - msgs.warn(f"A SkyRegions file already exists and you have not forced an overwrite:\n{self._outname}") - msgs.info(f"Saving regions to: {outfil}") - self._overwrite = True - msskyreg = buildimage.SkyRegions(image=inmask.astype(float), PYP_SPEC=self.spectrograph) - msskyreg.to_file(file_path=outfil) + if np.all(np.logical_not(inmask)): + msgs.warn("Sky regions are empty - master calibration frame will not be generated") + else: + # Build the master Sky Regions calibration frame + msskyreg = buildimage.SkyRegions(image=inmask.astype(float), PYP_SPEC=self.spectrograph) + return msskyreg + + def get_outname(self): + """ Get an output filename + + Returns + ------- + outfil : :obj:`str` + The output filename to use for the master Sky Regions calibration frame + """ + outfil = self._outname + if os.path.exists(self._outname) and not self._overwrite: + outfil = 'temp.fits' + msgs.warn(f"A SkyRegions file already exists and you have not forced an overwrite:\n{self._outname}") + msgs.info(f"Adopting the following output filename: {outfil}") + return outfil def recenter(self): xlim = self.axes['main'].get_xlim() diff --git a/pypeit/core/skysub.py b/pypeit/core/skysub.py index e2b94ec834..b486282c73 100644 --- a/pypeit/core/skysub.py +++ b/pypeit/core/skysub.py @@ -1592,13 +1592,14 @@ def generate_mask(pypeline, skyreg, slits, slits_left, slits_right, spat_flexure spec_min = np.append(spec_min, slits.specmin[sl]) spec_max = np.append(spec_max, slits.specmax[sl]) + # Check if no regions were added + if left_edg.shape[1] == 0: + return np.zeros((slits.nspec, slits.nspat), dtype=bool) + # Now that we have sky region traces, utilise the SlitTraceSet to define the regions. # We will then use the slit_img task to create a mask of the sky regions. - # TODO: I don't understand why slmsk needs to be instantiated. SlitTraceSet - # does this internally. - slmsk = np.zeros(left_edg.shape[1], dtype=slittrace.SlitTraceSet.bitmask.minimum_dtype()) slitreg = slittrace.SlitTraceSet(left_edg, righ_edg, pypeline, nspec=slits.nspec, - nspat=slits.nspat, mask=slmsk, specmin=spec_min, + nspat=slits.nspat, specmin=spec_min, specmax=spec_max, binspec=slits.binspec, binspat=slits.binspat, pad=0) # Generate the mask, and return diff --git a/pypeit/scripts/skysub_regions.py b/pypeit/scripts/skysub_regions.py index eb254f308e..f1347f40c8 100644 --- a/pypeit/scripts/skysub_regions.py +++ b/pypeit/scripts/skysub_regions.py @@ -95,7 +95,13 @@ def main(args): initial=args.initial, flexure=spat_flexure) # Get the results - skyreg.get_result() + msskyreg = skyreg.get_result() + + if msskyreg is not None: + outfil = skyreg.get_outname() + setup, calib_id, detname = msskyreg.parse_calib_key(calib_key) + msskyreg.set_paths(calib_dir, setup, calib_id, detname) + msskyreg.to_file(file_path=outfil) # Reset the defaults skyreg.finalize() From 55e989453023273fcd775cbb30edb9acc47078bc Mon Sep 17 00:00:00 2001 From: rcooke Date: Thu, 2 Nov 2023 19:50:06 +0000 Subject: [PATCH 2/4] cleanup results --- pypeit/core/gui/skysub_regions.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/pypeit/core/gui/skysub_regions.py b/pypeit/core/gui/skysub_regions.py index be326301da..5f460d0193 100644 --- a/pypeit/core/gui/skysub_regions.py +++ b/pypeit/core/gui/skysub_regions.py @@ -580,17 +580,17 @@ def get_result(self): Returns an instance of the :class:`SkyRegions` class. If None is returned, the user has requested to not use the updates. """ - # Only do this if the user wishes to save the result - msskyreg = None - if self._use_updates: - # Generate the mask - inmask = skysub.generate_mask(self.pypeline, self._skyreg, self.slits, self.slits_left, self.slits_right) - if np.all(np.logical_not(inmask)): - msgs.warn("Sky regions are empty - master calibration frame will not be generated") - else: - # Build the master Sky Regions calibration frame - msskyreg = buildimage.SkyRegions(image=inmask.astype(float), PYP_SPEC=self.spectrograph) - return msskyreg + if not self._use_updates: + return None + + # Generate the mask + inmask = skysub.generate_mask(self.pypeline, self._skyreg, self.slits, self.slits_left, self.slits_right) + if np.all(np.logical_not(inmask)): + msgs.warn("Sky regions are empty - master calibration frame will not be generated") + return None + + # Build the master Sky Regions calibration frame + return buildimage.SkyRegions(image=inmask.astype(float), PYP_SPEC=self.spectrograph) def get_outname(self): """ Get an output filename From 46ef5b3b8b2fa1df1f8ab5d16ac788e1b440d80a Mon Sep 17 00:00:00 2001 From: Ryan Cooke Date: Fri, 3 Nov 2023 14:05:32 +0000 Subject: [PATCH 3/4] Update 1.14.1dev.rst --- doc/releases/1.14.1dev.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/releases/1.14.1dev.rst b/doc/releases/1.14.1dev.rst index 6bffc2e1ef..50f962b506 100644 --- a/doc/releases/1.14.1dev.rst +++ b/doc/releases/1.14.1dev.rst @@ -61,5 +61,7 @@ Bug Fixes detector for Keck/LRIS RED. - Fixed a bug with the ``pypeit_identify`` script when using echelle data. Previously, the sigdetect parameter was a list of all orders, instead of a single value. +- Fixed a bug with the GUI ``pypeit_skysub_regions``. Previously, the calib header + information was not included, and this led to a calibration error. From 834611224ff53309770866c0378217835ad11f8f Mon Sep 17 00:00:00 2001 From: Ryan Cooke Date: Fri, 3 Nov 2023 18:25:42 +0000 Subject: [PATCH 4/4] rm master --- pypeit/core/gui/skysub_regions.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pypeit/core/gui/skysub_regions.py b/pypeit/core/gui/skysub_regions.py index 5f460d0193..b8e268197c 100644 --- a/pypeit/core/gui/skysub_regions.py +++ b/pypeit/core/gui/skysub_regions.py @@ -571,7 +571,7 @@ def operations(self, key, axisID): self.replot() def get_result(self): - """Generate a master calibration file containing a mask of the skysub regions, and print information + """Generate a calibration file containing a mask of the skysub regions, and print information for what the user should include in their .pypeit file Returns @@ -586,10 +586,10 @@ def get_result(self): # Generate the mask inmask = skysub.generate_mask(self.pypeline, self._skyreg, self.slits, self.slits_left, self.slits_right) if np.all(np.logical_not(inmask)): - msgs.warn("Sky regions are empty - master calibration frame will not be generated") + msgs.warn("Sky regions are empty - A sky regions calibration frame will not be generated") return None - # Build the master Sky Regions calibration frame + # Build the Sky Regions calibration frame return buildimage.SkyRegions(image=inmask.astype(float), PYP_SPEC=self.spectrograph) def get_outname(self): @@ -598,7 +598,7 @@ def get_outname(self): Returns ------- outfil : :obj:`str` - The output filename to use for the master Sky Regions calibration frame + The output filename to use for the Sky Regions calibration frame """ outfil = self._outname if os.path.exists(self._outname) and not self._overwrite: