Skip to content

Commit

Permalink
delete drizzled_model and update median model meta from the first image
Browse files Browse the repository at this point in the history
  • Loading branch information
mcara committed Oct 21, 2024
1 parent 0a686a1 commit e0d4a8d
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions jwst/outlier_detection/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ def median_without_resampling(input_models,
in_memory = not input_models._on_disk
ngroups = len(input_models)

if save_intermediate_results:
# create an empty image model for the median data
median_model = datamodels.ImageModel(None)

with input_models:
for i in range(len(input_models)):

Expand All @@ -128,6 +132,10 @@ def median_without_resampling(input_models,
err_computer = MedianComputer(input_shape, in_memory, buffer_size, dtype)
else:
err_computer = None
if save_intermediate_results:
# update median model's meta with meta from the first model:
median_model.update(drizzled_model)
median_model.meta.wcs = median_wcs

weight_threshold = compute_weight_threshold(weight, maskpt)
drizzled_data[weight < weight_threshold] = np.nan
Expand All @@ -137,6 +145,7 @@ def median_without_resampling(input_models,
err_computer.append(drizzled_err, i)

input_models.shelve(drizzled_model, i, modify=False)
del drizzled_model

# Perform median combination on set of drizzled mosaics
median_data = computer.evaluate()
Expand All @@ -147,13 +156,11 @@ def median_without_resampling(input_models,

if save_intermediate_results:
# Save median model to fits
median_model = datamodels.ImageModel(median_data)
median_model.data = median_data
if return_error:
median_model.err = median_err
median_model.update(drizzled_model)
median_model.meta.wcs = median_wcs
_fileio.save_median(median_model, make_output_path)
del drizzled_model


if return_error:
return median_data, median_wcs, median_err
Expand Down Expand Up @@ -219,6 +226,10 @@ def median_with_resampling(input_models,
indices_by_group = list(input_models.group_indices.values())
ngroups = len(indices_by_group)

if save_intermediate_results:
# create an empty image model for the median data
median_model = datamodels.ImageModel(None)

with input_models:
for i, indices in enumerate(indices_by_group):

Expand All @@ -238,13 +249,18 @@ def median_with_resampling(input_models,
err_computer = MedianComputer(input_shape, in_memory, buffer_size, dtype)
else:
err_computer = None
if save_intermediate_results:
# update median model's meta with meta from the first model:
median_model.update(drizzled_model)
median_model.meta.wcs = median_wcs

weight_threshold = compute_weight_threshold(drizzled_model.wht, maskpt)
drizzled_model.data[drizzled_model.wht < weight_threshold] = np.nan
computer.append(drizzled_model.data, i)
if return_error:
drizzled_model.err[drizzled_model.wht < weight_threshold] = np.nan
err_computer.append(drizzled_model.err, i)
del drizzled_model

# Perform median combination on set of drizzled mosaics
median_data = computer.evaluate()
Expand All @@ -255,15 +271,12 @@ def median_with_resampling(input_models,

if save_intermediate_results:
# Save median model to fits
median_model = datamodels.ImageModel(median_data)
median_model.data = median_data
if return_error:
median_model.err = median_err
median_model.update(drizzled_model)
median_model.meta.wcs = median_wcs
# drizzled model already contains asn_id
make_output_path = partial(make_output_path, asn_id=None)
_fileio.save_median(median_model, make_output_path)
del drizzled_model

if return_error:
return median_data, median_wcs, median_err
Expand Down

0 comments on commit e0d4a8d

Please sign in to comment.