"""
Script to measure and correct for flexure in multi-slit data.
.. include common links, assuming primary doc root is up one directory
.. include:: ../include/links.rst
"""
import os
from IPython import embed
import numpy as np
from pypeit import log
from pypeit import inputfiles
from pypeit.spectrographs.util import load_spectrograph
from pypeit.par import pypeitpar
from pypeit.core import flexure
from pypeit.scripts import scriptbase
# TODO: Maybe not a good idea to name this script the same as the
# flexure.MultiSlitFlexure class, but it is technically okay...
[docs]
class MultiSlitFlexure(scriptbase.ScriptBase):
[docs]
@classmethod
def get_parser(cls, width=None):
parser = super().get_parser(description='Calculate and apply flexure corrections for 1D '
'spectra produced by PypeIt.',
width=width, formatter=scriptbase.SmartFormatter)
parser.add_argument("flex_file", type=str,
help="R|File to guide flexure corrections for this multi-slit mode."
" This file must have the following format: \n\n"
"F|flexure read\n"
"F| filename\n"
"F| spec1dfile1\n"
"F| spec1dfile2\n"
"F| ... \n"
"F|flexure end\n"
"\n\n")
parser.add_argument("outroot", type=str,
help='Output fileroot for the flexure fits saved as FITS.')
parser.add_argument("--clobber", default=True,
action="store_true", help="Clobber output files")
parser.add_argument("--debug", default=False,
action="store_true", help="show debug plots?")
return parser
[docs]
@classmethod
def main(cls, args):
from astropy.io import fits
# Initialize the log
cls.init_log(args)
# Load the file
flexFile = inputfiles.FlexureFile.from_file(args.flex_file)
# Read in spectrograph from spec1dfile header
header = fits.getheader(flexFile.filenames[0])
spectrograph = load_spectrograph(header['PYP_SPEC'], pypeit_fits=True)
# Parameters
spectrograph_def_par = spectrograph.default_pypeit_par()
par = pypeitpar.PypeItPar.from_cfg_lines(
cfg_lines=spectrograph_def_par.to_config(),
merge_with=(flexFile.cfg_lines,))
# Loop to my loop
for filename in flexFile.filenames:
# Instantiate
mdFlex = flexure.MultiSlitFlexure(s1dfile=filename)
# Initalize
log.info("Setup")
mdFlex.init(spectrograph, par['flexure'])
# INITIAL SKY LINE STUFF
log.info("Measuring sky lines")
mdFlex.measure_sky_lines()
# FIT SURFACES
log.info("Fitting the surface")
mdFlex.fit_mask_surfaces()
# Apply
log.info("Applying flexure correction")
mdFlex.update_fit()
# REFIT FOR QA PLOTS
log.info("Generate QA")
mask = header['TARGET'].strip()
fnames = header['FILENAME'].split('.')
root = mask+'_'+fnames[2]
mdFlex.qa_plots('./', root)
# Write
log.info("Write to disk")
mdFlex.to_file(args.outroot+root+'.fits', overwrite=args.clobber)
# Apply??
print("All done!!")