Skip to content

Commit

Permalink
grab, makebias, etc: ctrl-C handling improved
Browse files Browse the repository at this point in the history
and I have also implemented file lists in grab and related scripts.
  • Loading branch information
trmrsh committed May 26, 2021
1 parent 75c20a9 commit 9b5b177
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 198 deletions.
107 changes: 53 additions & 54 deletions hipercam/scripts/grab.py
Expand Up @@ -4,7 +4,7 @@
import os
import time
import tempfile
from signal import signal, SIGINT
import signal

import numpy as np

Expand Down Expand Up @@ -158,6 +158,9 @@ def grab(args=None):
|grab| is used by several other scripts such as |averun| so take great
care when changing anything to do with its input parameters.
If you use the "temp" option to write to temporary files, then those
files will be deleted if you interrup with CTRL-C. This is to prevent
the accumulation of such frames.
"""

command, args = utils.script_args(args)
Expand Down Expand Up @@ -341,13 +344,14 @@ def grab(args=None):
bframe = None

# Finally, we can go
fnames = []
if temp:
fnames = []
tdir = utils.temp_dir()

with spooler.data_source(source, resource, first) as spool:
with CleanUp(fnames, temp) as cleanup:
# The above line to handle ctrl-c and temporaries

try:
with spooler.data_source(source, resource, first) as spool:

for mccd in spool:

Expand Down Expand Up @@ -453,62 +457,57 @@ def grab(args=None):
if last and nframe > last:
break

except KeyboardInterrupt:
# trap ctrl-C so we can delete temporary files if temp
if temp:
for fname in fnames:
os.remove(fname)
print("\ntemporary files deleted")
print("grab aborted")
else:
print("\ngrab aborted")
sys.exit(1)

if temp:
if len(fnames) == 0:
raise hcam.HipercamError(
'no files were grabbed; please check'
' input parameters, especially "first"'
)
if temp:
if len(fnames) == 0:
raise hcam.HipercamError(
'no files were grabbed; please check'
' input parameters, especially "first"'
)

# write the file names to a list
fd, fname = tempfile.mkstemp(suffix=hcam.LIST, dir=tdir)
with open(fname, "w") as fout:
for fnam in fnames:
fout.write(fnam + "\n")
os.close(fd)
print("temporary file names written to {:s}".format(fname))
# write the file names to a list
fd, fname = tempfile.mkstemp(suffix=hcam.LIST, dir=tdir)
cleanup.flist = fname
with open(fname, "w") as fout:
for fnam in fnames:
fout.write(fnam + "\n")
os.close(fd)
print("temporary file names written to {:s}".format(fname))

# return the name of the file list
return fname
# return the name of the file list
return fname

class CleanUp:
"""
to cope with removal of temporaries
Removes temporaries on ctrl-C. Context manager
"""
def __init__(self, fnames):
def __init__(self, fnames, temp):
self.fnames = fnames

Handle adjusting plot width defaults in a way that can be used
as a handler for signal
"""
def __init__(self, hipenv, hipconf, command, args, fig):
self.hipenv = hipenv
self.hipconf = hipconf
self.command = command
self.args = args
self.fig = fig
def __call__(self, signal_recieved, frame):
self.finish()
self.temp = temp
self.flist = None
self.original_sigint_handler = signal.getsignal(signal.SIGINT)
self.ok = True
signal.signal(signal.SIGINT, self._sigint_handler)

def _sigint_handler(self, signal_received, frame):
if self.temp:
for fname in self.fnames:
os.remove(fname)
if self.flist is not None:
os.remove(self.flist)
print("\ngrab aborted; temporary files deleted")
else:
print("\ngrab aborted")
self.ok = False
sys.exit(1)

def finish(self):
with Cline(self.hipenv, self.hipconf, self.command, self.args) as cl:
width = self.fig.get_figwidth()
height = self.fig.get_figheight()
cl.register("imwidth", Cline.LOCAL, Cline.HIDE)
cl.register("imheight", Cline.LOCAL, Cline.HIDE)
cl.set_default("imwidth",width)
cl.set_default("imheight",height)
print(f'\nImage display size updated to width, height = {width}, {height} [memory=True]')
def __enter__(self):
return self

def __exit__(self, type, value, traceback):
# Reset to original sigint handler
signal.signal(signal.SIGINT, self.original_sigint_handler)
if self.ok:
print("grab finished")
return True
else:
return False
106 changes: 53 additions & 53 deletions hipercam/scripts/makebias.py
Expand Up @@ -4,6 +4,7 @@
import os
import time
import warnings
import signal
import numpy as np

import hipercam as hcam
Expand Down Expand Up @@ -146,27 +147,23 @@ def makebias(args=None):

# Now the actual work.

# We pass full argument lists to grab and combine because with None as the
# command name, the default file mechanism is by-passed. 'prompt' is used
# to expose the hidden parameters. Every argument needed is passed to
# avoid any interactive prompting. Note that because of the Cline
# mechanisms, in particular prompting of one parameter depending on
# another, it can be tricky to get this right. The keyword 'list' can be
# helpful in case of difficulty. i.e. rather than just 'prompt' you would
# use 'prompt', 'list'
# We pass full argument lists to grab and combine because with
# None as the command name, the default file mechanism is
# by-passed. 'prompt' is used to expose the hidden parameters.

try:
print("\nCalling 'grab' ...")
args = [
None, "prompt", source, "yes", run,
str(first), str(last), str(twait), str(tmax),
"no", "none", "none", "none", "none", "f32",
]
resource = hcam.scripts.grab(args)

finished = False
# 'resource' is a list of temporary files at this point

print("\nCalling 'grab' ...")
with CleanUp(resource) as cleanup:

args = [
None, "prompt", source, "yes", run,
str(first), str(last), str(twait), str(tmax),
"no", "none", "none", "none", "none", "f32",
]
resource = hcam.scripts.grab(args)
# The above line to handle ctrl-c and temporaries

if first == 1:
# test readout mode if the first == 1 as, with non clear
Expand All @@ -190,47 +187,50 @@ def makebias(args=None):
"not have clear enabled since the first frame is "
"different from all others."
)
else:
warnings.warn(
f"{instrument} has readout modes with both clears enabled "
"or not between exposures. When no clear is enabled, the "
"first frame is different from all others and should "
"normally not be included when making a bias. This "
"message is a temporary stop gap until the nature of the "
"readout mode has been determined with respect to clears."
)
else:
warnings.warn(
f"{instrument} has readout modes with both clears enabled "
"or not between exposures. When no clear is enabled, the "
"first frame is different from all others and should "
"normally not be included when making a bias. This "
"message is a temporary stop gap until the nature of the "
"readout mode has been determined with respect to clears."
)

print("\nCalling 'combine' ...")
args = [
None,
"prompt",
resource,
"none",
"none",
"none",
"c",
str(sigma),
"b",
"yes",
"yes" if plot else "no",
"yes",
None, "prompt", resource,
"none", "none", "none",
"c", str(sigma), "b",
"yes", "yes" if plot else "no", "yes",
output,
]
hcam.scripts.combine(args)
finished = True

except KeyboardInterrupt:
print("makebias aborted")
pass

# remove temporary files
with open(resource) as fin:
for fname in fin:
fname = fname.strip()
os.remove(fname)
os.remove(resource)
print("temporary files have been deleted")
if finished:
print("makebias finished")


class CleanUp:
"""
Context manager to handle temporary files
"""
def __init__(self, flist):
self.flist = flist
self.ok = True

def _sigint_handler(self, signal_received, frame):
print("\nmakebias aborted")
self.ok = False
sys.exit(1)

def __enter__(self):
signal.signal(signal.SIGINT, self._sigint_handler)

def __exit__(self, type, value, traceback):
with open(self.flist) as fp:
for line in fp:
os.remove(line.strip())
os.remove(self.flist)
print('temporary files removed')
if self.ok:
return True
else:
return False

0 comments on commit 9b5b177

Please sign in to comment.