forked from NSLS2/srx-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogscan.py
More file actions
82 lines (69 loc) · 2.74 KB
/
logscan.py
File metadata and controls
82 lines (69 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from pathlib import Path
from prefect import flow, task, get_run_logger
from data_validation import get_run
def find_scanid(logfile_path, scanid):
is_scanid = False
with open(logfile_path) as lf:
for line in lf:
params = line.strip().split("\t")
if int(params[0]) == scanid:
is_scanid = True
break
return is_scanid
@task
def logscan_detailed(scanid, api_key=None, dry_run=False):
logger = get_run_logger()
h = get_run(scanid, api_key=api_key)
if (
"Beamline Commissioning (beamline staff only)".lower()
in h.start["proposal"]["type"].lower()
):
userdatadir = (
f"/nsls2/data/srx/proposals/commissioning/{h.start['data_session']}/"
)
else:
userdatadir = (
f"/nsls2/data/srx/proposals/{h.start['cycle']}/{h.start['data_session']}/"
)
if not Path(userdatadir).exists():
logger.info(
"Incorrect path. Check cycle and proposal id in document. Not running the logger on this document."
)
return
logfile_path = userdatadir + f"logfile{h.start['data_session']}.txt"
is_scanid = False
if Path(logfile_path).exists():
is_scanid = find_scanid(logfile_path, h.start["scan_id"])
if not is_scanid:
# Let's build the string
# Each scan should have a scan ID and UID
out_str = f"{h.start['scan_id']}\t{h.start['uid']}"
# I don't think the "scan" dictionary is guaranteed with each scan
# I think this is SRX-custom-scan specific
if "scan" in h.start:
# type probably exists if scan exists, but better to check
if "type" in h.start["scan"]:
out_str += f"\t{h.start['scan']['type']}"
# This is not in every scan type, e.g. peakup
if "scan_input" in h.start["scan"]:
out_str += f"\t{h.start['scan']['scan_input']}"
else:
# We should probably record what the scan was, e.g. count, scan, rel_scan
if "plan_name" in h.start:
out_str += f"\t{h.start['plan_name']}"
else:
out_str += "\tunknown scan"
out_str += "\n"
# Write to file
if dry_run:
logger.info(f"Dry run: scan_id: {h.start['scan_id']} output: {out_str}")
else:
with open(logfile_path, "a") as userlogf:
userlogf.write(out_str)
logger.info(f"Added {h.start['scan_id']} to the logs")
@flow(log_prints=True)
def logscan(ref, api_key=None, dry_run=False):
logger = get_run_logger()
logger.info("Start writing logfile...")
logscan_detailed(ref, dry_run=dry_run)
logger.info("Finish writing logfile.")