astropy / specreduce

Tools for the reduction of spectroscopic observations from Optical and NIR instruments
https://specreduce.readthedocs.io
62 stars 38 forks source link

compatibility issues between KosmosTrace and Background #101

Closed kecnry closed 2 years ago

kecnry commented 2 years ago

modified script to reproduce from the boxcar_extraction.ipynb notebook:

from pathlib import Path
from zipfile import ZipFile

from astropy.io import fits
from astropy.table import Table
from astropy.utils.data import download_file

from jwst import datamodels

from specreduce.extract import BoxcarExtract
from specreduce.tracing import FlatTrace, KosmosTrace
from specreduce.background import Background

import tempfile

zipped_datapath = Path(download_file('https://stsci.box.com/shared/static/qdj79y7rv99wuui2hot0l3kg5ohq0ah9.zip', cache=True))
data_dir = Path(tempfile.gettempdir())
with ZipFile(zipped_datapath, 'r') as sample_data_zip:
    sample_data_zip.extractall(data_dir)

s2dfile = str(data_dir / "nirspec_fssim_d1_s2d.fits")
s2d = datamodels.open(s2dfile)
image = s2d.slits[0].data

# this works:
trace = FlatTrace(image, 27)
bg = Background.two_sided(image, trace, separation=5, width=2)

# this raises an error that the background regions overlap (even though they shouldn't)
trace = KosmosTrace(image, 27)
bg = Background.two_sided(image, trace, separation=5, width=2)

# this raises an integer error because the trace goes out of bounds of the image:
trace = KosmosTrace(image, 27)
bg = Background.two_sided(image, trace, separation=6, width=2)

The integer error is a pretty easy fix (we can avoid the mask by acting on trace.data instead of trace), but we have to decide whether the background trace going out of bounds should raise an error, raise an error only if the entire boxcar falls outside the image, or continues without an error (and just includes pixels in the boxcar if within range and zeros or nans otherwise).

kecnry commented 2 years ago

with the same setup, the following also raises the background regions overlap error (@ojustino - can this scenario be included in #125?):

bg = Background(image, FlatTrace(image, 30), width=2)
ojustino commented 2 years ago

I also saw that the overlap error affected more than just backgrounds created with KosmosTrace, so I don't find this unexpected. I don't get an error when I run this line on that PR's branch, though I do have to enclose the FlatTrace in a list.

ojustino commented 2 years ago

We have resolved this issue by merging #125 and #127.