From 90713ca761e73828197dbd5721aa2f883d7f7053 Mon Sep 17 00:00:00 2001
From: Martin Teichmann <martin.teichmann@xfel.eu>
Date: Thu, 24 Feb 2022 17:51:01 +0100
Subject: [PATCH] pre-date HRIXS detector images

the HRIXS detector unfortunately gives no proper trainId when the image
is taken, but when the image is received. This pre-dates the image
to when it was actually taken.
---
 src/toolbox_scs/load.py | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/toolbox_scs/load.py b/src/toolbox_scs/load.py
index e9c9417..b311ae0 100644
--- a/src/toolbox_scs/load.py
+++ b/src/toolbox_scs/load.py
@@ -169,6 +169,14 @@ def load(proposalNB=None, runNB=None,
         if k not in rois:
             # no ROIs selection, we read everything
             arr = run.get_array(*v.values(), name=k)
+            
+            # the HRIXS detector puts the trainId when the image arrives at the
+            # computer. This is much later than it is actually taken. We estimate the
+            # readout time and subract it from the trainId in order to pre-date the
+            # image. 100000 means 1 MHz readout / 10 Hz XFEL rep-rate
+            if f == 'hRIXS_det':
+                arr = arr.assign_coords(trainId=arr['trainId']
+                                        - int(arr.shape[1] * arr.shape[2] // 100000) - 1)
             if len(arr) == 0:
                 log.warning(f'Empty array for {f}: {v["source"]}, {v["key"]}. '
                             'Skipping!')
-- 
GitLab