kaart.py 3.44 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
#!/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)
Tammo Jan Dijkema's avatar
Wrap    
Tammo Jan Dijkema committed
47
    receiver.connect("tcp://127.0.0.1:5558")
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
48
49
50
51
52
53
54
55
    while True:
        buff = receiver.recv()
        signal = np.frombuffer(buff, dtype="float32")[-1]

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

Tammo Jan Dijkema's avatar
Wrap    
Tammo Jan Dijkema committed
56
dt = Telescope(consoleHost='console')
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
57

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
data[:, :] = np.nan
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
60
61
62
63
64
npoints = np.zeros(data.shape, dtype='i8')

# create plot
plt.ion()
fig, ax = plt.subplots()
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
65
fig.canvas.manager.set_window_title(f'Scan met de Dwingeloo telescoop {Time.now().iso[:16]}')
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
66
67
fig.canvas.mpl_connect('close_event', set_stoprequest)

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

ax.tick_params(axis="y",direction="in", pad=-32)
ax.tick_params(axis="x",direction="in", pad=-15)

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
75
fig.tight_layout()
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
76
77
78
79

plt.show()
plt.pause(0.001)

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
80
prev_point = None
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
99
    if prev_point is not None:
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
100
        try:
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
101
            prev_point.remove()
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
102
103
104
105
106
107
108
109
110
111
        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
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

Tammo Jan Dijkema's avatar
Wrap    
Tammo Jan Dijkema committed
118
119
120
121
    ldeg = pos.l.deg
    if ldeg > 180:
        ldeg -= 360
    l = lon_to_pixel(ldeg)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
122
123
124
    b = lat_to_pixel(pos.b.deg)

    n = npoints[b, l]
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
125
126
    if np.isnan(data[b, l]):
        data[b, l] = 0
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
127
128
    data[b, l] = (signal + n * data[b, l]) / (n + 1)
    npoints[b, l] += 1
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
129
130
131
132
133
134
135

    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
136
137
    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
138
139
140
141
142
143
144
    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
145
146
147
148

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