Commit b17bf5bb authored by PERNOT Guillaume's avatar PERNOT Guillaume
Browse files

save acquisition date and state to fits header

parent 2de9badc
NISP test campaign automated acquisitions
=========================================
https://gitlab.lam.fr/gpernot/nisp-test-automaton
## Installation
......@@ -29,10 +30,14 @@ echo "maa 0.1,0.2,0.3,0.4,0.5" | nc localhost 9999
## Commands
* `maa` $`x`$,$`y`$,$`z`$,$`\theta`$,$`\phi`$ `[repeat]` - Move and acquire
### `maa` $`x`$,$`y`$,$`z`$,$`\theta`$,$`\phi`$ `[repeat]` - Move and acquire
Do a move_rel_pos_ats and acquire a fits file from `atik`.
Resulting file is copied in `outputs` directory.
Do a move_rel_pos_ats and acquire a fits file from `atik`.
Resulting file is copied in `outputs` directory.
If `repeat` is given, repeat the process `repeat` times.
If `repeat` is given, repeat the process `repeat` times.
*e.g.* :
```
echo "maa 0.1,0.2,0.3,0.4,0.5 4" | nc localhost 9999
```
......@@ -2,6 +2,7 @@ import pexpect
import logging
import shutil
import time
import datetime
import socket
import threading
from astropy.io import fits
......@@ -27,52 +28,60 @@ def cmd(proc, msg, expect, error):
return proc.after
def move_and_acquire(timestamp, atsctl, atik, rel_pos):
cmd(atsctl, 'move_rel_pos_ats {}'.format(rel_pos), ATSCTL_PROMPT, 'ERROR')
cmd(atsctl, 'wait_move', ATSCTL_PROMPT, 'ERROR')
#float_re = '([+-]?\ *\d+\.?\d*)'
cmd(atsctl, 'get_enc_position',
r'([+-]?\ *\d+\.?\d*),([+-]?\ *\d+\.?\d*),([+-]?\ *\d+\.?\d*),([+-]?\ *\d+\.?\d*),([+-]?\ *\d+\.?\d*)',
'ERROR')
x, y, z, theta, phi = atsctl.match.groups()
logging.info('New position : {} {} {} {} {}'.format(x, y, z, theta, phi))
def move_and_acquire(atsctl, atik, rel_pos, repeat=1):
cmd(atik, '@sint', '!SINT', '!ERROR')
for i in range(repeat):
# output.fits has been created
dst = 'outputs/{}.fits'.format(timestamp)
try:
shutil.copyfile('output.fits', dst)
except Exception as e:
logging.error('An error occured while moving output.fits to {} : {}'.format(dst, e))
raise
# add timestamp header
hdul = fits.open(dst)
timestamp = datetime.datetime.now()
logging.info('{} file written'.format(dst))
cmd(atsctl, 'move_rel_pos_ats {}'.format(rel_pos), ATSCTL_PROMPT, 'ERROR')
cmd(atsctl, 'wait_move', ATSCTL_PROMPT, 'ERROR')
#float_re = '([+-]?\ *\d+\.?\d*)'
cmd(atsctl, 'get_enc_position',
r'([+-]?\ *\d+\.?\d*),([+-]?\ *\d+\.?\d*),([+-]?\ *\d+\.?\d*),([+-]?\ *\d+\.?\d*),([+-]?\ *\d+\.?\d*)',
'ERROR')
x, y, z, theta, phi = map(float, atsctl.match.groups())
logging.info('New position : {} {} {} {} {}'.format(x, y, z, theta, phi))
cmd(atik, '@sint', '!SINT', '!ERROR')
# output.fits has been created
dst = 'outputs/{}.fits'.format(int(timestamp.timestamp()))
try:
shutil.copyfile('output.fits', dst)
except Exception as e:
logging.error('An error occured while moving output.fits to {} : {}'.format(dst, e))
raise
def handle_client_connection(client_socket, atsctl, atik):
# add timestamp header
with fits.open(dst, 'update') as hdul:
hdr = hdul[0].header
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)
timestamp = int(time.time())
logging.info('{} file written'.format(dst))
def handle_client_connection(client_socket, atsctl, atik):
request = client_socket.recv(1024)
logging.debug('net command: {}'.format(request))
cmd = request.split(maxsplit=1)
cmd = request.split()
error = None
if cmd[0] == b'maa':
# Move And Aquire command
try:
move_and_acquire(timestamp, atsctl, atik, cmd[1])
move_and_acquire(atsctl, atik,
cmd[1], # rel_pos
int(cmd[2]) if len(cmd) == 3 else 1) # repeat
except Exception as e:
error = str(e)
else:
error = 'Unhandled command {}'.format(cmd[0])
error = 'Unhandled command {}\n'.format(cmd[0])
if error:
client_socket.send(bytes(error, 'utf-8'))
else:
client_socket.send(b'ok')
client_socket.send(b'ok\n')
client_socket.close()
def tcp_server(atsctl, atik):
......
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