From 134870a3cf67d0725988e61a1162ad1e79b9fb5d Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Thu, 24 Feb 2022 09:26:51 +0100
Subject: [PATCH] JF correct json format error, and wierd fix only if
 calibration_metadata.yml exists

---
 ...Jungfrau_Gain_Correct_and_Verify_NBC.ipynb |  3 +--
 src/cal_tools/tools.py                        | 23 ++++++++++---------
 2 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb
index 2c485c79b..c4ad3a842 100644
--- a/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb
+++ b/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb
@@ -46,8 +46,7 @@
     "manual_slow_data = False  # if true, use manually entered bias_voltage and integration_time values\n",
     "integration_time = 4.96  # integration time in us, will be overwritten by value in file\n",
     "gain_setting = 0  # 0 for dynamic gain, 1 for dynamic HG0, will be overwritten by value in file\n",
-    "gain_mode = 0  # 0 for runs with dynamic gain setting, 1 for fixgain. It will be overwritten by value in file, if manual_slow_data is set to True.
-",
+    "gain_mode = 0  # 0 for runs with dynamic gain setting, 1 for fixgain. It will be overwritten by value in file, if manual_slow_data is set to True.\n",
     "mem_cells = 0  # leave memory cells equal 0, as it is saved in control information starting 2019.\n",
     "bias_voltage = 180  # will be overwritten by value in file\n",
     "\n",
diff --git a/src/cal_tools/tools.py b/src/cal_tools/tools.py
index 962f1a0db..71fdba355 100644
--- a/src/cal_tools/tools.py
+++ b/src/cal_tools/tools.py
@@ -786,18 +786,19 @@ class CalibrationMetadata(dict):
     def __init__(self, output_dir: Union[Path, str], *args, new=False):
         dict.__init__(self, args)
         self._yaml_fn = Path(output_dir) / "calibration_metadata.yml"
-        if (not new) and self._yaml_fn.exists():
-            with self._yaml_fn.open("r") as fd:
-                data = yaml.safe_load(fd)
-            if isinstance(data, dict):
-                self.update(data)
+        if self._yaml_fn.exists():
+            if new:
+                # TODO: update after resolving this discussion
+                # https://git.xfel.eu/detectors/pycalibration/-/merge_requests/624  # noqa
+                self.save()
             else:
-                print(f"Warning: existing {self._yaml_fn} is malformed, "
-                      "will be overwritten")
-        else:
-            # TODO: update after resolving this discussion
-            # https://git.xfel.eu/detectors/pycalibration/-/merge_requests/624
-            self.save()
+                with self._yaml_fn.open("r") as fd:
+                    data = yaml.safe_load(fd)
+                if isinstance(data, dict):
+                    self.update(data)
+                else:
+                    print(f"Warning: existing {self._yaml_fn} is malformed, "
+                           "will be overwritten")
 
     def save(self):
         with self._yaml_fn.open("w") as fd:
-- 
GitLab