From 02bcbd73ff9b168d7c4d282f2e6841b8685a548b Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Thu, 17 Feb 2022 11:27:27 +0100
Subject: [PATCH] change ccv_tag to variant and expose it in pycalibration and
 agipdlib

---
 src/cal_tools/agipdlib.py | 12 +++++-------
 src/cal_tools/tools.py    | 16 ++++++++--------
 2 files changed, 13 insertions(+), 15 deletions(-)

diff --git a/src/cal_tools/agipdlib.py b/src/cal_tools/agipdlib.py
index 4b4d1e3eb..c5ea91bfd 100644
--- a/src/cal_tools/agipdlib.py
+++ b/src/cal_tools/agipdlib.py
@@ -1026,7 +1026,7 @@ class AgipdCorrections:
                 creation_time,
                 print_once=0,
             )
-            cons_data[cname]["version"] = md.calibration_constant_version.ccv_tag
+            cons_data[cname]["variant"] = md.calibration_constant_version.variant
             when[cname] = md.calibration_constant_version.begin_at
 
         return cons_data, when
@@ -1249,15 +1249,13 @@ class AgipdCorrections:
         db_module = const_yaml[karabo_da]["physical-detector-unit"]
         for cname, mdata in const_yaml[karabo_da]["constants"].items():
             cons_data[cname] = {}
+            base_key = f"{db_module}/{cname}/0"
             when[cname] = mdata["creation-time"]
             if when[cname]:
                 with h5py.File(mdata["file-path"], "r") as cf:
-                    cons_data[cname]["data"] = np.copy(
-                        cf[f"{db_module}/{cname}/0/data"])
-                    # TODO: choose an acceptable name.
-                    if f"{db_module}/{cname}/0/version" in cf.keys():
-                        cons_data[cname]["version"] = cf[
-                            f"{db_module}/{cname}/0/version"]
+                    cons_data[cname]["data"] = np.copy(cf[f"{base_key}/data"])
+                    if "variant" in cf[base_key].attrs.keys():
+                        cons_data[cname]["variant"] = cf[base_key].attrs["variant"]  # noqa
             else:
                 # Create empty constant using the list elements
                 cons_data[cname]["data"] = getattr(
diff --git a/src/cal_tools/tools.py b/src/cal_tools/tools.py
index 1746c6f08..335b3e818 100644
--- a/src/cal_tools/tools.py
+++ b/src/cal_tools/tools.py
@@ -602,10 +602,10 @@ def get_from_db(karabo_id: str, karabo_da: str,
                         f" and data_set_name is {h5path}."
                     )
                 with h5py.File(Path(hdf5path, filename), "r") as f:
-                    arr = f[f"{h5path}/data"][()]
-                    version = f[f"{h5path}/version"][()]
-                metadata.calibration_constant.data = arr
-                metadata.calibration_constant_version.ccv_tag = version
+                    metadata.calibration_constant.data = f[f"{h5path}/data"][()]  # noqa
+                    if "variant" in f[h5path].attrs.keys():
+                        metadata.calibration_constant_version.variant = (
+                            f[h5path].attrs["variant"])
 
             if verbosity > 0:
                 if constant.name not in already_printed or verbosity > 1:
@@ -628,7 +628,7 @@ def send_to_db(db_module: str, karabo_id: str, constant, condition,
                timeout: int = 30000,
                ntries: int = 7,
                doraise: bool = False,
-               ccv_tag: Optional[int] = None):
+               variant: Optional[int] = None):
     """Return calibration constants and metadata requested from CalDB
 
     :param db_module: database module (PDU/Physical Detector Unit)
@@ -645,8 +645,8 @@ def send_to_db(db_module: str, karabo_id: str, constant, condition,
     ntries is set to 7 so that if the timeout started
     at 30s last timeout will be ~ 1h.
     :param doraise: if True raise errors during communication with DB
-    :param ccv_tag: A calibration constant version tag to save in the
-    h5 file along with the constant data.
+    :param variant: A calibration constant version variant attribute
+    for the constant file.
     """
 
     success = False
@@ -675,7 +675,7 @@ def send_to_db(db_module: str, karabo_id: str, constant, condition,
         metadata.calibration_constant_version.device_name = db_module
         metadata.calibration_constant_version.karabo_da = None
         metadata.calibration_constant_version.raw_data_location = file_loc
-        metadata.calibration_constant_version.ccv_tag = ccv_tag
+        metadata.calibration_constant_version.variant = variant
         if constant.data is None:
             raise ValueError(
                 "There is no data available to "
-- 
GitLab