From 496dd4f570538158a8f27ea36ca6abcb53b8a63c Mon Sep 17 00:00:00 2001 From: David Law Date: Sun, 6 Oct 2024 14:48:46 -0400 Subject: [PATCH] Add median filtering to background spectrum --- jwst/master_background/master_background_step.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/jwst/master_background/master_background_step.py b/jwst/master_background/master_background_step.py index 1ea83855ee..f88b76d2e8 100755 --- a/jwst/master_background/master_background_step.py +++ b/jwst/master_background/master_background_step.py @@ -2,6 +2,7 @@ import numpy as np from stdatamodels.properties import merge_tree from stdatamodels.jwst import datamodels +from scipy.signal import medfilt from jwst.datamodels import ModelContainer from jwst.stpipe import record_step_status @@ -25,6 +26,7 @@ class MasterBackgroundStep(Step): save_background = boolean(default=False) # Save computed master background force_subtract = boolean(default=False) # Force subtracting master background output_use_model = boolean(default=True) + median_kernel = integer(default=1) """ def process(self, input): @@ -51,6 +53,10 @@ def process(self, input): has already been applied and, if so, the master background step is skipped. If set to True, the step logic is bypassed and the master background is subtracted. + median_kernel : integer, optional + Optional user-supplied kernel with which to moving-median boxcar filter the master background + spectrum. Must be an odd integer. + Returns ------- result : `~jwst.datamodels.ImageModel`, `~jwst.datamodels.IFUImageModel`, `~jwst.datamodels.ModelContainer` @@ -140,6 +146,12 @@ def process(self, input): exptime_key='exposure_time', ) + # If requested, apply a moving-median boxcar filter to the master background spectrum + if (self.median_kernel > 1): + self.log.info('Applying moving-median boxcar of width %i',self.median_kernel) + master_background.spec[0].spec_table['surf_bright'] = medfilt( + master_background.spec[0].spec_table['surf_bright'], kernel_size=[self.median_kernel]) + background_data.close() result = ModelContainer()