示例#1
0
def telluric_correct(coadd: str,
                     output_path: str,
                     spectrograph: str,
                     user_config_lines: List[str],
                     debug: bool = False,
                     plot: bool = False):
    """
    method to telluric correct one coadded file
    """
    coadd_path = os.path.join(output_path, 'Science', coadd)
    spectrograph = load_spectrograph(spectrograph)
    par = spectrograph.default_pypeit_par()

    par['telluric']['objmodel'] = 'poly'
    par['telluric']['fit_wv_min_max'] = [5500, 11000]
    # maybe somehow choose between poly and exp??????? look at median
    par['telluric']['model'] = 'exp'
    par['telluric']['polyorder'] = 8

    if par['telluric']['telgridfile'] is None:
        if par['sensfunc']['IR']['telgridfile'] is not None:
            par['telluric']['telgridfile'] = par['sensfunc']['IR'][
                'telgridfile']

    par = pypeitpar.PypeItPar.from_cfg_lines(cfg_lines=par.to_config(),
                                             merge_with=user_config_lines)

    # Parse the output filename
    outfile = os.path.join(output_path, 'Science',
                           os.path.splitext(coadd)[0] + '_tellcorr.fits')
    modelfile = os.path.join(output_path, 'Science',
                             os.path.splitext(coadd)[0] + '_tellmodel.fits')

    try:
        TelPoly = telluric.poly_telluric(
            coadd_path,
            par['telluric']['telgridfile'],
            modelfile,
            outfile,
            z_obj=par['telluric']['redshift'],
            func=par['telluric']['func'],
            model=par['telluric']['model'],
            polyorder=par['telluric']['polyorder'],
            fit_wv_min_max=par['telluric']['fit_wv_min_max'],
            mask_lyman_a=par['telluric']['mask_lyman_a'],
            delta_coeff_bounds=par['telluric']['delta_coeff_bounds'],
            minmax_coeff_bounds=par['telluric']['minmax_coeff_bounds'],
            only_orders=par['telluric']['only_orders'],
            debug_init=debug,
            disp=debug,
            debug=debug,
            show=plot)
    except ValueError:
        print(f"ERROR!! Telluric correction of {coadd} FAILED!")
def apply_tell_from_file(z_obj,
                         stackfilename,
                         tell_method='qso',
                         instrument='NIRES',
                         telgridfile=None,
                         polyorder=3,
                         fit_region_min=[9200.0],
                         fit_region_max=[9700.0],
                         mask_lyman_a=True,
                         show=True,
                         debug=False):

    # output names
    if tell_method == 'poly':
        telloutfile = stackfilename.replace('.fits', '_polytellmodel.fits')
        outfile = stackfilename.replace('.fits', '_polytellcorr.fits')
    else:
        telloutfile = stackfilename.replace('.fits', '_tellmodel.fits')
        outfile = stackfilename.replace('.fits', '_tellcorr.fits')

    if tell_method == 'qso':
        pca_file = os.path.join(basedir,
                                'Dropbox/PypeIt_Redux/qso_pca_1200_3100.pckl')
        # run telluric.qso_telluric to get the final results
        # TODO: add other modes here
        TelQSO = telluric.qso_telluric(stackfilename,
                                       telgridfile,
                                       pca_file,
                                       z_obj,
                                       telloutfile,
                                       outfile,
                                       create_bal_mask=None,
                                       disp=disp,
                                       debug=debug,
                                       show=show)
    elif tell_method == 'poly':
        TelPoly = telluric.poly_telluric(stackfilename,
                                         telgridfile,
                                         telloutfile,
                                         outfile,
                                         z_obj=z_obj,
                                         polyorder=polyorder,
                                         fit_region_min=fit_region_min,
                                         fit_region_max=fit_region_max,
                                         func='legendre',
                                         model='exp',
                                         mask_lyman_a=mask_lyman_a,
                                         debug_init=debug,
                                         debug=debug,
                                         show=show)
示例#3
0
    def main(args):
        """
        Executes telluric correction.
        """

        import os

        from astropy.io import fits

        from pypeit import msgs
        from pypeit import io
        from pypeit.par import pypeitpar
        from pypeit.spectrographs.util import load_spectrograph
        from pypeit.core import telluric

        # Determine the spectrograph
        header = fits.getheader(args.spec1dfile)
        spectrograph = load_spectrograph(header['PYP_SPEC'])
        spectrograph_def_par = spectrograph.default_pypeit_par()

        # If the .tell file was passed in read it and overwrite default parameters
        par = spectrograph_def_par if args.tell_file is None else \
                pypeitpar.PypeItPar.from_cfg_lines(cfg_lines=spectrograph_def_par.to_config(),
                                                   merge_with=io.read_tellfile(args.tell_file))

        # If args was provided override defaults. Note this does undo .tell file
        if args.objmodel is not None:
            par['telluric']['objmodel'] = args.objmodel
        if args.pca_file is not None:
            par['telluric']['pca_file'] = args.pca_file
        if args.redshift is not None:
            par['telluric']['redshift'] = args.redshift
        if args.tell_grid is not None:
            par['telluric']['telgridfile'] = args.tell_grid

        if par['telluric']['telgridfile'] is None:
            if par['sensfunc']['IR']['telgridfile'] is not None:
                par['telluric']['telgridfile'] = par['sensfunc']['IR'][
                    'telgridfile']
            else:
                par['telluric'][
                    'telgridfile'] = 'TelFit_MaunaKea_3100_26100_R20000.fits'
                msgs.warn(
                    f"No telluric grid file given. Using {par['telluric']['telgridfile']}."
                )

        # Checks
        if par['telluric']['telgridfile'] is None:
            msgs.error('A file with the telluric grid must be provided.')
        elif not os.path.isfile(
                os.path.join(data.Paths.telgrid,
                             par['telluric']['telgridfile'])):
            msgs.error(
                f"{par['telluric']['telgridfile']} does not exist.  Check your "
                f"installation.")

        # Write the par to disk
        # TODO: Make it optional to write this file?  Is the relevant metadata
        # saved to the main output file?
        msgs.info(
            f'Writing the telluric fitting parameters to {args.par_outfile}')
        par['telluric'].to_config(args.par_outfile,
                                  section_name='telluric',
                                  include_descr=False)

        # Parse the output filename
        outfile = (os.path.basename(args.spec1dfile)).replace(
            '.fits', '_tellcorr.fits')
        modelfile = (os.path.basename(args.spec1dfile)).replace(
            '.fits', '_tellmodel.fits')
        msgs.info(f'Telluric-corrected spectrum will be saved to: {outfile}.')
        msgs.info(f'Best-fit telluric model will be saved to: {modelfile}.')

        # Run the telluric fitting procedure.
        if par['telluric']['objmodel'] == 'qso':
            # run telluric.qso_telluric to get the final results
            TelQSO = telluric.qso_telluric(
                args.spec1dfile,
                par['telluric']['telgridfile'],
                par['telluric']['pca_file'],
                par['telluric']['redshift'],
                modelfile,
                outfile,
                npca=par['telluric']['npca'],
                pca_lower=par['telluric']['pca_lower'],
                pca_upper=par['telluric']['pca_upper'],
                bounds_norm=par['telluric']['bounds_norm'],
                tell_norm_thresh=par['telluric']['tell_norm_thresh'],
                only_orders=par['telluric']['only_orders'],
                bal_wv_min_max=par['telluric']['bal_wv_min_max'],
                maxiter=par['telluric']['maxiter'],
                debug_init=args.debug,
                disp=args.debug,
                debug=args.debug,
                show=args.plot)
        elif par['telluric']['objmodel'] == 'star':
            TelStar = telluric.star_telluric(
                args.spec1dfile,
                par['telluric']['telgridfile'],
                modelfile,
                outfile,
                star_type=par['telluric']['star_type'],
                star_mag=par['telluric']['star_mag'],
                star_ra=par['telluric']['star_ra'],
                star_dec=par['telluric']['star_dec'],
                func=par['telluric']['func'],
                model=par['telluric']['model'],
                polyorder=par['telluric']['polyorder'],
                only_orders=par['telluric']['only_orders'],
                mask_abs_lines=par['telluric']['mask_abs_lines'],
                delta_coeff_bounds=par['telluric']['delta_coeff_bounds'],
                minmax_coeff_bounds=par['telluric']['minmax_coeff_bounds'],
                maxiter=par['telluric']['maxiter'],
                debug_init=args.debug,
                disp=args.debug,
                debug=args.debug,
                show=args.plot)
        elif par['telluric']['objmodel'] == 'poly':
            TelPoly = telluric.poly_telluric(
                args.spec1dfile,
                par['telluric']['telgridfile'],
                modelfile,
                outfile,
                z_obj=par['telluric']['redshift'],
                func=par['telluric']['func'],
                model=par['telluric']['model'],
                polyorder=par['telluric']['polyorder'],
                fit_wv_min_max=par['telluric']['fit_wv_min_max'],
                mask_lyman_a=par['telluric']['mask_lyman_a'],
                delta_coeff_bounds=par['telluric']['delta_coeff_bounds'],
                minmax_coeff_bounds=par['telluric']['minmax_coeff_bounds'],
                only_orders=par['telluric']['only_orders'],
                maxiter=par['telluric']['maxiter'],
                debug_init=args.debug,
                disp=args.debug,
                debug=args.debug,
                show=args.plot)
        else:
            msgs.error(
                "Object model is not supported yet. Must be 'qso', 'star', or 'poly'."
            )
示例#4
0
def main(args):
    """
    Executes telluric correction.
    """

    # Determine the spectrograph
    header = fits.getheader(args.spec1dfile)
    spectrograph = load_spectrograph(header['PYP_SPEC'])
    spectrograph_def_par = spectrograph.default_pypeit_par()

    # If the .tell file was passed in read it and overwrite default parameters
    if args.tell_file is not None:
        cfg_lines = read_tellfile(args.tell_file)
        par = pypeitpar.PypeItPar.from_cfg_lines(
            cfg_lines=spectrograph_def_par.to_config(), merge_with=cfg_lines)
    else:
        par = spectrograph_def_par

    # If args was provided override defaults. Note this does undo .tell file
    if args.objmodel is not None:
        par['tellfit']['objmodel'] = args.objmodel
    if args.pca_file is not None:
        par['tellfit']['pca_file'] = args.pca_file
    if args.redshift is not None:
        par['tellfit']['redshift'] = args.redshift

    if args.tell_grid is not None:
        par['tellfit']['tell_grid'] = args.tell_grid
    elif par['sensfunc']['IR']['telgridfile'] is not None:
        par['tellfit']['tell_grid'] = par['sensfunc']['IR']['telgridfile']
    else:
        msgs.warn('No telluric grid file given. Using {:}'.format(
            'TelFit_MaunaKea_3100_26100_R20000.fits'))
        par['tellfit']['tell_grid'] = resource_filename(
            'pypeit', '/data/telluric/TelFit_MaunaKea_3100_26100_R20000.fits')

    # Write the par to disk
    print("Writing the parameters to {}".format(args.par_outfile))
    par['tellfit'].to_config('telluric.par',
                             section_name='tellfit',
                             include_descr=False)

    # Parse the output filename
    outfile = (os.path.basename(args.spec1dfile)).replace(
        '.fits', '_tellcorr.fits')
    modelfile = (os.path.basename(args.spec1dfile)).replace(
        '.fits', '_tellmodel.fits')

    # Run the telluric fitting procedure.
    if par['tellfit']['objmodel'] == 'qso':
        # run telluric.qso_telluric to get the final results
        TelQSO = telluric.qso_telluric(
            args.spec1dfile,
            par['tellfit']['tell_grid'],
            par['tellfit']['pca_file'],
            par['tellfit']['redshift'],
            modelfile,
            outfile,
            npca=par['tellfit']['npca'],
            pca_lower=par['tellfit']['pca_lower'],
            pca_upper=par['tellfit']['pca_upper'],
            bounds_norm=par['tellfit']['bounds_norm'],
            tell_norm_thresh=par['tellfit']['tell_norm_thresh'],
            only_orders=par['tellfit']['only_orders'],
            bal_wv_min_max=par['tellfit']['bal_wv_min_max'],
            debug_init=args.debug,
            disp=args.debug,
            debug=args.debug,
            show=args.plot)
    elif par['tellfit']['objmodel'] == 'star':
        TelStar = telluric.star_telluric(
            args.spec1dfile,
            par['tellfit']['tell_grid'],
            modelfile,
            outfile,
            star_type=par['tellfit']['star_type'],
            star_mag=par['tellfit']['star_mag'],
            star_ra=par['tellfit']['star_ra'],
            star_dec=par['tellfit']['star_dec'],
            func=par['tellfit']['func'],
            model=par['tellfit']['model'],
            polyorder=par['tellfit']['polyorder'],
            only_orders=par['tellfit']['only_orders'],
            mask_abs_lines=par['tellfit']['mask_abs_lines'],
            delta_coeff_bounds=par['tellfit']['delta_coeff_bounds'],
            minmax_coeff_bounds=par['tellfit']['minmax_coeff_bounds'],
            debug_init=args.debug,
            disp=args.debug,
            debug=args.debug,
            show=args.plot)
    elif par['tellfit']['objmodel'] == 'poly':
        TelPoly = telluric.poly_telluric(
            args.spec1dfile,
            par['tellfit']['tell_grid'],
            modelfile,
            outfile,
            z_obj=par['tellfit']['redshift'],
            func=par['tellfit']['func'],
            model=par['tellfit']['model'],
            polyorder=par['tellfit']['polyorder'],
            fit_wv_min_max=par['tellfit']['fit_wv_min_max'],
            mask_lyman_a=par['tellfit']['mask_lyman_a'],
            delta_coeff_bounds=par['tellfit']['delta_coeff_bounds'],
            minmax_coeff_bounds=par['tellfit']['minmax_coeff_bounds'],
            only_orders=par['tellfit']['only_orders'],
            debug_init=args.debug,
            disp=args.debug,
            debug=args.debug,
            show=args.plot)
    else:
        msgs.error(
            "Object model is not supported yet. Please choose one of 'qso', 'star', 'poly'."
        )