def womrebindriver(hop, mode):
    """input info to call womrebin functions"""
    import matplotlib.pyplot as plt
    import numpy as np
    from tmath.wombat.inputter import inputter
    from tmath.wombat.waveparse import waveparse
    from tmath.wombat.yesno import yesno
    from tmath.wombat.womashrebin import womashrebin
    from tmath.wombat.womscipyrebin import womscipyrebin
    print('Current A/pix is {}'.format(hop[0].wave[1] - hop[0].wave[0]))
    print('Wavelength rage: {} to {}'.format(hop[0].wave[0], hop[0].wave[-1]))
    plt.cla()
    plt.plot(hop[0].wave, hop[0].flux, drawstyle='steps-mid')
    plt.xlabel('Wavelength')
    plt.ylabel('Flux')
    plt.title(hop[0].obname)
    done = False
    while (not done):
        newdelt=inputter('Rebin to how many Angstroms per pixel? ', \
                         'float',False)
        waveb, waver = waveparse()
        if (waveb > waver):
            waveb, waver = waver, waveb
        if (newdelt > 0):
            newbin = (waver - waveb) / newdelt + 1.0
            frac, whole = np.modf(newbin)
            if (frac > 0.000001):
                print('NON-INTEGER number of bins')
                testwave = newdelt * whole + waveb
                print('Closest match is: {} to {}'.format(waveb, testwave))
                print('Would you like this wavelength range?')
                answer = yesno('y')
                if (answer == 'y'):
                    waver = testwave
                    done = True
            else:
                print('Rebinning to {} A/pix, {} to {}'.format(newdelt,waveb,\
                                                       waver))
                print('Is this OK?')
                answer = yesno('y')
                if (answer == 'y'):
                    done = True

    nwave = np.arange(0, whole) * newdelt + waveb
    if (mode == 'ash'):
        nflux = womashrebin(hop[0].wave, hop[0].flux, nwave)
        nvar = womashrebin(hop[0].wave, hop[0].var, nwave)
    else:
        nflux = womscipyrebin(hop[0].wave, hop[0].flux, nwave)
        nvar = womscipyrebin(hop[0].wave, hop[0].var, nwave)
    print('Overplotting rebinned spectrum')
    plt.cla()
    plt.plot(hop[0].wave, hop[0].flux, drawstyle='steps-mid')
    plt.plot(nwave, nflux, drawstyle='steps-mid')

    #FIX header
    hop[0].wave = nwave.copy()
    hop[0].flux = nflux.copy()
    hop[0].var = nvar.copy()
    return hop
def womwaverange(wave, flux, mode):
    import matplotlib.pyplot as plt
    from tmath.wombat.waveparse import waveparse
    from tmath.wombat.womget_element import womget_element
    from tmath.wombat.inputter_single import inputter_single
    from tmath.wombat.yesno import yesno
    from tmath.wombat.wshow import wshow
    if (mode == 'none'):
        print('Enter method to select wavelength range')
        mode = inputter_single(
            'Enter (w)avelengths or mark with the (m)ouse? (w/m) ', 'wm')
        print(mode)
    print('Spectrum runs from {} to {}.'.format(wave[0], wave[-1]))
    plt.cla()
    plt.plot(wave, flux, drawstyle='steps-mid')
    plt.ylabel('Flux')
    plt.xlabel('Wavelength')
    wshow()
    done = False
    while (not done):
        if (mode == 'w'):
            waveb, waver = waveparse()
        else:
            pickpoints = plt.ginput(2, timeout=-1)
            waveb = pickpoints[0][0]
            waver = pickpoints[1][0]
        if (waveb > waver):
            waveb, waver = waver, waveb
        indexblue = womget_element(wave, waveb)
        indexred = womget_element(wave, waver)
        print('Range selected: {} to {}'.format(wave[indexblue],
                                                wave[indexred]))
        plt.plot(wave[indexblue:indexred+1],flux[indexblue:indexred+1], \
                 drawstyle='steps-mid',color='r')
        plt.pause(0.01)
        print('Is this range correct?')
        answer = yesno('y')
        if (answer == 'n'):
            plt.cla()
            plt.plot(wave, flux, drawstyle='steps-mid')
            plt.ylabel('Flux')
            plt.xlabel('Wavelength')
            wshow()
        else:
            done = True
    return wave[indexblue:indexred + 1], flux[indexblue:indexred + 1], mode
Ejemplo n.º 3
0
def wommkbb(hop):
    import numpy as np
    import matplotlib.pyplot as plt
    from tmath.wombat.waveparse import waveparse
    from tmath.wombat.inputter import inputter
    from tmath.wombat.inputter_single import inputter_single
    light_speed = 2.99792458e10
    h_planck = 6.6260755e-27
    k_boltzmann = 1.380658e-16
    print('This routine will create a blackbody curve for a given temperature')
    print('with 1 Angstrom bins')
    print('\n')
    temp = inputter('Enter temperature in degrees Kelvin: ', 'float', False)
    if (temp <= 0):
        temp = 1.0
    waveb, waver = waveparse()
    if (waver < waveb):
        waveb, waver = waver, waveb
    if (waver == waveb):
        waver = waver + 1.
    wave = np.arange(waveb, waver + 1)
    wavecm = wave / 1.e8
    answer = inputter_single('Calculate B_nu(n) or B_lambda(l) ', 'nl')
    if (answer.lower()[0] == 'l'):
        flux=(2.0*h_planck*light_speed*light_speed/wavecm**5)/ \
              (np.exp((h_planck*light_speed)/(wavecm*k_boltzmann*temp))-1.0)
        flux = np.pi * flux / (1.e8)
        ext = 'flm'
    else:
        nu = light_speed / wavecm
        flux=(2.0*h_planck*nu**3/light_speed/light_speed)/ \
              (np.exp((h_planck*nu)/(k_boltzmann*temp))-1.0)
        flux = np.pi * flux * 1.e11
        ext = 'fnu'
    spectxt = 'bb{}.{}'.format(temp, ext)
    plt.cla()
    plt.plot(wave, flux, drawstyle='steps-mid')
    plt.title(spectxt)
    hop[0].wave = wave.copy()
    hop[0].flux = flux.copy()
    hop[0].obname = spectxt
    hop[0].var = np.ones(wave.shape)
    hop[0].header = ''
    return hop
def womblo(hop, fig):
    """blotch out bad data"""
    import logging
    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.interpolate import splrep, splev
    from tmath.wombat.inputter_single import inputter_single
    from tmath.wombat.waveparse import waveparse
    from tmath.wombat.womwaverange import womwaverange
    from tmath.wombat.womget_element import womget_element
    from tmath.wombat.yesno import yesno
    global nsplinepoints, tmpsplptsx, tmpsplptsy, pflag
    print('Select regions to blotch')
    wave = hop[0].wave.copy()
    flux = hop[0].flux.copy()
    done = False
    while (not done):
        plt.cla()
        plt.plot(wave, flux, drawstyle='steps-mid')
        plt.xlabel('Wavelength')
        plt.ylabel('Flux')
        plt.title(hop[0].obname)
        wavesub, fluxsub, mode = womwaverange(wave, flux, 'none')
        wavebind = womget_element(wave, wavesub[0])
        waverind = womget_element(wave, wavesub[-1])
        plt.cla()
        plt.plot(wave[wavebind:waverind+1],flux[wavebind:waverind+1], \
                 drawstyle='steps-mid')
        plt.xlabel('Wavelength')
        plt.ylabel('Flux')
        plt.title(hop[0].obname)
        plt.pause(0.01)
        print('Do you want to enter blotch wavelengths by hand (w),')
        print('mark points (m), fit a spline (s), or quit (q)?')
        choice = inputter_single('(w/m/s/q): ', 'wmsq')
        if (choice == 'w') or (choice == 'm'):
            blotchgood = False
            while (not blotchgood):
                wavechoicedone = False
                while (not wavechoicedone):
                    if (choice == 'w'):
                        waveselb, waveselr = waveparse()
                    else:
                        print('Mark the two endpoints of the blotch region')
                        endpoints = plt.ginput(2, timeout=-1)
                        waveselb = endpoints[0][0]
                        waveselr = endpoints[1][0]
                    if (waveselb > waveselr):
                        waveselb, waveselr = waveselr, waveselb
                    waveselbind = womget_element(wave, waveselb)
                    waveselrind = womget_element(wave, waveselr)
                    print(waveselb, waveselr, waveselbind, waveselrind)
                    if (waveselbind == 0) or (waveselrind == (len(wave) - 1)):
                        print('Wavelengths incorrect--too close to endpoints')
                    else:
                        wavechoicedone = True
                contblue = flux[waveselbind - 1]
                contred = flux[waveselrind + 1]
                delta = (contred - contblue) / (waveselrind - waveselbind + 1)
                fluxcor = flux.copy()
                for i in range(waveselbind, waveselrind + 1):
                    fluxcor[i] = contblue + (i - waveselbind + 1) * delta
                plt.plot(wave[wavebind:waverind+1],fluxcor[wavebind:waverind+1], \
                 drawstyle='steps-mid')
                plt.pause(0.01)
                print('Is this acceptable')
                answer = yesno('y')
                if (answer == 'y'):
                    flux = fluxcor.copy()
                    blotchgood = True
                    logging.info('File {} blotched from {} to {}'.format(
                        hop[0].obname, wave[waveselbind], wave[waveselrind]))
        elif (choice == 's'):
            xmin, xmax = plt.xlim()
            ymin, ymax = plt.ylim()
            plt.xlim([xmin, xmax])
            plt.ylim([ymin, ymax])
            nsplinepoints = 0
            tmpsplptsx = []
            tmpsplptsy = []

            spldone = False
            while (not spldone):
                plt.cla()
                plt.plot(wave[wavebind:waverind+1],flux[wavebind:waverind+1], \
                         drawstyle='steps-mid')
                if (len(tmpsplptsx) > 0):
                    plt.plot(tmpsplptsx, tmpsplptsy, 'ro')
                plt.xlabel('Wavelength')
                plt.ylabel('Flux')
                plt.title(hop[0].obname)
                plt.xlim([xmin, xmax])
                plt.ylim([ymin, ymax])
                cid = fig.canvas.mpl_connect('button_press_event', onclick)
                print('\nClick on continuum points for spline fit.')
                print(
                    'Spline will replace values between first and last point')
                print('Left button    = add point')
                print('Middle button  = delete point')
                print('Right button   = done\n')
                pflag = ''
                while (pflag != 'done'):
                    plt.pause(0.01)
                fig.canvas.mpl_disconnect(cid)

                splptsy = [z for _, z in sorted(zip(tmpsplptsx, tmpsplptsy))]
                splptsx = sorted(tmpsplptsx)
                spline = splrep(splptsx, splptsy, k=3)
                splblueindex = womget_element(wave, splptsx[0])
                splredindex = womget_element(wave, splptsx[-1])
                splwave = wave[splblueindex:splredindex + 1].copy()
                splineresult = splev(splwave, spline)
                fluxcor = flux.copy()
                fluxcor[splblueindex:splredindex + 1] = splineresult.copy()
                plt.plot(splwave, splineresult, drawstyle='steps-mid')
                print('Is this acceptable')
                answer = yesno('y')
                if (answer == 'y'):
                    flux = fluxcor.copy()
                    spldone = True
                    logging.info(
                        'File {} blotched  with spline from {} to {}'.format(
                            hop[0].obname, wave[splblueindex],
                            wave[splredindex]))
        else:
            done = True
        print('Do another region?')
        another = yesno('n')
        if (another == 'n'):
            done = True
    hop[0].flux = flux.copy()
    return hop
def wommkatmdisp(hop):
    import numpy as np
    import matplotlib.pyplot as plt
    from tmath.wombat.waveparse import waveparse
    from tmath.wombat.inputter import inputter
    from tmath.wombat.airtovac import airtovac
    light_speed = 2.99792458e10
    h_planck = 6.6260755e-27
    k_boltzmann = 1.380658e-16
    print('This routine will create a curve of the dispersion of light')
    print('by the atmosphere in arc seconds per 1 Angstrom bin')
    print('\n')
    print('Enter airmass for the calculation: ')
    airmass = inputter('(default = 1.5): ', 'float', True, 1.5)
    if (airmass < 1.0) or (airmass > 7.0):
        airmass = 1.5
    print('Enter temperature at telescope in degrees Celsius: ')
    temp = inputter('(default = 7C [45F]): ', 'float', True, 7.0)
    if (temp <= -100) or (temp >= 100):
        temp = 7.0
    print('Enter barometric pressure at telescope in mm of Hg: ')
    press = inputter('(default = 600 mm Hg): ', 'float', True, 600.0)
    if (press <= 0):
        press = 600.0
    print('Enter water vapor pressure at telescope in mm of Hg: ')
    water = inputter('(default = 8 mm Hg): ', 'float', True, 8.0)
    if (water <= 0):
        water = 8.0

    waveb, waver = waveparse()
    if (waver < waveb):
        waveb, waver = waver, waveb
    if (waver == waveb):
        waver = waver + 1.
    print('\nOK, calculating dispersion of light in arc seconds over the')
    print('range {}A to {}A at temperature {}C, pressure {} mm Hg,'.format(
        waveb, waver, temp, press))
    print('and water vapor pressure {} mm Hg at airmass {}.'.format(
        water, airmass))
    print('Zero is set at 5000A.')

    wave = np.arange(waveb, waver + 1.)
    vacuum = airtovac(wave)
    lfactor = (1.e4 / vacuum)**2
    waterfactor = water * ((0.0624 - 0.000680 * lfactor) /
                           (1.0 + 0.003661 * temp))
    nstp = 1E-6 * (64.328 + (29498.1 / (146.0 - lfactor)) + (255.4 /
                                                             (41 - lfactor)))
    n = (nstp)*((press*(1.0+(1.049-0.0157*temp)*1E-6*press))/  \
              (720.883*(1.0+0.003661*temp)))-waterfactor*1.0E-6
    n = n + 1.0
    five = airtovac(np.array([5000.0]))
    fivefact = (1.e4 / five)**2
    wfive = water * ((0.0624 - 0.000680 * fivefact) / (1.0 + 0.003661 * temp))
    nstpfive = 1E-6 * (64.328 + (29498.1 / (146.0 - fivefact)) +
                       (255.4 / (41 - fivefact)))
    nfive = (nstpfive)*((press*(1.0+(1.049-0.0157*temp)*1E-6*press))/ \
                        (720.883*(1.0+0.003661*temp)))-wfive*1.0E-6
    nfive = nfive + 1.0
    cosz = 1. / airmass
    tanz = (np.sqrt(1 - cosz**2)) / cosz
    flux = 206265. * (n - nfive) * tanz
    spectxt = 'Atmospheric dispersion curve at z = {}'.format(airmass)
    plt.cla()
    plt.plot(wave, flux, drawstyle='steps-mid')
    plt.title(spectxt)
    hop[0].wave = wave.copy()
    hop[0].flux = flux.copy()
    hop[0].obname = spectxt
    hop[0].var = np.ones(wave.shape)
    hop[0].header = ''
    return hop