Skip to content

Commit 431aac6

Browse files
committed
add saving shower_model
1 parent 9c605ed commit 431aac6

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

jwst/straylight/straylight.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import numpy as np
1010
import logging
11-
11+
from jwst import datamodels
1212
from stdatamodels.jwst.datamodels import dqflags
1313
from astropy.stats import sigma_clipped_stats as scs
1414
from astropy.convolution import convolve_fft, Gaussian2DKernel
@@ -230,7 +230,7 @@ def correct_xartifact(input_model, modelpars):
230230
return input_model
231231

232232
def clean_showers(input_model, allregions, shower_plane=3, shower_x_stddev=18.0, shower_y_stddev=5.0,
233-
shower_low_reject=0.1, shower_high_reject=99.9):
233+
shower_low_reject=0.1, shower_high_reject=99.9, save_shower_model=False):
234234
"""
235235
Corrects the MIRI MRS data for straylight produced by residual cosmic ray showers.
236236
@@ -298,7 +298,44 @@ def clean_showers(input_model, allregions, shower_plane=3, shower_x_stddev=18.0,
298298
# Subtract the shower model from the original data
299299
input_model.data = input_model.data - shower_model
300300

301+
if save_shower_model:
302+
output_shower_model = _make_intermediate_model(input_model, shower_model)
303+
else:
304+
output_shower_model = None
305+
301306
# Delete our temporary working copy of the data
302307
del usedata
303308

304-
return input_model
309+
return input_model, output_shower_model
310+
311+
312+
def _make_intermediate_model(input_model, intermediate_data):
313+
"""
314+
Make a data model to contain intermediate outputs.
315+
316+
The output model type depends on the shape of the input
317+
intermediate data.
318+
319+
Parameters
320+
----------
321+
input_model : `~jwst.datamodel.JwstDataModel`
322+
The input data.
323+
intermediate_data : array-like
324+
The intermediate data to save.
325+
326+
Returns
327+
-------
328+
intermediate_model : ~jwst.datamodel.JwstDataModel`
329+
A model containing only the intermediate data and top-level
330+
metadata matching the input.
331+
"""
332+
if intermediate_data.ndim == 4:
333+
intermediate_model = datamodels.RampModel(data=intermediate_data)
334+
elif intermediate_data.ndim == 3:
335+
intermediate_model = datamodels.CubeModel(data=intermediate_data)
336+
else:
337+
intermediate_model = datamodels.ImageModel(data=intermediate_data)
338+
339+
# Copy metadata from input model
340+
intermediate_model.update(input_model)
341+
return intermediate_model

jwst/straylight/straylight_step.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class StraylightStep (Step):
2121
shower_y_stddev = float(default=5) # Y standard deviation for shower model
2222
shower_low_reject = float(default=0.1) # Low percentile of pixels to reject
2323
shower_high_reject = float(default=99.9) # High percentile of pixels to reject
24+
save_shower_model = boolean(default=False) # Save the shower model
2425
""" # noqa: E501
2526

2627
reference_file_types = ['mrsxartcorr', 'regions']
@@ -56,9 +57,19 @@ def process(self, input):
5657
self.regions_name = self.get_reference_file(input_model, 'regions')
5758
with datamodels.RegionsModel(self.regions_name) as f:
5859
allregions = f.regions
59-
result = straylight.clean_showers(result, allregions, self.shower_plane,
60-
self.shower_x_stddev, self.shower_y_stddev,
61-
self.shower_low_reject, self.shower_high_reject)
60+
result, output_shower_model = straylight.clean_showers(result, allregions,
61+
self.shower_plane,
62+
self.shower_x_stddev,
63+
self.shower_y_stddev,
64+
self.shower_low_reject,
65+
self.shower_high_reject,
66+
self.save_shower_model)
67+
if self.save_shower_model and output_shower_model:
68+
shower_path = self.make_output_path(basepath=input_model.meta.filename,
69+
suffix="shower_model")
70+
self.log.info(f"Saving shower model file {shower_path}")
71+
output_shower_model.save(shower_path)
72+
output_shower_model.close()
6273

6374
result.meta.cal_step.straylight = 'COMPLETE'
6475

@@ -69,6 +80,7 @@ def process(self, input):
6980
self.log.warning('Straylight step will be skipped')
7081
result.meta.cal_step.straylight = 'SKIPPED'
7182

83+
7284
return result
7385

7486

0 commit comments

Comments
 (0)