Commit a13b8e7e authored by PERNOT Guillaume's avatar PERNOT Guillaume

added stats.csv, output to destdir

fixed scan2d and scan3d
parent c079570c
......@@ -43,7 +43,8 @@ class ATS_Shell(cmd.Cmd, object):
return 'ats pos : {},{},{},{},{}'.format(self.x, self.y, self.z, self.theta, self.phi)
def do_wait_move(self, arg):
time.sleep(2)
#time.sleep(1)
pass
if __name__ == "__main__":
......
from pexpect.replwrap import REPLWrapper
import os
import logging
import shutil
import time
......@@ -11,7 +12,13 @@ import numpy as np
import re
BIND_IP = '0.0.0.0'
DOC = """Available commands:
maa x,y,z,theta,phi repeat time destdir
scan2d y_inc z_inc n_y n_z time destdir
scan3d x_inc y_inc z_inc n_x n_y n_z time destdir
"""
BIND_IP = '127.0.0.1'
BIND_PORT = 9999
DEVEL = True
......@@ -29,7 +36,8 @@ ATIK_PROMPT = 'AtikCtl>'
logging.basicConfig(filename='atsctl.log', level=logging.DEBUG)
def cmd(proc, msg, expect=None, error=None):
print(msg)
if DEVEL:
print(msg)
output = proc.run_command(msg, timeout=None)
if error is not None and re.match(error, output):
......@@ -47,28 +55,44 @@ def cmd(proc, msg, expect=None, error=None):
logging.info(output)
return m
def compute_flux(filename):
def compute_stats(filename):
"""Return flux and max pixel coords"""
tophat_kernel = Tophat2DKernel(4)
with fits.open(filename) as hdul:
img = hdul[0].data
smoothed = convolve(img, tophat_kernel)
data = np.extract(smoothed > np.average(smoothed) + 2*np.std(smoothed),
img - np.average(img))
return np.sum(data)
return np.sum(data), np.unravel_index(np.argmax(img, axis=None), img.shape)
def acquire_and_save(atsctl, atik, timestamp):
def save_stats(destdir, filename, x, y, z, theta, phi, flux, coord):
with open('{}/stats.csv'.format(destdir), 'a') as f:
f.write('{},{},{},{},{},{},{},{},{}\n'.format(filename, x, y, z, theta, phi,
flux, coord[1], coord[0]))
def get_position(atsctl):
#float_re : '([+-]?\ *\d+\.?\d*)'
m = cmd(atsctl, 'get_enc_position',
r'(.+),(.+),(.+),(.+),(.+)\r\n')
x, y, z, theta, phi = map(float, m.groups())
logging.info('New position : {} {} {} {} {}'.format(x, y, z, theta, phi))
logging.info('Position : {} {} {} {} {}'.format(x, y, z, theta, phi))
return (x, y, z, theta, phi)
def acquire_and_save(atsctl, atik, destdir):
timestamp = datetime.datetime.now()
# create destdir
os.makedirs(destdir, exist_ok=True)
x, y, z, theta, phi = get_position(atsctl)
cmd(atik, '@sint', '!SINT', '!ERROR')
flux = compute_flux('output.fits')
# output.fits has been created
dst = 'outputs/{}_{}.fits'.format(int(timestamp.timestamp()*1000), int(flux))
flux, coord = compute_stats('output.fits')
dst = '{}/{}.fits'.format(destdir, int(timestamp.timestamp()*1000))
try:
shutil.copyfile('output.fits', dst)
except Exception as e:
......@@ -81,46 +105,53 @@ def acquire_and_save(atsctl, atik, timestamp):
hdr['DATE-OBS'] = '{}.{}'.format(timestamp.strftime('%Y-%m-%dT%H:%M:%S'),
int(timestamp.microsecond/1000))
hdr['ENC-POS'] = '{},{},{},{},{}'.format(x, y, z, theta, phi)
# save to stats.csv
save_stats(destdir, dst, x, y, z, theta, phi, flux, coord)
logging.info('{} file written'.format(dst))
def move_and_acquire(atsctl, atik, rel_pos, repeat=1, _time=0.005):
def move_and_acquire(atsctl, atik, rel_pos, repeat, _time, destdir):
cmd(atik, '@time {}'.format(_time), '!TIME', '!ERROR')
timestamp = datetime.datetime.now()
acquire_and_save(atsctl, atik, timestamp)
# Do an acquisition before any move
acquire_and_save(atsctl, atik, destdir)
for i in range(repeat):
print("maa {}/{}".format(i+1, repeat))
timestamp = datetime.datetime.now()
cmd(atsctl, 'move_rel_pos_ats {}'.format(rel_pos), error='ERROR')
cmd(atsctl, 'wait_move', error='ERROR')
acquire_and_save(atsctl, atik, timestamp)
acquire_and_save(atsctl, atik, destdir)
def scan2d(atsctl, atik, y_inc, z_inc, n_y, n_z, _time):
def scan2d(atsctl, atik, y_inc, z_inc, n_y, n_z, _time, destdir):
cmd(atik, '@time {}'.format(_time), '!TIME', '!ERROR')
for z in range(n_z):
print("scan2d {}/{}".format(z+1, n_z))
for y in range(n_y):
timestamp = datetime.datetime.now()
acquire_and_save(atsctl, atik, timestamp)
cmd(atsctl, 'move_rel_pos_ats 0,{},0,0,0'.format(y_inc), error='ERROR')
cmd(atsctl, 'wait_move', error='ERROR')
acquire_and_save(atsctl, atik, destdir)
if y < n_y-1:
cmd(atsctl, 'move_rel_pos_ats 0,{},0,0,0'.format(y_inc), error='ERROR')
cmd(atsctl, 'wait_move', error='ERROR')
cmd(atsctl, 'move_rel_pos_ats 0,0,{},0,0'.format(z_inc), error='ERROR')
cmd(atsctl, 'wait_move', error='ERROR')
y_inc = -y_inc # reverse scan
def scan3d(atsctl, atik, x_inc, y_inc, z_inc, n_x, n_y, n_z, _time):
def scan3d(atsctl, atik, x_inc, y_inc, z_inc, n_x, n_y, n_z, _time, destdir):
cmd(atik, '@time {}'.format(_time), '!TIME', '!ERROR')
for z in range(n_z):
print("scan3d {}/{}".format(z+1, n_z))
for y in range(n_y):
for x in range(n_x):
timestamp = datetime.datetime.now()
acquire_and_save(atsctl, atik, timestamp)
cmd(atsctl, 'move_rel_pos_ats {},0,0,0,0'.format(x_inc), error='ERROR')
acquire_and_save(atsctl, atik, destdir)
if x < n_x-1:
cmd(atsctl, 'move_rel_pos_ats {},0,0,0,0'.format(x_inc), error='ERROR')
cmd(atsctl, 'wait_move', error='ERROR')
if y < n_y-1:
cmd(atsctl, 'move_rel_pos_ats 0,{},0,0,0'.format(y_inc), error='ERROR')
cmd(atsctl, 'wait_move', error='ERROR')
cmd(atsctl, 'move_rel_pos_ats 0,{},0,0,0'.format(y_inc), error='ERROR')
cmd(atsctl, 'wait_move', error='ERROR')
x_inc = -x_inc # reverse scan
cmd(atsctl, 'move_rel_pos_ats 0,0,{},0,0'.format(z_inc), error='ERROR')
cmd(atsctl, 'wait_move', error='ERROR')
......@@ -134,30 +165,32 @@ def handle_client_connection(client_socket, atsctl, atik):
error = None
if cmd[0] == 'maa':
# Move And Aquire command
# maa x,y,z,theta,phi [repeat [time]]
# maa x,y,z,theta,phi repeat time destdir
try:
move_and_acquire(atsctl, atik,
cmd[1], # rel_pos
int(cmd[2]) if len(cmd) >= 3 else 1, # repeat
float(cmd[3]) if len(cmd) >= 4 else 0.005 # time
int(cmd[2]), # repeat
float(cmd[3]), # time
cmd[4] # destdir
)
except Exception as e:
error = str(e)
elif cmd[0] == 'scan2d':
# Aquire 2d scene
# scan2d y_inc z_inc n_y n_z time
# eg: scan2d 0.1 0.05 20 20 0.005
# scan2d y_inc z_inc n_y n_z time destdir
# eg: scan2d 0.1 0.05 20 20 0.005 destdir
scan2d(atsctl, atik,
float(cmd[1]), # y_inc
float(cmd[2]), # z_inc
int(cmd[3]), # n_y
int(cmd[4]), # n_z
float(cmd[5]), # time
cmd[6] # destdir
)
elif cmd[0] == 'scan3d':
# Aquire 3d scene
# scan3d x_inc y_inc z_inc n_x n_y n_z time
# eg: scan3d 0.1 0.1 0.05 20 20 20 0.005
# scan3d x_inc y_inc z_inc n_x n_y n_z time destdir
# eg: scan3d 0.1 0.1 0.05 20 20 20 0.005 destdir
scan3d(atsctl, atik,
float(cmd[1]), # x_inc
float(cmd[2]), # y_inc
......@@ -166,6 +199,7 @@ def handle_client_connection(client_socket, atsctl, atik):
int(cmd[5]), # n_y
int(cmd[6]), # n_z
float(cmd[7]), # time
cmd[8] # destdir
)
else:
error = 'Unhandled command {}\n'.format(cmd[0])
......@@ -174,6 +208,10 @@ def handle_client_connection(client_socket, atsctl, atik):
client_socket.send(bytes(error, 'utf-8'))
else:
client_socket.send(b'ok\n')
x, y, z, theta, phi = get_position(atsctl)
print('Final position: {},{},{},{},{}'.format(x, y, z, theta, phi))
client_socket.close()
def tcp_server(atsctl, atik):
......@@ -193,8 +231,9 @@ def tcp_server(atsctl, atik):
if __name__ == '__main__':
atsctl = REPLWrapper(ATSCTL_PATH, orig_prompt=ATSCTL_PROMPT, prompt_change=None)
print(DOC)
atsctl = REPLWrapper(ATSCTL_PATH, orig_prompt=ATSCTL_PROMPT, prompt_change=None)
atik = REPLWrapper(ATIK_PATH, orig_prompt=ATIK_PROMPT, prompt_change=None)
cmd(atik, '@connect', '!CONNECT', '!ERROR')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment