/
asystent.py
executable file
·532 lines (445 loc) · 17 KB
/
asystent.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import time
import pyds9 as ds9
import os
import glob
from collections import deque
import astropy.io.fits as fits
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
import subprocess as sub
from astropy.coordinates import SkyCoord
from astropy import units as u
from astropy.wcs import WCS
from astropy.time import Time
import warnings
import threading
import numpy as np
import scipy.optimize as optimize
import math
from photutils import CircularAnnulus, CircularAperture
from photutils import aperture_photometry
import matplotlib.pyplot as plt
import matplotlib
import sys
# import pygame
from photutils import DAOStarFinder
from astropy.stats import sigma_clipped_stats
import matplotlib.image as mpimg
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadBuilder
from pymodbus.payload import BinaryPayloadDecoder
from collections import OrderedDict
import struct
from datetime import datetime as dt
warnings.filterwarnings('ignore')
watchdog_img = mpimg.imread('/opt/ObsAssistant/watchdog.dif')
matplotlib.use('QT4Agg')
# photometry, backgrund estimation, snr etc.
radius = 30 # radius from object center (fits coordinates) for backgroun est.
sigma = 2.0 # The number of standard deviations to use as the clipping limit.
fwhm_daofind = 2.0 # estimate FWHM for daofind
fwhm_multiplier = 2 # fwhm * fwhm_multiplier = aperture
annulis_in = 2 # value add to aperture
annulis_out = 4 # value add to aperture
# solve-filed param
time_limit = 30 # time limit before giving up
scale_low = 1.11 # arcsec
scale_high = 1.13 # arcsec
solve_radius = 2 # deg
solve_depth = '40,80,100,160,250'
# head Key
ra_key = 'RA'
dec_key = 'DEC'
filter_key = 'FILTER'
object_key = 'OBJECT'
exp_key = 'EXPTIME'
date_key = 'DATE-OBS'
time_key = 'TIME-OBS'
# watchdog param
file_to_watch = ["*.fits"]
sleep_time = 1.0
max_sleep = 300
sleep_dur = 0
# plot parameters
max_plot_len = 50
x_plot = 10
y_plot = 8
imageSize = 1000
# clear
files_to_rm = ['*.axy', '*.corr', '*.xyls', '*.match',
'*.new', '*.rdls', '*.solved', '*.wcs']
# MODBUS
modbus_ip = '192.168.2.16'
modbus_port = 502
modbus_UNIT = 0x1
c_registers = 2
##############
flux_tab = deque(maxlen=max_plot_len)
snr_tab = deque(maxlen=max_plot_len)
fwhm_tab = deque(maxlen=max_plot_len)
pol_tab = deque(maxlen=max_plot_len)
pol_flux_tab = deque(maxlen=max_plot_len)
plt.ion()
avg_pol = 0
im_object_name = ''
im_counter = 0
plot_clear = False
##############
class WatchObs(PatternMatchingEventHandler):
patterns = file_to_watch
def on_created(self, event):
print("Got it!", event.src_path)
self.file_to_open = event.src_path
global pol_tab
global avg_pol
global sleep_dur
global im_object_name
global im_counter
global plot_clear
sleep_dur = 0
fits_coo = open_file(self.file_to_open)
if solve_field(fits_coo):
im_counter += 1
solve_coo, solve_file_hdr, solve_file_data = open_solve_file(
str(self.file_to_open).split(".")[0]+".new")
if modbus_client:
if write_modbus(solve_coo, solve_file_hdr):
print('MODBUS UPDATE')
else:
print('MODBUS FAIL!')
show_ds9(fits_coo, solve_coo, solve=True)
star = Star()
x, y = star_pix(solve_file_hdr, fits_coo)
if ((x < imageSize) and (y < imageSize)):
#fileName = self.file_to_open.split("/")[-1]
if fits_coo.separation(solve_coo) < 8 * u.arcmin:
x, y = star.centroid(x, y, solve_file_data, radius)
fwhm_x, fwhm_y = star.get_fwhm(x, y, radius,
solve_file_data, medv=None)
flux, aperture_area = star.phot(x, y, radius,
solve_file_data, fwhm_x, fwhm_y)
signal_to_noise, bkg_median = star.snr(x, y, radius,
solve_file_data,
fwhm_x, fwhm_y,
flux, aperture_area)
flux_tab.append(flux)
snr_tab.append(signal_to_noise)
fwhm_tab.append((fwhm_x + fwhm_y)/2.)
if filter_check(solve_file_hdr):
pol_tab, avg_pol = pol_calc(flux, solve_file_hdr)
else:
pass
solve_file_object_name = solve_file_hdr[object_key]
plot(flux_tab, snr_tab, fwhm_tab, pol_tab, avg_pol,
solve_file_hdr, plot_clear, im_counter, bkg_median)
else:
show_ds9(None, None, solve=False)
"""
def on_modified(self, event):
print("Got it!", event.src_path)
global sleep_dur
sleep_dur = 0
self.file_to_open = event.src_path
fits_coo = open_file(self.file_to_open)
if solve_field(fits_coo):
solve_coo, solve_file_hdr, solve_file_data = open_solve_file(
str(self.file_to_open).split(".")[0]+".new")
show_ds9(fits_coo, solve_coo)
"""
class Star:
# code copy from GINGA https://ginga.readthedocs.org/en/latest/
def __init__(self):
self.lock = threading.RLock()
self.skylevel_magnification = 1.05
self.skylevel_offset = 40.0
def gaussian(self, x, p):
y = (1.0 / (p[1] * np.sqrt(2*np.pi)) *
np.exp(-(x - p[0])**2 / (2*p[1]**2))) * p[2]
return y
def calc_fwhm(self, arr1d, medv=None, gauss_fn=None):
if not gauss_fn:
gauss_fn = self.gaussian
N = len(arr1d)
X = np.array(range(N))
Y = arr1d
if medv is None:
medv = np.median(Y)
Y = Y - medv
maxv = Y.max()
Y = Y.clip(0, maxv)
p0 = [0, N-1, maxv]
def errfunc(p, x, y): return gauss_fn(x, p) - y
with self.lock:
p1, success = optimize.leastsq(errfunc, p0[:], args=(X, Y))
if not success:
raise IQCalcError("FWHM gaussian fitting failed")
mu, sdev, maxv = p1
fwhm = 2.0 * np.sqrt(2.0 * np.log(2.0)) * sdev
return (float(fwhm), float(mu), float(sdev), maxv)
def get_fwhm(self, x, y, radius, data, medv=None):
if medv is None:
medv = np.median(data)
x0, y0, xarr, yarr = self.cut_cross(x, y, radius, data)
fwhm_x, cx, sdx, maxx = self.calc_fwhm(xarr, medv=medv)
fwhm_y, cy, sdy, maxy = self.calc_fwhm(yarr, medv=medv)
return fwhm_x, fwhm_y
def centroid(self, x, y, data, radius):
dist = 1024
x0, y0, arr = self.cut_region(x, y, radius, data)
mean, median, std = sigma_clipped_stats(arr, sigma=sigma)
daofind = DAOStarFinder(threshold=5.*std, fwhm=fwhm_daofind)
sources = daofind.find_stars(arr - median)
cx, cy = x, y
for i in sources:
dist_temp = math.sqrt(abs(i[1]+x0-x)**2 + abs(i[2]+y0-y)**2)
if dist_temp < dist:
dist = dist_temp
cx = i[1] + x0
cy = i[2] + y0
return (cx, cy)
def cut_region(self, x, y, radius, data):
n = radius
ht, wd = data.shape
x0, x1 = max(0, x-n), min(wd-1, x+n)
y0, y1 = max(0, y-n), min(ht-1, y+n)
arr = data[int(y0):int(y1)+1, int(x0):int(x1)+1]
return (x0, y0, arr)
def cut_cross(self, x, y, radius, data):
n = radius
ht, wd = data.shape
x0, x1 = max(0, x-n), min(wd-1, x+n)
y0, y1 = max(0, y-n), min(ht-1, y+n)
xarr = data[int(y), int(x0):int(x1)+1]
yarr = data[int(y0):int(y1)+1, int(x)]
return (x0, y0, xarr, yarr)
def snr(self, x, y, radius, data, fwhm_x, fwhm_y, flux, aperture_area):
x0, y0, arr = self.cut_region(x, y, radius, data)
mean, median, std = sigma_clipped_stats(arr, sigma=sigma)
try:
signal_to_noise = flux / math.sqrt(flux +
aperture_area * math.pow(std, 2))
except ValueError:
signal_to_noise = 0
return signal_to_noise, median
def phot(self, x, y, radius, data, fwhm_x, fwhm_y):
x0, y0, arr = self.cut_region(x, y, radius, data)
mean, median, std = sigma_clipped_stats(arr, sigma=sigma)
aperture_r = ((fwhm_x + fwhm_y)/2.0) * fwhm_multiplier
# r_in = aperture_r + annulis_in
# r_out = aperture_r + annulis_out
apertures = CircularAperture((x, y), aperture_r)
# annulus_apertures = CircularAnnulus((x, y), r_in, r_out)
rawflux_table = aperture_photometry(data, apertures)
# bkgflux_table = aperture_photometry(data, annulus_apertures)
# phot_table = hstack([rawflux_table, bkgflux_table],
# table_names=['raw', 'bkg'])
aperture_area = np.pi * aperture_r ** 2
# annulus_area = np.pi * (r_out ** 2 - r_in ** 2)
# bkg_sum = phot_table['aperture_sum_bkg'] *\
# aperture_area / annulus_area
# print 'bkg', phot_table['aperture_sum_bkg'] / annulus_area
# final_sum = phot_table['aperture_sum_raw'] - bkg_sum
final_sum = rawflux_table['aperture_sum'] - aperture_area * median
return final_sum[0], aperture_area
def show_ds9(fits_coo, solve_coo, solve=True):
file_to_show = str(event_handler.file_to_open)
if solve:
file_to_show = file_to_show.split(".")[0] + ".new"
try:
d = ds9.ds9()
d.set("file " + file_to_show)
d.set('scale zscale')
d.set('zoom to fit')
if solve:
d.set('match frame wcs')
d.set('regions', 'fk5; line(' + str(solve_coo.ra.deg) + ',' +
str(solve_coo.dec.deg) + ',' + str(fits_coo.ra.deg) +
',' + str(fits_coo.dec.deg) + ')')
d.set('regions', 'fk5; circle(' + str(fits_coo.ra.deg) +
',' + str(fits_coo.dec.deg) + ',7")')
except:
print("DS9 PROBLEM")
def solve_field(fits_coo):
solve_field_command = ['solve-field',
'--ra', '%s' % (fits_coo.ra.deg),
'--dec', '%s' % (fits_coo.dec.deg),
'--radius', '%1.1f' % solve_radius,
'--depth', solve_depth,
'--cpulimit', '%f' % time_limit,
'--scale-units', 'arcsecperpix',
'--scale-low', '%.5f' % scale_low,
'--scale-high', '%.5f' % scale_high,
'--overwrite',
'--no-verify',
'--no-plots',
str(event_handler.file_to_open)]
sub.Popen(solve_field_command, stdout=sub.PIPE,
stderr=sub.PIPE).communicate()
if os.path.exists(str(event_handler.file_to_open).split(".")[0]+'.new'):
return True
else:
print('solve error')
return False
def open_file(file_to_open):
hdr = fits.getheader(file_to_open)
print('fits coo:', str(hdr[ra_key])+" "+str(hdr[dec_key]))
fits_coo = SkyCoord(hdr[ra_key]+" "+hdr[dec_key],
'icrs', unit=(u.hour, u.deg))
return fits_coo
def open_solve_file(file_to_open):
f = fits.open(file_to_open)
data = f[0].data
hdr = f[0].header
w = WCS(hdr)
wx, wy = w.wcs_pix2world(hdr['NAXIS1']/2, hdr['NAXIS2']/2, 1)
solve_coo = SkyCoord(wx, wy, unit='deg')
print('real center:', str(solve_coo.ra.to_string(u.hour)),
str(solve_coo.dec.to_string(u.deg)))
return solve_coo, hdr, data.astype(int)
def star_pix(solve_file_hdr, fits_coo):
w = WCS(solve_file_hdr)
px, py = w.wcs_world2pix(fits_coo.ra.deg, fits_coo.dec.deg, 1)
return int(px), int(py)
def pol_calc(flux, solve_file_hdr):
global avg_pol
pol_nr = [int(s) for s in solve_file_hdr['FILTER'] if s.isdigit()][0]
if pol_nr == int(len(pol_flux_tab)+1):
pol_flux_tab.append(flux)
else:
del pol_flux_tab[:]
if len(pol_flux_tab) == 4:
u_st = (pol_flux_tab[1] - pol_flux_tab[0]) /\
(pol_flux_tab[1] + pol_flux_tab[0])
q_st = (pol_flux_tab[3] - pol_flux_tab[2]) /\
(pol_flux_tab[3] + pol_flux_tab[2])
pd = np.sqrt(np.power(u_st, 2) + np.power(q_st, 2))
pol_tab.append(0)
avg_pol = reduce(lambda a, b: a + b, pol_tab) / len(pol_tab)
del pol_flux_tab[:]
return pol_tab, avg_pol
def filter_check(solve_file_hdr):
filter_name = solve_file_hdr['FILTER']
if 'P' in filter_name:
return True
else:
return False
def plot(flux_tab, snr_tab, fwhm_tab, pol_tab, avg_pol, hdr, clear, im_counter, bkg_median):
plt.figure(1, figsize=(x_plot, y_plot))
if clear:
plt.clf()
plt.clf()
ax01 = plt.subplot(521)
ax01.cla()
time.sleep(0.1)
ax01.text(0.1, 0.8, "Object: "+hdr[object_key], fontsize=14)
ax01.text(0.1, 0.55, "Filter: "+hdr[filter_key], fontsize=14)
ax01.text(0.1, 0.3, "Exptime: "+str(hdr[exp_key]), fontsize=14)
ax01.text(0.1, 0.05, "BKG: "+str(bkg_median), fontsize=14)
ax01.text(0.5, 0.8, "NUM: "+str(im_counter),
fontsize=15, fontweight='bold')
ax01.text(0.5, 0.4, str(hdr[time_key][:8]),
fontsize=15, fontweight='bold')
ax01.axes.get_xaxis().set_visible(False)
ax01.axes.get_yaxis().set_visible(False)
ax02 = plt.subplot(522)
ax02.imshow(watchdog_img)
ax02.axes.get_xaxis().set_visible(False)
ax02.axes.get_yaxis().set_visible(False)
ax1 = plt.subplot(512)
ax1.set_title('FLUX [counts]: %.2f' % (flux_tab[-1]))
ax1.set_xlim(-0.2, len(flux_tab) - 0.8)
ax1.set_ylim(min(flux_tab) * 0.9, max(flux_tab) * 1.1)
ax1.plot(flux_tab, 'ro')
ax2 = plt.subplot(513)
ax2.set_title('SNR: %.2f' % (snr_tab[-1]))
ax2.set_xlim(-0.2, len(snr_tab) - 0.8)
ax2.set_ylim(min(snr_tab) * 0.9, max(snr_tab) * 1.1)
ax2.plot(snr_tab, 'ro')
ax3 = plt.subplot(514)
ax3.set_title('FWHM [pix]: %.2f' % (fwhm_tab[-1]))
ax3.set_xlim(-0.2, len(fwhm_tab) - 0.8)
ax3.set_ylim(min(fwhm_tab) * 0.9, max(fwhm_tab) * 1.1)
ax3.plot(fwhm_tab, 'ro')
plt.tight_layout()
plt.draw()
plt.pause(0.01)
def clear():
print('cleaning.....')
for i in files_to_rm:
files = glob.glob(path + i)
print(path + i)
for j in files:
os.remove(j)
def connect_modbus(modbus_ip, modbus_port):
try:
client = ModbusClient(modbus_ip, port=modbus_port)
except:
return None
if client.connect():
return client
return None
def read_modbus():
pass
def write_modbus(solve_coo, solve_file_hdr):
if solve_coo is None:
return False
ra = solve_coo.ra.deg
if ra > 180:
ra -= 360
ra = ra * np.pi/180.
dec = solve_coo.dec.deg * np.pi/180.
ha = calculate_ha(solve_coo, solve_file_hdr)
val_dict = OrderedDict([
(24592, ra),
(24590, dec),
(24594, ha)])
for address, value in val_dict.items():
builder = BinaryPayloadBuilder(byteorder=Endian.Big,
wordorder=Endian.Big)
print(address, value)
builder.add_32bit_float(value)
payload = builder.build()
registers = builder.to_registers()
rr = modbus_client.write_registers(address, registers, unit=modbus_UNIT)
time.sleep(0.1)
if rr.isError():
return False
return True
def calculate_ha(coo, hdr):
hdr_time = 'T'.join([hdr[date_key], hdr[time_key]])
exp_time = hdr[exp_key]
# time correction in s. (time for analize)
const_corr = 5.
t_hdr = Time(hdr_time, format='isot', scale='utc')
t_hdr.delta_ut1_utc = 0.
st_hdr = t_hdr.sidereal_time('apparent', 20.0672)
ha_hdr = st_hdr.hour - coo.ra.hour + (exp_time / 3600.) + (const_corr / 3600.)
ha_hdr_rad = (ha_hdr * 15 * np.pi) / 180.
return ha_hdr_rad
if __name__ == "__main__":
args = sys.argv[1:]
if len(args) > 0:
path = args[0]
else:
print("error - write path to files")
modbus_client = connect_modbus(modbus_ip, modbus_port)
event_handler = WatchObs()
observer = Observer()
observer.schedule(event_handler, path=path, recursive=False)
observer.start()
#pygame.init()
try:
while True:
time.sleep(sleep_time)
sleep_dur += 1
#if sleep_dur >= max_sleep:
# pygame.mixer.music.load('/opt/ObsAssistant/Angry-dog.mp3')
# pygame.mixer.music.play()
# time.sleep(7)
#else:
# pygame.mixer.music.stop()
except KeyboardInterrupt:
clear()
observer.stop()
observer.join()