diff 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
line wrap: on
line diff
--- a/logpps.py	Tue Jan 19 17:32:57 2021 +1030
+++ b/logpps.py	Wed Aug 04 16:07:44 2021 +0930
@@ -28,7 +28,8 @@
 # CREATE TABLE ppslog (
 #   name TEXT,
 #   time TIMESTAMP WITH TIME ZONE,
-#   delta NUMERIC(15, 12)
+#   delta12 NUMERIC(15, 12),
+#   delta13 NUMERIC(15, 12)
 # );
 
 import datetime
@@ -49,11 +50,14 @@
     res = u.ask('*IDN?')
     print('IDN reports ' + res)
 
+    hostname = 'radartest1'
+    nchan = 2
     #dbh = sqlite3.connect('logpps.db')
     dbh = psycopg2.connect('host=vm11 user=ppslog dbname=ppslog')
-    test(u, dbh, 'ncu-iono2-tx')
+    #dbh = None
+    test(u, nchan, dbh, hostname)
 
-def test(u, dbh = None, name = None):
+def test(u, nchan, dbh = None, name = None):
     if dbh != None:
         cur = dbh.cursor()
 
@@ -63,11 +67,11 @@
     #u.write('DATA:ENC RIB')		# Big endian signed
     #u.write('DATA:WIDTH 2')		# 2 bytes wide
 
-    vscale1 = float(u.ask('CH1:SCALE?').split()[1])
-    print(('Channel 1 scale is %.2f volts/div' % (vscale1)))
-
-    vscale2 = float(u.ask('CH2:SCALE?').split()[1])
-    print(('Channel 2 scale is %.2f volts/div' % (vscale2)))
+    vscales = []
+    for i in range(nchan):
+        tmp = float(u.ask('CH%d:SCALE?' % (i + 1)).split()[1])
+        vscales.append(tmp)
+        print(('Channel %d scale is %.2f volts/div' % (i + 1, tmp)))
 
     hscale = float(u.ask('HOR:MAIN:SCALE?').split()[1])
     print(('Horizontal scale is %.5f nsec/div' % (hscale * 1e9)))
@@ -75,36 +79,44 @@
     acqwindow = hscale * 10.0
 
     while True:
-        ary1, ary2 = acquire(u, vscale1, vscale2)
-        #pylab.plot(ary1)
-        #pylab.plot(ary2)
+        arys = acquire(u, vscales)
+        #for a in arys:
+        #    pylab.plot(a)
         #pylab.show()
-        sampletime = acqwindow / len(ary1)
-        d = getpdiffedge(ary1, ary2) * sampletime
+        sampletime = acqwindow / len(arys[0])
+        deltas = []
+        for i in range(nchan - 1):
+            delta = getpdiffedge(arys[0], arys[i + 1]) * sampletime
+            deltas.append(delta)
+            print('Delta 1-%d is %.1f nsec' % (i + 2, delta * 1e9))
 
-        print('Delta is %.1f nsec' % (d * 1e9))
         if dbh != None:
             now = datetime.datetime.now()
-            cur.execute('INSERT INTO ppslog(name, time, delta) VALUES(%s, %s, %s)', (name, now, d))
+            d12 = deltas[0]
+            d13 = None
+            if nchan > 2:
+                d13 = deltas[1]
+            cur.execute('INSERT INTO ppslog(name, time, delta12, delta13) VALUES(%s, %s, %s, %s)', (name, now, d12, d13))
             dbh.commit()
 
 def getchannel(u, ch, vscale):
         u.write('DAT:SOU CH%d' % (ch))	# Set the curve source to desired channel
         result = u.ask('CURVE?', 1.0)	# Ask for the curve data
-        data1 = result[13:]		# Chop off the header (should verify this really..)
+        data1 = buffer(result[13:])		# Chop off the header (should verify this really..)
         ary = numpy.frombuffer(data1, dtype = '>h')
         ary = ary / 32768.0 * vscale # Scale to volts
         return ary
 
-def acquire(u, vscale1, vscale2):
+def acquire(u, vscales):
         u.write('ACQ:STATE 1')	# Do a single acquisition
         u.write('*OPC?')
         u.read(2.0)	# Wait for it to complete
 
-        ary1 = getchannel(u, 1, vscale1)
-        ary2 = getchannel(u, 2, vscale2)
+        arys = []
+        for i in range(len(vscales)):
+            arys.append(getchannel(u, i + 1, vscales[i]))
 
-        return ary1, ary2
+        return arys
 
 def getpdiffedge(ary1, ary2):
     '''Return phase difference in samples between two signals by edge detection'''