Source code for pyreduce.instruments.JWST_NIRISS
"""
Handles instrument specific info for the HARPS spectrograph
Mostly reading data from the header
"""
import logging
import os.path
from astropy import units as q
from astropy.io import fits
from astropy.time import Time
from ..common import Instrument
logger = logging.getLogger(__name__)
[docs]
class JWST_NIRISS(Instrument):
[docs]
def split_observation(self, fname, channel):
hdu = fits.open(fname)
dirname = os.path.dirname(fname)
fname = os.path.basename(fname)
header = hdu[0].header
if "mjd-obs" not in header:
if len(header["DATE-OBS"]) <= 10:
time = header["DATE-OBS"] + "T" + header["TIME-OBS"]
else:
time = header["DATE-OBS"]
header["MJD-OBS"] = Time(time).mjd
header2 = fits.Header()
header2["EXTNAME"] = "SCI"
shape = hdu["SCI"].data.shape
nframes = shape[0]
ngroups = shape[1]
if nframes == 1 and ngroups == 1:
return [os.path.join(dirname, fname)]
data = hdu["SCI"].data.reshape((-1, *shape[-2:]))
bias = data[0]
primary = fits.PrimaryHDU(header=header)
files = []
os.makedirs(os.path.join(dirname, "split"), exist_ok=True)
for i in range(1, nframes * ngroups):
data[i] - bias
bias = data[i]
fname_this = os.path.join(dirname, "split", f"pyreduce_{i}_{fname}")
header["MJD-OBS"] += header["TFRAME"] * q.s.to(q.day)
secondary = fits.ImageHDU(data=data, header=header2)
hdu_this = fits.HDUList([primary, secondary])
hdu_this.writeto(fname_this, overwrite=True)
files.append(fname_this)
hdu.close()
return files
[docs]
def sort_files(self, input_dir, target, night, channel, **kwargs):
files = super().sort_files(input_dir, target, night, channel, **kwargs)
for i, (_k, file) in enumerate(files):
files_split = []
for f in file["science"]:
files_split += self.split_observation(f, channel)
files[i][1]["science"] = files_split
return files
[docs]
def get_wavecal_filename(self, header, channel, **kwargs):
"""Get the filename of the wavelength calibration config file"""
cwd = os.path.dirname(__file__)
fname = f"wavecal_{channel}_2D.npz"
fname = os.path.join(cwd, fname)
return fname