-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmqReader.py
78 lines (57 loc) · 2.25 KB
/
zmqReader.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
import time
import zmq
import sys
import numpy as np
import json
import threading
#from neventarray import *
def multiplyNEventArray(data,multiplier) :
return np.tile(data,multiplier)
event_t = np.dtype([("ts",np.uint32),
("data",np.uint32)])
class generatorReceiver :
def __init__ (self, fulladdress) :
self.fulladdress = fulladdress
self.count = 0
self.size = 0
self.context = zmq.Context()
self.socket = self.connect()
self.run()
def connect(self) :
zmq_socket = self.context.socket(zmq.PULL)
zmq_socket.connect(self.fulladdress)
return zmq_socket
def stats(self) :
while True:
time.sleep(10)
print "Received",self.count,"events (",self.size/1.e6,"MB) @ ",self.size*self.count/(10.*1e6)," MB/s"
self.count = 0
def run(self) :
pulseID = 0
thread = threading.Thread(target=self.stats)
thread.daemon = True
thread.start()
while(True):
dataHeader = self.socket.recv_json()
buf = self.socket.recv(copy=True)
ne = dataHeader["ds"][1]
if zmq.getsockopt(zmq.RECVMORE):
data = np.frombuffer(buffer(buf),dtype=event_t)
if data.size < ne:
print "pulse ",dataHeader["pid"]," incomplete"
for i in data:
print i["ts"], (i["data"] & 0xfff), (i["data"] & 0xfff000) >> 12,(i["data"] & 0xf000000) >> 24,(i["data"] & 0x1000000) >> 28,(i["data"] & 0x2000000) >> 29 ,(i["data"] & 0x4000000) >> 30,(i["data"] & 0x8000000) >> 31
timestamp = dataHeader["st"]
if not int(dataHeader["pid"]) == (pulseID+1):
print "Lost pulse ",dataHeader["pid"]
pulseID = int(dataHeader["pid"])
self.size = data.size*event_t.itemsize+sys.getsizeof(dataHeader)
self.count = self.count+1
def main(argv):
fulladdress = argv[1]
generatorReceiver(fulladdress)
if __name__ == "__main__":
if len(sys.argv) < 1:
print "Error, port required"
sys.exit(2)
main(sys.argv)