Commit fc102800db58ac77d0e40fc9e8520b36b8542739
1 parent
8b2c182ee1
Exists in
master
better method to get x pos from filename
Showing 1 changed file with 3 additions and 1 deletions Inline Diff
fit_waist.py
1 | #!/usr/bin/python | |||
2 | ||||
from scipy.optimize import curve_fit | 1 | 3 | from scipy.optimize import curve_fit | |
import csv, numpy, glob | 2 | 4 | import csv, numpy, glob | |
from scipy.special import erf | 3 | 5 | from scipy.special import erf | |
import matplotlib.pyplot as plt | 4 | 6 | import matplotlib.pyplot as plt | |
5 | 7 | |||
'''power function to optimize''' | 6 | 8 | '''power function to optimize''' | |
def P(x, Po, Pmax, xo, w): | 7 | 9 | def P(x, Po, Pmax, xo, w): | |
return Po+0.5*Pmax*(1.-erf(2.**0.5*(x-xo)/w)) | 8 | 10 | return Po+0.5*Pmax*(1.-erf(2.**0.5*(x-xo)/w)) | |
9 | 11 | |||
10 | 12 | |||
'''load and fit beam section''' | 11 | 13 | '''load and fit beam section''' | |
files = glob.glob('*.dat') | 12 | 14 | files = glob.glob('*.dat') | |
files.sort() | 13 | 15 | files.sort() | |
data_waist = [] | 14 | 16 | data_waist = [] | |
plt.close() | 15 | 17 | plt.close() | |
fig, p = plt.subplots(2, 1) | 16 | 18 | fig, p = plt.subplots(2, 1) | |
for f in files: | 17 | 19 | for f in files: | |
with open(f, 'r') as dest_f: | 18 | 20 | with open(f, 'r') as dest_f: | |
raw = csv.reader(dest_f, delimiter = '\t', quotechar = '"') | 19 | 21 | raw = csv.reader(dest_f, delimiter = '\t', quotechar = '"') | |
data = [value for value in raw] | 20 | 22 | data = [value for value in raw] | |
data = numpy.asarray(data, dtype = float) | 21 | 23 | data = numpy.asarray(data, dtype = float) | |
xmes = data[:,0] | 22 | 24 | xmes = data[:,0] | |
Pmes = data[:,1] | 23 | 25 | Pmes = data[:,1] | |
24 | 26 | |||
'''optimization with non-linear least squares method''' | 25 | 27 | '''optimization with non-linear least squares method''' | |
Ppopt, Pcov = curve_fit(P, xmes, Pmes, method = 'trf') | 26 | 28 | Ppopt, Pcov = curve_fit(P, xmes, Pmes, method = 'trf') | |
data_waist.append([int(f[-7:-4]), Ppopt[3]]) | 27 | 29 | data_waist.append([int(f.split('-')[-1].split('.')[0]), Ppopt[3]]) | |
28 | 30 | |||
'''plot''' | 29 | 31 | '''plot''' | |
p[0].plot(xmes, Pmes, 'o') | 30 | 32 | p[0].plot(xmes, Pmes, 'o') | |
p[0].plot(numpy.linspace(xmes[0], xmes[-1], 100), P(numpy.linspace(xmes[0], xmes[-1], 100), *Ppopt)) | 31 | 33 | p[0].plot(numpy.linspace(xmes[0], xmes[-1], 100), P(numpy.linspace(xmes[0], xmes[-1], 100), *Ppopt)) | |
32 | 34 | |||
p[0].grid() | 33 | 35 | p[0].grid() | |
34 | 36 | |||
'''return waist(z) table''' | 35 | 37 | '''return waist(z) table''' | |
data_waist = numpy.asarray(data_waist, dtype = float) | 36 | 38 | data_waist = numpy.asarray(data_waist, dtype = float) | |
print(data_waist) | 37 | 39 | print(data_waist) | |
38 | 40 | |||
'''waist function to optimize''' | 39 | 41 | '''waist function to optimize''' | |
def W(z, w0, z0): | 40 | 42 | def W(z, w0, z0): | |
return w0*(1.+((z-z0)*1542e-6/(numpy.pi*w0**2))**2)**0.5 | 41 | 43 | return w0*(1.+((z-z0)*1542e-6/(numpy.pi*w0**2))**2)**0.5 | |
42 | 44 |