Commit c079570c authored by PERNOT Guillaume's avatar PERNOT Guillaume

use REPLWrapper

compute image flux
parent b8a0fc90
import pexpect
from pexpect.replwrap import REPLWrapper
import logging
import shutil
import time
......@@ -6,6 +6,10 @@ import datetime
import socket
import threading
from astropy.io import fits
from astropy.convolution import convolve, Tophat2DKernel
import numpy as np
import re
BIND_IP = '0.0.0.0'
BIND_PORT = 9999
......@@ -24,33 +28,47 @@ ATIK_PROMPT = 'AtikCtl>'
logging.basicConfig(filename='atsctl.log', level=logging.DEBUG)
def cmd(proc, msg, expect, error):
proc.sendline(msg)
def cmd(proc, msg, expect=None, error=None):
print(msg)
i = proc.expect([expect, error], timeout=None)
if i == 1:
logging.error(proc.after)
raise Exception(proc.after)
output = proc.run_command(msg, timeout=None)
if error is not None and re.match(error, output):
logging.error(output)
raise Exception(output)
if expect is None:
return None
m = re.match(expect, output)
if m is None:
logging.error(output)
raise Exception(output)
else:
logging.info(proc.after)
return proc.after
logging.info(output)
return m
def compute_flux(filename):
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)
def acquire_and_save(atsctl, atik, timestamp):
#float_re : '([+-]?\ *\d+\.?\d*)'
cmd(atsctl, 'get_enc_position',
r'([+-]?\ *\d+\.?\d*),([+-]?\ *\d+\.?\d*),([+-]?\ *\d+\.?\d*),([+-]?\ *\d+\.?\d*),([+-]?\ *\d+\.?\d*)',
'ERROR')
#cmd(atsctl, 'get_enc_position',
# r'\r\n(.+),(.+),(.+),(.+),(.+)\r\n',
# 'ERROR')
x, y, z, theta, phi = map(float, atsctl.match.groups())
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))
cmd(atik, '@sint', '!SINT', '!ERROR')
flux = compute_flux('output.fits')
# output.fits has been created
dst = 'outputs/{}.fits'.format(int(timestamp.timestamp()*1000))
dst = 'outputs/{}_{}.fits'.format(int(timestamp.timestamp()*1000), int(flux))
try:
shutil.copyfile('output.fits', dst)
except Exception as e:
......@@ -63,7 +81,6 @@ 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)
logging.info('{} file written'.format(dst))
def move_and_acquire(atsctl, atik, rel_pos, repeat=1, _time=0.005):
......@@ -77,8 +94,8 @@ def move_and_acquire(atsctl, atik, rel_pos, repeat=1, _time=0.005):
timestamp = datetime.datetime.now()
cmd(atsctl, 'move_rel_pos_ats {}'.format(rel_pos), ATSCTL_PROMPT, 'ERROR')
cmd(atsctl, 'wait_move', ATSCTL_PROMPT, 'ERROR')
cmd(atsctl, 'move_rel_pos_ats {}'.format(rel_pos), error='ERROR')
cmd(atsctl, 'wait_move', error='ERROR')
acquire_and_save(atsctl, atik, timestamp)
def scan2d(atsctl, atik, y_inc, z_inc, n_y, n_z, _time):
......@@ -87,10 +104,10 @@ def scan2d(atsctl, atik, y_inc, z_inc, n_y, n_z, _time):
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), ATSCTL_PROMPT, 'ERROR')
cmd(atsctl, 'wait_move', ATSCTL_PROMPT, 'ERROR')
cmd(atsctl, 'move_rel_pos_ats 0,0,{},0,0'.format(z_inc), ATSCTL_PROMPT, 'ERROR')
cmd(atsctl, 'wait_move', ATSCTL_PROMPT, 'ERROR')
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):
......@@ -100,13 +117,13 @@ def scan3d(atsctl, atik, x_inc, y_inc, z_inc, n_x, n_y, n_z, _time):
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), ATSCTL_PROMPT, 'ERROR')
cmd(atsctl, 'wait_move', ATSCTL_PROMPT, 'ERROR')
cmd(atsctl, 'move_rel_pos_ats 0,{},0,0,0'.format(y_inc), ATSCTL_PROMPT, 'ERROR')
cmd(atsctl, 'wait_move', ATSCTL_PROMPT, 'ERROR')
cmd(atsctl, 'move_rel_pos_ats {},0,0,0,0'.format(x_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), ATSCTL_PROMPT, 'ERROR')
cmd(atsctl, 'wait_move', ATSCTL_PROMPT, '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 handle_client_connection(client_socket, atsctl, atik):
......@@ -176,11 +193,9 @@ def tcp_server(atsctl, atik):
if __name__ == '__main__':
atsctl = pexpect.spawn(ATSCTL_PATH)
atsctl.expect(ATSCTL_PROMPT)
atsctl = REPLWrapper(ATSCTL_PATH, orig_prompt=ATSCTL_PROMPT, prompt_change=None)
atik = pexpect.spawn(ATIK_PATH)
atik.expect(ATIK_PROMPT)
atik = REPLWrapper(ATIK_PATH, orig_prompt=ATIK_PROMPT, prompt_change=None)
cmd(atik, '@connect', '!CONNECT', '!ERROR')
cmd(atik, '@cool on', '!COOL', '!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