Пример #1
0
def main(args, unit_test=False):
    """ Runs fluxing steps
    """
    import os
    import numpy as np

    from pypeit import fluxspec
    from pypeit.core import flux
    from pypeit.par import pypeitpar

    # Load the file
    spectrograph, config_lines, flux_dict = read_fluxfile(args.flux_file)

    # Parameters
    spectrograph_def_par = spectrograph.default_pypeit_par()
    par = pypeitpar.PypeItPar.from_cfg_lines(
        cfg_lines=spectrograph_def_par.to_config(), merge_with=config_lines)

    if unit_test:
        path = os.path.join(os.getenv('PYPEIT_DEV'), 'Cooked', 'Science')
        par['fluxcalib']['std_file'] = os.path.join(
            path, par['fluxcalib']['std_file'])
        for kk, spec1d_file, flux_file in zip(
                np.arange(len(flux_dict['spec1d_files'])),
                flux_dict['spec1d_files'], flux_dict['flux_files']):
            flux_dict['spec1d_files'][kk] = os.path.join(path, spec1d_file)
            flux_dict['flux_files'][kk] = os.path.join(path, flux_file)

    # Write the par to disk
    print("Writing the parameters to {}".format(args.par_outfile))
    par.to_config(args.par_outfile)

    # Instantiate
    FxSpec = fluxspec.instantiate_me(spectrograph,
                                     par['fluxcalib'],
                                     debug=args.debug)

    # Generate sensfunc??
    if par['fluxcalib']['std_file'] is not None:
        # Load standard
        _, _ = FxSpec.load_objs(par['fluxcalib']['std_file'], std=True)
        ## For echelle, the code will deal with the standard star in the ech_fluxspec.py
        #if not spectrograph.pypeline == 'Echelle':
        # Find the star
        _ = FxSpec.find_standard()
        # Sensitivity
        _ = FxSpec.generate_sensfunc()
        # Output
        _ = FxSpec.save_sens_dict(FxSpec.sens_dict,
                                  par['fluxcalib']['sensfunc'])
        # Show
        if args.plot:
            FxSpec.show_sensfunc()

    # Flux?
    if len(flux_dict) > 0:
        for spec1d_file, flux_file in zip(flux_dict['spec1d_files'],
                                          flux_dict['flux_files']):
            FxSpec.flux_science(spec1d_file)
            FxSpec.write_science(flux_file)
Пример #2
0
def main(args):
    """ Runs the 1d coadding steps
    """
    # Load the file
    config_lines, spec1dfiles, objids = read_coaddfile(args.coadd1d_file)
    # Append path for testing
    if args.test_spec_path is not None:
        spec1dfiles = [
            os.path.join(args.test_spec_path, ifile) for ifile in spec1dfiles
        ]
    # Read in spectrograph from spec1dfile header
    header = fits.getheader(spec1dfiles[0])

    # NOTE: This was some test code for Travis. Keep it around for now
    # in case we need to do this again. (KBW)
    #    try:
    #        header = fits.getheader(spec1dfiles[0])
    #    except Exception as e:
    #        raise Exception('{0}\n {1}\n {2}\n'.format(spec1dfiles[0], os.getcwd(),
    #                        os.getenv('TRAVIS_BUILD_DIR', default='None'))) from e

    spectrograph = load_spectrograph(header['PYP_SPEC'])

    # Parameters
    spectrograph_def_par = spectrograph.default_pypeit_par()
    par = pypeitpar.PypeItPar.from_cfg_lines(
        cfg_lines=spectrograph_def_par.to_config(), merge_with=config_lines)
    # Write the par to disk
    print("Writing the parameters to {}".format(args.par_outfile))
    par.to_config(args.par_outfile)
    sensfile = par['coadd1d']['sensfuncfile']
    coaddfile = par['coadd1d']['coaddfile']

    # Testing?
    if args.test_spec_path is not None:
        if sensfile is not None:
            sensfile = os.path.join(args.test_spec_path, sensfile)
        coaddfile = os.path.join(args.test_spec_path, coaddfile)

    if spectrograph.pypeline is 'Echelle' and sensfile is None:
        msgs.error(
            'You must specify set the sensfuncfile in the .coadd1d file for Echelle coadds'
        )

    # Instantiate
    coAdd1d = coadd1d.CoAdd1D.get_instance(spec1dfiles,
                                           objids,
                                           spectrograph=spectrograph,
                                           par=par['coadd1d'],
                                           sensfile=sensfile,
                                           debug=args.debug,
                                           show=args.show)
    # Run
    coAdd1d.run()
    # Save to file
    coAdd1d.save(coaddfile)
    msgs.info('Coadding complete')
Пример #3
0
def main(args):
    """ Runs the 1d coadding steps
    """
    # Load the file
    config_lines, spec1dfiles, objids = read_coaddfile(args.coadd1d_file)
    # Append path for testing
    if args.test_spec_path is not None:
        spec1dfiles = [os.path.join(args.test_spec_path, ifile) for ifile in spec1dfiles]
    # Read in spectrograph from spec1dfile header
    header = fits.getheader(spec1dfiles[0])

    # NOTE: This was some test code for Travis. Keep it around for now
    # in case we need to do this again. (KBW)
#    try:
#        header = fits.getheader(spec1dfiles[0])
#    except Exception as e:
#        raise Exception('{0}\n {1}\n {2}\n'.format(spec1dfiles[0], os.getcwd(),
#                        os.getenv('TRAVIS_BUILD_DIR', default='None'))) from e

    spectrograph = load_spectrograph(header['PYP_SPEC'])

    # Parameters
    spectrograph_def_par = spectrograph.default_pypeit_par()
    par = pypeitpar.PypeItPar.from_cfg_lines(cfg_lines=spectrograph_def_par.to_config(), merge_with=config_lines)
    # Write the par to disk
    print("Writing the parameters to {}".format(args.par_outfile))
    par.to_config(args.par_outfile)
    sensfile = par['coadd1d']['sensfuncfile']
    coaddfile = par['coadd1d']['coaddfile']
    # Testing?
    if args.test_spec_path is not None:
        if sensfile is not None:
            sensfile = os.path.join(args.test_spec_path, sensfile)
        coaddfile = os.path.join(args.test_spec_path, coaddfile)

    if spectrograph.pypeline is 'Echelle' and sensfile is None:
        msgs.error('You must specify set the sensfuncfile in the .coadd1d file for Echelle coadds')

    # TODO JFH I really dislike that the parsets are used to hold actually run time specific information and not
    # i.e. parameter defaults, or values of parameters. The problem is there is no other easy way to pass this information
    # in via a .coadd1d file, since the parsets parse in a simply way. Otherwise I have to waste time trying to parse
    # text files, whereas there are things like yaml and json that do this well already.

    # Instantiate
    coadd = coadd1d.CoAdd1D.get_instance(spec1dfiles, objids, sensfile=sensfile, par=par['coadd1d'],
                                       debug=args.debug, show=args.show)
    # Run
    coadd.run()
    # Save to file
    coadd.save(coaddfile)
    msgs.info('Coadding complete')
Пример #4
0
    def main(args):
        """ Runs fluxing steps
        """
        # Load the file
        config_lines, spec1dfiles, sensfiles_in = read_fluxfile(args.flux_file)
        # Read in spectrograph from spec1dfile header
        header = fits.getheader(spec1dfiles[0])
        spectrograph = load_spectrograph(header['PYP_SPEC'])

        # Parameters
        spectrograph_def_par = spectrograph.default_pypeit_par()
        par = pypeitpar.PypeItPar.from_cfg_lines(
            cfg_lines=spectrograph_def_par.to_config(),
            merge_with=config_lines)
        # Write the par to disk
        print("Writing the parameters to {}".format(args.par_outfile))
        par.to_config(args.par_outfile)

        # Chck the sizes of the inputs
        nspec = len(spec1dfiles)
        if len(sensfiles_in) == 1:
            # Use one sens file for each spec1d
            sensfiles = nspec * sensfiles_in
        elif len(sensfiles_in) == nspec:
            # One sensfile specified / spec1d
            sensfiles = sensfiles_in
        elif len(sensfiles_in
                 ) == 0 and par['fluxcalib']['use_archived_sens'] == True:
            # No sensfile specified, but an archived sensfunc can be used.
            sf_archive = SensFileArchive.get_instance(spectrograph.name)
            sensfiles = nspec * [
                sf_archive.get_archived_sensfile(spec1dfiles[0])
            ]
        else:
            msgs.error(
                'Invalid format for .flux file.' + msgs.newline() +
                'You must specify a single sensfile on the first line of the flux block,'
                + msgs.newline() +
                'or specify a  sensfile for every spec1dfile in the flux block,'
                + msgs.newline() +
                'or specify "use_archived_sens = True" to use an archived sensfile.'
                + msgs.newline() +
                'Run pypeit_flux_calib --help for information on the format')

        # Instantiate
        FxCalib = fluxcalibrate.FluxCalibrate.get_instance(
            spec1dfiles, sensfiles, par=par['fluxcalib'], debug=args.debug)
        msgs.info('Flux calibration complete')
        return 0
Пример #5
0
def main(args):
    """ Runs fluxing steps
    """
    # Load the file
    config_lines, spec1dfiles, sensfiles = read_fluxfile(args.flux_file)
    # Read in spectrograph from spec1dfile header
    header = fits.getheader(spec1dfiles[0])
    spectrograph = load_spectrograph(header['PYP_SPEC'])

    # Parameters
    spectrograph_def_par = spectrograph.default_pypeit_par()
    par = pypeitpar.PypeItPar.from_cfg_lines(
        cfg_lines=spectrograph_def_par.to_config(), merge_with=config_lines)
    # Write the par to disk
    print("Writing the parameters to {}".format(args.par_outfile))
    par.to_config(args.par_outfile)

    # Instantiate
    FxCalib = fluxcalibrate.FluxCalibrate.get_instance(spec1dfiles,
                                                       sensfiles,
                                                       par=par['fluxcalib'],
                                                       debug=args.debug)
    msgs.info('Flux calibration complete')
Пример #6
0
def main(args):
    """ Executes 2d coadding
    """

    # Load the file
    if args.file is not None:
        spectrograph, config_lines, spec2d_files = read_coadd2d_file(args.file)
        # Parameters
        # TODO: Shouldn't this reinstantiate the same parameters used in
        # the PypeIt run that extracted the objects?  Why are we not
        # just passing the pypeit file?
        spectrograph_def_par = spectrograph.default_pypeit_par()
        par = par.PypeItPar.from_cfg_lines(
            cfg_lines=spectrograph_def_par.to_config(),
            merge_with=config_lines)
    elif args.obj is not None:
        # TODO: This needs to define the science path
        spec2d_files = glob.glob('./Science/spec2d_*' + args.obj + '*')
        head0 = fits.getheader(spec2d_files[0])
        spectrograph_name = head0['SPECTROG']
        spectrograph = load_spectrograph(spectrograph_name)
        par = spectrograph.default_pypeit_par()
    else:
        msgs.error(
            'You must either input a coadd2d file with --file or an object name with --obj'
        )

    # If detector was passed as an argument override whatever was in the coadd2d_file
    if args.det is not None:
        msgs.info("Restricting reductions to detector={}".format(args.det))
        par['rdx']['detnum'] = int(args.det)

    # Get headers and base names
    spec1d_files = [
        files.replace('spec2d', 'spec1d') for files in spec2d_files
    ]
    head1d = fits.getheader(spec1d_files[0])
    head2d = fits.getheader(spec2d_files[0])
    if args.basename is None:
        filename = os.path.basename(spec2d_files[0])
        basename = filename.split('_')[1]
    else:
        basename = args.basename

    # Write the par to disk
    par_outfile = basename + '_coadd2d.par'
    print("Writing the parameters to {}".format(par_outfile))
    par.to_config(par_outfile)

    # Now run the coadds

    skysub_mode = head2d['SKYSUB']
    ir_redux = True if 'DIFF' in skysub_mode else False

    # Print status message
    msgs_string = 'Reducing target {:s}'.format(basename) + msgs.newline()
    msgs_string += 'Performing coadd of frames reduce with {:s} imaging'.format(
        skysub_mode)
    msgs_string += msgs.newline(
    ) + 'Combining frames in 2d coadd:' + msgs.newline()
    for file in spec2d_files:
        msgs_string += '{0:s}'.format(os.path.basename(file)) + msgs.newline()
    msgs.info(msgs_string)

    # TODO: This needs to be added to the parameter list for rdx
    redux_path = os.getcwd()
    master_dirname = os.path.basename(head2d['PYPMFDIR']) + '_coadd'
    master_dir = os.path.join(redux_path, master_dirname)

    # Make the new Master dir
    if not os.path.isdir(master_dir):
        msgs.info(
            'Creating directory for Master output: {0}'.format(master_dir))
        os.makedirs(master_dir)

    # Instantiate the sci_dict
    sci_dict = OrderedDict()  # This needs to be ordered
    sci_dict['meta'] = {}
    sci_dict['meta']['vel_corr'] = 0.
    sci_dict['meta']['ir_redux'] = ir_redux

    # Find the detectors to reduce
    detectors = PypeIt.select_detectors(detnum=par['rdx']['detnum'],
                                        ndet=spectrograph.ndet)
    if len(detectors) != spectrograph.ndet:
        msgs.warn('Not reducing detectors: {0}'.format(' '.join([
            str(d) for d in set(np.arange(spectrograph.ndet)) - set(detectors)
        ])))

    # Loop on detectors
    for det in detectors:
        msgs.info("Working on detector {0}".format(det))
        sci_dict[det] = {}

        # Read in the images stacks and other clibration/meta data for this detector
        stack_dict = coadd2d.load_coadd2d_stacks(spec2d_files, det)

        sci_dict[det]['sciimg'], sci_dict[det]['sciivar'], sci_dict[det]['skymodel'], \
                sci_dict[det]['objmodel'], sci_dict[det]['ivarmodel'], sci_dict[det]['outmask'], \
                sci_dict[det]['specobjs'] \
                        = coadd2d.extract_coadd2d(stack_dict, master_dir, det, ir_redux=ir_redux,
                                                  par=par, show=args.show, show_peaks=args.peaks,
                                                  std=args.std, samp_fact=args.samp_fact)

    # Make the new Science dir
    # TODO: This needs to be defined by the user
    scipath = os.path.join(redux_path, 'Science_coadd')
    if not os.path.isdir(scipath):
        msgs.info('Creating directory for Science output: {0}'.format(scipath))
        os.makedirs(scipath)

    # Save the results
    save.save_all(sci_dict, stack_dict['master_key_dict'], master_dir,
                  spectrograph, head1d, head2d, scipath, basename)
Пример #7
0
def main(args):
    """ Executes 2d coadding
    """

    # Load the file
    if args.file is not None:
        spectrograph, config_lines, spec2d_files = read_coadd2d_file(args.file)
        # Parameters
        spectrograph_def_par = spectrograph.default_pypeit_par()
        par = pypeitpar.PypeItPar.from_cfg_lines(cfg_lines=spectrograph_def_par.to_config(),
                                                 merge_with=config_lines)
    elif args.obj is not None:
        spec2d_files = glob.glob('./Science/spec2d_' + args.obj + '*')
        head0 = fits.getheader(spec2d_files[0])
        spectrograph_name = head0['SPECTROG']
        spectrograph = load_spectrograph(spectrograph_name)
        par = spectrograph.default_pypeit_par()
    else:
        msgs.error('You must either input a coadd2d file with --file or an object name with --obj')

    # If detector was passed as an argument override whatever was in the coadd2d_file
    if args.det is not None:
        msgs.info("Restricting reductions to detector={}".format(args.det))
        par['rdx']['detnum'] = int(args.det)

    # Write the par to disk
    print("Writing the parameters to {}".format(args.par_outfile))
    par.to_config(args.par_outfile)

    # Now run the coadds
    spec1d_files = [files.replace('spec2d', 'spec1d') for files in spec2d_files]
    head1d = fits.getheader(spec1d_files[0])
    head2d = fits.getheader(spec2d_files[0])
    filename = os.path.basename(spec2d_files[0])
    basename = filename.split('_')[1]

    skysub_mode = head2d['SKYSUB']
    ir_redux = True if 'DIFF' in skysub_mode else False

    # Print status message
    msgs_string = 'Reducing target {:s}'.format(basename) + msgs.newline()
    msgs_string += 'Performing coadd of frames reduce with {:s} imaging'.format(skysub_mode) + msgs.newline()
    msgs_string += 'Combining frames in 2d coadd:' + msgs.newline()
    for file in spec2d_files:
        msgs_string += '{0:s}'.format(os.path.basename(file)) + msgs.newline()
    msgs.info(msgs_string)

    redux_path = './'
    master_dirname = os.path.basename(head2d['PYPMFDIR'])+'/'
    master_dir = os.path.join(redux_path,os.path.normpath(master_dirname) + '_coadd/')

    # Make the new master dir and Science dir
    if os.path.exists(master_dir):
        msgs.info("The following directory already exists:"+msgs.newline()+master_dir)
    else:
        os.mkdir(master_dir)

    # Instantiate the sci_dict
    sci_dict = OrderedDict()  # This needs to be ordered
    sci_dict['meta'] = {}
    sci_dict['meta']['vel_corr'] = 0.
    sci_dict['meta']['ir_redux'] = ir_redux

    # Find the detectors to reduce
    detectors = select_detectors(par, spectrograph)
    if len(detectors) != spectrograph.ndet:
        msgs.warn('Not reducing detectors: {0}'.format(' '.join([str(d) for d in
        set(np.arange(spectrograph.ndet)) - set(detectors)])))

    # Loop on detectors
    for det in detectors:
        msgs.info("Working on detector {0}".format(det))
        sci_dict[det] = {}

        # Read in the images stacks and other clibration/meta data for this detector
        stack_dict = coadd2d.load_coadd2d_stacks(spec2d_files, det)

        sci_dict[det]['sciimg'], sci_dict[det]['sciivar'], sci_dict[det]['skymodel'], \
        sci_dict[det]['objmodel'], sci_dict[det]['ivarmodel'], sci_dict[det]['outmask'], \
        sci_dict[det]['specobjs'] = coadd2d.extract_coadd2d(stack_dict, master_dir, ir_redux=ir_redux, par=par,
                                                            show=args.show, show_peaks=args.peaks, std=args.std)

    # Make the science directory, and write outputs to disk
    master_key_dict = stack_dict['master_key_dict']
    scipath = redux_path + 'Science_coadd'
    if os.path.exists(scipath):
        msgs.info("The following directory already exists:" + msgs.newline() + scipath)
    else:
        os.mkdir(scipath)
    save.save_all(sci_dict, master_key_dict, master_dir, spectrograph, head1d, head2d, scipath, basename)