forked from awade/netgpibdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AG4395A.py
executable file
·407 lines (321 loc) · 12.9 KB
/
AG4395A.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# This module provides data access to Agilent 4395 network analyzer
# Standard Library imports
import re
#import sys
#from math import floor
from numpy import logspace, log10
import time
# Required custom libraries
import netgpib
#import termstatus
####################
# GPIB
####################
def connectGPIB(ipAddress,gpibAddress):
print('Connecting to '+str(ipAddress)+':'+str(gpibAddress)+'...')
gpibObj=netgpib.netGPIB(ipAddress, gpibAddress,tSleep=0.2)
print('Connected.')
print("Instrument ID: ")
idnString=gpibObj.query("*IDN?")
print(idnString.splitlines()[-1])
return(gpibObj)
####################
# Settings helpers
####################
def reset(gpibObj):
# Call reset command, manual states it takes 12 sec to finish
print('Resetting AG4395A...')
gpibObj.command("*RST")
time.sleep(12)
print('Done!')
####################
# Compatibility with old netgpibdata script
####################
def getdata(gpibObj, dataFile, paramFile):
# For compatibility with old netgpibdata
timeStamp = time.strftime('%b %d %Y - %H:%M:%S', time.localtime())
(freq,data)=download(gpibObj)
writeHeader(dataFile, timeStamp)
writeData(dataFile, freq, data, delimiter = ', ')
def getparam(gpibObj, fileRoot, dataFile, paramFile):
# For compatibility with old netgpibdata
timeStamp = time.strftime('%b %d %Y - %H:%M:%S', time.localtime())
writeHeader(paramFile, timeStamp)
writeParams(gpibObj, paramFile)
####################
# Fetching data
####################
def download(gpibObj):
# Put the analyzer on hold
gpibObj.command('HOLD')
#Set the output format to ASCII
# TODO: FORM2 binary transfer is many times faster.
# Figure out the string decoding (read raw in netgpib?)
gpibObj.command('FORM4')
data=[]
freqs=[]
nDisp = int(gpibObj.query('DUAC?')) +1
if nDisp == 1:
chans=[int(gpibObj.query('CHAN2?'))+1]
else:
chans=[1,2]
for chan in chans:
gpibObj.command('CHAN'+str(chan))
freqString = gpibObj.query('OUTPSWPRM?',1024)
chanFreqs = [float(s) for s in re.findall(r'[-+.E0-9]+',freqString)]
freqs.append(chanFreqs)
dataString = gpibObj.query('OUTPDTRC?',1024)
chanData = [float(s) for s in re.findall(r'[-+.E0-9]+',dataString)]
# If we're taking TFs, but just downloading real data,
# discard the null complex parts
if all(val==0 for val in chanData[1::2]):
chanData=chanData[::2]
data.append(chanData)
return(freqs,data)
####################
# Output file writing
####################
def writeHeader(dataFile, timeStamp):
dataFile.write('# AG4395A Measurement - Timestamp: ' + timeStamp+'\n')
def writeData(dataFile, freq, data, delimiter=' '):
print('Writing measurement data to file...')
#Write data vectors
if len(freq) > 1: #Dual chan
if freq[0] == freq[1]: #Shared Freq axis
for i in range(len(freq[0])):
dataFile.write(str(freq[0][i]) + delimiter + str(data[0][i])
+ delimiter + str(data[1][i]) + '\n')
else: #Unequal axes! Kind of awkward to output nicely
print('Unequal Frequency Axes, stacking output')
for i in range(len(freq[0])):
dataFile.write(str(freq[0][i]) + delimiter + str(data[0][i]) + '\n')
# Print unit line?
dataFile.write('# Channel 2 Data\n')
for i in range(len(freq[1])):
dataFile.write(str(freq[1][i]) + delimiter + str(data[1][i]) + '\n')
else: #Single display
for i in range(len(freq[0])):
dataFile.write(str(freq[0][i])+ delimiter +str(data[0][i])+'\n')
####################
# Run measurements
####################
def multiMeasure(gpibObj, params):
# Assuming that measurement has already been set up
if not params.has_key('nSegment') or int(params['nSegment']) == 1:
measure(gpibObj,params)
(freq, data) = download(gpibObj)
else:
nseg = params['nSegment']
nDisp = int(gpibObj.query('DUAC?')[0])+1
# We're doing a pseudo-log sweep!
# Need to find segment limits...
unitDict={'MHz':1e6,'kHz':1e3,'Hz':1.0,'mHz':1e-3}
f1m = re.search('([0-9]+)([A-Za-z]+)',params['startFreq'])
f2m = re.search('([0-9]+)([A-Za-z]+)',params['stopFreq'])
f1 = float(f1m.group(1))*unitDict[f1m.group(2)]
f2 = float(f2m.group(1))*unitDict[f2m.group(2)]
fLims = round(logspace(log10(f1),log10(f2),nseg+1))
# Loop over segments and measure
for ii in range(nseg):
if nDisp == 2:
gpibObj.command('CHAN1')
gpibObj.command('STAR '+str(fLims[ii]))
gpibObj.command('STOP '+str(fLims[ii+1]))
gpibObj.command('CHAN2')
gpibObj.command('STAR '+str(fLims[ii]))
gpibObj.command('STOP '+str(fLims[ii+1]))
print('Segment '+str(ii+1)+' of '+str(nseg)+'...')
measure(gpibObj,params)
(fi, di) =download(gpibObj)
if ii==0:
freq=fi
data=di
else:
freq = [prev+new for (prev,new) in zip(freq,fi)]
data = [prev+new for (prev,new) in zip(data,di)]
def measure(gpibObj, params):
gpibObj.command("TRGS INT")
gpibObj.command('CLES')
gpibObj.command('*SRE 4')
gpibObj.command('ESNB 1')
print('Preparing to trigger...')
nDisp = int(gpibObj.query('DUAC?')[0])+1
nAvg = str(params.get('averages',1))
if nDisp == 2:
gpibObj.command('CHAN1')
gpibObj.command('AVER ON')
gpibObj.command('AVERREST')
gpibObj.command('AVERFACT '+nAvg)
gpibObj.command('CHAN2')
gpibObj.command('AVER ON')
gpibObj.command('AVERFACT '+nAvg)
gpibObj.command('AVERREST')
print('Taking '+nAvg+' averages...')
gpibObj.command('NUMG '+nAvg)
while not int(gpibObj.srq()):
time.sleep(1)
print('Done!')
####################
# Saving and setting measurement parameters
####################
def _parseUnit(string):
# Assumes a series of numbers with a few letters at the end
# e.g. 30.123kHz
prefixD={'n':1e-9,'u':1e-6,'m':1e-3,'k':1e3,'M':1e6,'G':1e9}
unit = ''.join([s for s in string if s.isalpha()])
val = string[:-len(unit)]
mult = prefixD.get(unit[0],1.0)
return mult*float(val)
def setParameters(gpibObj, params):
gpibObj.command('PRES')
time.sleep(5)
gpibObj.command('TRGS INT') # get triggering from GPIB
if params['measType'] == 'Spectrum':
gpibObj.command('SA')
# Set up channel inputs
if params['dualChannel'].lower() == 'dual' and len(params['channels'])==2:
gpibObj.command('DUAC ON')
for jj in range(len(params['channels'])):
gpibObj.command('CHAN'+str(jj+1))
gpibObj.command('MEAS '+params['channels'][jj])
if params['specType'].lower() == 'noise':
gpibObj.command('FMT '+params['specType'][0:5].upper())
elif params['specType'].lower() != 'spectrum':
raise ValueError('specType not parsing!')
# For now, default noise to V^2/Hz and spectrum to dBm
if params['specType'].lower() =='noise':
gpibObj.command('SAUNIT V')
else:
gpibObj.command('SAUNIT DBM')
gpibObj.command('AVERFACT '+str(params['averages']))
gpibObj.command('AVER OFF')
gpibObj.command('STAR '+params['startFreq'])
gpibObj.command('STOP '+params['stopFreq'])
gpibObj.command('BWAUTO ON')
gpibObj.command('BWSRAT '+str(params['bwSpanRatio']))
if isinstance(params['attenuation'], basestring):
if params['attenuation'].lower() == 'auto':
gpibObj.command('ATTAUTO ON')
else:
raise ValueError('Attenuation String not parseable!')
else:
gpibObj.command('ATTAUTO OFF')
gpibObj.command('ATT' +params['channels'][jj] +' ' +str(params['attenuation'])+'DB')
elif params['measType'] == 'TF':
gpibObj.command('NA')
gpibObj.command('DUAC ON')
if not any([params['inputMode'] == s for s in ('AR','BR','AB')]):
raise ValueError('Bad inputMode! Must be "AR", "BR" or "AB"!')
if isinstance(params['attenuation'], basestring):
if params['attenuation'].lower() == 'auto':
gpibObj.command('ATTAUTO ON')
else:
raise ValueError('Attenuation String not parseable!')
else:
gpibObj.command('ATTAUTO OFF')
for chan in params['inputMode']:
gpibObj.command('ATT' + chan +' ' +str(params['attenuation'])+'DB')
gpibObj.command('SWETAUTO') # auto sweep time
if params['sweepType'] == 'Linear':
gpibObj.command('SWPT LINF') # sweep lin freq.
else:
gpibObj.command('SWPT LOGF') # sweep log freq.
IF = params['ifBandwidth'] # Need to convert to int...
if IF == 'auto':
gpibObj.command('BWAUTO ON')
else:
gpibObj.command('BW '+str(IF)) #to set the IF bandwidth
gpibObj.command('POWE '+str(params['excAmp'])) # units are in dBm
gpibObj.command('CHAN1') # choose the active channel
gpibObj.command('MEAS '+params['inputMode'])
if 'mag' in params['dataMode'].lower():
gpibObj.command('FMT LINM')
elif 'reim' in params['dataMode'].lower():
gpibObj.command('FMT REAL')
else:
gpibObj.command('FMT LOGM')
gpibObj.command('CHAN2')
gpibObj.command('MEAS '+params['inputMode'])
if 'reim' in params['dataMode'].lower():
gpibObj.command('FMT IMAG')
else:
gpibObj.command('FMT PHAS')
gpibObj.command('AVER ON') # average ON
gpibObj.command('POIN '+str(params['numOfPoints']))
gpibObj.command('STAR '+params['startFreq'])
gpibObj.command('STOP '+params['stopFreq'])
else:
raise ValueError('Can only set parameters for measType="Spectrum" or'
'"TF".')
time.sleep(15)
print('Parameters set!')
def _joinParam(thingList):
return ', '.join(['{:}'.format(thing) for thing in thingList])
def writeParams(gpibObj, paramFile):
#Check the analyzer mode
measType={1: 'Network Analyzer', 0: 'Spectrum'}[int(gpibObj.query('NA?'))]
nDisp = int(gpibObj.query('DUAC?')) +1
if nDisp == 1:
chans=[int(gpibObj.query('CHAN2?'))+1]
else:
chans=[1,2]
bw=[]
bwAuto=[]
meas=[]
fmt=[]
saUnit=[]
fStart=[]
fStop=[]
nPoint=[]
for chan in chans:
#Change the active channel
gpibObj.command('CHAN'+str(chan))
# Get bandwidth information
bw.append(float(gpibObj.query('BW?')))
bwAuto.append({0: 'Off', 1: 'On'}[int(gpibObj.query('BWAUTO?'))])
# What measurement?
meas.append(gpibObj.query('MEAS?')[:-1])
fmt.append(gpibObj.query('FMT?')[:-1])
if measType=='Spectrum':
saUnit.append(gpibObj.query('SAUNIT?')[:-1])
if 'NOISE' in fmt[-1]: saUnit[-1] += '/rtHz'
else:
excAmp = str(float(gpibObj.query('POWE?'))) + 'dBm'
fStart.append(float(gpibObj.query('STAR?')))
fStop.append(float(gpibObj.query('STOP?')))
nPoint.append(int(gpibObj.query('POIN?')))
fStart = _joinParam(fStart)
fStop = _joinParam(fStop)
bw = _joinParam(bw)
bwAuto = _joinParam(bwAuto)
meas = _joinParam(meas)
fmt = _joinParam(fmt)
saUnit = _joinParam(saUnit)
nPoint = _joinParam(nPoint)
# Get attenuator information
attR = str(int(gpibObj.query('ATTR?'))) + 'dB '
attA = str(int(gpibObj.query('ATTA?'))) + 'dB '
attB = str(int(gpibObj.query('ATTB?'))) + 'dB '
# Averages
nAvg = str(int(gpibObj.query('AVERFACT?')))
print("Writing to the parameter file.")
paramFile.write('#---------- Measurement Parameters ------------\n')
paramFile.write('# Start Frequency (Hz): '+fStart+'\n')
paramFile.write('# Stop Frequency (Hz): '+fStop+'\n')
paramFile.write('# Frequency Points: '+nPoint+'\n')
paramFile.write('# Measurement Format: '+fmt+'\n')
paramFile.write('# Measuremed Input: '+meas+'\n')
paramFile.write('#---------- Analyzer Settings ----------\n')
paramFile.write('# Number of Averages: '+nAvg+'\n')
paramFile.write('# Auto Bandwidth: '+bwAuto+'\n')
paramFile.write('# IF Bandwidth: '+bw+'\n')
paramFile.write('# Input Attenuators (R,A,B): '+attR+attA+attB+'\n')
if measType == 'Spectrum':
paramFile.write('# Units: '+saUnit+'\n')
else:
paramFile.write('# Excitation amplitude = '+excAmp+'\n')
paramFile.write('#---------- Measurement Data ----------\n')
paramFile.write('# [Freq(Hz) ')
for chan in chans:
paramFile.write('Chan '+str(chan)+' ')
paramFile.write(']\n')