-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvbds-centroid.py
More file actions
53 lines (46 loc) · 1.6 KB
/
vbds-centroid.py
File metadata and controls
53 lines (46 loc) · 1.6 KB
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
import asyncio
import websockets
from photutils import centroid_com, centroid_1dg, centroid_2dg
from astropy.io import fits
import argparse
# Example usage: python3 async-websockets.py --host 192.168.178.77 -p 7777 -s XXX
ap = argparse.ArgumentParser()
ap.add_argument("--host", required=True, help="VBDS server host")
ap.add_argument("-p", "--port", required=True, type=int, help="VBDS server port")
ap.add_argument("-s", "--subscribe", required=True, help="VBDS stream name to subscribe to")
args = vars(ap.parse_args())
host = args["host"]
port = args["port"]
stream = args["subscribe"]
route = "/vbds/access/streams"
uri = f"ws://{host}:{port}{route}/{stream}"
print(f"URI = {uri}")
# called when an image is received
def receivedImage(image):
print(f"Image size is {len(image)}")
hdulist = fits.HDUList(file=image)
data = hdulist[0].data
do_centroid(data)
def do_centroid(data):
x1, y1 = centroid_com(data)
print((x1, y1))
# x2, y2 = centroid_1dg(data)
# print((x2, y2))
# x3, y3 = centroid_2dg(data)
# print((x3, y3))
# Reads image data from the VBDS web socket in chunks, puts them together and calls receivedImage()
async def receive(uri):
async with websockets.connect(uri) as websocket:
chunks = []
while True:
bs = await websocket.recv()
if (len(bs) == 1):
image = b"".join(chunks)
chunks = []
receivedImage(image)
await websocket.send("ACK")
else:
chunks.append(bs)
# Receive loop
asyncio.get_event_loop().run_until_complete(
receive(uri))