Commit c81fde478cab41c8449fd9423b8cb51ac9906e9c
1 parent
7fa40df51d
Exists in
master
replace 4 spaces by tabs
Showing 2 changed files with 152 additions and 152 deletions Side-by-side Diff
fit_waist.py
... | ... | @@ -5,7 +5,7 @@ |
5 | 5 | |
6 | 6 | '''power function to optimize''' |
7 | 7 | def P(x, Po, Pmax, xo, w): |
8 | - return Po+0.5*Pmax*(1.-erf(2.**0.5*(x-xo)/w)) | |
8 | + return Po+0.5*Pmax*(1.-erf(2.**0.5*(x-xo)/w)) | |
9 | 9 | |
10 | 10 | |
11 | 11 | '''load and fit beam section''' |
12 | 12 | |
13 | 13 | |
... | ... | @@ -15,20 +15,20 @@ |
15 | 15 | plt.close() |
16 | 16 | fig, p = plt.subplots(2, 1) |
17 | 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] | |
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 | 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]), abs(Ppopt[3])]) | |
25 | + '''optimization with non-linear least squares method''' | |
26 | + Ppopt, Pcov = curve_fit(P, xmes, Pmes) | |
27 | + data_waist.append([int(f[-7:-4]), abs(Ppopt[3])]) | |
28 | 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)) | |
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 | 32 | |
33 | 33 | p[0].grid() |
34 | 34 | |
... | ... | @@ -38,7 +38,7 @@ |
38 | 38 | |
39 | 39 | '''waist function to optimize''' |
40 | 40 | def W(z, w0, z0): |
41 | - return w0*(1.+((z-z0)*1542e-6/(numpy.pi*w0**2))**2)**0.5 | |
41 | + return w0*(1.+((z-z0)*1542e-6/(numpy.pi*w0**2))**2)**0.5 | |
42 | 42 | |
43 | 43 | popt, cov = curve_fit(W, data_waist[:,0], data_waist[:,1]) |
44 | 44 | print(popt[0], popt[1]) |
pm100d_waist.py
... | ... | @@ -13,167 +13,167 @@ |
13 | 13 | #============================================================================== |
14 | 14 | |
15 | 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') | |
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 | 22 | |
23 | - parser.add_argument('-p', | |
24 | - action='store', | |
25 | - dest='path', | |
26 | - default=PATH, | |
27 | - help='Absolute path (default '+PATH+')') | |
23 | + parser.add_argument('-p', | |
24 | + action='store', | |
25 | + dest='path', | |
26 | + default=PATH, | |
27 | + help='Absolute path (default '+PATH+')') | |
28 | 28 | |
29 | - parser.add_argument('-o', | |
30 | - action='store', | |
31 | - dest='ofile', | |
32 | - default=OFILENAME, | |
33 | - help='Output data filename (default '+OFILENAME+')') | |
29 | + parser.add_argument('-o', | |
30 | + action='store', | |
31 | + dest='ofile', | |
32 | + default=OFILENAME, | |
33 | + help='Output data filename (default '+OFILENAME+')') | |
34 | 34 | |
35 | - args = parser.parse_args() | |
36 | - return args | |
35 | + args = parser.parse_args() | |
36 | + return args | |
37 | 37 | |
38 | 38 | #============================================================================== |
39 | 39 | |
40 | 40 | class usbtmc: |
41 | - def __init__(self, device): | |
42 | - self.device = device | |
43 | - self.FILE = os.open(device, os.O_RDWR) | |
41 | + def __init__(self, device): | |
42 | + self.device = device | |
43 | + self.FILE = os.open(device, os.O_RDWR) | |
44 | 44 | |
45 | - def write(self, command): | |
46 | - os.write(self.FILE, command); | |
45 | + def write(self, command): | |
46 | + os.write(self.FILE, command); | |
47 | 47 | |
48 | - def read(self, length = 4000): | |
49 | - return os.read(self.FILE, length) | |
48 | + def read(self, length = 4000): | |
49 | + return os.read(self.FILE, length) | |
50 | 50 | |
51 | - def getName(self): | |
52 | - self.write("*IDN?") | |
53 | - return self.read(300) | |
51 | + def getName(self): | |
52 | + self.write("*IDN?") | |
53 | + return self.read(300) | |
54 | 54 | |
55 | - def sendReset(self): | |
56 | - self.write("*RST") | |
55 | + def sendReset(self): | |
56 | + self.write("*RST") | |
57 | 57 | |
58 | 58 | #============================================================================== |
59 | 59 | |
60 | 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' | |
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 | 68 | |
69 | - def write(self, command): | |
70 | - """Send an arbitrary command directly to the scope""" | |
71 | - self.meas.write(command) | |
69 | + def write(self, command): | |
70 | + """Send an arbitrary command directly to the scope""" | |
71 | + self.meas.write(command) | |
72 | 72 | |
73 | - def read(self, command): | |
74 | - """Read an arbitrary amount of data directly from the scope""" | |
75 | - return self.meas.read(command) | |
73 | + def read(self, command): | |
74 | + """Read an arbitrary amount of data directly from the scope""" | |
75 | + return self.meas.read(command) | |
76 | 76 | |
77 | - def reset(self): | |
78 | - """Reset the instrument""" | |
79 | - self.meas.sendReset() | |
77 | + def reset(self): | |
78 | + """Reset the instrument""" | |
79 | + self.meas.sendReset() | |
80 | 80 | |
81 | - def value(self): | |
82 | - self.write('MEAS?') | |
83 | - return self.read(300) | |
81 | + def value(self): | |
82 | + self.write('MEAS?') | |
83 | + return self.read(300) | |
84 | 84 | |
85 | 85 | #============================================================================== |
86 | 86 | |
87 | 87 | def acqu_PM100D(instrument, path, ofile): |
88 | 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. | |
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 | 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') | |
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 | 101 | |
102 | - # Infinite loop | |
103 | - while True: | |
104 | - try: | |
105 | - keypressed = raw_input("Press Enter to continue...\n") | |
102 | + # Infinite loop | |
103 | + while True: | |
104 | + try: | |
105 | + keypressed = raw_input("Press Enter to continue...\n") | |
106 | 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) | |
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 | 114 | |
115 | - xmes.append(dx) | |
116 | - Pmes.append(float(sensors_values)) | |
115 | + xmes.append(dx) | |
116 | + Pmes.append(float(sensors_values)) | |
117 | 117 | |
118 | - dx = dx + 0.05 | |
118 | + dx = dx + 0.05 | |
119 | 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. | |
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 | 128 | |
129 | - xmes = [] | |
130 | - Pmes = [] | |
131 | - line1, = ax1.plot(xmes, Pmes, 'o') | |
129 | + xmes = [] | |
130 | + Pmes = [] | |
131 | + line1, = ax1.plot(xmes, Pmes, 'o') | |
132 | 132 | |
133 | - else: | |
134 | - raise KeyboardInterrupt | |
133 | + else: | |
134 | + raise KeyboardInterrupt | |
135 | 135 | |
136 | 136 | |
137 | - line1.set_data(xmes, Pmes) | |
138 | - ax1.relim() | |
139 | - ax1.autoscale_view() | |
140 | - fig.canvas.draw() | |
137 | + line1.set_data(xmes, Pmes) | |
138 | + ax1.relim() | |
139 | + ax1.autoscale_view() | |
140 | + fig.canvas.draw() | |
141 | 141 | |
142 | - except Exception as ex: | |
143 | - print 'Exception during controler data reading: ' + str(ex) | |
142 | + except Exception as ex: | |
143 | + print 'Exception during controler data reading: ' + str(ex) | |
144 | 144 | |
145 | - except KeyboardInterrupt: | |
146 | - print '\n --> Disconnected' | |
147 | - data_file.close() | |
145 | + except KeyboardInterrupt: | |
146 | + print '\n --> Disconnected' | |
147 | + data_file.close() | |
148 | 148 | |
149 | - # To stop the loop in a clean way | |
150 | - break | |
149 | + # To stop the loop in a clean way | |
150 | + break | |
151 | 151 | |
152 | 152 | #============================================================================== |
153 | 153 | |
154 | 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 | |
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 | 164 | |
165 | - try: | |
166 | - pm100 = instrument("/dev/usbtmc0") | |
165 | + try: | |
166 | + pm100 = instrument("/dev/usbtmc0") | |
167 | 167 | |
168 | - # acquisition with 2 sec. timeout | |
169 | - acqu_PM100D(pm100, path, ofile) | |
168 | + # acquisition with 2 sec. timeout | |
169 | + acqu_PM100D(pm100, path, ofile) | |
170 | 170 | |
171 | - except Exception as ex: | |
172 | - print 'Oups '+str(ex) | |
173 | - print 'Program ending\n' | |
171 | + except Exception as ex: | |
172 | + print 'Oups '+str(ex) | |
173 | + print 'Program ending\n' | |
174 | 174 | |
175 | 175 | #============================================================================== |
176 | 176 | |
177 | 177 | if __name__ == "__main__": |
178 | - main() | |
178 | + main() |