-
Notifications
You must be signed in to change notification settings - Fork 0
/
PowerCalib1.py
323 lines (286 loc) · 10.3 KB
/
PowerCalib1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 21 19:56:08 2020
@author: Mai Tai
"""
import pyvisa
import numpy as np
from instrumental import instrument, u, list_instruments
import time
from datetime import datetime
import pickle
import pprint
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import os
from statistics import stdev
from tqdm import tqdm
from scipy.interpolate import interp1d
import nidaqmx
#%% initialize
try:
C = instrument('C')
except:
C = instrument('C')
print("C.serial = " + C.serial)
#C.offset = Offset*u.degree
if __name__ == '__main__':
daq = instrument('daq')
rm = pyvisa.ResourceManager()
Pmeter = rm.open_resource('ASRL3::INSTR')
MaiTai = rm.open_resource('ASRL1::INSTR')
#%%
def Shutter(op):
"""Helper function for instrumental to avoid clutter and make code
more readable
>>>returns string"""
if op == 1:
MaiTai.write("SHUT 1")
#tqdm.write("Shutter Opened")
else:
MaiTai.write("SHUT 0")
#tqdm.write("Shutter Closed")
def Power():
"""Reads power from Gentec TPM300 via VISA commands
The while loop avoids outputting invalid token
>>>returns float
to-do: incorporate different power ranges (itteratively check all avaliable
ranges and chose the best fit. Log this choice)"""
while True:
try:
Pread = Pmeter.query("*READPOWER:")
Power = float(Pread.split('e')[0].split('+')[1])
return Power
except:
continue
def PowAvg():
P = []
for i in range(0,5,1):
p = Power()
P.append(p)
time.sleep(1)
PowerAvg = np.mean(P)
PowerStd = np.std(P)
return PowerAvg, PowerStd
def MoveWav(position):
"""Helper function for instrumental to avoid clutter and make code
more readable
>>>returns null"""
MaiTai.write(f"WAV {position}")
def ReadWav():
"""Helper function for instrumental to avoid clutter and make code
more readable
>>>returns int"""
w = int(MaiTai.query("WAV?").split('n')[0])
return w
def Shutter(op):
"""Helper function for instrumental to avoid clutter and make code
more readable
>>>returns string"""
if op == 1:
MaiTai.write("SHUT 1")
print("Shutter Opened")
else:
MaiTai.write("SHUT 0")
print("Shutter Closed")
def MoveRot(position, Wait):
"""Helper function for instrumental to avoid clutter and make code
more readable
>>>returns null"""
C.move_to(position*u.degree, wait = Wait)
def Photodiode(Duration, Fsamp):
Vlist = []
Vstdlist = []
for i in range(0,5):
V = daq.ai0.read(duration=Duration*u.seconds,fsamp=Fsamp*u.hertz)
Volts = V['Dev1/ai0'].mean()
#Vstd = V['Dev1/ai0'].std()
Vlist.append(Volts)
#Vstdlist.append(Vstd)
Voltage = sum(Vlist)/len(Vlist)
#Vstd = stdev(Vlist,Voltage)*u.volt
return Voltage#, Vstd
#%%
import nidaqmx
from nidaqmx.constants import TerminalConfiguration
from instrumental import u
def PD():
with nidaqmx.Task() as task:
ai_channel = task.ai_channels.add_ai_voltage_chan("Dev1/ai0",terminal_config = TerminalConfiguration.RSE)
r = task.read(number_of_samples_per_channel=100)
m = np.mean(r)
delta = np.std(r)
return m, delta
#%%
def PowerRotLoop(pstart, pstop, pstep, pwait):
"""Main acquisition code to collect power as a function of
rotation stage angle. This can be run separately, but is embedded in
WavLoop for wavelength dependent calibration
************
pstart = Initial angular position in degrees
pstop = Final angular position in degrees
pstep = Angular step size in degrees
pwait = wait time between steps in seconds
>>>>>returns P = 2D Array"""
Shutter(0)
print("Homing")
C.home(wait = True)
time.sleep(5)
print('Homing finished')
Pwr = []
Pwrstd = []
Pos = []
Vol = []
Volstd = []
Shutter(1)
for i in np.arange(pstart-pstep,pstop + pstep,pstep):
if i>=pstart:
MoveRot(i,True)
time.sleep(pwait)
print(str(C.position) + '>>>>>> ' + str(Power()) +' mW')
Pos.append(float(str(C.position).split(' ')[0]))
Pwr.append(PowAvg()[0])
Pwrstd.append(PowAvg()[1])
V, Vstd = PD() ###HARDCODED
Vol.append(float(str(V).split(' ')[0]))
Volstd.append(float(str(Vstd).split(' ')[0]))
else:
MoveRot(i,True)
time.sleep(pwait)
P = np.asarray([Pos,Pwr,Pwrstd,Vol, Volstd])
Shutter(0)
return P
def WavLoop(wavstart,wavstop,wavstep,wavwait,
wpstart, wpstop, wpstep, wpwait,
filename):
"""Main acquisiton code used to collect wavelength dependent calibration.
First, the laser wavelength is set to wavstart. Then, PowerRotLoop is
called to collect Power vs Angle for said wavelength. The laser
wavelength is then set to the next step.
*************
Figure out the inputs yourself, it's pretty self explantory.
********
>>>>>returns W = a list of 2D arrays """
print('Starting Wavelength Loop')
def Sin2(angle,mag,xoffset, yoffset):
return mag*np.sin(angle*2*np.pi/360 - xoffset)**2 + yoffset
def PowerFit(datax,datay):
popt, pcov = curve_fit(Sin2, datax, datay, p0 = [1,1,1])
return popt, pcov
def InvSinSqr(y, mag, xoffset, yoffset):
return (360/(2*np.pi))*(np.arcsin(-np.sqrt((y-yoffset)/mag))+xoffset)
if (950 >= wavstart >= 750 and 950 >= wavstop >= 750):
MoveWav(wavstart)
time.sleep(wavwait*2)
W = []
for w in np.arange(wavstart,wavstop+wavstep,wavstep):
MoveWav(w)
time.sleep(wavwait)
wavelength = ReadWav()
print (f'Starting Power Loop at {wavelength}nm')
Pwr = PowerRotLoop(wpstart, wpstop, wpstep, wpwait)
W.append([wavelength, Pwr])
W = np.asarray(W)
np.save(f'{filename}',W, allow_pickle = True)
#PvA = PowerFit(W)
return W
else:
print('Wavelengths out of range')
#%% Save Function
def SaveAsFiles(filename):
A = np.load(filename, allow_pickle=True)
B = [np.transpose(A[i,1]) for i in np.arange(0,A[:,0].size,1)]
cwd = os.getcwd()
today = datetime.today()
path = 'PowerCalibration' + str(today.date())
os.mkdir(path)
os.chdir(path)
[np.savetxt(f'{A[i,0]}.csv',B[i], delimiter=',') for i in np.arange(len(B))]
os.chdir(cwd)
def Sin2(angle,mag,xoffset, yoffset):
return mag*np.sin(angle*2*np.pi/360 - xoffset)**2 + yoffset
def PowerFit(datax,datay):
popt, pcov = curve_fit(Sin2, datax, datay, p0 = [1,1,1],bounds=([0,0,0], [1000, 10, 10]))
return popt, pcov
def InvSinSqr(y, mag, xoffset, yoffset):
return np.mod((360/(2*np.pi))*(np.arcsin(np.sqrt(np.abs((y-yoffset)/mag)))+xoffset),180)
def PCFit(file):
pc = np.load(file, allow_pickle=True)
wavelengths = pc[:,0]
PC = []
PCcov = []
Angles = []
xx = np.arange(2,21,1)
XX = np.linspace(0,30,100)
for i in range(0,len(pc),1):
params, cov = PowerFit(pc[i,1][0],pc[i,1][1])
PC.append(params)
PCcov.append(cov)
analyticsin = InvSinSqr(XX,*params)
interpangles = interp1d(XX,analyticsin)
angles = interpangles(xx)
Angs = dict(zip(xx,angles))
Angles.append(Angs)
PC = np.asarray(PC)
PCcov = np.asarray(PCcov)
#Angles = np.asarray(Angles)
WavPowAng = dict(zip(wavelengths,Angles))
return PC, PCcov, WavPowAng, pc
# =============================================================================
# def Sin2(angle,mag,xoffset, yoffset):
# return mag*np.sin(angle*2*np.pi/360 - xoffset)**2 + yoffset
# def PowerFit(datax,datay):
# popt, pcov = curve_fit(Sin2, datax, datay, p0 = [1,1,1])
# return popt, pcov
# # =============================================================================
# # def InvSinSqrPos(y, mag, xoffset, yoffset):
# # return (360/(2*np.pi))*(np.arcsin(np.sqrt((y-yoffset)/mag))+xoffset)
# # def InvSinSqrNeg(y, mag, xoffset, yoffset):
# # return (360/(2*np.pi))*(np.arcsin(-np.sqrt((y-yoffset)/mag))+xoffset)
# # =============================================================================
#
#
# def PCFit(file):
# pc = np.load(file, allow_pickle=True)
# wavelengths = pc[:,0]
# PC = []
# PCcov = []
# Angles = []
# xx = np.arange(2,21,1)
# XX = np.arange(0,50,.01)
#
# for i in range(0,len(pc),1):
# params, cov = PowerFit(pc[i,1][0],pc[i,1][1])
# PC.append(params)
# PCcov.append(cov)
# analyticsin = Sin2(XX,*params)
# interpangles = interp1d(XX,analyticsin)
# angles = interpangles(xx)
# Angs = dict(zip(xx,angles))
# Angles.append(Angs)
# PC = np.asarray(PC)
# PCcov = np.asarray(PCcov)
# #Angles = np.asarray(Angles)
# WavPowAng = dict(zip(wavelengths,Angles))
#
# return PC, PCcov, WavPowAng
#
# =============================================================================
# =============================================================================
# #%%
# def Sin2(angle,mag,xoffset, yoffset):
# return mag*np.sin(angle*2*np.pi/360 - xoffset)**2 + yoffset
# # =============================================================================
# # def PowerFit(xdata, ydata):
# # popt, pcov = curve_fit(Sin2, data[0], data[1])
# # return popt, pcov
# # =============================================================================
#
# #%%
# def InvSineSqr(y,y0,xc,w,A):
# return xc + (w/(4*np.pi))*np.arcsin(np.sqrt((y - y0)/A))
# =============================================================================
#%%Notes
"""Parameters for WavLoop shoiuld be ~ (780,920,2,60,0,10,1,5,'filename')
for this to run in ~xxxhrs(last =6hrs)
Further testing is needed to determine optimal parameters"""