Commit d0c0cc957820b7887d568b55fa5f7acaf2ab64df
Committed by
GitHub
1 parent
00bf9bcbf7
Exists in
master
Add files via upload
Showing 2 changed files with 230 additions and 0 deletions Side-by-side Diff
fit_waist.py
| 1 | +from scipy.optimize import curve_fit | |
| 2 | +import csv, numpy, glob | |
| 3 | +from scipy.special import erf | |
| 4 | +import matplotlib.pyplot as plt | |
| 5 | + | |
| 6 | +'''power function to optimize''' | |
| 7 | +def P(x, Po, Pmax, xo, w): | |
| 8 | + return Po+0.5*Pmax*(1.-erf(2.**0.5*(x-xo)/w)) | |
| 9 | + | |
| 10 | + | |
| 11 | +'''load and fit beam section''' | |
| 12 | +files = glob.glob('*.dat') | |
| 13 | +files.sort() | |
| 14 | +data_waist = [] | |
| 15 | +plt.close() | |
| 16 | +fig, p = plt.subplots(2, 1) | |
| 17 | +for f in files: | |
| 18 | + with open(f, 'r') as dest_f: | |
| 19 | + raw = csv.reader(dest_f, delimiter = '\t', quotechar = '"') | |
| 20 | + data = [value for value in raw] | |
| 21 | + data = numpy.asarray(data, dtype = float) | |
| 22 | + xmes = data[:,0] | |
| 23 | + Pmes = data[:,1] | |
| 24 | + | |
| 25 | + '''optimization with non-linear least squares method''' | |
| 26 | + Ppopt, Pcov = curve_fit(P, xmes, Pmes) | |
| 27 | + data_waist.append([int(f[-7:-4]), Ppopt[3]]) | |
| 28 | + | |
| 29 | + '''plot''' | |
| 30 | + p[0].plot(xmes, Pmes, 'o') | |
| 31 | + p[0].plot(numpy.linspace(xmes[0], xmes[-1], 100), P(numpy.linspace(xmes[0], xmes[-1], 100), *Ppopt)) | |
| 32 | + | |
| 33 | +p[0].grid() | |
| 34 | + | |
| 35 | +'''return waist(z) table''' | |
| 36 | +data_waist = numpy.asarray(data_waist, dtype = float) | |
| 37 | +print(data_waist) | |
| 38 | + | |
| 39 | +'''waist function to optimize''' | |
| 40 | +def W(z, w0, z0): | |
| 41 | + return w0*(1.+((z-z0)*1542e-6/(numpy.pi*w0**2))**2)**0.5 | |
| 42 | + | |
| 43 | +popt, cov = curve_fit(W, data_waist[:,0], data_waist[:,1]) | |
| 44 | +print(popt[0], popt[1]) | |
| 45 | + | |
| 46 | +p[1].plot(data_waist[:,0], data_waist[:,1], 'bo') | |
| 47 | +p[1].plot(data_waist[:,0], -data_waist[:,1], 'bo') | |
| 48 | +p[1].plot(numpy.linspace(data_waist[0,0], data_waist[-1,0], 100), W(numpy.linspace(data_waist[0,0], data_waist[-1,0], 100), *popt), 'r') | |
| 49 | +p[1].plot(numpy.linspace(data_waist[0,0], data_waist[-1,0], 100), -W(numpy.linspace(data_waist[0,0], data_waist[-1,0], 100), *popt), 'r') | |
| 50 | +p[1].grid() | |
| 51 | + | |
| 52 | +plt.show() |
pm100d_waist.py
| 1 | +#!/usr/bin/env python | |
| 2 | + | |
| 3 | +import argparse, time, os | |
| 4 | +import matplotlib.pyplot as plt | |
| 5 | + | |
| 6 | +#============================================================================== | |
| 7 | + | |
| 8 | +# Path | |
| 9 | +PATH = os.getcwd() | |
| 10 | +# File footer | |
| 11 | +OFILENAME = '130' | |
| 12 | + | |
| 13 | +#============================================================================== | |
| 14 | + | |
| 15 | +def parse(): | |
| 16 | + """ | |
| 17 | + Specific parsing procedure for transfering data from Thorlabs PM100D. | |
| 18 | + :returns: populated namespace (parser) | |
| 19 | + """ | |
| 20 | + parser = argparse.ArgumentParser(description = 'Acquire data from Thorlabs PM100D', | |
| 21 | + epilog = 'Example: \'./pm100d.py -st 10 -o toto\' logs PM100D every 10 seconds to output file YYYYMMDD-HHMMSS-toto.dat') | |
| 22 | + | |
| 23 | + parser.add_argument('-p', | |
| 24 | + action='store', | |
| 25 | + dest='path', | |
| 26 | + default=PATH, | |
| 27 | + help='Absolute path (default '+PATH+')') | |
| 28 | + | |
| 29 | + parser.add_argument('-o', | |
| 30 | + action='store', | |
| 31 | + dest='ofile', | |
| 32 | + default=OFILENAME, | |
| 33 | + help='Output data filename (default '+OFILENAME+')') | |
| 34 | + | |
| 35 | + args = parser.parse_args() | |
| 36 | + return args | |
| 37 | + | |
| 38 | +#============================================================================== | |
| 39 | + | |
| 40 | +class usbtmc: | |
| 41 | + def __init__(self, device): | |
| 42 | + self.device = device | |
| 43 | + self.FILE = os.open(device, os.O_RDWR) | |
| 44 | + | |
| 45 | + def write(self, command): | |
| 46 | + os.write(self.FILE, command); | |
| 47 | + | |
| 48 | + def read(self, length = 4000): | |
| 49 | + return os.read(self.FILE, length) | |
| 50 | + | |
| 51 | + def getName(self): | |
| 52 | + self.write("*IDN?") | |
| 53 | + return self.read(300) | |
| 54 | + | |
| 55 | + def sendReset(self): | |
| 56 | + self.write("*RST") | |
| 57 | + | |
| 58 | +#============================================================================== | |
| 59 | + | |
| 60 | +class instrument: | |
| 61 | + """Class to control a SCPI compatible instrument""" | |
| 62 | + def __init__(self, device): | |
| 63 | + print 'Connecting to device %s...' %device | |
| 64 | + self.meas = usbtmc(device) | |
| 65 | + self.name = self.meas.getName() | |
| 66 | + print self.name | |
| 67 | + print ' --> Ok' | |
| 68 | + | |
| 69 | + def write(self, command): | |
| 70 | + """Send an arbitrary command directly to the scope""" | |
| 71 | + self.meas.write(command) | |
| 72 | + | |
| 73 | + def read(self, command): | |
| 74 | + """Read an arbitrary amount of data directly from the scope""" | |
| 75 | + return self.meas.read(command) | |
| 76 | + | |
| 77 | + def reset(self): | |
| 78 | + """Reset the instrument""" | |
| 79 | + self.meas.sendReset() | |
| 80 | + | |
| 81 | + def value(self): | |
| 82 | + self.write('MEAS?') | |
| 83 | + return self.read(300) | |
| 84 | + | |
| 85 | +#============================================================================== | |
| 86 | + | |
| 87 | +def acqu_PM100D(instrument, path, ofile): | |
| 88 | + | |
| 89 | + t0 = time.time() | |
| 90 | + filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(t0)) + '-' + ofile + '.dat' | |
| 91 | + print('Opening %s' %filename) | |
| 92 | + data_file = open(filename, 'wr', 0) | |
| 93 | + dx = 0. | |
| 94 | + | |
| 95 | + xmes = [] | |
| 96 | + Pmes = [] | |
| 97 | + plt.ion() | |
| 98 | + fig = plt.figure() | |
| 99 | + ax1 = fig.add_subplot(111) | |
| 100 | + line1, = ax1.plot(xmes, Pmes, 'o') | |
| 101 | + | |
| 102 | + # Infinite loop | |
| 103 | + while True: | |
| 104 | + try: | |
| 105 | + keypressed = raw_input("Press Enter to continue...\n") | |
| 106 | + | |
| 107 | + if keypressed=='': | |
| 108 | + # Power values | |
| 109 | + sensors_values = instrument.value() | |
| 110 | + sensors_values = sensors_values.replace('E', 'e') | |
| 111 | + string = "%f\t%s" % (dx , sensors_values) | |
| 112 | + data_file.write(string) # Write in a file | |
| 113 | + print(string) | |
| 114 | + | |
| 115 | + xmes.append(dx) | |
| 116 | + Pmes.append(float(sensors_values)) | |
| 117 | + | |
| 118 | + dx = dx + 0.05 | |
| 119 | + | |
| 120 | + elif keypressed.isdigit(): | |
| 121 | + print('Closing %s' %filename) | |
| 122 | + data_file.close() | |
| 123 | + t0 = time.time() | |
| 124 | + filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(t0)) + '-' + keypressed + '.dat' | |
| 125 | + print('Opening %s' %filename) | |
| 126 | + data_file = open(filename, 'wr', 0) | |
| 127 | + dx = 0. | |
| 128 | + | |
| 129 | + xmes = [] | |
| 130 | + Pmes = [] | |
| 131 | + line1, = ax1.plot(xmes, Pmes, 'o') | |
| 132 | + | |
| 133 | + else: | |
| 134 | + raise KeyboardInterrupt | |
| 135 | + | |
| 136 | + | |
| 137 | + line1.set_data(xmes, Pmes) | |
| 138 | + ax1.relim() | |
| 139 | + ax1.autoscale_view() | |
| 140 | + fig.canvas.draw() | |
| 141 | + | |
| 142 | + except Exception as ex: | |
| 143 | + print 'Exception during controler data reading: ' + str(ex) | |
| 144 | + | |
| 145 | + except KeyboardInterrupt: | |
| 146 | + print '\n --> Disconnected' | |
| 147 | + data_file.close() | |
| 148 | + | |
| 149 | + # To stop the loop in a clean way | |
| 150 | + break | |
| 151 | + | |
| 152 | +#============================================================================== | |
| 153 | + | |
| 154 | +def main(): | |
| 155 | + """ | |
| 156 | + Main script | |
| 157 | + """ | |
| 158 | + # Parse command line | |
| 159 | + args = parse() | |
| 160 | + # path | |
| 161 | + path = args.path | |
| 162 | + # Data output filename | |
| 163 | + ofile = args.ofile | |
| 164 | + | |
| 165 | + try: | |
| 166 | + pm100 = instrument("/dev/usbtmc0") | |
| 167 | + | |
| 168 | + # acquisition with 2 sec. timeout | |
| 169 | + acqu_PM100D(pm100, path, ofile) | |
| 170 | + | |
| 171 | + except Exception as ex: | |
| 172 | + print 'Oups '+str(ex) | |
| 173 | + print 'Program ending\n' | |
| 174 | + | |
| 175 | +#============================================================================== | |
| 176 | + | |
| 177 | +if __name__ == "__main__": | |
| 178 | + main() |