def main(name, specpath, refpath, outdir, maskpath, suffix):
    targ = spectrum.read_hires_fits(specpath, maskfile=maskpath)
    ref = spectrum.read_fits(refpath)

    # create diagnostic file
    outdir = os.path.join(outdir, name)
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    filepath = os.path.join(outdir, name+suffix+'_spec.h5')
    f = h5py.File(filepath, 'w')

    shifted = shift(targ, ref, store=f)
    shifted.to_hdf(f)
    # get wavelength limits
    w_min = shifted.w[0]
    w_max = shifted.w[-1]
    ref_trunc = ref.cut(w_min, w_max)
    f.create_dataset('s_ref', data=ref_trunc.s)
    f.create_dataset('serr_ref', data=ref_trunc.serr)
    # store unshifted spectrum
    f.create_dataset('s_unshifted', data=targ.s)
    f.create_dataset('serr_unshifted', data=targ.serr)
    f.create_dataset('w_unshifted', data=targ.w)

    # store metadata
    f.attrs['cps_name'] = name
    f.attrs['obs'] = os.path.basename(specpath)
    f.attrs['ref'] = os.path.basename(refpath)

    f.close()
示例#2
0
def main(name, specpath, refpath, outdir, maskpath, suffix):
    targ = spectrum.read_hires_fits(specpath, maskfile=maskpath)
    ref = spectrum.read_fits(refpath)

    # create diagnostic file
    outdir = os.path.join(outdir, name)
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    filepath = os.path.join(outdir, name + suffix + '_spec.h5')
    f = h5py.File(filepath, 'w')

    shifted = shift(targ, ref, store=f)
    shifted.to_hdf(f)
    # get wavelength limits
    w_min = shifted.w[0]
    w_max = shifted.w[-1]
    ref_trunc = ref.cut(w_min, w_max)
    f.create_dataset('s_ref', data=ref_trunc.s)
    f.create_dataset('serr_ref', data=ref_trunc.serr)
    # store unshifted spectrum
    f.create_dataset('s_unshifted', data=targ.s)
    f.create_dataset('serr_unshifted', data=targ.serr)
    f.create_dataset('w_unshifted', data=targ.w)

    # store metadata
    f.attrs['cps_name'] = name
    f.attrs['obs'] = os.path.basename(specpath)
    f.attrs['ref'] = os.path.basename(refpath)

    f.close()
def main():
    # Shift the references onto the NSO wavlength scale
    spectra_dir = os.path.join(SPECMATCHDIR, 'spectra/')
    shifted_dir = os.path.join(SPECMATCHDIR, 'shifted_spectra/')

    if not os.path.exists(shifted_dir):
        os.mkdir(shifted_dir)

    # Save base
    r = REFERENCES[0]
    infile = os.path.join(spectra_dir, r[0] + '.fits')
    base_ref = spectrum.read_fits(infile)
    base_w = base_ref.w
    outfile = os.path.join(shifted_dir, r[0] + '_adj.fits')
    copyfile(infile, outfile)

    # Shift each spectrum in order
    for r in REFERENCES[1:]:
        print("Shifting spectrum {0} onto reference {1}".format(r[0], r[3]))

        reffile = os.path.join(shifted_dir, r[3] + '_adj.fits')
        ref_spec = spectrum.read_fits(reffile)

        maskfile = os.path.join(SPECMATCHDIR, 'hires_telluric_mask.csv')

        # Shift each chip
        chips = ['b', 'r', 'i']
        shifted = []
        for c in chips:
            infile = os.path.join(spectra_dir, c + r[0] + '.fits')
            outfile = os.path.join(shifted_dir, c + r[0] + '_adj.fits')
            targ_unshifted = spectrum.read_hires_fits(infile,
                                                      maskfile=maskfile)

            shift_data = {}

            targ_shifted = shift.shift(targ_unshifted,
                                       ref_spec,
                                       store=shift_data)

            shift.save_shift_to_fits(outfile,
                                     targ_shifted,
                                     targ_unshifted,
                                     shift_data,
                                     clobber=True)

            shifted.append(targ_shifted)

        # Flatten spectrum and save
        print("Flattening spectra into single file")
        outfile = os.path.join(shifted_dir, r[0] + '_adj.fits')
        flattened = spectrum.Spectrum.combine_spectra(shifted, base_w)
        flattened.to_fits(outfile)
def main(args):
    filename = os.path.splitext(args.input_file)[0]
    infile = os.path.join(args.dir, filename + '.fits')

    spec = spectrum.read_hires_fits(infile)

    d_spec = degrade_spec(spec, args.R)

    suffix = "_R={0:d}".format(args.R)
    outfile = os.path.join(args.dir, filename + suffix + '.fits')

    # Save degraded spectrum
    d_spec.to_hires_fits(outfile, clobber=True)
def main(args):
    filename = os.path.splitext(args.input_file)[0]
    infile = os.path.join(args.dir, filename + '.fits')

    spec = spectrum.read_hires_fits(infile)

    d_spec = degrade_spec(spec, args.R)

    suffix = "_R={0:d}".format(args.R)
    outfile = os.path.join(args.dir, filename + suffix + '.fits')

    # Save degraded spectrum
    d_spec.to_hires_fits(outfile, clobber=True)
def main():
    # Shift the references onto the NSO wavlength scale
    spectra_dir = os.path.join(SPECMATCHDIR, 'spectra/')
    shifted_dir = os.path.join(SPECMATCHDIR, 'shifted_spectra/')

    if not os.path.exists(shifted_dir):
        os.mkdir(shifted_dir)

    # Save base
    r = REFERENCES[0]
    infile = os.path.join(spectra_dir, r[0]+'.fits')
    base_ref = spectrum.read_fits(infile)
    base_w = base_ref.w
    outfile = os.path.join(shifted_dir, r[0]+'_adj.fits')
    copyfile(infile, outfile)

    # Shift each spectrum in order
    for r in REFERENCES[1:]:
        print("Shifting spectrum {0} onto reference {1}".format(
            r[0], r[3]))

        reffile = os.path.join(shifted_dir, r[3]+'_adj.fits')
        ref_spec = spectrum.read_fits(reffile)

        maskfile = os.path.join(SPECMATCHDIR, 'hires_telluric_mask.csv')

        # Shift each chip
        chips = ['b', 'r', 'i']
        shifted = []
        for c in chips:
            infile = os.path.join(spectra_dir, c + r[0] + '.fits')
            outfile = os.path.join(shifted_dir, c + r[0] + '_adj.fits')
            targ_unshifted = spectrum.read_hires_fits(infile, maskfile=maskfile)

            shift_data = {}

            targ_shifted = shift.shift(targ_unshifted, ref_spec, store=shift_data)

            shift.save_shift_to_fits(outfile, targ_shifted, targ_unshifted,
                                     shift_data, clobber=True)

            shifted.append(targ_shifted)

        # Flatten spectrum and save
        print("Flattening spectra into single file")
        outfile = os.path.join(shifted_dir, r[0] + '_adj.fits')
        flattened = spectrum.Spectrum.combine_spectra(shifted, base_w)
        flattened.to_fits(outfile)
示例#7
0
def main(args):
    filename = os.path.splitext(args.input_file)[0]
    infile = os.path.join(args.dir, filename + '.fits')

    spec = spectrum.read_hires_fits(infile)

    for i in range(args.num):
        noised_spec = add_noise(spec, args.snr)

        if args.num == 1:
            suffix = "_snr={0:.0f}".format(args.snr)
        else:
            suffix = "_snr={0:.0f}_i={1:d}".format(args.snr, i + 1)
        outfile = os.path.join(args.dir, filename + suffix + '.fits')

        noised_spec.to_hires_fits(outfile, clobber=True)
def main(args):
    filename = os.path.splitext(args.input_file)[0]
    infile = os.path.join(args.dir, filename + '.fits')

    spec = spectrum.read_hires_fits(infile)

    for i in range(args.num):
        noised_spec = add_noise(spec, args.snr)

        if args.num == 1:
            suffix = "_snr={0:.0f}".format(args.snr)
        else:
            suffix = "_snr={0:.0f}_i={1:d}".format(args.snr, i + 1)
        outfile = os.path.join(args.dir, filename + suffix + '.fits')

        noised_spec.to_hires_fits(outfile, clobber=True)
示例#9
0
# code-stop-spectra-selected-stars
fig.set_tight_layout(True)
fig.savefig('quickstart-spectra-selected-stars.png')


# code-start-pop-library
idx1 = lib.get_index('190406')
G_star = lib.pop(idx1)
idx2 = lib.get_index('GL699')
M_star = lib.pop(idx2)
# code-stop-pop-library


# code-start-read-spectrum-G
from specmatchemp import spectrum
G_spectrum = spectrum.read_hires_fits('../samples/rj59.1923.fits').cut(5130,5210)
G_spectrum.name = 'HD190406'
# code-stop-read-spectrum-G


# code-start-shift-spectrum-G
from specmatchemp.specmatch import SpecMatch
sm_G = SpecMatch(G_spectrum, lib)
sm_G.shift()
# code-stop-shift-spectrum-G


# code-start-plot-shifts-G
fig = plt.figure(figsize=(10,5))
sm_G.target_unshifted.plot(normalize=True, plt_kw={'color':'forestgreen'}, text='Target (unshifted)')
sm_G.target.plot(offset=0.5, plt_kw={'color':'royalblue'}, text='Target (shifted): HD190406')
示例#10
0
def shift_spectrum(specpath,
                   plot_level=0,
                   indir=None,
                   outdir="./",
                   suffix="_adj",
                   mask=True,
                   no_bootstrap=False,
                   flatten=False):
    """Shift a target spectrum given an observation code.

    Saves the shifted spectrum in a fits file.

    Args:
        specpath (str): Path to spectrum or its CPS observation id jXX.XXXX
        plot_level (int, 0-2): Level of plotting to save
        indir (str): Directory to look in for target spectrum
        outdir (str): Directory to store output files
        suffix (str): String to append to output file names
        mask (bool): Use a mask to remove telluric lines
        no_bootstrap (bool): Shift a spectrum without bootstrapping
        flatten (bool): If multiple chips are provided, flatten into a single
            spectrum file.
    Returns:
        shifted, unshifted, shift_data
    """
    # Check if specpath is a path or an observation ID
    if os.path.exists(specpath):
        targ_path = specpath
        targid = os.path.splitext(os.path.basename(specpath))[0]
    else:
        return _multishift_spectrum(specpath, plot_level, indir, outdir,
                                    suffix, mask, no_bootstrap, flatten)

    # if a different directory is provided, copy the file into specmatchemp
    # working directory
    specdir = os.path.join(SPECMATCHDIR, 'spectra')
    shiftedspecdir = os.path.join(SPECMATCHDIR, 'shifted_spectra')
    if indir != specdir and indir is not None:
        copy(targ_path, specdir)

    # load target and references
    if mask:
        maskfile = os.path.join(SPECMATCHDIR, 'hires_telluric_mask.csv')
    else:
        maskfile = None
    targ_spec = spectrum.read_hires_fits(targ_path, maskfile)

    if no_bootstrap:
        # Shift directly onto NSO spectrum
        ref_specs = [
            spectrum.read_fits(os.path.join(shiftedspecdir, 'nso_adj.fits'))
        ]
        shift_data = {}
        print("Shifting directly against NSO spectrum.")
        shifted = shift.shift(targ_spec, ref_specs[0], store=shift_data)
        shift_data['shift_reference'] = 0
    else:
        # Shift spectrum onto boostrapped spectra
        ref_specs = [
            spectrum.read_fits(os.path.join(shiftedspecdir,
                                            r[0] + '_adj.fits'))
            for r in SHIFT_REFERENCES
        ]
        shift_data = {}
        shifted = shift.bootstrap_shift(targ_spec, ref_specs, store=shift_data)

    # Save shifted spectrum
    outpath = os.path.join(shiftedspecdir, targid + suffix + '.fits')
    shift.save_shift_to_fits(outpath,
                             shifted,
                             targ_spec,
                             shift_data,
                             clobber=True)

    if outdir != shiftedspecdir:
        if not os.path.exists(outdir):
            os.mkdir(outdir)
        copy(outpath, outdir)

    # Generate representative plots
    if plot_level == 1:
        plotfile = os.path.join(outdir, targid + "_shift_plots.pdf")
        print("Saving plots to " + plotfile)

        with PdfPages(plotfile) as pdf:
            # Get reference used
            shift_ref = ref_specs[shift_data['shift_reference']]
            # Plot single order
            plot_shift_data(targ_spec, shifted, shift_ref, shift_data, pdf, 2)
    # Generate individual plots for every order
    elif plot_level == 2:
        plotfile = os.path.join(outdir, targid + "_shift_plots.pdf")
        print("Saving plots to " + plotfile)
        with PdfPages(plotfile) as pdf:
            # Get reference used
            shift_ref = ref_specs[shift_data['shift_reference']]
            num_orders = shift_data['num_orders']
            for i in range(num_orders):
                plot_shift_data(targ_spec,
                                shifted,
                                shift_ref,
                                shift_data,
                                pdf,
                                i,
                                singleorder=True)

    return shifted, targ_spec, shift_data
示例#11
0
def specmatch_spectrum(specpath,
                       plot_level=0,
                       inlib=False,
                       outdir="./",
                       num_best=5,
                       suffix="",
                       wavlim='all',
                       lib_subset=None,
                       name=None,
                       n_lib_subset=None):
    """Perform the specmatch on a given spectrum

    Args:
        specpath (str): Path to target spectrum
        plot_level (int, 0-2): Level of plots
            0 - No plots saved, 1 - Representative plots, 2 - All plots
        inlib (str or False): String to search within library for to exclude
            from matching process
        outdir (str): Output file directory
        num_best (int): Number of best matches to use at lincomb stage
        suffix (str): String to append to output file names

    Returns:
        specmatch.SpecMatch object
    """
    if not os.path.exists(specpath):
        raise ValueError(specpath + " does not exist!")

    if wavlim == 'all':
        target = spectrum.read_hires_fits(specpath)
    else:
        target = spectrum.read_hires_fits(specpath).cut(*wavlim)

    # Determine the name of the target
    if inlib:
        name = inlib
    elif name is None:
        name = os.path.basename(specpath)[:-5]

    if n_lib_subset is not None:
        lib = library.read_hdf(wavlim='none')
        lib_subset = lib.library_params.lib_index
        lib_subset = np.random.choice(lib_subset,
                                      size=n_lib_subset,
                                      replace=False)

    lib = library.read_hdf(wavlim=wavlim, lib_index_subset=lib_subset)
    sm = specmatch.SpecMatch(target, lib)
    sm.shift()

    if inlib:
        targ_idx = lib.get_index(inlib)
        targ_param, targ_spec = lib[targ_idx]
        sm.match(ignore=targ_idx, wavlim=wavlim)
    else:
        targ_param = None
        sm.match(wavlim=wavlim)

    sm.target.name = name  # attach target name

    sm.lincomb(num_best)

    # Print results
    print("SpecMatch Results for {0}".format(name))
    sm.results_to_txt(sys.stdout)

    outdir = os.path.join(outdir, name)
    if not os.path.exists(outdir):
        os.makedirs(outdir)

    # Save final results
    outpath = os.path.join(outdir, name + suffix + '_results.txt')
    with open(outpath, 'w') as f:
        if inlib:
            f.write('Library Parameters\n')
            f.write('------------------\n')
            f.write('Teff: {0:.0f} +/- {1:.0f} K\n'.format(
                targ_param['Teff'], targ_param['u_Teff']))
            f.write('Radius: {0:.3f} +/- {1:.3f} Rsun\n'.format(
                targ_param['radius'], targ_param['u_radius']))
            f.write('[Fe/H]: {0:.2f} +/- {1:.2f} dex\n'.format(
                targ_param['feh'], targ_param['u_feh']))
        f.write('\n')
        sm.results_to_txt(f, verbose=True)
        print("created {}".format(outpath))

    # Save full results
    outpath = os.path.join(outdir, name + suffix + '_sm.hdf')
    sm.to_hdf(outpath)
    print("created {}".format(outpath))

    # Create representative plots
    if plot_level is not None and plot_level > 0:
        plotspath = os.path.join(outdir, name + suffix + '_plots.pdf')
        with PdfPages(plotspath) as pdf:
            region = (5100, 5200)
            wavlim = (5160, 5190)
            order = 2

            plot_shifts(sm, pdf, order, wavlim)
            plot_match(sm, pdf, region, wavlim, targ_param)
            plot_lincomb(sm, pdf, region, wavlim, targ_param)
            print("created {}".format(plotspath))

    # Create full plots
    if plot_level == 2:
        shiftplotspath = os.path.join(outdir,
                                      name + suffix + '_shift_plots.pdf')
        with PdfPages(shiftplotspath) as pdf:
            for order in range(np.shape(sm.target_unshifted.w)[0]):
                plot_shifts(sm, pdf, order, wavlim='all')

        matchplotspath = os.path.join(outdir,
                                      name + suffix + '_match_plots.pdf')
        with PdfPages(matchplotspath) as pdf:
            for reg in sm.regions:
                plot_match(sm, pdf, reg, wavlim='all', targ_param=targ_param)

        lincombplotspath = os.path.join(outdir,
                                        name + suffix + '_lincomb_plots.pdf')
        with PdfPages(lincombplotspath) as pdf:
            for reg in sm.lincomb_regions:
                plot_lincomb(sm, pdf, reg, wavlim='all', targ_param=targ_param)

        print("created {}".format(plotspath))

    return sm
示例#12
0
def shift_spectrum(specpath, plot_level=0, indir=None, outdir="./",
                   suffix="_adj", mask=True, no_bootstrap=False,
                   flatten=False):
    """Shift a target spectrum given an observation code.

    Saves the shifted spectrum in a fits file.

    Args:
        specpath (str): Path to spectrum or its CPS observation id jXX.XXXX
        plot_level (int, 0-2): Level of plotting to save
        indir (str): Directory to look in for target spectrum
        outdir (str): Directory to store output files
        suffix (str): String to append to output file names
        mask (bool): Use a mask to remove telluric lines
        no_bootstrap (bool): Shift a spectrum without bootstrapping
        flatten (bool): If multiple chips are provided, flatten into a single
            spectrum file.
    Returns:
        shifted, unshifted, shift_data
    """
    # Check if specpath is a path or an observation ID
    if os.path.exists(specpath):
        targ_path = specpath
        targid = os.path.splitext(os.path.basename(specpath))[0]
    else:
        return _multishift_spectrum(specpath, plot_level, indir, outdir,
                                    suffix, mask, no_bootstrap, flatten)

    # if a different directory is provided, copy the file into specmatchemp
    # working directory
    specdir = os.path.join(SPECMATCHDIR, 'spectra')
    shiftedspecdir = os.path.join(SPECMATCHDIR, 'shifted_spectra')
    if indir != specdir and indir is not None:
        copy(targ_path, specdir)

    # load target and references
    if mask:
        maskfile = os.path.join(SPECMATCHDIR, 'hires_telluric_mask.csv')
    else:
        maskfile = None
    targ_spec = spectrum.read_hires_fits(targ_path, maskfile)

    if no_bootstrap:
        # Shift directly onto NSO spectrum
        ref_specs = [spectrum.read_fits(os.path.join(shiftedspecdir,
                     'nso_adj.fits'))]
        shift_data = {}
        print("Shifting directly against NSO spectrum.")
        shifted = shift.shift(targ_spec, ref_specs[0], store=shift_data)
        shift_data['shift_reference'] = 0
    else:
        # Shift spectrum onto boostrapped spectra
        ref_specs = [spectrum.read_fits(os.path.join(shiftedspecdir,
                     r[0] + '_adj.fits')) for r in SHIFT_REFERENCES]
        shift_data = {}
        shifted = shift.bootstrap_shift(targ_spec, ref_specs, store=shift_data)

    # Save shifted spectrum
    outpath = os.path.join(shiftedspecdir, targid + suffix + '.fits')
    shift.save_shift_to_fits(outpath, shifted, targ_spec, shift_data,
                             clobber=True)

    if outdir != shiftedspecdir:
        if not os.path.exists(outdir):
            os.mkdir(outdir)
        copy(outpath, outdir)

    # Generate representative plots
    if plot_level == 1:
        plotfile = os.path.join(outdir, targid + "_shift_plots.pdf")
        print("Saving plots to " + plotfile)

        with PdfPages(plotfile) as pdf:
            # Get reference used
            shift_ref = ref_specs[shift_data['shift_reference']]
            # Plot single order
            plot_shift_data(targ_spec, shifted, shift_ref, shift_data, pdf, 2)
    # Generate individual plots for every order
    elif plot_level == 2:
        plotfile = os.path.join(outdir, targid + "_shift_plots.pdf")
        print("Saving plots to " + plotfile)
        with PdfPages(plotfile) as pdf:
            # Get reference used
            shift_ref = ref_specs[shift_data['shift_reference']]
            num_orders = shift_data['num_orders']
            for i in range(num_orders):
                plot_shift_data(targ_spec, shifted, shift_ref, shift_data,
                                pdf, i, singleorder=True)

    return shifted, targ_spec, shift_data
示例#13
0
def specmatch_spectrum(specpath, plot_level=0, inlib=False, outdir="./",
                       num_best=5, suffix="", wavlim='all', lib_subset=None,
                       name=None, n_lib_subset=None):
    """Perform the specmatch on a given spectrum

    Args:
        specpath (str): Path to target spectrum
        plot_level (int, 0-2): Level of plots
            0 - No plots saved, 1 - Representative plots, 2 - All plots
        inlib (str or False): String to search within library for to exclude
            from matching process
        outdir (str): Output file directory
        num_best (int): Number of best matches to use at lincomb stage
        suffix (str): String to append to output file names

    Returns:
        specmatch.SpecMatch object
    """
    if not os.path.exists(specpath):
        raise ValueError(specpath + " does not exist!")

    if wavlim == 'all':
        target = spectrum.read_hires_fits(specpath)
    else:
        target = spectrum.read_hires_fits(specpath).cut(*wavlim)

    # Determine the name of the target
    if inlib:
        name = inlib
    elif name is None:
        name = os.path.basename(specpath)[:-5]

    if n_lib_subset is not None:
        lib = library.read_hdf(wavlim='none')
        lib_subset = lib.library_params.lib_index
        lib_subset = np.random.choice(
            lib_subset, size=n_lib_subset, replace=False
        )

    lib = library.read_hdf(wavlim=wavlim, lib_index_subset=lib_subset)
    sm = specmatch.SpecMatch(target, lib)
    sm.shift()

    if inlib:
        targ_idx = lib.get_index(inlib)
        targ_param, targ_spec = lib[targ_idx]
        sm.match(ignore=targ_idx, wavlim=wavlim)
    else:
        targ_param = None
        sm.match(wavlim=wavlim)

    sm.target.name = name  # attach target name

    sm.lincomb(num_best)

    # Print results
    print("SpecMatch Results for {0}".format(name))
    sm.results_to_txt(sys.stdout)

    outdir = os.path.join(outdir, name)
    if not os.path.exists(outdir):
        os.makedirs(outdir)

    # Save final results
    outpath = os.path.join(outdir, name + suffix + '_results.txt')
    with open(outpath, 'w') as f:
        if inlib:
            f.write('Library Parameters\n')
            f.write('------------------\n')
            f.write('Teff: {0:.0f} +/- {1:.0f} K\n'.format(
                targ_param['Teff'], targ_param['u_Teff']))
            f.write('Radius: {0:.3f} +/- {1:.3f} Rsun\n'.format(
                targ_param['radius'], targ_param['u_radius']))
            f.write('[Fe/H]: {0:.2f} +/- {1:.2f} dex\n'.format(
                targ_param['feh'], targ_param['u_feh']))
        f.write('\n')
        sm.results_to_txt(f, verbose=True)
        print("created {}".format(outpath))

    # Save full results
    outpath = os.path.join(outdir, name + suffix + '_sm.hdf')
    sm.to_hdf(outpath)
    print("created {}".format(outpath))

    # Create representative plots
    if plot_level is not None and plot_level > 0:
        plotspath = os.path.join(outdir, name + suffix + '_plots.pdf')
        with PdfPages(plotspath) as pdf:
            region = (5100, 5200)
            wavlim = (5160, 5190)
            order = 2

            plot_shifts(sm, pdf, order, wavlim)
            plot_match(sm, pdf, region, wavlim, targ_param)
            plot_lincomb(sm, pdf, region, wavlim, targ_param)
            print("created {}".format(plotspath))

    # Create full plots
    if plot_level == 2:
        shiftplotspath = os.path.join(outdir, name + suffix +
                                      '_shift_plots.pdf')
        with PdfPages(shiftplotspath) as pdf:
            for order in range(np.shape(sm.target_unshifted.w)[0]):
                plot_shifts(sm, pdf, order, wavlim='all')

        matchplotspath = os.path.join(outdir, name + suffix +
                                      '_match_plots.pdf')
        with PdfPages(matchplotspath) as pdf:
            for reg in sm.regions:
                plot_match(sm, pdf, reg, wavlim='all', targ_param=targ_param)

        lincombplotspath = os.path.join(outdir, name + suffix +
                                        '_lincomb_plots.pdf')
        with PdfPages(lincombplotspath) as pdf:
            for reg in sm.lincomb_regions:
                plot_lincomb(sm, pdf, reg, wavlim='all', targ_param=targ_param)

        print("created {}".format(plotspath))

    return sm