Mercurial > ~darius > hgwebdir.cgi > pyinst
comparison logpps.py @ 69:7386f2888508
Make function more configurable
author | Daniel O'Connor <doconnor@gsoft.com.au> |
---|---|
date | Wed, 04 Aug 2021 16:07:44 +0930 |
parents | ffc9292eb00b |
children | 6ffa6fcf278e |
comparison
equal
deleted
inserted
replaced
68:f95db5ea2fe1 | 69:7386f2888508 |
---|---|
26 # | 26 # |
27 # Expected DB schema | 27 # Expected DB schema |
28 # CREATE TABLE ppslog ( | 28 # CREATE TABLE ppslog ( |
29 # name TEXT, | 29 # name TEXT, |
30 # time TIMESTAMP WITH TIME ZONE, | 30 # time TIMESTAMP WITH TIME ZONE, |
31 # delta NUMERIC(15, 12) | 31 # delta12 NUMERIC(15, 12), |
32 # delta13 NUMERIC(15, 12) | |
32 # ); | 33 # ); |
33 | 34 |
34 import datetime | 35 import datetime |
35 import matplotlib.pylab as pylab | 36 import matplotlib.pylab as pylab |
36 import numpy | 37 import numpy |
47 | 48 |
48 # See "TDS2000 Programmer.pdf" | 49 # See "TDS2000 Programmer.pdf" |
49 res = u.ask('*IDN?') | 50 res = u.ask('*IDN?') |
50 print('IDN reports ' + res) | 51 print('IDN reports ' + res) |
51 | 52 |
53 hostname = 'radartest1' | |
54 nchan = 2 | |
52 #dbh = sqlite3.connect('logpps.db') | 55 #dbh = sqlite3.connect('logpps.db') |
53 dbh = psycopg2.connect('host=vm11 user=ppslog dbname=ppslog') | 56 dbh = psycopg2.connect('host=vm11 user=ppslog dbname=ppslog') |
54 test(u, dbh, 'ncu-iono2-tx') | 57 #dbh = None |
58 test(u, nchan, dbh, hostname) | |
55 | 59 |
56 def test(u, dbh = None, name = None): | 60 def test(u, nchan, dbh = None, name = None): |
57 if dbh != None: | 61 if dbh != None: |
58 cur = dbh.cursor() | 62 cur = dbh.cursor() |
59 | 63 |
60 u.write('ACQ:MODE SAMPLE') | 64 u.write('ACQ:MODE SAMPLE') |
61 u.write('ACQ:STATE STOP') | 65 u.write('ACQ:STATE STOP') |
62 | 66 |
63 #u.write('DATA:ENC RIB') # Big endian signed | 67 #u.write('DATA:ENC RIB') # Big endian signed |
64 #u.write('DATA:WIDTH 2') # 2 bytes wide | 68 #u.write('DATA:WIDTH 2') # 2 bytes wide |
65 | 69 |
66 vscale1 = float(u.ask('CH1:SCALE?').split()[1]) | 70 vscales = [] |
67 print(('Channel 1 scale is %.2f volts/div' % (vscale1))) | 71 for i in range(nchan): |
68 | 72 tmp = float(u.ask('CH%d:SCALE?' % (i + 1)).split()[1]) |
69 vscale2 = float(u.ask('CH2:SCALE?').split()[1]) | 73 vscales.append(tmp) |
70 print(('Channel 2 scale is %.2f volts/div' % (vscale2))) | 74 print(('Channel %d scale is %.2f volts/div' % (i + 1, tmp))) |
71 | 75 |
72 hscale = float(u.ask('HOR:MAIN:SCALE?').split()[1]) | 76 hscale = float(u.ask('HOR:MAIN:SCALE?').split()[1]) |
73 print(('Horizontal scale is %.5f nsec/div' % (hscale * 1e9))) | 77 print(('Horizontal scale is %.5f nsec/div' % (hscale * 1e9))) |
74 # TEK2024B doesn't grok HOR:DIV? so hard code 10 (has 8 vertically) | 78 # TEK2024B doesn't grok HOR:DIV? so hard code 10 (has 8 vertically) |
75 acqwindow = hscale * 10.0 | 79 acqwindow = hscale * 10.0 |
76 | 80 |
77 while True: | 81 while True: |
78 ary1, ary2 = acquire(u, vscale1, vscale2) | 82 arys = acquire(u, vscales) |
79 #pylab.plot(ary1) | 83 #for a in arys: |
80 #pylab.plot(ary2) | 84 # pylab.plot(a) |
81 #pylab.show() | 85 #pylab.show() |
82 sampletime = acqwindow / len(ary1) | 86 sampletime = acqwindow / len(arys[0]) |
83 d = getpdiffedge(ary1, ary2) * sampletime | 87 deltas = [] |
88 for i in range(nchan - 1): | |
89 delta = getpdiffedge(arys[0], arys[i + 1]) * sampletime | |
90 deltas.append(delta) | |
91 print('Delta 1-%d is %.1f nsec' % (i + 2, delta * 1e9)) | |
84 | 92 |
85 print('Delta is %.1f nsec' % (d * 1e9)) | |
86 if dbh != None: | 93 if dbh != None: |
87 now = datetime.datetime.now() | 94 now = datetime.datetime.now() |
88 cur.execute('INSERT INTO ppslog(name, time, delta) VALUES(%s, %s, %s)', (name, now, d)) | 95 d12 = deltas[0] |
96 d13 = None | |
97 if nchan > 2: | |
98 d13 = deltas[1] | |
99 cur.execute('INSERT INTO ppslog(name, time, delta12, delta13) VALUES(%s, %s, %s, %s)', (name, now, d12, d13)) | |
89 dbh.commit() | 100 dbh.commit() |
90 | 101 |
91 def getchannel(u, ch, vscale): | 102 def getchannel(u, ch, vscale): |
92 u.write('DAT:SOU CH%d' % (ch)) # Set the curve source to desired channel | 103 u.write('DAT:SOU CH%d' % (ch)) # Set the curve source to desired channel |
93 result = u.ask('CURVE?', 1.0) # Ask for the curve data | 104 result = u.ask('CURVE?', 1.0) # Ask for the curve data |
94 data1 = result[13:] # Chop off the header (should verify this really..) | 105 data1 = buffer(result[13:]) # Chop off the header (should verify this really..) |
95 ary = numpy.frombuffer(data1, dtype = '>h') | 106 ary = numpy.frombuffer(data1, dtype = '>h') |
96 ary = ary / 32768.0 * vscale # Scale to volts | 107 ary = ary / 32768.0 * vscale # Scale to volts |
97 return ary | 108 return ary |
98 | 109 |
99 def acquire(u, vscale1, vscale2): | 110 def acquire(u, vscales): |
100 u.write('ACQ:STATE 1') # Do a single acquisition | 111 u.write('ACQ:STATE 1') # Do a single acquisition |
101 u.write('*OPC?') | 112 u.write('*OPC?') |
102 u.read(2.0) # Wait for it to complete | 113 u.read(2.0) # Wait for it to complete |
103 | 114 |
104 ary1 = getchannel(u, 1, vscale1) | 115 arys = [] |
105 ary2 = getchannel(u, 2, vscale2) | 116 for i in range(len(vscales)): |
117 arys.append(getchannel(u, i + 1, vscales[i])) | |
106 | 118 |
107 return ary1, ary2 | 119 return arys |
108 | 120 |
109 def getpdiffedge(ary1, ary2): | 121 def getpdiffedge(ary1, ary2): |
110 '''Return phase difference in samples between two signals by edge detection''' | 122 '''Return phase difference in samples between two signals by edge detection''' |
111 | 123 |
112 # Rescale to 0-1 | 124 # Rescale to 0-1 |