Skip to content

Commit

Permalink
Merge pull request #16 from DiamondLightSource/14_tidy-up-i24-ssx
Browse files Browse the repository at this point in the history
Tidy up I24 serial code
  • Loading branch information
Tom-Willemsen authored Jun 21, 2023
2 parents a7d496d + 586ed84 commit 5233f07
Show file tree
Hide file tree
Showing 25 changed files with 1,407 additions and 9,974 deletions.
217 changes: 124 additions & 93 deletions src/mx_bluesky/I24/serial/extruder/i24ssx_Extruder_Collect_py3v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,53 @@
"""
from __future__ import annotations

import argparse
import inspect
import logging as lg
import pathlib
import logging
import sys
import time
from datetime import datetime
from pathlib import Path
from time import sleep

from mx_bluesky.I24.serial import log
from mx_bluesky.I24.serial.dcid import DCID, SSXType
from mx_bluesky.I24.serial.parameters.constants import PARAM_FILE_PATH
from mx_bluesky.I24.serial.setup_beamline import caget, caput, pv
from mx_bluesky.I24.serial.setup_beamline import setup_beamline as sup
from mx_bluesky.I24.serial.write_nexus import call_nexgen

lg.basicConfig(
format="%(asctime)s %(levelname)s: \t%(message)s",
level=lg.DEBUG,
filename=time.strftime("logs/i24_%d%B%y.log").lower(),
)
usage = "%(prog)s command [options]"
logger = logging.getLogger("I24ssx.extruder")


def setup_logging():
logfile = time.strftime("i24_%d%B%y.log").lower()
log.config(logfile)


def flush_print(text):
sys.stdout.write(str(text))
sys.stdout.flush()


def initialise_extruderi24():
def _coerce_to_path(path: Path | str) -> Path:
if not isinstance(path, Path):
return Path(path)
return path


def initialise_extruderi24(args=None):
name = inspect.stack()[0][3]
print("Initialise Parameters for extruder data collection")
lg.info("%s I24 extruder initialisation" % name)
logger.info("%s I24 extruder initialisation" % name)

# define visit using the below line
# visit = "/dls/i24/data/2022/mx31930-2/"
visit = "/dls/i24/data/2023/cm33852-2/"
lg.info("%s Visit defined %s" % (name, visit))
visit = caget(pv.ioc12_gp1)
logger.info("%s Visit defined %s" % (name, visit))

# Define detector in use
det_type = sup.get_detector_type()

caput(pv.ioc12_gp1, str(visit))
caput(pv.ioc12_gp2, "test")
caput(pv.ioc12_gp3, "testrun")
caput(pv.ioc12_gp4, "100")
Expand All @@ -55,17 +63,18 @@ def initialise_extruderi24():
caput(pv.ioc12_gp15, det_type.name)
caput(pv.pilat_cbftemplate, 0)
print("Done Done Done")
lg.info("%s Initialsation complete" % name)
logger.info("%s Initialsation complete" % name)


def moveto(place):
def moveto(args):
place = args.place
name = inspect.stack()[0][3]
lg.info("%s Move to %s" % (name, place))
logger.info("%s Move to %s" % (name, place))

det_type = caget(pv.ioc12_gp15)

if place == "laseron":
lg.info("%s laser on%s" % (name, place))
logger.info("%s laser on%s" % (name, place))
if det_type == "pilatus":
caput(pv.zebra1_out1_ttl, 60.0)
caput(pv.zebra1_soft_in_b0, 1.0)
Expand All @@ -74,7 +83,7 @@ def moveto(place):
caput(pv.zebra1_soft_in_b0, 1.0)

if place == "laseroff":
lg.info("%s laser off%s" % (name, place))
logger.info("%s laser off%s" % (name, place))
if det_type == "pilatus":
caput(pv.zebra1_soft_in_b0, 0.0)
caput(pv.zebra1_out1_ttl, 0.0)
Expand All @@ -86,14 +95,14 @@ def moveto(place):
caput(pv.det_z, 1480)


def write_parameter_file():
def write_parameter_file(param_path: Path | str = PARAM_FILE_PATH):
name = inspect.stack()[0][3]

param_path = "/dls_sw/i24/scripts/extruder/"
param_path = _coerce_to_path(param_path)
param_fid = "parameters.txt"

lg.info("%s Writing Parameter File \n%s" % (name, param_path + param_fid))
print("\nWriting Parameter File ", param_path + param_fid)
logger.info("%s Writing Parameter File \n%s" % (name, param_path / param_fid))
print("\nWriting Parameter File ", param_path / param_fid)

visit = caget(pv.ioc12_gp1)
directory = caget(pv.ioc12_gp2)
Expand All @@ -118,31 +127,30 @@ def write_parameter_file():
# high probability of users accidentally overwriting data. Use a dash
filename = filename + "-"
print("Requested filename ends in a number. Appended dash:", filename)
lg.info("%s Requested filename ends in a number. Appended dash")

f = open(param_path + param_fid, "w")
f.write("visit \t\t%s\n" % visit)
f.write("directory \t%s\n" % directory)
f.write("filename \t%s\n" % filename)
f.write("num_imgs \t%s\n" % num_imgs)
f.write("exp_time \t%s\n" % exp_time)
f.write("det_dist \t%s\n" % det_dist)
f.write("det_type \t%s\n" % det_type)
f.write("pump_probe \t%s\n" % pump_status)
f.write("pump_exp \t%s\n" % pump_exp)
f.write("pump_delay \t%s\n" % pump_delay)
f.close()

lg.info("%s visit %s" % (name, visit))
lg.info("%s directory %s" % (name, directory))
lg.info("%s filename %s" % (name, filename))
lg.info("%s num_imgs %s" % (name, num_imgs))
lg.info("%s exp_time %s" % (name, exp_time))
lg.info("%s det_dist %s" % (name, det_dist))
lg.info("%s det_type %s" % (name, det_type))
lg.info("%s pump_probe %s" % (name, pump_status))
lg.info("%s pump_exp %s" % (name, pump_exp))
lg.info("%s pump_delay %s" % (name, pump_delay))
logger.info("%s Requested filename ends in a number. Appended dash")

with open(param_path / param_fid, "w") as f:
f.write("visit \t\t%s\n" % visit)
f.write("directory \t%s\n" % directory)
f.write("filename \t%s\n" % filename)
f.write("num_imgs \t%s\n" % num_imgs)
f.write("exp_time \t%s\n" % exp_time)
f.write("det_dist \t%s\n" % det_dist)
f.write("det_type \t%s\n" % det_type)
f.write("pump_probe \t%s\n" % pump_status)
f.write("pump_exp \t%s\n" % pump_exp)
f.write("pump_delay \t%s\n" % pump_delay)

logger.info("%s visit %s" % (name, visit))
logger.info("%s directory %s" % (name, directory))
logger.info("%s filename %s" % (name, filename))
logger.info("%s num_imgs %s" % (name, num_imgs))
logger.info("%s exp_time %s" % (name, exp_time))
logger.info("%s det_dist %s" % (name, det_dist))
logger.info("%s det_type %s" % (name, det_type))
logger.info("%s pump_probe %s" % (name, pump_status))
logger.info("%s pump_exp %s" % (name, pump_exp))
logger.info("%s pump_delay %s" % (name, pump_delay))

print("\n")
print("visit:", visit)
Expand All @@ -157,9 +165,10 @@ def write_parameter_file():
print("pump_delay:", pump_delay)


def scrape_parameter_file():
param_path = "/dls_sw/i24/scripts/extruder/"
with open(param_path + "parameters.txt", "r") as filein:
def scrape_parameter_file(param_path: Path | str = PARAM_FILE_PATH):
param_path = _coerce_to_path(param_path)

with open(param_path / "parameters.txt", "r") as filein:
f = filein.readlines()
for line in f:
entry = line.rstrip().split()
Expand Down Expand Up @@ -197,10 +206,10 @@ def scrape_parameter_file():
)


def run_extruderi24():
def run_extruderi24(args=None):
print("Starting i24")
name = inspect.stack()[0][3]
lg.info("%s" % name)
logger.info("%s" % name)

start_time = datetime.now()
print("Start time", start_time.ctime())
Expand All @@ -219,7 +228,7 @@ def run_extruderi24():
pump_delay,
) = scrape_parameter_file()

lg.info("%s Start Time = % s" % (name, start_time))
logger.info("%s Start Time = % s" % (name, start_time))

# Setting up the beamline
caput("BL24I-PS-SHTR-01:CON", "Reset")
Expand All @@ -239,8 +248,8 @@ def run_extruderi24():
filepath = visit + directory
print("Filepath", filepath)
print("Filename", filename)
lg.info("%s Filepath %s" % (name, filepath))
lg.info("%s Filename %s" % (name, filename))
logger.info("%s Filepath %s" % (name, filepath))
logger.info("%s Filename %s" % (name, filename))

# For zebra
# The below will need to be determined emprically. A value of 0.0 may be ok (????)
Expand All @@ -261,16 +270,18 @@ def run_extruderi24():
if det_type == "pilatus":
print("Using pilatus mini cbf")
caput(pv.pilat_cbftemplate, 0)
lg.info("%s Pilatus quickshot setup: filepath %s" % (name, filepath))
lg.info("%s Pilatus quickshot setup: filepath %s" % (name, filename))
lg.info("%s Pilatus quickshot setup: number of images %s" % (name, num_imgs))
lg.info("%s Pilatus quickshot setup: exposure time %s" % (name, exp_time))
logger.info("%s Pilatus quickshot setup: filepath %s" % (name, filepath))
logger.info("%s Pilatus quickshot setup: filepath %s" % (name, filename))
logger.info(
"%s Pilatus quickshot setup: number of images %s" % (name, num_imgs)
)
logger.info("%s Pilatus quickshot setup: exposure time %s" % (name, exp_time))

if pump_status == "true":
print("pump probe experiment")
lg.info("%s Pump probe extruder data collection" % name)
lg.info("%s Pump exposure time %s" % (name, pump_exp))
lg.info("%s Pump delay time %s" % (name, pump_delay))
logger.info("%s Pump probe extruder data collection" % name)
logger.info("%s Pump exposure time %s" % (name, pump_exp))
logger.info("%s Pump delay time %s" % (name, pump_delay))
sup.pilatus("fastchip", [filepath, filename, num_imgs, exp_time])
sup.zebra1(
"zebratrigger-pilatus",
Expand All @@ -287,7 +298,7 @@ def run_extruderi24():
)
elif pump_status == "false":
print("Static experiment: no photoexcitation")
lg.info("%s Static experiment: no photoexcitation" % name)
logger.info("%s Static experiment: no photoexcitation" % name)
sup.pilatus("quickshot", [filepath, filename, num_imgs, exp_time])
gate_start = 1.0
gate_width = (float(exp_time) * float(num_imgs)) + float(0.5)
Expand All @@ -296,16 +307,16 @@ def run_extruderi24():
elif det_type == "eiger":
# Test moving seqID+1 to here
caput(pv.eiger_seqID, int(caget(pv.eiger_seqID)) + 1)
lg.info("%s Eiger quickshot setup: filepath %s" % (name, filepath))
lg.info("%s Eiger quickshot setup: filepath %s" % (name, filename))
lg.info("%s Eiger quickshot setup: number of images %s" % (name, num_imgs))
lg.info("%s Eiger quickshot setup: exposure time %s" % (name, exp_time))
logger.info("%s Eiger quickshot setup: filepath %s" % (name, filepath))
logger.info("%s Eiger quickshot setup: filepath %s" % (name, filename))
logger.info("%s Eiger quickshot setup: number of images %s" % (name, num_imgs))
logger.info("%s Eiger quickshot setup: exposure time %s" % (name, exp_time))

if pump_status == "true":
print("pump probe experiment")
lg.info("%s Pump probe extruder data collection" % name)
lg.info("%s Pump exposure time %s" % (name, pump_exp))
lg.info("%s Pump delay time %s" % (name, pump_delay))
logger.info("%s Pump probe extruder data collection" % name)
logger.info("%s Pump exposure time %s" % (name, pump_exp))
logger.info("%s Pump delay time %s" % (name, pump_delay))
sup.eiger("triggered", [filepath, filename, num_imgs, exp_time])
sup.zebra1(
"zebratrigger-eiger",
Expand All @@ -322,20 +333,20 @@ def run_extruderi24():
)
elif pump_status == "false":
print("Static experiment: no photoexcitation")
lg.info("%s Static experiment: no photoexcitation" % name)
logger.info("%s Static experiment: no photoexcitation" % name)
gate_start = 1.0
gate_width = (float(exp_time) * float(num_imgs)) + float(0.5)
sup.eiger("quickshot", [filepath, filename, num_imgs, exp_time])
sup.zebra1("quickshot", [gate_start, gate_width])
else:
lg.warning("%s Unknown Detector Type, det_type = %s" % (name, det_type))
logger.warning("%s Unknown Detector Type, det_type = %s" % (name, det_type))
print("Unknown detector type")

# Do DCID creation BEFORE arming the detector
dcid = DCID(
emit_errors=False,
ssx_type=SSXType.EXTRUDER,
visit=pathlib.Path(visit).name,
visit=Path(visit).name,
image_dir=filepath,
start_time=start_time,
num_images=num_imgs,
Expand All @@ -344,7 +355,7 @@ def run_extruderi24():

# Collect
print("\nFast Shutter Opening")
lg.info("%s Fast shutter opened" % (name))
logger.info("%s Fast shutter opened" % (name))
caput(pv.zebra1_soft_in_b1, 1)
if det_type == "pilatus":
print("pilatus acquire ON")
Expand Down Expand Up @@ -376,7 +387,7 @@ def run_extruderi24():
i += 1
if int(caget(pv.ioc12_gp8)) != 0:
aborted = True
lg.warning("%s Data Collection Aborted" % (name))
logger.warning("%s Data Collection Aborted" % (name))
print(50 * "ABORTED ")
if det_type == "pilatus":
caput(pv.pilat_acquire, 0)
Expand All @@ -391,14 +402,16 @@ def run_extruderi24():
else:
aborted = True
print("Data Collection ended due to GP 8 not equalling 0")
lg.warning("%s Data Collection ended due to GP 8 not equalling 0" % (name))
logger.warning(
"%s Data Collection ended due to GP 8 not equalling 0" % (name)
)
break
break
print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")

caput(pv.ioc12_gp8, 1)
print("\nFast Shutter Closing")
lg.info("%s Fast shutter closed" % (name))
logger.info("%s Fast shutter closed" % (name))
caput(pv.zebra1_soft_in_b1, 0)
print("\nZebra DISARMED")
caput(pv.zebra1_pc_disarm, 1)
Expand Down Expand Up @@ -434,23 +447,41 @@ def run_extruderi24():
dcid.notify_end()
print("Start time", start_time.ctime())
print("End time", end_time.ctime())
lg.info("%s End Time = %s" % (name, end_time))
logger.info("%s End Time = %s" % (name, end_time))
return 1


def main(args):
command = args[0]
print(args)
print("done")
if command == "initialise":
initialise_extruderi24()
elif command == "run":
run_extruderi24()
elif command == "moveto":
moveto(args[1])
else:
print("Unknown arg")
if __name__ == "__main__":
setup_logging()

parser = argparse.ArgumentParser(usage=usage, description=__doc__)
subparsers = parser.add_subparsers(
help="Choose command.",
required=True,
dest="sub-command",
)

if __name__ == "__main__":
main(sys.argv[1:])
parser_init = subparsers.add_parser(
"initialise",
description="Initialise extruder on beamline I24.",
)
parser_init.set_defaults(func=initialise_extruderi24)
parser_run = subparsers.add_parser(
"run",
description="Run extruder on I24.",
)
parser_run.set_defaults(func=run_extruderi24)
parser_mv = subparsers.add_parser(
"moveto",
description="Move extruder to requested setting on I24.",
)
parser_mv.add_argument(
"place",
type=str,
choices=["laseron", "laseroff", "enterhutch"],
help="Requested setting.",
)
parser_mv.set_defaults(func=moveto)

args = parser.parse_args()
args.func(args)
Loading

0 comments on commit 5233f07

Please sign in to comment.