Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JP-3790 add residual fringe correction to extract1d table #9073

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions jwst/extract_1d/extract_1d_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ class Extract1dStep(Step):

ifu_rfcorr : bool
Switch to select whether or not to apply a 1d residual fringe correction
for MIRI MRS IFU spectra. Default is False.

for MIRI MRS IFU spectra. Default is True.
ifu_set_srctype : str
For MIRI MRS IFU data override srctype and set it to either POINT or EXTENDED.

Expand Down Expand Up @@ -172,7 +171,7 @@ class Extract1dStep(Step):
center_xy = float_list(min=2, max=2, default=None) # IFU extraction x/y center
ifu_autocen = boolean(default=False) # Auto source centering for IFU point source data.
bkg_sigma_clip = float(default=3.0) # background sigma clipping threshold for IFU
ifu_rfcorr = boolean(default=False) # Apply 1d residual fringe correction
ifu_rfcorr = boolean(default=True) # Apply 1d residual fringe correction
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One concern with changing this to True is that it would then require failsafe cases for all non-MRS uses of extract1d, which may have unintended consequences. May be easier to keep as False, since the only purpose of the flag now is to print a message saying how the flag isn't necessary any longer.

ifu_set_srctype = option("POINT", "EXTENDED", None, default=None) # user-supplied source type
ifu_rscale = float(default=None, min=0.5, max=3) # Radius in terms of PSF FWHM to scale extraction radii
ifu_covar_scale = float(default=1.0) # Scaling factor to apply to errors to account for IFU cube covariance
Expand Down
73 changes: 44 additions & 29 deletions jwst/extract_1d/ifu.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@

ifu_rfcorr : bool
Switch to select whether or not to apply a 1d residual fringe correction
for MIRI MRS IFU spectra. Default is False.
for MIRI MRS IFU spectra. Default is True.

ifu_rscale: float
For MRS IFU data a value for changing the extraction radius. The value provided is the number of PSF
Expand Down Expand Up @@ -106,7 +106,8 @@
else:
log.info(f"Source type = {source_type}")

output_model = datamodels.MultiSpecModel()
#output_model = datamodels.MultiSpecModel()
output_model = datamodels.MrsSpecModel()
output_model.update(input_model, only="PRIMARY")

slitname = input_model.meta.exposure.type
Expand Down Expand Up @@ -183,21 +184,21 @@

# Apply residual fringe to the flux array
try:
temp_flux = rfutils.fit_residual_fringes_1d(temp_flux, wavelength, channel=channel,
temp_flux_rf = rfutils.fit_residual_fringes_1d(temp_flux, wavelength, channel=channel,

Check warning on line 187 in jwst/extract_1d/ifu.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/ifu.py#L187

Added line #L187 was not covered by tests
dichroic_only=False, max_amp=None)
except Exception:
log.info("Flux residual fringe correction failed- skipping.")

# Apply residual fringe to the surf_bright array
try:
surf_bright = rfutils.fit_residual_fringes_1d(surf_bright, wavelength, channel=channel,
surf_bright_rf = rfutils.fit_residual_fringes_1d(surf_bright, wavelength, channel=channel,

Check warning on line 194 in jwst/extract_1d/ifu.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/ifu.py#L194

Added line #L194 was not covered by tests
dichroic_only=False, max_amp=None)
except Exception:
log.info("Surf bright residual fringe correction failed- skipping.")

# Apply residual fringe to the background array
try:
background = rfutils.fit_residual_fringes_1d(background, wavelength, channel=channel,
background_rf = rfutils.fit_residual_fringes_1d(background, wavelength, channel=channel,

Check warning on line 201 in jwst/extract_1d/ifu.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/ifu.py#L201

Added line #L201 was not covered by tests
dichroic_only=False, max_amp=None)
except Exception:
log.info("Background residual fringe correction failed- skipping.")
Expand All @@ -208,17 +209,20 @@
"the flux will not be correct.")
pixel_solid_angle = 1.

# Convert flux from MJy / steradian to Jy.
# Convert flux from MJy / steradian to Jy
flux = temp_flux * pixel_solid_angle * 1.e6
flux_rf = temp_flux_rf * pixel_solid_angle * 1.e6

Check warning on line 214 in jwst/extract_1d/ifu.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/ifu.py#L214

Added line #L214 was not covered by tests
f_var_poisson *= (pixel_solid_angle ** 2 * 1.e12) # (MJy / sr)**2 --> Jy**2
f_var_rnoise *= (pixel_solid_angle ** 2 * 1.e12) # (MJy / sr)**2 --> Jy**2
f_var_flat *= (pixel_solid_angle ** 2 * 1.e12) # (MJy / sr)**2 --> Jy**2
# surf_bright and background were computed above
del temp_flux
del temp_flux_rf

Check warning on line 220 in jwst/extract_1d/ifu.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/ifu.py#L220

Added line #L220 was not covered by tests
error = np.sqrt(f_var_poisson + f_var_rnoise + f_var_flat)
sb_error = np.sqrt(sb_var_poisson + sb_var_rnoise + sb_var_flat)
berror = np.sqrt(b_var_poisson + b_var_rnoise + b_var_flat)
spec_dtype = datamodels.SpecModel().spec_table.dtype
spec_dtype = datamodels.MrsSpecModel().mrs_spec_table.dtype

Check warning on line 224 in jwst/extract_1d/ifu.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/ifu.py#L224

Added line #L224 was not covered by tests


# If we only used the Poisson variance array as a vehicle to pass through
# non-differentiated errors, clear it again here so that only the total
Expand Down Expand Up @@ -250,31 +254,38 @@
list(
zip(wavelength, flux, error, f_var_poisson, f_var_rnoise, f_var_flat,
surf_bright, sb_error, sb_var_poisson, sb_var_rnoise, sb_var_flat,
dq, background, berror, b_var_poisson, b_var_rnoise, b_var_flat, npixels)
dq, background, berror, b_var_poisson, b_var_rnoise, b_var_flat, npixels,
flux_rf, surf_bright_rf, background_rf)
),
dtype=spec_dtype
)

spec = datamodels.SpecModel(spec_table=otab)
if ((input_model.meta.instrument.name == 'MIRI') & (extract_params['ifu_rfcorr'] is True)):
spec = datamodels.MrsSpecModel(mrs_spec_table=otab)

Check warning on line 263 in jwst/extract_1d/ifu.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/ifu.py#L262-L263

Added lines #L262 - L263 were not covered by tests
else:
spec = datamodels.SpecModel(spec_table=otab)

Check warning on line 265 in jwst/extract_1d/ifu.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/ifu.py#L265

Added line #L265 was not covered by tests
spec.meta.wcs = spec_wcs.create_spectral_wcs(ra, dec, wavelength)
spec.spec_table.columns['wavelength'].unit = 'um'
spec.spec_table.columns['flux'].unit = "Jy"
spec.spec_table.columns['flux_error'].unit = "Jy"
spec.spec_table.columns['flux_var_poisson'].unit = "Jy^2"
spec.spec_table.columns['flux_var_rnoise'].unit = "Jy^2"
spec.spec_table.columns['flux_var_flat'].unit = "Jy^2"
spec.spec_table.columns['surf_bright'].unit = "MJy/sr"
spec.spec_table.columns['sb_error'].unit = "MJy/sr"
spec.spec_table.columns['sb_var_poisson'].unit = "(MJy/sr)^2"
spec.spec_table.columns['sb_var_rnoise'].unit = "(MJy/sr)^2"
spec.spec_table.columns['sb_var_flat'].unit = "(MJy/sr)^2"
spec.spec_table.columns['background'].unit = "MJy/sr"
spec.spec_table.columns['bkgd_error'].unit = "MJy/sr"
spec.spec_table.columns['bkgd_var_poisson'].unit = "(MJy/sr)^2"
spec.spec_table.columns['bkgd_var_rnoise'].unit = "(MJy/sr)^2"
spec.spec_table.columns['bkgd_var_flat'].unit = "(MJy/sr)^2"
spec.mrs_spec_table.columns['wavelength'].unit = 'um'
spec.mrs_spec_table.columns['flux'].unit = "Jy"
spec.mrs_spec_table.columns['flux_error'].unit = "Jy"
spec.mrs_spec_table.columns['flux_var_poisson'].unit = "Jy^2"
spec.mrs_spec_table.columns['flux_var_rnoise'].unit = "Jy^2"
spec.mrs_spec_table.columns['flux_var_flat'].unit = "Jy^2"
spec.mrs_spec_table.columns['surf_bright'].unit = "MJy/sr"
spec.mrs_spec_table.columns['sb_error'].unit = "MJy/sr"
spec.mrs_spec_table.columns['sb_var_poisson'].unit = "(MJy/sr)^2"
spec.mrs_spec_table.columns['sb_var_rnoise'].unit = "(MJy/sr)^2"
spec.mrs_spec_table.columns['sb_var_flat'].unit = "(MJy/sr)^2"
spec.mrs_spec_table.columns['background'].unit = "MJy/sr"
spec.mrs_spec_table.columns['bkgd_error'].unit = "MJy/sr"
spec.mrs_spec_table.columns['bkgd_var_poisson'].unit = "(MJy/sr)^2"
spec.mrs_spec_table.columns['bkgd_var_rnoise'].unit = "(MJy/sr)^2"
spec.mrs_spec_table.columns['bkgd_var_flat'].unit = "(MJy/sr)^2"
spec.mrs_spec_table.columns['rf_flux'].unit = "Jy"
spec.mrs_spec_table.columns['rf_surf_bright'].unit = "MJy/sr"
spec.mrs_spec_table.columns['rf_background'].unit = "MJy/sr"

Check warning on line 285 in jwst/extract_1d/ifu.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/ifu.py#L267-L285

Added lines #L267 - L285 were not covered by tests
spec.slit_ra = ra
spec.slit_dec = dec

if slitname is not None and slitname != "ANY":
spec.name = slitname

Expand Down Expand Up @@ -305,10 +316,14 @@
radius = radius_match[i]
apcorr.find_apcorr_func(i, radius)

apcorr.apply(spec.spec_table)

output_model.spec.append(spec)
apcorr.apply(spec.mrs_spec_table)

Check warning on line 319 in jwst/extract_1d/ifu.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/ifu.py#L319

Added line #L319 was not covered by tests

#output_model.spec.append(spec)
output_model = spec
print(spec.mrs_spec_table['FLUX'])
print(spec.mrs_spec_table['RF_FLUX'])
print(spec.mrs_spec_table['RF_SURF_BRIGHT'])
print(spec.mrs_spec_table['RF_BACKGROUND'])

Check warning on line 326 in jwst/extract_1d/ifu.py

View check run for this annotation

Codecov / codecov/patch

jwst/extract_1d/ifu.py#L322-L326

Added lines #L322 - L326 were not covered by tests
# See output_model.spec[0].meta.wcs instead.
output_model.meta.wcs = None

Expand Down
Loading