Esempio n. 1
0
def get_avpow(lvnum, **kwargs):

    T_R = kwargs.get('T_R', 1.)
    op_flat = kwargs.get('op_flat', True)

    import matplotlib.pyplot as plt
    import numpy as np
    import gen_reader as gr

    if op_flat == True:
        flat_T = .89
    if op_flat == False:
        flat_T = 1.

    if len(str(lvnum)) == 1:
        lvnum = '000' + str(lvnum)
    if len(str(lvnum)) == 2:
        lvnum = '00' + str(lvnum)
    if len(str(lvnum)) == 3:
        lvnum = '0' + str(lvnum)
    lv_matrix = gr.reader('/home/chris/anaconda/data/' + str(date) + '/lv/' +
                          str(lvnum) + '_laser_temp.txt',
                          header=False,
                          delimeter=';')
    avpow = np.mean(
        lv_matrix[:, 1]) * T_R * flat_T  #1.57 is power meter compensation

    return avpow
Esempio n. 2
0
def lv_energy(lvnum, **kwargs):

    T_R = kwargs.get('T_R', 1.)
    op_flat = kwargs.get('op_flat', True)

    import matplotlib.pyplot as plt
    import numpy as np
    import gen_reader as gr

    if op_flat == True:
        flat_T = .89
    if op_flat == False:
        flat_T = 1.

    if len(str(lvnum)) == 1:
        lvnum = '000' + str(lvnum)
    if len(str(lvnum)) == 2:
        lvnum = '00' + str(lvnum)
    if len(str(lvnum)) == 3:
        lvnum = '0' + str(lvnum)
    lv_matrix = gr.reader('/home/james/anaconda3/data/' + str(date) + '/lv/' +
                          str(lvnum) + '_laser_temp.txt',
                          header=False,
                          delimeter=';')

    return lv_matrix[:, 1] * T_R * flat_T
Esempio n. 3
0
def data_lists(date):

    import numpy as np
    import gen_reader as gr

    header, full_list = gr.reader('/home/chris/anaconda/data/' + str(date) +
                                  '/run_data.csv',
                                  dtype=int,
                                  delimeter=',')

    runlist = full_list[:, 0]
    lvlist = full_list[:, 1]
    scplist = np.vstack((runlist, full_list[:, 2]))
    pulselist = full_list[:, 3]

    return runlist, lvlist, scplist, pulselist
Esempio n. 4
0
def lv_time(lvnum):

    import matplotlib.pyplot as plt
    import numpy as np
    import gen_reader as gr

    if len(str(lvnum)) == 1:
        lvnum = '000' + str(lvnum)
    if len(str(lvnum)) == 2:
        lvnum = '00' + str(lvnum)
    if len(str(lvnum)) == 3:
        lvnum = '0' + str(lvnum)
    lv_matrix = gr.reader('/home/james/anaconda3/data/' + str(date) + '/lv/' +
                          str(lvnum) + '_laser_temp.txt',
                          header=False,
                          delimeter=';')
    avpow = lv_matrix[:, -1]

    return avpow
Esempio n. 5
0
def lv_ave(runtable, **kwargs):  #obsolete

    savefile = kwargs.get('savefile', False)

    import matplotlib.pyplot as plt
    import numpy as np
    import asciitable as asc
    import gen_reader as gr

    datafile = open(
        runtable,
        'r',
    )

    runnum, lvnum = [], []
    for line in datafile:
        line = line.strip()
        column = line.split(',')
        runnum.append(float(column[0]))
        lvnum.append(column[1])
    datafile.close()
    runnum = np.array(runnum)

    # Do the average on the lv files
    avpow = []
    for i in lvnum:
        lv_matrix = gr.reader('/home/james/anaconda3/data/' + str(date) +
                              '/lv/' + str(i) + '_laser_temp.txt',
                              header=False,
                              delimeter=';')
        avpow.append(float(np.mean(lv_matrix[:, 1])))
    avpow = np.array(avpow)
    runtable = np.vstack((runnum, avpow))
    if savefile != False:
        plt.figure('lv_ave')
        plt.clf()
        plt.plot(runnum, 1000 * avpow, 'bo')
        plt.xlabel('Run')
        plt.ylabel('Laser Power (mW)')
        plt.savefig('/home/james/anaconda3/plots/' + str(date) + '/' +
                    savefile + '.png')
    return runtable
Esempio n. 6
0
def prof_solo(profnum, z, **kwargs):

    # Define kwargs
    imsave = kwargs.get('savefile', 'none')
    fitrange = kwargs.get('fit_range', 'full')
    xstep = kwargs.get('xstep', 'auto')
    units = kwargs.get('units', 'microns')

    # Import modules
    import numpy as np
    import matplotlib.pyplot as plt
    import math
    from scipy.special import erf
    from scipy.optimize import curve_fit
    import pylab
    import os
    import gen_reader as gr
    import utility as ut

    # Define fit function (erf for low to high power)
    def fit_erf(x, a, x_0, w, offset):
        return (a / 2.) * (1 + erf((np.sqrt(2) * (x - x_0)) / w)) + offset

    # Read in data from file
    if xstep == 'auto':
        matrix = gr.reader('/home/chris/anaconda/data/' + str(date) +
                           '/lv/prof' + str(profnum) + '/prof' + str(profnum) +
                           '_amp25_zpos_micron_' + z + '.txt',
                           header=False)
        pos, pows = (.166E-3 / 25.) * matrix[:, 0], matrix[:, 1]
    if xstep != 'auto':
        matrix = gr.reader('/home/chris/anaconda/data/' + str(date) +
                           '/lv/profman' + str.zfill(profnum, 2) + '/profman' +
                           str.zfill(profnum, 2) + '_z-pos-' + units +
                           str.zfill(z, 5) + '_x-step-' + units +
                           str.zfill(xstep, 5) + '.txt',
                           header=False)
        pows = matrix[:, 0]
        pos = 1E-3 * np.arange(0, float(xstep) * len(pows), float(xstep))
        if units == 'minches':
            pos = pos * 25.4

    # Make position array and power array (normalized low to high power)
    if pows[0] > pows[-1]:
        pows_norm = np.flipud(np.array(pows)) / np.amax(np.array(pows))
    else:
        pows_norm = np.array(pows) / np.amax(np.array(pows))
    # Get initial parameter guesses
    param_guess = [1, 0, 0, 0]
    # Guess for w
    bnds = ut.bound_finder(pows_norm, [.1, .9])
    param_guess[2] = pos[bnds[1]] - pos[bnds[0]]
    # Guess for x_0
    for i in range(len(pows)):
        if pows_norm[i] <= .5:
            param_guess[1] = pos[i]
            continue
        else:
            if pows_norm[i] - .5 >= .5 - pows_norm[i - 1]:
                param_guess[1] = pos[i - 1]
                break
            else:
                break
    # Do the fit
    if fitrange != 'full':
        param, covar = curve_fit(fit_erf,
                                 pos[fitrange[0]:fitrange[1]],
                                 pows_norm[fitrange[0]:fitrange[1]],
                                 p0=param_guess)
    if fitrange == 'full':
        param, covar = curve_fit(fit_erf, pos, pows_norm, p0=param_guess)

    # Plot the fits
    x = np.linspace(0, np.amax(pos), 100)
    fit = fit_erf(x, param[0], param[1], param[2], param[3])
    fig = plt.figure('laser_prof')
    plt.clf()
    ax = fig.add_subplot(111)
    plt.plot(pos, pows_norm, 'bo', label='Data')
    plt.plot(x, fit, 'k--', label='Fit')
    plt.axis([0, 1.1 * np.amax(pos), 0, 1.25])
    plt.xlabel('Razor Blade Position (mm)')
    plt.ylabel('Normalized Laser Power')
    plt.legend()
    bbox_props = dict(boxstyle='square', fc='.9', ec='k', alpha=1.0)
    fig_text = 'Spot Size: ' + ut.rnd(1000 * param[2], 2) + ' +/- ' + ut.rnd(
        1000 * np.sqrt(covar[2, 2]), 2) + ' micron'
    #fig_text='Amplitude: '+repr(round(param[0],2))+'\nOffset: '+repr(round(param[1],2))+' mm'+'\nSpot Size: '+repr(1000*round(param[2]),3)+' micron'
    plt.text(.02,
             .97,
             fig_text,
             fontsize=10,
             bbox=bbox_props,
             va='top',
             ha='left',
             transform=ax.transAxes)
    if imsave != 'none':
        ut.create_dir(imsave)
        plt.savefig(imsave + 'beam_profile_' + str.zfill(z, 5) + '.png')
    plt.show()

    return param[2], np.sqrt(covar[2, 2]), pos, pows_norm
Esempio n. 7
0
def beam_profile(maindir, dx_in, imsave, **kwargs):

    # Define Keyword Arguments
    z_units = kwargs.get('z_units', 'standard')
    data_units = kwargs.get('data_units', 'standard')
    afters = kwargs.get('after', False)
    param_guess = kwargs.get('p0', 'find')
    param_guess2 = kwargs.get('p0_waist', 'find')
    zvals = kwargs.get('zvals', 'default')
    fitlim = kwargs.get('fitlim', 'full')
    stages = kwargs.get('stage_type', 'auto')

    # Import Modules
    import numpy as np
    import matplotlib.pyplot as plt
    import math
    from scipy.special import erf
    from scipy.optimize import curve_fit
    import pylab
    import os
    import utility as ut
    import gen_reader as gr

    # Create directory for imsave
    ut.create_dir(imsave)

    # Define fit function (erf for low to high power)
    def fit_erf(x, a, x_0, w):
        return (a / 2.) * (1 + erf((np.sqrt(2) * (x - x_0)) / w))

    # Get the files in the directory and remove '.DS_Store' if it exists
    filedirs = os.listdir(maindir)
    filedirs.sort()

    # If all dx are the same
    if len(dx_in) == len(filedirs):
        dx = dx_in
    else:
        dx = np.zeros((len(filedirs), ), dtype=float)
        dx[:] = dx_in

    # Fit the data to get spot size for each z-position
    beam_width = np.zeros((len(filedirs), ), dtype=float)
    weights = np.zeros((len(filedirs), ), dtype=float)
    z = np.zeros((len(filedirs), ), dtype=int)
    after = np.zeros((len(filedirs), ), dtype=int)
    for filedir in filedirs:
        if stages == 'manual':
            if afters == True:
                pos, pows, z[filedirs.index(filedir)], after[filedirs.index(
                    filedir)] = pow_ave(maindir + '/' + filedir,
                                        dx[filedirs.index(filedir)],
                                        data_units,
                                        zvals=zvals)
            if afters == False:
                pos, pows, z[filedirs.index(filedir)] = pow_ave(
                    maindir + '/' + filedir,
                    dx[filedirs.index(filedir)],
                    data_units,
                    zvals=zvals)
        if stages == 'auto':
            z[filedirs.index(filedir)] = np.float(filedir[-9:-4])
            matrix = gr.reader(maindir + filedir, header=False)
            pos, pows = (.166E-3 / 25.) * matrix[:, 0], matrix[:, 1]
        if stages == 'profman':
            xstep, z[filedirs.index(
                filedir)] = filedir[-9:-4], filedir[-29:-24]
            matrix = gr.reader(maindir + filedir, header=False)
            pows = matrix[:, 0]
            pos = 1E-3 * np.arange(0, float(xstep) * len(pows), float(xstep))

        # Normalize and reorder the data for low to high power
        pows_norm = pows / np.amax(pows)
        if pows_norm[0] > pows_norm[-1]:
            pows_norm = np.flipud(pows_norm)

        # Get initial parameter guesses
        if param_guess == 'find':
            param_guess = [1, 0, 0]
            # Guess for w
            bnds = ut.bound_finder(pows_norm, [.1, .9])
            param_guess[2] = pos[bnds[1]] - pos[bnds[0]]
            for i in range(len(pows)):
                if pows_norm[i] <= .5:
                    param_guess[1] = pos[i]
                    continue
                else:
                    if pows_norm[i] - .5 >= .5 - pows_norm[i - 1]:
                        param_guess[1] = pos[i - 1]
                        break
                    else:
                        break

        # Do the fit
        if fitlim == 'full':
            posf = pos
            pows_normf = pows_norm
        if fitlim != 'full':
            posf = pos[fitlim[0]:fitlim[1]]
            pows_normf = pows_norm[fitlim[0]:fitlim[1]]
        param, covar = curve_fit(fit_erf, posf, pows_normf, p0=param_guess)
        beam_width[filedirs.index(filedir)] = param[2]
        weights[filedirs.index(filedir)] = 1 / np.sqrt(covar[2, 2])

        # Plot Fit
        x = np.linspace(0, pos[-1], 100)
        fit = fit_erf(x, param[0], param[1], param[2])
        fig = plt.figure('profile')
        plt.clf()
        ax = fig.add_subplot(111)
        plt.plot(pos, pows_norm, 'bo', label='Data')
        plt.plot(x, fit, 'k--', label='Fit')
        plt.axis([0, 1.1 * np.amax(pos), 0, 1.25])
        plt.xlabel('Razor Blade Position (mm)')
        plt.ylabel('Normalized Laser Power')
        plt.legend()
        bbox_props = dict(boxstyle='square', fc='.9', ec='k', alpha=1.0)
        fig_text = 'Spot Size: ' + repr(round(
            1000. * param[2], 2)) + ' +/- ' + repr(
                round(1000 * np.sqrt(covar[2, 2]), 2)) + ' micron'
        #fig_text='Amplitude: '+repr(round(param[0],2))+'\nOffset: '+repr(round(param[1],2))+' mm'+'\nSpot Size: '+repr(1000*round(param[2]),3)+' micron'
        plt.text(.02,
                 .97,
                 fig_text,
                 fontsize=10,
                 bbox=bbox_props,
                 va='top',
                 ha='left',
                 transform=ax.transAxes)
        if zvals == 'default':
            pylab.savefig(imsave + '/' + repr(z[filedirs.index(filedir)]) +
                          '_fit.png')
        if zvals != 'default':
            pylab.savefig(imsave + '/' + repr(zvals[filedirs.index(filedir)]) +
                          '_fit.png')
        plt.show()

    # Fit the beam widths to find beam waist
    # Define the fit function
    wlength = 572  # nm

    def fit_waist(z, w_0, f):
        z_0 = (math.pi * np.square(w_0)) / (wlength * 1E-6)
        return w_0 * np.sqrt(1 + np.square((z - f) / z_0))

    # Switch to manual z vals
    if zvals != 'default':
        z = np.array(zvals)

    # Convert z-positions to array with 0 = lowest position and units are mm
    if z_units == 'standard':
        conv = 25.4
    if z_units == 'mm':
        conv = 1
    if z_units == 'micron':
        conv = .001
    z = (z - z[0]) * conv

    # Set initial parameters for beam fit
    if param_guess2 == 'find':
        # Get initial parameter guesses
        param_guess2 = []
        slope = (beam_width[1] - beam_width[0]) / (z[1])
        theta = 2 * np.arctan(np.absolute(slope))
        param_guess2.append(2 * (wlength * 1E-6) / (math.pi * theta))
        param_guess2.append(-beam_width[0] / slope)

    # Normalize the weights
    weights_n = weights / np.amax(weights)

    # Do the fit
    param2, covar2 = curve_fit(fit_waist,
                               z,
                               beam_width,
                               p0=param_guess2,
                               sigma=weights_n)

    # Plot the fit and data
    z_cont = np.linspace(0, np.amax(z), 100)
    fit2 = fit_waist(z_cont, param2[0], param2[1])
    fig = plt.figure('Beam Fit')
    plt.clf()
    ax = fig.add_subplot(111)
    plt.plot(z, beam_width, 'bo', label='Data')
    plt.plot(z_cont, fit2, 'k--', label='Fit')
    plt.axis([0, np.amax(z), 0, 1.25 * np.amax(beam_width)])
    plt.xlabel('z (mm)')
    plt.ylabel('Spot Size (mm)')
    plt.legend()
    plt.errorbar(z, beam_width, xerr=None, yerr=1 / weights, fmt=None)
    bbox_props = dict(boxstyle='square', fc='.9', ec='k', alpha=1.0)
    fig_text = 'Beam Waist: ' + repr(round(
        1000 * param2[0], 2)) + ' +/- ' + repr(
            round(1000 * np.sqrt(covar2[0, 0]),
                  2)) + ' micron \nWaist Pos: ' + repr(round(
                      param2[1], 2)) + ' +/- ' + repr(
                          round(np.sqrt(covar2[1, 1]), 2)) + ' mm'
    plt.text(.02,
             .97,
             fig_text,
             fontsize=10,
             bbox=bbox_props,
             va='top',
             ha='left',
             transform=ax.transAxes)
    pylab.savefig(imsave + '/beam_fit.png')
    plt.show()