Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremykubica committed Nov 15, 2024
1 parent d9d4828 commit 236e225
Show file tree
Hide file tree
Showing 5 changed files with 352 additions and 288 deletions.
7 changes: 5 additions & 2 deletions src/kbmod/image_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,10 @@ def toWorkUnit(self, search_config=None, **kwargs):
for std in self.get_standardizers(**kwargs):
for img in std["std"].toLayeredImage():
layeredImages.append(img)

imgstack = ImageStack(layeredImages)
img_metadata = Table()

if None not in self.wcs:
return WorkUnit(imgstack, search_config, per_image_wcs=list(self.wcs))
return WorkUnit(imgstack, search_config)
img_metadata["per_image_wcs"] = list(self.wcs)
return WorkUnit(imgstack, search_config, org_image_meta=img_metadata)
29 changes: 17 additions & 12 deletions src/kbmod/reprojection.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ def reproject_work_unit(
A `kbmod.WorkUnit` reprojected with a common `astropy.wcs.WCS`, or `None` in the case
where `write_output` is set to True.
"""
if work_unit.reprojected:
raise ValueError("Unable to reproject a reprojected WorkUnit.")

show_progress = is_interactive() if show_progress is None else show_progress
if (work_unit.lazy or write_output) and (directory is None or filename is None):
raise ValueError("can't write output to sharded fits without directory and filename provided.")
Expand Down Expand Up @@ -195,7 +198,7 @@ def _reproject_work_unit(

# Create a list of the correct WCS. We do this extraction once and reuse for all images.
if frame == "original":
wcs_list = work_unit.get_constituent_meta("original_wcs")
wcs_list = work_unit.get_constituent_meta("per_image_wcs")
elif frame == "ebd":
wcs_list = work_unit.get_constituent_meta("ebd_wcs")
else:
Expand Down Expand Up @@ -283,24 +286,25 @@ def _reproject_work_unit(
stack.append_image(new_layered_image, force_move=True)

if write_output:
# Create a copy of the WorkUnit to write the global metadata.
# We preserve the metgadata for the consituent images.
new_work_unit = copy(work_unit)
new_work_unit._per_image_indices = unique_obstime_indices
new_work_unit.wcs = common_wcs
new_work_unit.reprojected = True

hdul = new_work_unit.metadata_to_primary_header()
hdul = new_work_unit.metadata_to_hdul()
hdul.writeto(os.path.join(directory, filename))
else:
# Create a new WorkUnit with the new ImageStack and global WCS.
# We preserve the metgadata for the consituent images.
new_wunit = WorkUnit(
im_stack=stack,
config=work_unit.config,
wcs=common_wcs,
constituent_images=work_unit.get_constituent_meta("data_loc"),
per_image_wcs=work_unit._per_image_wcs,
per_image_ebd_wcs=work_unit.get_constituent_meta("ebd_wcs"),
per_image_indices=unique_obstime_indices,
geocentric_distances=work_unit.get_constituent_meta("geocentric_distance"),
reprojected=True,
org_image_meta=work_unit.org_img_meta,
)

return new_wunit
Expand Down Expand Up @@ -425,7 +429,7 @@ def _reproject_work_unit_in_parallel(
new_work_unit.wcs = common_wcs
new_work_unit.reprojected = True

hdul = new_work_unit.metadata_to_primary_header()
hdul = new_work_unit.metadata_to_hdul()
hdul.writeto(os.path.join(directory, filename))
else:
stack = ImageStack([])
Expand All @@ -446,17 +450,15 @@ def _reproject_work_unit_in_parallel(
# sort by the time_stamp
stack.sort_by_time()

# Add the imageStack to a new WorkUnit and return it.
# Add the imageStack to a new WorkUnit and return it. We preserve the metgadata
# for the consituent images.
new_wunit = WorkUnit(
im_stack=stack,
config=work_unit.config,
wcs=common_wcs,
constituent_images=work_unit.get_constituent_meta("data_loc"),
per_image_wcs=work_unit._per_image_wcs,
per_image_ebd_wcs=work_unit.get_constituent_meta("ebd_wcs"),
per_image_indices=unique_obstimes_indices,
geocentric_distances=work_unit.get_constituent_meta("geocentric_distances"),
reprojected=True,
org_image_meta=work_unit.org_img_meta,
)

return new_wunit
Expand Down Expand Up @@ -549,6 +551,7 @@ def reproject_lazy_work_unit(
if not result.result():
raise RuntimeError("one or more jobs failed.")

# We use new metadata for the new images and the same metadata for the original images.
new_work_unit = copy(work_unit)
new_work_unit._per_image_indices = unique_obstimes_indices
new_work_unit.wcs = common_wcs
Expand Down Expand Up @@ -591,6 +594,8 @@ def _validate_original_wcs(work_unit, indices, frame="original"):
else:
raise ValueError("Invalid projection frame provided.")

if len(original_wcs) == 0:
raise ValueError(f"No WCS found for frame {frame}")
if np.any(original_wcs) is None:
# find indices where the wcs is None
bad_indices = np.where(original_wcs == None)
Expand Down
Loading

0 comments on commit 236e225

Please sign in to comment.