From 66969e223f5748f2c514bd2b8521eabf38bdd7cb Mon Sep 17 00:00:00 2001
From: Laurent Mercadier <laurent.mercadier@xfel.eu>
Date: Wed, 23 Oct 2019 14:13:01 +0200
Subject: [PATCH] Concatenate: simplifies sorting and add attrs of each run

---
 Load.py | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/Load.py b/Load.py
index a26a487..094c280 100644
--- a/Load.py
+++ b/Load.py
@@ -453,7 +453,7 @@ def load(fields, runNB, proposalNB, subFolder='raw', display=False, validate=Fal
     return result
 
 def concatenateRuns(runs):
-    """ Concatenate a list of two runs with identical data variables along the
+    """ Sorts and concatenate a list of runs with identical data variables along the
         trainId dimension.
         
         Input:
@@ -461,12 +461,16 @@ def concatenateRuns(runs):
         Output:
             a concatenated xarray Dataset
     """
-    keys = sorted(runs[0].keys())
-    for run in runs[1:]:
-        if sorted(run.keys()) != keys:
+    firstTid = {i: int(run.trainId[0].values) for i,run in enumerate(runs)}
+    orderedDict = dict(sorted(firstTid.items(), key=lambda t: t[1]))
+    orderedRuns = [runs[i] for i in orderedDict]
+    keys = orderedRuns[0].keys()
+    for run in orderedRuns[1:]:
+        if run.keys() != keys:
             print('data fields between different runs are not identical. Cannot combine runs.')
             return
     
-    result = xr.concat(runs, dim='trainId')
-    result = result.sortby(result.trainId)
+    result = xr.concat(orderedRuns, dim='trainId')
+    result.attrs['run'] = [run.attrs['run'] for run in orderedRuns]
+    result.attrs['runFolder'] = [run.attrs['runFolder'] for run in orderedRuns]
     return result
-- 
GitLab