Ejemplo n.º 1
0
def run_fitradecsp(ra, dec, step, nstep, w_center, w_range, ifit1, starobs,
                   specfiles, wdir):
    """
    Setup and call fitradecsp. This creates a file called spec.out

    Parameters
    ----------
    starobs : list
        List of StarObservation structures one for each fiber
    specfiles : list
        List of filename of the different spec files
    """

    with open(os.path.join(wdir, 'list'), 'w') as f:
        for st, sp in zip(starobs, specfiles):
            f.write('%s %.7f %.7f %.6f %s\n' %
                    (sp, st.ra, st.dec, st.structaz, st.expname))

    input = '{ra:.7f} {dec:.7f} {step:d} {nstep:d} {wcen:f} {wr:f} {ifit1:d}\n'

    run_command(_vdrp_bindir + '/fitradecsp',
                input.format(ra=ra,
                             dec=dec,
                             step=step,
                             nstep=nstep,
                             wcen=w_center,
                             wr=w_range,
                             ifit1=ifit1),
                wdir=wdir)
Ejemplo n.º 2
0
def call_fitonevp(wave, outname, wdir):
    """
    Call fitonevp

    Requires fitghsp.in created by apply_factor_spline

    Parameters
    ----------
    wave : float
        Wavelength
    outname : str
        Output filename
    wdir : str
        Name of the work directory
    """
    input = '0 0\n{wave:f}\n/vcps\n'

    run_command(_vdrp_bindir + '/fitonevp', input.format(wave=wave), wdir=wdir)

    shutil.move(os.path.join(wdir, 'pgplot.ps'),
                os.path.join(wdir, outname + 'tot.ps'))
    shutil.move(os.path.join(wdir, 'out'),
                os.path.join(wdir, outname + 'spec.dat'))
    shutil.move(os.path.join(wdir, 'lines.out'),
                os.path.join(wdir, outname + 'spec.res'))

    splinedata = np.loadtxt(os.path.join(wdir, 'splines.out'))

    with open(os.path.join(wdir, outname + 'spece.dat'), 'w') as f:
        for d in splinedata:
            f.write('%.4f\t%.7f\t%.8e\t%.7f\t%.8e\n' %
                    (d[0], d[1], d[3], d[2] * 1e17, d[4] * 1e17))
Ejemplo n.º 3
0
def call_fit2d(ra, dec, outname, wdir):
    """
    Call fit2d. Calculate the 2D spatial fit based on fwhm, fiber locations,
    and ADC. This convolves the PSF over each fiber, for a given input
    position. It fits the ampltiude, minimizing to a chi^2.

    Requires input files generated by run_fit2d

    Parameters
    ----------
    ra : float
        Right Ascension of the star.
    dec : float
        Declination of the star.
    outname : str
        Output filename.
    wdir : str
        Name of the work directory
    """
    input = '{ra:f} {dec:f}\n/vcps\n'

    run_command(_vdrp_bindir + '/fit2d',
                input.format(ra=ra, dec=dec),
                wdir=wdir)

    shutil.move(os.path.join(wdir, 'pgplot.ps'), os.path.join(wdir, outname))
    shutil.move(os.path.join(wdir, 'out'), os.path.join(wdir, 'out2d'))
Ejemplo n.º 4
0
def call_getnormexp(nightshot, normdir, fwhmdir, wdir):
    """
    Call getnormexp. Get fwhm and relative normalizations for the frames.

    Parameters
    ----------
    name : str
        Observation name
    """
    input = '{name:s}\n'

    fwhm_file = fwhmdir + '/' + nightshot + '/fwhm.out'

    try:
        shutil.copy2(normdir + '/' + nightshot + '/norm.dat', wdir)
    except FileNotFoundError:
        _logger.warn('norm.dat is missing for %s' % nightshot)
    try:
        shutil.copy2(fwhm_file, wdir)
    except FileNotFoundError:
        _logger.warn('fwhm.out is missing for %s' % nightshot)

    _logger.info('Grabbing FWHM file from' + fwhm_file)

    run_command(_vdrp_bindir + '/getnormexp',
                input.format(name=nightshot),
                wdir=wdir)
Ejemplo n.º 5
0
def call_mkimage3d(wdir):
    """
    Run the mkimage3d command, creating an output file called image3d.fits
    """

    if os.path.exists(os.path.join(wdir, './image3d.fits')):
        os.remove(os.path.join(wdir, './image3d.fits'))
    print(os.path.exists(os.path.join(wdir, './image3d.fits')))

    run_command(_vdrp_bindir + '/mkimage3d', wdir=wdir)
Ejemplo n.º 6
0
def run_combsed(sedlist, sigmacut, rmscut, outfile, wdir, plotfile=None):
    """


    Parameters
    ----------
    sedlist : list
        List of filenames of SED fits
    sigmacut : float
        Cut value for sigma
    rmscut : float
        Cut value for rms
    outfile : str
        Output filename
    plotfile : str (optional)
        Optional plot output filename

    Returns
    -------
    n, biwt, error
    """
    with open(os.path.join(wdir, 'list'), 'w') as f:
        for l in sedlist:
            f.write('%s\n' % l)

    input = '{:f} {:f}\n'
    run_command(vp._vdrp_bindir + '/combsed',
                input.format(sigmacut, rmscut), wdir=wdir)

    shutil.move(os.path.join(wdir, 'comb.out'), os.path.join(wdir, outfile))

    if plotfile is not None:

        fdata = np.loadtxt(os.path.join(wdir, 'out'), dtype=float,
                           usecols=[1, 2, 4, 5])
        idata = np.loadtxt(os.path.join(wdir, 'out'), dtype=int,
                           usecols=[0, 3])

        f_in = os.path.join(wdir, 'in')
        f_in2 = os.path.join(wdir, 'in2')
        with open(f_in, 'w') as f, open(f_in2, 'w') as f2:
            for di, df, sf in zip(idata, fdata, sedlist):
                if di[1] == 0:
                    f.write('%s %d %f %f %d %f %f\n'
                            % (sf, di[0], df[0], df[1],
                               di[1], df[2], df[3]))
                    f2.write('%s %d %f %f %d %f %f\n'
                             % (sf, di[0], df[0], df[1],
                                di[1], df[2], df[3]))
            f2.write('%s\n' % outfile)

        run_command(vp._vdrp_bindir + '/plotseda', '/vcps\n', wdir=wdir)

        shutil.move(os.path.join(wdir, 'pgplot.ps'),
                    os.path.join(wdir, plotfile))
Ejemplo n.º 7
0
def call_imextsp(filename, ifuslot, wl, wlw, tpavg, norm, outfile, wdir):
    """
    Equivalent of the rextsp script,
    a wrapper around the imextsp fortran routine.

    Extracts the spectrum from the multi fits files and writes the tmp*dat.
    This also calculates the appropriate photon errors, using counting and
    sky residual errors. This applies the throughput and fiber to fiber.

    Parameters
    ----------
    filename : str
        The filename to process
    ifuslot : str
        The ifuslot name
    wl : float
        The central extraction wavelength
    wlw : float
        The width of the extraction window around wl
    tpavg : float
        Throughput average for the spectrum
    norm : str
        File with fiber to fiber normaliztion for the spectrum
    outfile : str
        Name of the output filename
    wdir : str
        Name of the work directory
    """
    input = '"{filename:s}"\n{ifuslot} {wl} {wlw}\n"{tpavg}"\n"{norm}"\n'

    try:
        os.remove(os.path.join(wdir, 'out.sp'))
    except OSError:
        pass

    try:
        os.remove(os.path.join(wdir, outfile))
    except OSError:
        pass

    s = input.format(filename=os.path.join(wdir, filename),
                     ifuslot=ifuslot,
                     wl=wl,
                     wlw=wlw,
                     tpavg=tpavg,
                     norm=norm)

    run_command(_vdrp_bindir + '/imextsp', s, wdir=wdir)

    shutil.move(os.path.join(wdir, 'out.sp'), os.path.join(wdir, 'specin'))

    run_command(_vdrp_bindir + '/specclean', wdir=wdir)

    shutil.move(os.path.join(wdir, 'out'), os.path.join(wdir, outfile))
Ejemplo n.º 8
0
def call_fitem(wl, wdir):
    """
    Call fitem requires input files created by run_fitem

    The line fitter. It fits a gauss-hermite. input is fitghsp.in.

    Parameters
    ----------
    wl : float
        Wavelength
    """

    input = '{wl:f}\n/vcps\n'

    run_command(_vdrp_bindir + '/fitem', input.format(wl=wl), wdir=wdir)
Ejemplo n.º 9
0
def call_sumspec(starname, wdir):
    """
    Call sumpspec. Sums a set of spectra, and then bins to 100AA bins.
    Used for SED fitting.

    Parameters
    ----------
    starname : str
        Star name used to create the outputn filename (adds specf.dat)
    """
    if os.path.exists(os.path.join(wdir, 'sumspec.out')):
        os.remove(os.path.join(wdir, 'sumspec.out'))
    with open(os.path.join(wdir, 'list'), 'w') as f:
        f.write(starname + 'specf.dat')

    run_command(_vdrp_bindir + '/sumspec', wdir=wdir)
Ejemplo n.º 10
0
def call_sumsplines(nspec, wdir):
    """
    Call sumsplines, calculate a straight sum of the spectra in a list,
    including errors. Expects the spectra to be called tmp101 to
    tmp100+nspec.

    Creates a file called splines.out

    Parameters
    ----------
    nspec : int
        Number of spectra to read.
    wdir : str
        Name of the work directory
    """
    with open(os.path.join(wdir, 'list'), 'w') as f:
        for i in range(0, nspec):
            f.write('tmp{c}.dat\n'.format(c=i + 101))

    run_command(_vdrp_bindir + '/sumsplines', wdir=wdir)
Ejemplo n.º 11
0
def run_biwt(data, outfile, wdir):
    """
    Calculate biweight of the supplied data.

    Parameters
    ----------
    data : list
        List of the data to be run through biwt.

    Returns
    -------
    n, biwt, error
    """
    with open(os.path.join(wdir, 'tp.dat'), 'w') as f:
        for d in data:
            f.write('%f\n' % d)

    run_command(vp._vdrp_bindir + '/biwt', 'tp.dat\n1\n', wdir=wdir)

    os.remove(os.path.join(wdir, 'tp.dat'))

    shutil.move(os.path.join(wdir, 'biwt.out'), os.path.join(wdir, outfile))
Ejemplo n.º 12
0
def run_getsdss(filename, sdss_file, wdir):
    """
    Run getsdss on filename. Equivalent to rsdss file.

    Parameters
    ----------
    filename : str
        Filename with spectral data
    sdss_file : str
        Full path and filename to the sdss g-band filter curve.

    Returns
    -------
    The flux in the g-Band.

    """
    shutil.copy2(os.path.join(wdir, sdss_file),
                 os.path.join(wdir, 'sdssg.dat'))
    shutil.copy2(os.path.join(wdir, filename), os.path.join(wdir, 's1'))

    run_command(vp._vdrp_bindir + '/getsdssg', wdir=wdir)

    return float(np.loadtxt(os.path.join(wdir, 'out')))
Ejemplo n.º 13
0
def run_sumlineserr(specfiles, wdir):
    """
    Prepare input and run sumlineserr. It sums a set of spectra, and then bins
    to 100AA bins. Used for SED fitting.

    Parameters
    ----------
    specfiles : list
        List of spectrum filenames.

    """

    indata = np.loadtxt(os.path.join(wdir, 'out2d'),
                        dtype='U50',
                        ndmin=2,
                        usecols=[8, 9, 10, 11, 12, 13, 14])

    with open(os.path.join(wdir, 'list2'), 'w') as f:
        for spf, d in zip(specfiles, indata):
            f.write('%s %s %s %s %s %s %s %s\n' %
                    (spf, d[0], d[1], d[2], d[3], d[4], d[5], d[6]))

    run_command(vp._vdrp_bindir + '/sumlineserr', wdir=wdir)
Ejemplo n.º 14
0
def call_mkimage(ra, dec, starobs, wdir):
    """
    Call mkimage, equivalent of rmkim

    Reads the out2d file and creates three images of the
    emission line data, best fit model and residuals, called
    im[123].fits.

    Parameters
    ----------
    ra : float
        Right Ascension of the star.
    dec : float
        Declination of the star.
    starobs : list
        List of StarObservation objects for the star
    """

    gausa = np.loadtxt(os.path.join(wdir, 'out2d'), ndmin=1, usecols=[9])

    # First write the first j4 input file
    with open(os.path.join(wdir, 'j4'), 'w') as f:
        for obs in starobs:
            f.write('%f %f %f\n' %
                    (3600. * (obs.ra - ra) * np.cos(dec / 57.3), 3600 *
                     (obs.dec - dec), obs.avg))

    run_command(_vdrp_bindir + '/mkimage', wdir=wdir)

    shutil.move(os.path.join(wdir, 'image.fits'),
                os.path.join(wdir, 'im1.fits'))

    with open(os.path.join(wdir, 'j4'), 'w') as f:
        for i in range(0, len(starobs)):
            f.write('%f %f %f\n' %
                    (3600. * (starobs[i].ra - ra) * np.cos(dec / 57.3), 3600 *
                     (starobs[i].dec - dec), starobs[i].avg - gausa[i]))

    run_command(_vdrp_bindir + '/mkimage', wdir=wdir)

    shutil.move(os.path.join(wdir, 'image.fits'),
                os.path.join(wdir, 'im2.fits'))

    with open(os.path.join(wdir, 'j4'), 'w') as f:
        for i in range(0, len(starobs)):
            f.write('%f %f %f\n' %
                    (3600. * (starobs[i].ra - ra) * np.cos(dec / 57.3), 3600 *
                     (starobs[i].dec - dec), gausa[i]))

    run_command(_vdrp_bindir + '/mkimage', wdir=wdir)

    shutil.move(os.path.join(wdir, 'image.fits'),
                os.path.join(wdir, 'im3.fits'))