Exemplo n.º 1
0
def sensfunc(obs_spectra, std_spectra, ext_spectra, airmass, exptime):
    #re-interpt the std_spectra over the same wavelength
    std_spectra.interp(obs_spectra.warr)

    #re-interp the ext_spetra over the sam ewavelength
    ext_spectra.interp(obs_spectra.warr)
  
    #create the calibration spectra
    cal_spectra=Spectra(obs_spectra.warr, obs_spectra.farr.copy())

    #set up the bandpass
    bandpass=np.diff(obs_spectra.warr).mean()
    print bandpass

    #correct for extinction
    cal_spectra.farr=cal_spectra.farr/10**(-0.4*airmass*ext_spectra.farr)
 
    #correct for the exposure time and calculation the sensitivity curve
    cal_spectra.farr=cal_spectra.farr/exptime/bandpass/std_spectra.farr

    #now fit cal_spectra with a low order fit
    mask=(cal_spectra.farr>0)
    fit=interfit(cal_spectra.warr[mask], cal_spectra.farr[mask], function='polynomial', order=5, niter=5)
    fit.fit()
    figure()
    warr=cal_spectra.warr
    plot(warr, fit(warr))
    plot(warr, cal_spectra.farr)
    show()
    cal_spectra.farr=fit(warr)

    return cal_spectra
Exemplo n.º 2
0
def specsens(specfile, outfile, stdfile, extfile, airmass=None, exptime=None,
                stdzp=3.68e-20, function='polynomial', order=3, thresh=3, niter=5, 
                clobber=True, logfile='salt.log',verbose=True):

   with logging(logfile,debug) as log:

       #read in the specfile and create a spectrum object
       obs_spectra=st.readspectrum(specfile, error=True, ftype='ascii')

       #read in the std file and convert from magnitudes to fnu
       #then convert it to fwave (ergs/s/cm2/A)
       std_spectra=st.readspectrum(stdfile, error=False, ftype='ascii')
       std_spectra.flux=Spectrum.magtoflux(std_spectra.flux, stdzp)
       std_spectra.flux=Spectrum.fnutofwave(std_spectra.wavelength, std_spectra.flux)


       #read in the extinction file (leave in magnitudes)
       ext_spectra=st.readspectrum(extfile, error=False, ftype='ascii')

       #determine the airmass if not specified
       if saltio.checkfornone(airmass) is None:
           message='Airmass was not supplied'
           raise SALTSpecError(message)

       #determine the exptime if not specified
       if saltio.checkfornone(airmass) is None:
           message='Exposure Time was not supplied'
           raise SALTSpecError(message)
          
       #calculate the calibrated spectra
       log.message('Calculating the calibration curve for %s' % specfile)
       cal_spectra=sensfunc(obs_spectra, std_spectra, ext_spectra, airmass, exptime)

       #fit the spectra--first take a first cut of the spectra
       #using the median absolute deviation to throw away bad points
       cmed=np.median(cal_spectra.flux)
       cmad=saltstat.mad(cal_spectra.flux)
       mask=(abs(cal_spectra.flux-cmed)<thresh*cmad)
       mask=(cal_spectra.flux>0)

       #now fit the data
       fit=interfit(cal_spectra.wavelength[mask], cal_spectra.flux[mask], function=function, order=order, thresh=thresh, niter=niter)
       fit.interfit()

       #print 'plotting...'
       #figure()
       #plot(cal_spectra.wavelength, cal_spectra.flux)
       #plot(obs_spectra.wavelength, obs_spectra.flux*cal_spectra.flux.mean()/obs_spectra.flux.mean())
       #plot(std_spectra.wavelength, std_spectra.flux*cal_spectra.flux.mean()/std_spectra.flux.mean())
       #plot(cal_spectra.wavelength, fit(cal_spectra.wavelength))
       #show()

       #write the spectra out
       cal_spectra.flux=fit(cal_spectra.wavelength)
       st.writespectrum(cal_spectra, outfile, ftype='ascii')
Exemplo n.º 3
0
def fitsky(xarr, data, var_arr, function='polynomial', order=2, thresh=3):
    """For each column, fit a function to the column after rejecting
       sources and then create a sky image from that
    """
    sdata = 0.0 * data
    for i in xarr:
        yarr = data[:, i]
        yind = np.arange(len(yarr))
        m = np.median(yarr)
        s = stats.median_absolute_deviation(yarr)
        mask = (abs(yarr - m) < thresh * s)
        try:
            it = interfit(yind[mask], yarr[mask], function='poly', order=2)
            it.fit()
            sdata[:, i] = it(yind)
        except:
            sdata[:, i] = 0.0 * sdata[:, i] + m
    return sdata
Exemplo n.º 4
0
def fitsky(xarr, data, var_arr, function='polynomial', order=2, thresh=3):
    """For each column, fit a function to the column after rejecting
       sources and then create a sky image from that
    """
    sdata = 0.0 * data
    for i in xarr:
        yarr = data[:, i]
        yind = np.arange(len(yarr))
        m = np.median(yarr)
        s = stats.median_absolute_deviation(yarr)
        mask = (abs(yarr - m) < thresh * s)
        try:
            it = interfit(yind[mask], yarr[mask], function='poly', order=2)
            it.fit()
            sdata[:, i] = it(yind)
        except:
            sdata[:, i] = 0.0 * sdata[:, i] + m
    return sdata
Exemplo n.º 5
0
def sensfunc(obs_spectra, std_spectra, ext_spectra, airmass, exptime):
    # re-interpt the std_spectra over the same wavelength
    std_spectra.interp(obs_spectra.warr)

    # re-interp the ext_spetra over the sam ewavelength
    ext_spectra.interp(obs_spectra.warr)

    # create the calibration spectra
    cal_spectra = Spectra(obs_spectra.warr, obs_spectra.farr.copy())

    # set up the bandpass
    bandpass = np.diff(obs_spectra.warr).mean()
    print bandpass

    # correct for extinction
    cal_spectra.farr = cal_spectra.farr / \
        10 ** (-0.4 * airmass * ext_spectra.farr)

    # correct for the exposure time and calculation the sensitivity curve
    cal_spectra.farr = cal_spectra.farr / exptime / bandpass / std_spectra.farr

    # now fit cal_spectra with a low order fit
    mask = (cal_spectra.farr > 0)
    fit = interfit(cal_spectra.warr[mask],
                   cal_spectra.farr[mask],
                   function='polynomial',
                   order=5,
                   niter=5)
    fit.fit()
    figure()
    warr = cal_spectra.warr
    plot(warr, fit(warr))
    plot(warr, cal_spectra.farr)
    show()
    cal_spectra.farr = fit(warr)

    return cal_spectra
Exemplo n.º 6
0
def continuum_subtract(spec, function='polynomial', order=7):
    """Fit a function to a spectra and subtract the continuum"""
    wc=interfit(spec.wavelength, spec.flux, function=function, order=order)
    wc.interfit()
 
    return spec.flux-wc(spec.wavelength)
Exemplo n.º 7
0
def continuum_subtract(spec, function='polynomial', order=7):
    """Fit a function to a spectra and subtract the continuum"""
    wc=interfit(spec.wavelength, spec.flux, function=function, order=order)
    wc.interfit()
 
    return spec.flux-wc(spec.wavelength)