Further fixups to get calibcant-analyze.py working.
[calibcant.git] / calibcant / analyze.py
index b6807f882970c4b1c68247188c309f251ac5c5f2..ab3fbbb7c771c5e0da230b878cfa17af38d9fefb 100644 (file)
@@ -95,6 +95,8 @@ from .temperature_analyze import analyze as _temperature_analyze
 from .temperature_analyze import save as _temperature_save
 from .vibration_analyze import analyze as _vibration_analyze
 from .vibration_analyze import save as _vibration_save
+from .util import SaveSpec as _SaveSpec
+from .util import save as _save
 
 
 def analyze(bumps, temperatures, vibrations):
@@ -189,6 +191,20 @@ def plot(bumps, temperatures, vibrations):
     return figure
 _plot = plot  # alternative name for use inside analyze_all()
 
+def save_results(filename=None, group='/', bump=None,
+                 temperature=None, vibration=None, spring_constant=None,
+                 spring_constant_deviation=None):
+    specs = [
+        _SaveSpec(item=bump, relpath='raw/photodiode-sensitivity',
+                  array=True, units='V/m'),
+        _SaveSpec(item=temperature, relpath='raw/temperature',
+                  array=True, units='K'),
+        _SaveSpec(item=vibration, relpath='raw/vibration',
+                  array=True, units='V^2/Hz'),
+        _SaveSpec(item=spring_constant, relpath='processed/spring-constant',
+                  units='N/m', deviation=spring_constant_deviation),
+        ]
+    _save(filename=filename, group=group, specs=specs)
 
 def analyze_all(config, data, raw_data, maximum_relative_error=1e-5,
                 filename=None, group=None, plot=False, dry_run=False):
@@ -260,15 +276,18 @@ def analyze_all(config, data, raw_data, maximum_relative_error=1e-5,
             vibrations_changed) and not dry_run:
             calibration_group = _h5_create_group(group, 'calibration')
             if bumps_changed:
-                calib_save(group=calibration_group, bump=data['bump'])
+                save_results(
+                    group=calibration_group, bump=data['bump'])
             if temperatures_changed:
-                calib_save(
+                save_results(
                     group=calibration_group, temperature=data['temperature'])
             if vibrations_changed:
-                calib_save(
+                save_results(
                     group=calibration_group, vibration=data['vibration'])
             if changed:
-                calib_save(group=calibration_group, k=k, k_s=k_s)
+                save_results(
+                    group=calibration_group,
+                    spring_constant=k, spring_constant_deviation=k_s)
     finally:
         if f:
             f.close()
@@ -342,6 +361,7 @@ def check_calibration(k, k_s, maximum_relative_error, **kwargs):
     else:
         rel_error = abs(new_k-k)/k
         if rel_error > maximum_relative_error:
+            changed = True
             _LOG.warn(("new analysis doesn't match for the spring constant: "
                        "{} != {} (difference: {}, relative error: {})").format(
                     new_k, k, new_k-k, rel_error))
@@ -350,8 +370,9 @@ def check_calibration(k, k_s, maximum_relative_error, **kwargs):
         _LOG.warn('new analysis for the spring constant deviation: {}'.format(
                 new_k_s))
     else:
-        rel_error = abs(new_k-k)/k
+        rel_error = abs(new_k_s-k_s)/k_s
         if rel_error > maximum_relative_error:
+            changed = True
             _LOG.warn(
                 ("new analysis doesn't match for the spring constant deviation"
                  ": {} != {} (difference: {}, relative error: {})").format(