kaart.py 3.13 KB
Newer Older
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
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
#!/usr/bin/env python3

"""
Receive numbers from gnuradio (over UDP) at about 2 per second, plot them
on a map

Tammo Jan Dijkema, July 2021
"""


import time
import matplotlib.pyplot as plt
import pandas as pd
import zmq
import re
import struct
import numpy as np
from telescope import Telescope
from astropy.coordinates import get_sun, SkyCoord, AltAz, EarthLocation, Galactic
from astropy.time import Time
import astropy.units as u
from matplotlib import cm
from threading import Thread

plt.ion()

import logging
logger = logging.getLogger()
logger.setLevel(level=logging.INFO)

stoprequest = False

def set_stoprequest(event):
    global stoprequest
    stoprequest = True

signal = 0.
prev_signal = 0.

datafile = open(f"data{int(Time.now().unix)}.txt", "w")
print("Time (UTC),separation (deg),azimuth from north (deg), elevation (deg), signal, source", file=datafile)

def signal_thread():
    global signal
    context = zmq.Context()
    receiver = context.socket(zmq.PULL)
    receiver.connect("tcp://127.0.0.1:5557")
    while True:
        buff = receiver.recv()
        signal = np.frombuffer(buff, dtype="float32")[-1]

s = Thread(target=signal_thread)
s.daemon = True
s.start()

dt = Telescope(consoleHost='localhost')

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
58
data = np.zeros((360, 720), dtype=float)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
59
60
61
62
63
64
65
npoints = np.zeros(data.shape, dtype='i8')

# create plot
plt.ion()
fig, ax = plt.subplots()
fig.canvas.mpl_connect('close_event', set_stoprequest)

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
66
plot = ax.imshow(data, origin='lower', extent=(-180.25, 179.75, -90.25, 89.75))
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
67
68
ax.set_xlabel("Galactische longitude (graden)")
ax.set_ylabel("Galactische latitude (graden)")
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
69
fig.tight_layout()
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
70
71
72
73

plt.show()
plt.pause(0.001)

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
74
prev_point = None
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
maxsignal = 0.
minsignal = 1.0e9

while not stoprequest:
    plt.pause(0.2)
    if signal == prev_signal:
        continue
    prev_signal = signal

    pos = dt.radec
    if pos is None:
        print("Telescope not ready yet")
        continue

    pos = pos.transform_to(Galactic)
    l = round(pos.l.deg)
    b = round(pos.b.deg)

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
93
    if prev_point is not None:
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
94
        try:
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
95
            prev_point.remove()
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
96
97
98
99
100
101
102
103
104
105
        except:
            pass

    #print(Time.now(), separation, az.to(u.deg).value, alt.to(u.deg).value, signal, clean_name(closest_source), file=datafile, flush=True, sep=',')

    if signal > maxsignal:
        maxsignal = signal
    if signal < minsignal:
        minsignal = signal

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
106
107
108
109
110
111
112
113
114
115
116
117
    def lon_to_pixel(lon):
        return round(2 * lon) + 360

    def lat_to_pixel(lat):
        return round(2 * lat) + 180

    l = lon_to_pixel(pos.l.deg)
    b = lat_to_pixel(pos.b.deg)

    n = npoints[b, l]
    data[b, l] = (signal + n * data[b, l]) / (n + 1)
    npoints[b, l] += 1
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
118
119
120
121
122
123
124

    plot.set_data(data)
    plot.set_clim(vmin=minsignal, vmax=maxsignal)
    fig.canvas.flush_events()

    xmin, xmax = ax.get_xlim()
    ymin, ymax = ax.get_ylim()
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
125
126
    prev_point = plt.Circle((pos.l.deg, pos.b.deg), radius=0.25, facecolor='none', edgecolor='r')
    ax.add_patch(prev_point)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
127
128
129
130
131
132
133
    ax.set_xlim(xmin, xmax)
    ax.set_ylim(ymin, ymax) 

    # show the plot
    plt.show()

    plt.pause(0.0001)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
134
135
136
137

filetimestamp = f"{int(Time.now().unix)}.txt"
np.save("map-" + filetimestamp, data)
np.save("npoints-" + filetimestamp, npoints)