-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetspec.py
298 lines (247 loc) · 10.9 KB
/
getspec.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
import numpy, time
from sys import platform
# Caleb's programs
import os
from profunc import windir
from LabJack_control import LJ_streamTP
extra_sleep_fraction = 0.01 # fraction of the total sweep time to add to waiting time for a singe sweep
extra_sleep_float = 0.0 # seconds added to total sweep time
min_sleep_time = 120
maxDiffTime = 120 # extra time given to finish a sweep (if needed) seconds
GPIB_str = "GPIB0::5::INSTR"
try:
import visa
sa = visa.instrument(GPIB_str)
except:
pass
def is_number(value):
try:
float(value)
return True
except ValueError:
return False
def start_spec_grab(spec_filename,
verbose=False, linear_sc=True,
freq_start=0.0, freq_stop=5.0,
sweep_time='AUTO', video_band=10, resol_band=30, attenu=0,
aveNum=1, lin_ref_lev=500):
spec_filename = windir(spec_filename)
#################################
### format numbers to strings ###
#################################
freq_start_str = str('%1.3f' % freq_start) + " GHz"
freq_stop_str = str('%1.3f' % freq_stop ) + " GHz"
if is_number(sweep_time):
sweep_time_str = str('%3.1f' % sweep_time) + " S"
else:
sweep_time_str = sweep_time
if is_number(video_band):
video_band_str = str('%4.0f' % video_band ) + " kHz"
else:
video_band_str = video_band
if is_number(resol_band):
resol_band_str = str('%4.0f' % resol_band ) + " kHz"
else:
resol_band_str = resol_band
attenu_str = str(attenu)+"DB"
#########################
### Set Sweep Options ###
#########################
# set the start frequency
sa.write("FA " + freq_start_str)
#set the stop frequency
sa.write("FB " + freq_stop_str )
# set the sweep time
sa.write("ST " + sweep_time_str)
# set the video bandwidth resolution
sa.write("VB " + video_band_str)
# set the video bandwidth resolution
sa.write("RB " + resol_band_str)
# set the attenuation on the input\
sa.write("AT " + attenu_str)
# averaging is turned on or off here
sa.write("VAVG " + str(aveNum))
#set the scale
if linear_sc:
sa.write("LN")
# Reference Level
sa.write("RL " + str('%3.f' % lin_ref_lev) +" uV")
else:
sa.write("LG 10DB")
# put the 'trace data format' of returned values as real numbers, option P
sa.write('tdf P')
sa.write('ST ?')
sweep_time_float = float(sa.read())
#print "sweep time float:", sweep_time_float
#################
### The Sweep ###
#################
# trigger a new sweep to start
sa.write('clrw tra')
#sa.write('TS')
sweep_sleep = (sweep_time_float*aveNum)*(1+extra_sleep_fraction) + extra_sleep_float
if sweep_sleep < min_sleep_time:
sweep_sleep = min_sleep_time
if verbose:
print "spectrometer sweep, frequency start: "+ freq_start_str + ' frequency stop: '+freq_stop_str
return sweep_sleep
def finish_spec_grab(spec_filename,freq_start=0., freq_stop=5., VAVG=1, verbose=False, linear_sc=False):
freq_list=list(numpy.arange(freq_start, freq_stop,((freq_stop-freq_start)/600.0)))
freq_list.append(freq_stop)
# get the trace data from the sweep that was just preformed
finished=False
startTime=time.time()
while not finished:
diffTime = time.time() - startTime
sa.write('Done ?')
done_output = sa.read()
sa.write('VAVG ?')
avag_output=sa.read()
print avag_output
if (((int(done_output)==1) and (int(avag_output)==VAVG)) or (maxDiffTime < diffTime)):
finished=True
else:
time.sleep(1)
if verbose: print 'Extra spectra time'
sa.write('tra?')
#count = 0
#while True:
# if 5 <= count:
# break
# sa.write("DONE?")
# if sa.read() == '1':
# break
# else:
# time.sleep(1)
# count = count + 1
raw_trace = sa.read()
#####################################
### Save the trace data to a file ###
#####################################
trace_list = raw_trace.rsplit(',')
if not os.path.isfile(spec_filename):
n = open(spec_filename, 'w')
if linear_sc:
n.write('GHz,pwr\n')
else:
n.write('GHz,dB\n')
else:
n = open(spec_filename, 'a')
for freq_index in range(len(freq_list)):
write_line = str(freq_list[freq_index]) + "," + str(trace_list[freq_index]) + "\n"
n.write(write_line)
n.close()
# check for errors
sa.write('ERR?')
error_code = sa.read()
if error_code == '0':
if verbose:
print "no errors from spectrum analyzer"
elif error_code == '112':
print error_code, 'error code: unrecognized command'
elif error_code == '109':
print error_code, 'error code: Ctrl Fail, Analyzer was unable to take \
control of the bus'
else:
print error_code, "error code"
return
def getspec(spec_filename,
verbose=False, linear_sc=True,
freq_start=0.0, freq_stop=5.0,
sweep_time='AUTO', video_band=10, resol_band=30, attenu=0,
aveNum=1, lin_ref_lev=500):
sweep_sleep = start_spec_grab(spec_filename=spec_filename,
verbose=verbose, linear_sc=linear_sc,
freq_start=freq_start, freq_stop=freq_stop,
sweep_time=sweep_time, video_band=video_band,
resol_band=resol_band, attenu=attenu,
aveNum=aveNum, lin_ref_lev=lin_ref_lev)
if verbose:print "Sleeping for",sweep_sleep,'seconds'
time.sleep(sweep_sleep)
finish_spec_grab(spec_filename, freq_start=freq_start, freq_stop=freq_stop, VAVG=aveNum,
verbose=verbose, linear_sc=linear_sc)
return
def getspecPlusTP(spec_filename, TP_filename, TPSampleFrequency, verbose=False, linear_sc=True,
freq_start=0.0, freq_stop=5.0, sweep_time='AUTO', video_band=10, resol_band=30, attenu=0,
aveNum=1, lin_ref_lev=500, get_total_power=True):
sweep_sleep = start_spec_grab(spec_filename=spec_filename,
verbose=verbose, linear_sc=linear_sc,
freq_start=freq_start, freq_stop=freq_stop,
sweep_time=sweep_time, video_band=video_band,
resol_band=resol_band, attenu=attenu,
aveNum=aveNum, lin_ref_lev=lin_ref_lev)
if get_total_power:
if verbose:
print "Getting total power from the LabJack while sweeping for " + str('%2.3f' % sweep_sleep) + "s"
# let the LabJack get total power data while the spectral sweep is taking place
LJ_streamTP(TP_filename, TPSampleFrequency, sweep_sleep, verbose)
finish_spec_grab(spec_filename, freq_start=freq_start, freq_stop=freq_stop, VAVG=aveNum,
verbose=verbose, linear_sc=linear_sc)
return
def get_multi_band_spec(spec_filename, TP_filename, TPSampleFrequency, verbose=False, linear_sc=True,
spec_freq_vector=[], sweep_time='AUTO', video_band=10, resol_band=30, attenu=0,
aveNum=1, lin_ref_lev=500, get_total_power=True):
len_spec_freq_vector = len(spec_freq_vector)
if len_spec_freq_vector < 2:
print "The frequency vector has less than 2 elements the function get_multi_band_specs requires a frequency\n"+\
"vector with a length of 2 or greater. A length of 2 specifies a single band to sweep. This function\n"+\
"return without doing anything. Bummer."
else:
for band_index in range(len_spec_freq_vector-1):
getspecPlusTP(spec_filename=spec_filename, TP_filename=TP_filename,
TPSampleFrequency=TPSampleFrequency, verbose=verbose, linear_sc=linear_sc,
freq_start=spec_freq_vector[band_index], freq_stop=spec_freq_vector[band_index+1],
sweep_time=sweep_time, video_band=video_band, resol_band=resol_band, attenu=attenu,
aveNum=aveNum, lin_ref_lev=lin_ref_lev, get_total_power=get_total_power)
return
def LOfreqSweep():
from LOinput import setfreq
from time import sleep
from control import setmag_highlow, setSIS_only, setfeedback
sleep_time = 10
sisBias=59039
sisShot=55000
LOfreq_list = list(range(650,693))
freq_vector = [0.4,1.0,1.6,2.2,2.8,3.4,4.0,4.6,5.2]
test_dir = 'C:\\Users\\chwheele\\Google Drive\\Kappa\\NA38\\IVsweep\\IFsweep\\'
setfeedback(True)
setmag_highlow(100000)
if not os.path.isdir(test_dir):
os.mkdir(test_dir)
for LOfreq in LOfreq_list:
setfreq(LOfreq)
# biased SIS junction
setSIS_only(sispot=sisBias,feedback=True)
sleep(sleep_time)
for freq_index in range(len(freq_vector)-1):
getspecPlusTP(spec_filename=test_dir+str(LOfreq)+'spec_biased'+'.csv',
TP_filename=test_dir+str(LOfreq)+'_biased.csv',
TPSampleFrequency=100, verbose=True, linear_sc=True,
freq_start=freq_vector[freq_index], freq_stop=freq_vector[freq_index+1], sweep_time='AUTO', video_band=30, resol_band=30, attenu=0,
aveNum=16, lin_ref_lev=100)
# normal SIS junction for shot noise source
setSIS_only(sispot=sisShot,feedback=True)
sleep(sleep_time)
for freq_index in range(len(freq_vector)-1):
getspecPlusTP(spec_filename=test_dir+str(LOfreq)+'spec_biased'+'.csv',
TP_filename=test_dir+str(LOfreq)+'_biased.csv',
TPSampleFrequency=100, verbose=True, linear_sc=True,
freq_start=freq_vector[freq_index], freq_stop=freq_vector[freq_index+1], sweep_time='AUTO',
video_band=30, resol_band=30, attenu=0,
aveNum=16, lin_ref_lev=100)
return
#######################
###### Test Area ######
#######################
if __name__ == "__main__":
# LOfreqSweep()
freq_vector = [0,0.4,1.0,1.6,2.2,2.8,3.4,4.0,4.6,5.2,11.2,23.2]
for freq_index in range(len(freq_vector)-1):
spec_filename='C:\\Users\\chwheele\\Google Drive\\Kappa\\NA38\\IVsweep'+\
'\\test\\testSpec'+str(freq_index+1)+'.csv'
TP_filename='C:\\Users\\chwheele\\Google Drive\\Kappa\\NA38\\IVsweep'+\
'\\test\\testTP'+str(freq_index+1)+'.csv'
getspec(spec_filename=spec_filename, verbose=True, linear_sc=True,
freq_start=freq_vector[freq_index], freq_stop=freq_vector[freq_index+1], sweep_time='AUTO',
video_band=30, resol_band=30, attenu=0,
aveNum=16, lin_ref_lev=100)