diff --git a/CHANGES.rst b/CHANGES.rst index df9a3247d..092b6b0b7 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -51,6 +51,12 @@ resample -------- - Implement resampling step. [#787] +tweakreg +-------- + +- Fix a bug due to which source catalog may contain sources + outside of the bounding box. [#947] + 0.12.0 (2023-08-18) =================== diff --git a/romancal/tweakreg/tweakreg_step.py b/romancal/tweakreg/tweakreg_step.py index fcf5f3b06..fdd5fd362 100644 --- a/romancal/tweakreg/tweakreg_step.py +++ b/romancal/tweakreg/tweakreg_step.py @@ -190,8 +190,6 @@ def process(self, input): "Please either run SourceDetectionStep or provide a" "custom source catalog." ) - # set meta.tweakreg_catalog - image_model.meta["tweakreg_catalog"] = catalog # remove 4D numpy array from meta.source_detection if is_tweakreg_catalog_present: del image_model.meta.source_detection["tweakreg_catalog"] @@ -214,16 +212,41 @@ def process(self, input): "'xcentroid' and 'ycentroid'." ) + filename = image_model.meta.filename + # filter out sources outside the WCS bounding box bb = image_model.meta.wcs.bounding_box - if bb is not None: + x = catalog["x"] + y = catalog["y"] + if bb is None: + r, d = image_model.meta.wcs(x, y) + mask = np.isfinite(r) & np.isfinite(d) + catalog = catalog[mask] + + n_removed_src = np.sum(np.logical_not(mask)) + if n_removed_src: + self.log.info( + f"Removed {n_removed_src} sources from {filename}'s " + "catalog whose image coordinates could not be " + "converted to world coordinates." + ) + else: + # assume image coordinates of all sources within a bounding box + # can be converted to world coordinates. ((xmin, xmax), (ymin, ymax)) = bb - x = catalog["x"] - y = catalog["y"] mask = (x > xmin) & (x < xmax) & (y > ymin) & (y < ymax) catalog = catalog[mask] - filename = image_model.meta.filename + n_removed_src = np.sum(np.logical_not(mask)) + if n_removed_src: + self.log.info( + f"Removed {n_removed_src} sources from {filename}'s " + "catalog that were outside of the bounding box." + ) + + # set meta.tweakreg_catalog + image_model.meta["tweakreg_catalog"] = catalog + nsources = len(catalog) if nsources == 0: self.log.warning(f"No sources found in {filename}.")