예제 #1
0
def main(args):

    import os
    import sys
    import astropy.io.fits as fits
    from pypeit import masterframe
    from pypeit.spectrographs.util import load_spectrograph
    from pypeit.core import parse
    from pypeit.core import gui
    from pypeit.core.wavecal import waveio, templates
    from pypeit.wavecalib import WaveCalib
    from pypeit import slittrace
    from pypeit.images.buildimage import ArcImage

    # Load the MasterArc file
    if os.path.exists(args.arc_file):
        arcfil = args.arc_file
    else:
        try:
            arcfil = "Masters/{0:s}".format(args.arc_file)
        except FileNotFoundError:
            print("Could not find MasterArc file.")
            sys.exit()
    msarc = ArcImage.from_file(arcfil)

    mdir = msarc.head0['MSTRDIR']
    mkey = msarc.head0['MSTRKEY']

    # Load the spectrograph
    specname = msarc.head0['PYP_SPEC']
    spec = load_spectrograph(specname)
    par = spec.default_pypeit_par()['calibrations']['wavelengths']

    # Get the lamp list
    if args.lamps == '':
        lamplist = par['lamps']
        if lamplist is None:
            print("ERROR :: Cannot determine the lamps")
            sys.exit()
    else:
        lamplist = args.lamps.split(",")
    par['lamps'] = lamplist

    # Load the slits
    slits = slittrace.SlitTraceSet.from_file(args.slits_file)
    # Reset the mask
    slits.mask = slits.mask_init

    # Check if a solution exists
    solnname = os.path.join(mdir,
                            masterframe.construct_file_name(WaveCalib, mkey))
    wv_calib = waveio.load_wavelength_calibration(solnname) if os.path.exists(
        solnname) else None

    # Load the MasterFrame (if it exists and is desired)?
    wavecal = WaveCalib(msarc,
                        slits,
                        spec,
                        par,
                        binspectral=slits.binspec,
                        det=args.det,
                        master_key=mkey,
                        msbpm=msarc.fullmask)
    arccen, arc_maskslit = wavecal.extract_arcs()

    # Launch the identify window
    arcfitter = gui.identify.initialise(arccen,
                                        slit=int(args.slit),
                                        par=par,
                                        wv_calib_all=wv_calib,
                                        wavelim=[args.wmin, args.wmax],
                                        nonlinear_counts=spec.nonlinear_counts(
                                            msarc.detector))
    final_fit = arcfitter.get_results()

    # Ask the user if they wish to store the result in PypeIt calibrations
    if final_fit['rms'] < args.rmstol:
        ans = ''
        while ans != 'y' and ans != 'n':
            ans = input(
                "Would you like to store this wavelength solution in the archive? (y/n): "
            )
        if ans == 'y':
            gratname = fits.getheader(
                msarc.head0['F1'])[spec.meta['dispname']['card']].replace(
                    "/", "_")
            dispangl = "UNKNOWN"
            templates.pypeit_identify_record(final_fit, slits.binspec,
                                             specname, gratname, dispangl)
            print("Your wavelength solution has been stored")
            print("Please consider sending your solution to the PypeIt team!")
    else:
        print(
            "Final fit RMS: {0:0.3f} is larger than the allowed tolerance: {1:0.3f}"
            .format(final_fit['rms'], args.rmstol))
        print(
            "Set the variable --rmstol on the command line to allow a more flexible RMS tolerance"
        )
        ans = ''
        while ans != 'y' and ans != 'n':
            ans = input("Would you like to store the line IDs? (y/n): ")
        if ans == 'y':
            arcfitter.save_IDs()
예제 #2
0
def main(args):

    import os
    import sys
    from pypeit import masterframe
    from pypeit.spectrographs.util import load_spectrograph
    from pypeit.core.gui.identify import Identify
    from pypeit.core.wavecal import waveio
    from pypeit.wavecalib import WaveCalib
    from pypeit import slittrace
    from pypeit.images.buildimage import ArcImage

    # Load the MasterArc file
    if os.path.exists(args.arc_file):
        arcfil = args.arc_file
    else:
        try:
            arcfil = "Masters/{0:s}".format(args.arc_file)
        except FileNotFoundError:
            print("Could not find MasterArc file.")
            sys.exit()
    msarc = ArcImage.from_file(arcfil)

    mdir = msarc.head0['MSTRDIR']
    mkey = msarc.head0['MSTRKEY']

    # Load the spectrograph
    specname = msarc.head0['PYP_SPEC']
    spec = load_spectrograph(specname)
    par = spec.default_pypeit_par()['calibrations']['wavelengths']

    # Get the lamp list
    if args.lamps is None:
        lamplist = par['lamps']
        if lamplist is None:
            print("ERROR :: Cannot determine the lamps")
            sys.exit()
    else:
        lamplist = args.lamps.split(",")
    par['lamps'] = lamplist

    # Load the slits
    slits = slittrace.SlitTraceSet.from_file(args.slits_file)
    # Reset the mask
    slits.mask = slits.mask_init

    # Check if a solution exists
    solnname = os.path.join(mdir,
                            masterframe.construct_file_name(WaveCalib, mkey))
    wv_calib = waveio.load_wavelength_calibration(
        solnname) if os.path.exists(solnname) and args.solution else None

    # Load the MasterFrame (if it exists and is desired)?
    wavecal = WaveCalib(msarc,
                        slits,
                        spec,
                        par,
                        binspectral=slits.binspec,
                        det=args.det,
                        master_key=mkey,
                        msbpm=msarc.fullmask)
    arccen, arc_maskslit = wavecal.extract_arcs(slitIDs=[args.slit])

    # Launch the identify window
    arcfitter = Identify.initialise(arccen,
                                    slits,
                                    slit=int(args.slit),
                                    par=par,
                                    wv_calib_all=wv_calib,
                                    wavelim=[args.wmin, args.wmax],
                                    nonlinear_counts=spec.nonlinear_counts(
                                        msarc.detector))
    final_fit = arcfitter.get_results()

    # Ask the user if they wish to store the result in PypeIt calibrations
    arcfitter.store_solution(final_fit,
                             mdir,
                             slits.binspec,
                             rmstol=args.rmstol,
                             specname=specname)
예제 #3
0
파일: identify.py 프로젝트: tbowers7/PypeIt
    def main(args):

        import os
        import sys

        import numpy as np

        from pypeit import msgs
        from pypeit import masterframe
        from pypeit.spectrographs.util import load_spectrograph
        from pypeit.core.gui.identify import Identify
        from pypeit.core.wavecal import waveio
        from pypeit.wavecalib import BuildWaveCalib, WaveCalib
        from pypeit import slittrace
        from pypeit.images.buildimage import ArcImage

        # Load the MasterArc file
        msarc = ArcImage.from_file(args.arc_file)

        # Load the spectrograph
        spec = load_spectrograph(msarc.PYP_SPEC)
        par = spec.default_pypeit_par()['calibrations']['wavelengths']

        # Get the lamp list
        if args.lamps is None:
            lamps = par['lamps']
            if lamps is None or lamps == ['use_header']:
                msgs.error('Cannot determine the lamps; use --lamps argument')
        else:
            lamps = args.lamps.split(",")
        par['lamps'] = lamps

        # Load the slits
        slits = slittrace.SlitTraceSet.from_file(args.slits_file)
        # Reset the mask
        slits.mask = slits.mask_init

        # Check if a solution exists
        solnname = masterframe.construct_file_name(WaveCalib,
                                                   msarc.master_key,
                                                   master_dir=msarc.master_dir)
        wv_calib = waveio.load_wavelength_calibration(solnname) \
                        if os.path.exists(solnname) and args.solution else None

        # Load the MasterFrame (if it exists and is desired).  Bad-pixel mask
        # set to any flagged pixel in MasterArc.
        wavecal = BuildWaveCalib(msarc,
                                 slits,
                                 spec,
                                 par,
                                 lamps,
                                 binspectral=slits.binspec,
                                 det=args.det,
                                 master_key=msarc.master_key,
                                 msbpm=msarc.select_flag())
        arccen, arc_maskslit = wavecal.extract_arcs(slitIDs=[args.slit])

        # Launch the identify window
        # TODO -- REMOVE THIS HACK
        try:
            nonlinear_counts = msarc.detector.nonlinear_counts()
        except AttributeError:
            nonlinear_counts = None
        arcfitter = Identify.initialise(arccen,
                                        lamps,
                                        slits,
                                        slit=int(args.slit),
                                        par=par,
                                        wv_calib_all=wv_calib,
                                        wavelim=[args.wmin, args.wmax],
                                        nonlinear_counts=nonlinear_counts,
                                        pxtoler=args.pixtol,
                                        test=args.test,
                                        fwhm=args.fwhm,
                                        sigdetect=args.sigdetect,
                                        specname=spec.name,
                                        y_log=not args.linear)

        # Testing?
        if args.test:
            return arcfitter
        final_fit = arcfitter.get_results()

        # Build here to avoid circular import
        #  Note:  This needs to be duplicated in test_scripts.py
        # Wavecalib (wanted when dealing with multiple detectors, eg. GMOS)
        if 'WaveFit' in arcfitter._fitdict.keys():
            waveCalib = WaveCalib(
                nslits=1,
                wv_fits=np.atleast_1d(arcfitter._fitdict['WaveFit']),
                arc_spectra=np.atleast_2d(arcfitter.specdata).T,
                spat_ids=np.atleast_1d(int(arcfitter._spatid)),
                PYP_SPEC=msarc.PYP_SPEC,
                lamps=','.join(lamps))
        else:
            waveCalib = None

        # Ask the user if they wish to store the result in PypeIt calibrations
        arcfitter.store_solution(final_fit,
                                 slits.binspec,
                                 wvcalib=waveCalib,
                                 rmstol=args.rmstol,
                                 force_save=args.force_save)
예제 #4
0
파일: identify.py 프로젝트: Tang-SL/PypeIt
def main(args):

    import os
    import sys
    import numpy as np
    from pypeit import masterframe
    from pypeit.spectrographs.util import load_spectrograph
    from pypeit.core.gui.identify import Identify
    from pypeit.core.wavecal import waveio
    from pypeit.wavecalib import BuildWaveCalib, WaveCalib
    from pypeit import slittrace
    from pypeit.images.buildimage import ArcImage

    # Load the MasterArc file
    if os.path.exists(args.arc_file):
        arcfil = args.arc_file
    else:
        try:
            arcfil = "Masters/{0:s}".format(args.arc_file)
        except FileNotFoundError:
            print("Could not find MasterArc file.")
            sys.exit()
    msarc = ArcImage.from_file(arcfil)

    mdir = msarc.head0['MSTRDIR']
    mkey = msarc.head0['MSTRKEY']

    # Load the spectrograph
    specname = msarc.head0['PYP_SPEC']
    spec = load_spectrograph(specname)
    par = spec.default_pypeit_par()['calibrations']['wavelengths']

    # Get the lamp list
    if args.lamps is None:
        lamplist = par['lamps']
        if lamplist is None:
            print("ERROR :: Cannot determine the lamps")
            sys.exit()
    else:
        lamplist = args.lamps.split(",")
    par['lamps'] = lamplist

    # Load the slits
    slits = slittrace.SlitTraceSet.from_file(args.slits_file)
    # Reset the mask
    slits.mask = slits.mask_init

    # Check if a solution exists
    solnname = os.path.join(mdir,
                            masterframe.construct_file_name(WaveCalib, mkey))
    wv_calib = waveio.load_wavelength_calibration(
        solnname) if os.path.exists(solnname) and args.solution else None

    # Load the MasterFrame (if it exists and is desired)?
    wavecal = BuildWaveCalib(msarc,
                             slits,
                             spec,
                             par,
                             binspectral=slits.binspec,
                             det=args.det,
                             master_key=mkey,
                             msbpm=msarc.fullmask)
    arccen, arc_maskslit = wavecal.extract_arcs(slitIDs=[args.slit])

    # Launch the identify window
    arcfitter = Identify.initialise(arccen,
                                    slits,
                                    slit=int(args.slit),
                                    par=par,
                                    wv_calib_all=wv_calib,
                                    wavelim=[args.wmin, args.wmax],
                                    nonlinear_counts=spec.nonlinear_counts(
                                        msarc.detector),
                                    pxtoler=args.pixtol,
                                    test=args.test,
                                    fwhm=args.fwhm)
    # Testing?
    if args.test:
        return arcfitter
    final_fit = arcfitter.get_results()

    # Build here to avoid circular import
    #  Note:  This needs to be duplicated in test_scripts.py
    # Wavecalib (wanted when dealing with multiple detectors, eg. GMOS)
    if 'WaveFit' in arcfitter._fitdict.keys():
        waveCalib = WaveCalib(
            nslits=1,
            wv_fits=np.atleast_1d(arcfitter._fitdict['WaveFit']),
            arc_spectra=np.atleast_2d(arcfitter.specdata).T,
            spat_ids=np.atleast_1d(arcfitter._slit),
            PYP_SPEC=specname,
        )
    else:
        waveCalib = None

    # Ask the user if they wish to store the result in PypeIt calibrations
    arcfitter.store_solution(final_fit,
                             mdir,
                             slits.binspec,
                             wvcalib=waveCalib,
                             rmstol=args.rmstol,
                             force_save=args.force_save)
예제 #5
0
def main(args):

    import os
    import sys
    import astropy.io.fits as fits
    from pypeit.masterframe import MasterFrame
    from pypeit.spectrographs.util import load_spectrograph
    from pypeit.core import parse
    from pypeit.core.gui import identify as gui_identify
    from pypeit.core.wavecal import waveio, templates
    from pypeit.wavecalib import WaveCalib
    from pypeit import edgetrace
    from pypeit.images import pypeitimage

    # Load the MasterArc file
    if os.path.exists(args.file):
        arcfil = args.file
    else:
        try:
            arcfil = "Masters/{0:s}".format(args.file)
        except FileNotFoundError:
            print("Could not find MasterArc file.")
            sys.exit()
    msarc = pypeitimage.PypeItImage.from_file(arcfil)

    mdir = msarc.head0['MSTRDIR']
    mkey = msarc.head0['MSTRKEY']

    # Load the spectrograph
    specname = msarc.head0['PYP_SPEC']
    spec = load_spectrograph(specname)
    par = spec.default_pypeit_par()['calibrations']['wavelengths']

    # Get the lamp list
    if args.lamps is None:
        lamplist = par['lamps']
        if lamplist is None:
            print("ERROR :: Cannot determine the lamps")
            sys.exit()
    else:
        lamplist = args.lamps.split(",")
    par['lamps'] = lamplist

    # Load the tslits_dict
    trc_file = os.path.join(mdir, MasterFrame.construct_file_name('Edges', mkey, file_format='fits.gz'))
    tslits_dict = edgetrace.EdgeTraceSet.from_file(trc_file).convert_to_tslits_dict()

    # Check if a solution exists
    solnname = os.path.join(mdir, MasterFrame.construct_file_name('WaveCalib', mkey, file_format='json'))
    wv_calib = waveio.load_wavelength_calibration(solnname) if os.path.exists(solnname) else None

    # Load the MasterFrame (if it exists and is desired)?
    wavecal = WaveCalib(msarc, tslits_dict, spec, par)
    arccen, arc_maskslit = wavecal.extract_arcs()

    binspec, binspat = parse.parse_binning(spec.get_meta_value(msarc.head0['F1'], 'binning'))

    # Launch the identify window
    arcfitter = gui_identify.initialise(arccen, slit=int(args.slit), par=par, wv_calib_all=wv_calib)
    final_fit = arcfitter.get_results()

    # Ask the user if they wish to store the result in PypeIt calibrations
    ans = ''
    while ans != 'y' and ans != 'n':
        ans = input("Would you like to store this wavelength solution in the archive? (y/n):")
    if ans == 'y' and final_fit['rms'] < 0.1:
        gratname = fits.getheader(msarc.head0['F1'])[spec.meta['dispname']['card']].replace("/", "_")
        dispangl = "UNKNOWN"
        templates.pypeit_identify_record(final_fit, binspec, specname, gratname, dispangl, outdir=mdir)
        print("Your wavelength solution has been stored")
        print("Please consider sending your solution to the PypeIt team!")