From 1f7c3b8718cca224c5f5135ef198631020875977 Mon Sep 17 00:00:00 2001
From: Robert Izzard <r.izzard@surrey.ac.uk>
Date: Sat, 16 Oct 2021 16:35:24 +0100
Subject: [PATCH] add ensemble_setting function to return the value of a
 parameter in an ensemble

---
 binarycpython/utils/functions.py | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/binarycpython/utils/functions.py b/binarycpython/utils/functions.py
index 60876ea0c..f70db74f9 100644
--- a/binarycpython/utils/functions.py
+++ b/binarycpython/utils/functions.py
@@ -2268,3 +2268,35 @@ def load_ensemble(filename):
     else:
         jfile = open(filename)
     return json.load(jfile)
+
+def ensemble_setting(ensemble,parameter_name):
+    """
+    Function to get the setting of parameter_name in the given ensemble, or return the default value.
+    """
+    value = None
+
+    try:
+        value = ensemble['metadata']['settings']['population_settings']['bse_options'][parameter_name]
+    except KeyError:
+        value = None
+
+    if value is None:
+        try:
+            value = ensemble['metadata']['settings']['population_settings']['grid_options'][parameter_name]
+        except KeyError:
+            value = None
+
+    if value is None:
+        try:
+            value = ensemble['metadata']['settings']['population_settings']['custom_options'][parameter_name]
+        except KeyError:
+            value = None
+
+    # not found, try the default
+    if value is None:
+        try:
+            value = ensemble['metadata']['settings']['binary_c_defaults'][parameter_name]
+        except KeyError:
+            value = None
+
+    return value
-- 
GitLab