Newer
Older
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import sys
import datetime
import numpy as np
from astropy.time import Time
from util import functions as ufun
from util import variables as uvars
from util.master_listener_davide import thread
from util.mini_os import obsController
vertighel
committed
### Focus only on the 8x8 pixel matrix centered on the centroid
def focus_on_centroid(a, cx, cy, s=4):
# a is an array-like matrix , s is an Int
cx = int(cx)*32 + 32 # cx and cy are normalized in [-1,1] -> now are renormalized in [0,64]
cy = int(cy)*32 + 32
if(s>cx or s>cy):
s = min(cx,cy)
if(s>len(a)-cx or s>len(a)-cy):
s = min(s , len(a)-cx , len(a)-cy)
a_focused = a[cx-s : cx+s , cy-s : cy+s]
return a_focused
vertighel
committed
def __init__(self):
self.counter = 0
self.todos = []
self.rotating_ncpa = False
self.ncpa_file = "No file"
self.json_state = {}
self.obsController = obsController
vertighel
committed
def reset(self):
print("reset")
status = self.obsController.RTCTTResetBCU()
return status
vertighel
committed
def resurrect(self):
print("import")
from SHINS_SW_TEST.SHINS_TEST_DEBUG import SHINS_TEST_DEBUG
print("init")
status = SHINS_TEST_DEBUG(test_s="10", test_s1="true") # init
time.sleep(1)
print("setup start")
status = SHINS_TEST_DEBUG(test_s="11") # setup and start
print(status)
return status
vertighel
committed
def resurrect2(self):
print('resurrect2')
rtc_s = uvars.rtc_s_base.copy()
time.sleep(10)
print("init")
status = self.obsController.RTCTTInit(rtc_s)
print("Exit status: {}".format(status))
ufun.error_handler(status, "RTCTTIsInit")
print("Wait 10 s...")
time.sleep(10)
# SETUP REAL-TIME RTCTT #
print("RTCTT setup... Wait 15 s...")
status = self.obsController.RTCTTSetup()
print("Exit status: {}".format(status))
ufun.error_handler(status, "RTCTTSetup")
# START REAL-TIME RTCTT #
print("RTCTT start")
time.sleep(10)
status = self.obsController.RTCTTStart()
print("Exit status: {}".format(status))
ufun.error_handler(status, "RTCTTStart")
vertighel
committed
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
def uploadRTC(self, modal=True):
rtc_s = uvars.rtc_s_base.copy()
if modal:
print("Enabling Modal Mode")
else:
print("Enabling Zonal Mode")
rtc_s["TTM2CFILE"] = "ZONAL"
# INIT REAL-TIME RTCTT #
print("Enumerating RTC setup")
for element, value in rtc_s.items():
print("Element : % 20s - Value % 20s " % (element, value))
status = self.obsController.RTCTTInit(rtc_s)
print("Exit status: {}".format(status))
ufun.error_handler(status, "RTCTTIsInit")
print("Wait 5 s...")
time.sleep(5)
# SETUP REAL-TIME RTCTT #
print("RTCTT setup... Wait 15 s...")
status = self.obsController.RTCTTSetup()
print("Exit status: {}".format(status))
ufun.error_handler(status, "RTCTTSetup")
# START REAL-TIME RTCTT #
print("RTCTT start")
status = self.obsController.RTCTTStart()
print("Exit status: {}".format(status))
ufun.error_handler(status, "RTCTTStart")
mode = "Modal" if modal else "Zonal"
print("Enabling {} Mode... DONE".format(mode))
vertighel
committed
def zonalCommand(self, data):
print("chiamata effettuata")
normalize_strain = 1#6.666667
act = int(data[0])
strain = float(data[1])
print(strain)
strain = strain/normalize_strain
print("Act on actuator number {} with absolute intensity {}".format(act,strain))
reserved = [0,0]
actuators = np.zeros(97).tolist()
actuators[act-1] = strain
print("vector for RTCTTModeUpload: ")
# print(actuators)
try:
status = self.obsController.RTCTTModeUpload(reserved+actuators, absolute=False)
except Exception as e:
print(e)
status = 1
return status
vertighel
committed
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
def move_soul(self, data, apply_movement=False):
self.soul_deltas = data
print("Received data: {}".format(data))
PSFcx = float(data[0])
PSFcx = 0 if PSFcx < 0 else PSFcx
PSFcx = 2048 if PSFcx > 2048 else PSFcx
PSFcy = float(data[1])
PSFcy = 0 if PSFcy < 0 else PSFcy
PSFcy = 2048 if PSFcy > 2048 else PSFcy
deltaX = PSFcx - uvars.SOUL_TARGET_POS[0]
deltaY = PSFcy - uvars.SOUL_TARGET_POS[1]
print("Calling Here OffsetXYAO with SCICAM deltas Dx = {} , Dy = {}".format(deltaX , deltaY))
# GET CURRENT DROT #
status, drotDict = self.obsController.getPosition("DEGREE", {"DROT":-9999.9})
drotPos = drotDict["DROT"]
print("DROT pos is {}".format(drotPos))
# Transform to SOUL delta #
step_x, step_y = ufun.transform_offset(disp = [deltaX,deltaY], thetaRot = drotPos , conv = "SOUL")
print("Calculatd steps to apply to SOUL : {} , {}".format(step_x , step_y))
if apply_movement:
max_step = 1
step_x = step_x if (abs(step_x) <= max_step) else max_step*np.sign(step_x)
step_y = step_y if (abs(step_y) <= max_step) else max_step*np.sign(step_y)
print("Applying these steps to SOUL : {} , {}".format(step_x , step_y))
try:
status = self.obsController.OffsetXYAO(step_x , step_y , "LEFT")
print("Function is returning NOW")
except Exception as e:
print(e)
status = 1
else:
from util.shinsLogger import logger
resume = "Delta X = {} mm , Delta Y = {} mm".format(step_x,step_y)
logger.info("--------------------------------------------------------------------------")
logger.info("| {:^70s} |".format(resume))
logger.info("--------------------------------------------------------------------------")
logger.debug("SCICAM deltas = ({},{}), DROT pos = {}, computed SOUL deltas = ({},{})".format(deltaX,deltaY,drotPos,np.round(step_x,3),np.round(step_y,3)))
return status, [np.round(step_x,4), np.round(step_y,4)]
def manageDerotation(self, data):
action = str(data[0])
try:
mode = str(data[1])
pos_ang = float(data[2])
pos_ang = pos_ang-360 if pos_ang>180 else pos_ang
except Exception as e:
# print(e)
mode, pos_ang = None, None
if(action == "Start_Tracking"):
print("DAO_______________Derotation mode: {} with angle {}".format(mode,pos_ang))
ins_s = {"DROT_Mode":mode}
async = self.obsController.begin_setupInstrument(ins_s)
status = self.obsController.end_setupInstrument(async)
if mode == "FIELD_FREE":
print("Launching now a {} derotation".format(mode))
status = self.obsController.trackingStart2(0,0)
elif mode == "FIELD_FIXED":
print("Launching now a {} derotation".format(mode))
dateMjd= Time.now()
status = self.obsController.trackingStart2(dateMjd.mjd, pos_ang)
else:
print("{} mode: NO derotation".format(mode))
status = self.obsController.trackingStop()
elif(action == "Stop_Tracking"):
print("DAO__________________________STOP Derotation!")
status = self.obsController.trackingStop()
else:
print("Something went wrong, no action received")
print("Reached end of command")
return status
vertighel
committed
def read(self, id):
for todo in self.todos:
if todo["id"] == id:
return todo
vertighel
committed
def create(self, data):
todo = data
self.todos.append(todo)
return todo
vertighel
committed
def update(self, id, data):
todo = self.read(id)
todo.update(data)
return todo
vertighel
committed
def delete(self, id):
todo = self.read(id)
self.todos.remove(todo)
vertighel
committed
@property
def centroid(self):
status, [cx, cy] = self.obsController.getInternalSeqFloatVariable("RtcCentroidOrigin")
return [cx, cy]
vertighel
committed
@centroid.setter
def centroid(self, cx_cy):
cx, cy = cx_cy
status = self.obsController.RTCTTSetCentroidPos(cx, cy)
return status
vertighel
committed
def flatten(self):
modes = [0] * 96#39
status = self.obsController.RTCTTModeUpload(modes, absolute=True)
status = self.load_ncpa(name="zeri.txt")
return status
vertighel
committed
def close_loop(self):
status = self.obsController.RTCTTCloseLoop()
return status
vertighel
committed
def open_loop(self):
status = self.obsController.RTCTTOpenLoop()
return status
vertighel
committed
def frequency(self, freq):
"""Set the loop frequency"""
rtc_s = {"TTFRAMERATE": freq}
status = self.obsController.RTCTTExecuteSetup(rtc_s)
return status
vertighel
committed
def tint(self, t):
"""Set integration time"""
rtc_s = {
"TTCAMTINT" : t,
}
status = self.obsController.RTCTTExecuteSetup(rtc_s)
return status
vertighel
committed
def patrol(self, b):
"""Set params to enable or disable patrol camera mode"""
if b:
print("Switch to patrol mode")
rtc_patrol = {
"TTM2CFILE": "20220603-Zernike-FullPupil-Arcetri-1u-rms-4rtc.dat",
"TTWINCOORDX": "0",
"TTWINCOORDY": "0",
"TTWINCOLS": "640",
"TTWINROWS": "512",
vertighel
committed
"TTFRAMERATE": "200",
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
"TTNSLOPEPIXELS": "16",
"TTBIASFILE": "",
"TTPIXELGAINFILE": "",
"TTCAMTINT": "0.004995574",
"TTPIXELGAINMODE": "FLATGAIN",
}
else:
print("Switch to cropped mode")
rtc_small = {
"TTM2CFILE": "20220603-Zernike-FullPupil-Arcetri-1u-rms-4rtc.dat",
"TTWINCOORDX": "224",
"TTWINCOORDY": "212",
"TTWINCOLS": "64",
"TTWINROWS": "64",
"TTFRAMERATE": "1000",
"TTNSLOPEPIXELS": "4096",
"TTBIASFILE": "2022-02-19-23-45-23_PixelBias_19.95°C_1000Hz-064x064.dat",
"TTPIXELGAINFILE": "flat_gain_64x64.dat",
"TTCAMTINT": "0.000994919",
"TTPIXELGAINMODE": "FLATGAIN",
}
rtc_s = rtc_patrol if b else rtc_small
status = self.obsController.RTCTTSwitchCcdMode(rtc_s)
return status
def temperature(self, temp):
"""Set the loop temperature"""
status = self.obsController.RTCTTSendCameraCommand("set temperatures sensor "+temp)
return status
def pid(self, p):
"""Set the RTC PID"""
rtc_s = {
"TTPIDKP": str(p[0]),
"TTPIDKI": str(p[1]),
"TTPIDKD": str(p[2]),
"TTPIDTF": str(p[3]),
"TTPIDTS": str(p[4]),
}
rtc_s["TTPIDTS"]=str(1e-3)
rtc_s["TTPIDTF"]=rtc_s["TTPIDTS"]
print(rtc_s)
status = self.obsController.RTCTTExecuteSetup(rtc_s)
return status
vertighel
committed
def threshold(self, t):
"""Set the RTC Threshold"""
status = self.obsController.RTCTTWriteSingle(4416, t, "dsp")
return status
vertighel
committed
def get_threshold(self):
"""Get the RTC Threshold"""
status, threshold = self.obsController.RTCTTReadSingle(4416, "dsp")
return threshold
##############
# Dark stuff #
##############
def todos_dark(self):
"""List dark files"""
return [ str(dark.name).decode('utf-8') for dark in sorted(uvars.dark_path.glob("*.dat")) ]
vertighel
committed
def new_dark(self):
"""Create a new dark"""
rtc_s = {
"TTDIAGENABLED" : "true", # or "1"
"TTDIAGDECIMATION" : "0",
"TTPIXELENABLED" : "1",
"TTPIXELDECIMATION" : "0", # if 0 master diagnostic stops
"TTLOOPENABLED" : "false",
"TTSAVEASBIAS" : "true",
"TTUPDATEBIAS" : "true",
"TTTESTTIME" : "1000000",
}
status, filename = self.obsController.RTCTTTeccamExpose(rtc_s)
return filename
vertighel
committed
def load_dark(self, name):
"""Load a dark file"""
rtc_s = {"TTBIASFILE": name}
status = self.obsController.RTCTTExecuteSetup(rtc_s)
return status
vertighel
committed
def get_current_dark(self):
pass
##############
# Gain stuff #
##############
def todos_gain(self):
"""List gain files"""
return [ str(gain.name).decode('utf-8') for gain in sorted(uvars.gain_path.glob("*.dat")) ]
vertighel
committed
def new_gain(self, gain_type, centroid_size):
"""Create a new gain"""
rtc_s = {
"TTPIXELGAINMODE": gain_type,
"TTPIXELGAINRADIUS": centroid_size,
}
status = self.obsController.RTCTTExecuteSetup(rtc_s)
return status
vertighel
committed
def load_gain(self, name):
"""Load a gain file"""
rtc_s = {
"TTPIXELGAINMODE": "FILEGAIN",
"TTPIXELGAINFILE": name,
}
status = self.obsController.RTCTTExecuteSetup(rtc_s)
return status
##############
# Flat stuff #
##############
def todos_flat(self):
"""List flat files"""
return [ str(flat.name).decode('utf-8') for flat in sorted(uvars.flat_path.glob("*.txt")) ]
def load_flat(self, name):
"""Load a flat file"""
rtc_s = {"TTDMFLATFILE": name}
status = self.obsController.RTCTTExecuteSetup(rtc_s)
return status
vertighel
committed
def tiptilt(self, modes=[0,0,0,0], absolute=False):
print("Loaded Modes {}".format(modes))
status = self.obsController.RTCTTModeUpload(modes=modes, absolute=absolute)
return status
vertighel
committed
def teccam_custom_command(self, command):
status, res = self.obsController.RTCTTSendCameraCommand(command)
return res
vertighel
committed
def save(self):
data = self.data()
now = Time.now().isot
vertighel
committed
filename = "/data/rtc/rtc-panel-save-{}.json".format(now)
with open(filename, 'w') as f:
json.dump(data, f)
return filename
vertighel
committed
def save_flat(self):
"""Save the current flat in a file"""
print("hereeee")
flat_name = ufun.save_flat(self.obsController, self.thread.shape)
print("doneee")
print(flat_name)
return flat_name
vertighel
committed
def cloud_samples(self, s):
print("prima : samples = {}".format(self.thread.cloud_samples))
status = self.thread.cloud_samples = s
print("dopo : samples = {}".format(self.thread.cloud_samples))
return status
vertighel
committed
def get_cloud_samples(self):
return self.thread.cloud_samples
##############
# NCPA stuff #
##############
def todos_ncpa(self):
"""List ncpa files"""
return [ str(ncpa.name).decode('utf-8') for ncpa in sorted(uvars.ncpa_path.glob("*.txt")) ]
vertighel
committed
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
def load_ncpa(self, name):
"""Load a ncpa file"""
# load saved file
mode_file = open(str(uvars.ncpa_path)+name, 'r')
data = mode_file.read()
data_into_list = data.split('\n')
mode_vector = [float(element) for element in data_into_list[:-1]]
# edit mode vector obtained from loaded file
print("v=[{}], len = {}".format(mode_vector, len(mode_vector)))
angle_where_ncpa_was_optimized = float(mode_vector.pop())
print("angle read from ncpa file: {}".format(angle_where_ncpa_was_optimized))
print("v=[{}], len = {}".format(mode_vector, len(mode_vector)))
mode_vector = mode_vector[2:]
print("final mode vector=[{}], len = {}".format(mode_vector, len(mode_vector)))
#apply correct rotation (if necessary)
actual_drot_pos = float(self.obsController.getPosition("DEGREE", {"DROT":-9999})[1]["DROT"])
drot_diff = actual_drot_pos -angle_where_ncpa_was_optimized
if(abs(drot_diff)>0.5):
ruotati = ufun.rotate_modes(mode_vector, -1*drot_diff)
else:
ruotati = mode_vector
riservati = [0,0]
try:
status = self.obsController.RTCTTModeUpload( riservati + list(ruotati), absolute=True)
except Exception as e:
print(e)
status = 1
self.obsController.setContext({"NCPA_FILE":name});
print("Finished")
return status
vertighel
committed
def save_ncpa(self):
"""Save the current ncpa in a file"""
ncpa_name = ufun.save_ncpa(os=self.obsController)
print(ncpa_name)
return ncpa_name
vertighel
committed
def getNCPARotationStatus(self):
print("Status ncpa rotation in arrivo... status is {}".format(self.rotating_ncpa))
return self.rotating_ncpa
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
def rotateNCPA(self, action):
if action == "start":
print("START NCPA Rotation")
self.rotating_ncpa = True
self.obsController.setContext({"NCPA_ROT_STATUS":"Rotation ON"});
elif action == "stop":
print("STOP NCPA Rotation")
self.rotating_ncpa = False
self.obsController.setContext({"NCPA_ROT_STATUS":"Rotation OFF"});
else:
print("Wrong command in rotateNCPA")
riservati = [0,0]
sign = -1 # rotation direction
sleep = 5#10
threshold = 0.5
ins_s = {"DROT":-9999}
angle_where_ncpa_was_optimized = float(self.obsController.getPosition("DEGREE", ins_s)[1]["DROT"])
status, total_vector = self.obsController.getInternalSeqFloatVariable("RtcAppliedMode")
mode_vector = total_vector[2:]
position_of_last_update = angle_where_ncpa_was_optimized
while self.rotating_ncpa:
bearing = float(self.obsController.getPosition("DEGREE", ins_s)[1]["DROT"])
diff = bearing - position_of_last_update
print("Bearing: {}; Diff wrt previous update: {}".format(bearing, diff))
if abs(diff) > threshold:
print("Above threshold: rotating modes!")
ruotati = ufun.rotate_modes(mode_vector, sign * (bearing - angle_where_ncpa_was_optimized) )[0:36]
self.obsController.RTCTTModeUpload( riservati + list(ruotati), absolute=True)
position_of_last_update = bearing
else:
print("Below threshold: no mode rotation")
time.sleep(sleep)
return 1
vertighel
committed
##############
# Plot stuff #
##############
@property
def rtc_image(self):
dummy_64 = np.zeros(shape=(64,64))
# Diagostic stuff
try:
#print("trying to get the image")
image = self.thread.img.T # transpose
#print("there is an image. probably diagnostic is arriving")
#print(image[40][40:60])
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
except AttributeError as e:
print("no image")
print("if NO image but diagnostic data from os are coming then try reset and resurrect rtc")
print("can also try to request fps to the console on rtc panel, and then again reset+resurrect")
image = dummy_64
except IndexError as e:
print("too many indices")
image = dummy_64
except Exception as e:
print("Generic error")
print(e)
return image
def data_cred(self, image, centroid):
# # OS
status, darkarray = self.obsController.getInternalSeqFloatVariable("RtcPixelBias")
try:
darkimage = np.array(darkarray).reshape(image.shape).T
except Exception as e:
darkimage = np.zeros(shape=(image.shape))
try:
cx,cy = centroid
image = image[2:-2 , 2:-2]
darkimage = darkimage[2:-2 , 2:-2]
darkedData = image - darkimage
cred = {
"data": image.tolist(),
"dark": darkimage.tolist(),
#"darkedData" = darkedData,
"min": image.min(), #image[1:-1, 1:-1].min(),
"max": image.max(), #image[1:-1, 1:-1].max(),
"minDark" : darkedData.min(), #darkedData[3:, 3:].min(),
"maxDark" : darkedData.max(), #darkedData[3:, 3:].max(),
"cx": cx,
"cy": cy,
"image_shape": list(image.shape),
}
except Exception as e:
print("error building the cred response")
print(e)
print("image shape : {} ".format(image.shape))
#print("image shape : {} , darkedData shape : {}".format(image.shape, darkedData.shape))
cred = None
return cred
# OS
status, pixgain = self.obsController.getInternalSeqFloatVariable("RtcPixelGain")
try:
pixgain = np.array(pixgain).reshape(imshape).T
"data": pixgain.reshape(imshape).tolist(),
"min": pixgain[3:, 3:].min(),
"max": pixgain[3:, 3:].max(),
}
except Exception as e:
print("error building the gain response")
print(e)
gain = None
return gain
vertighel
committed
def data_dm(self):
signs_list = []
with open(str(uvars.sign_map), "r") as f:
for line in f:
for x in line.split():
signs_list.append(int(x))
signs = np.array(signs_list)
# Diagnostics
shape = self.thread.shape
# OS
status, dmflat = self.obsController.getInternalSeqFloatVariable("RtcDmFlat")
try:
dm_shape_no_flat = (shape*signs) - (np.array(dmflat[0:97]))
dm = {
"actuators": uvars.actuators.tolist(),
"shape": (shape*signs).tolist(),
"flat": (np.array(dmflat[0:97])).tolist(),
"min": round(shape.min(),2),
"max": round(shape.max(),2),
"min_no_flat" : round(min(dm_shape_no_flat),2),
"max_no_flat" : round(max(dm_shape_no_flat),2),
vertighel
committed
# "image_shape": list(imshape),
"skp_cmd" : self.thread.tt_cmd[1]
}
except Exception as e:
print("error building the dm response")
print(e)
dm = None
return dm
def data_cloud(self, imshape, centroid):
vertighel
committed
# Diagnostics
centroids_x = self.thread.centroid_x
centroids_y = self.thread.centroid_y
cloud = np.flip(self.thread.centroid_matrix.T, 1)
vertighel
committed
cx,cy = centroid
rmsX = np.std(centroids_x)*21 # px 2 mas, plate scale = 21 mas/px
rmsY = np.std(centroids_y)*21 # px 2 mas
wx = (np.max(centroids_x)-np.min(centroids_x))*21 # px to mas
wy = (np.max(centroids_y)-np.min(centroids_y))*21 # px to mas
try:
cld = {
"data": cloud.tolist(),
"min": cloud.min(),
"max": cloud.max(),
"samples": len(self.thread.centroid_x),
"cx": cx - self.thread.cx,
"cy": cy - self.thread.cy,
"wx": round(wx, 5),
"wy": round(wy, 5),
"RMS_X": round(rmsX, 5),
"RMS_Y": round(rmsY, 5),
"cloud_samples": self.thread.cloud_samples,
}
except Exception as e:
print("error building the cloud response")
print(e)
cld = None
return cld
vertighel
committed
vertighel
committed
# Diagnostics
vertighel
committed
# OS
centroid = self.centroid
data_cloud = self.data_cloud(imshape, centroid)
data_cred = self.data_cred(image, centroid)
cropped_cred = focus_on_centroid( np.array(data_cred["data"]) , data_cloud["cx"] ,data_cloud["cy"], s=4)
flux_cred = np.sum(cropped_cred)
data_cred["tot_flux"] = flux_cred # added data flux to jso
cropped_cred_darked = focus_on_centroid( np.array(data_cred["data"])-np.array(data_cred["dark"]) , data_cloud["cx"] ,data_cloud["cy"], s=4)
flux_cred_darked = np.sum(cropped_cred_darked)
data_cred["tot_flux_darked"] = flux_cred_darked # added darked flux to json
response = {
"cred": data_cred,
vertighel
committed
"dm": self.data_dm(), # removed imshape as parameter