From ad3de0aa15501f8a62137f79c3fad410ce92abb6 Mon Sep 17 00:00:00 2001
From: David Hendriks <davidhendriks93@gmail.com>
Date: Tue, 10 Mar 2020 23:04:54 +0000
Subject: [PATCH] updated the write_binary_c_calls_to_file with the option to
 also write all the default values

---
 binarycpython/utils/grid.py    | 53 +++++++++++++++++++++++++++++-----
 tests/population/grid_tests.py |  7 +++++
 2 files changed, 53 insertions(+), 7 deletions(-)

diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py
index 0b916daa5..52235de3f 100644
--- a/binarycpython/utils/grid.py
+++ b/binarycpython/utils/grid.py
@@ -1187,20 +1187,26 @@ class Population(object):
         total_starcount = system_generator(self)
         self.grid_options["total_starcount"] = total_starcount
 
-    def write_binary_c_calls_to_file(self, output_dir=None, output_filename=None):
+    def write_binary_c_calls_to_file(self, output_dir=None, output_filename=None, include_defaults=False):
         """
         Function that loops over the gridcode and writes the generated parameters to a file. In the form of a commandline call
-         
+
+        Only useful when you have a variable grid as system_generator. MC wouldnt be that useful
+
+        Also, make sure that in this export there are the basic parameters like m1,m2,sep, orb-per, ecc, probability etc.
+
         On default this will write to the datadir, if it exists
+
+        # warning; dont use yet. not fully tested. 
         """
 
         if self.grid_options["system_generator"]:
+            # Check if there is an output dir configured
             if self.custom_options.get("data_dir", None):
                 binary_c_calls_output_dir = self.custom_options["data_dir"]
-                print("yo")
+            # otherwise check if theres one passed to the function
             else:
                 if not output_dir:
-                    # if self.grid_options['verbose'] > 0:
                     print(
                         "Error. No data_dir configured and you gave no output_dir. Aborting"
                     )
@@ -1208,18 +1214,31 @@ class Population(object):
                 else:
                     binary_c_calls_output_dir = output_dir
 
+            # check if theres a filename passed to the function
             if output_filename:
                 binary_c_calls_filename = output_filename
+            # otherwise use default value
             else:
                 binary_c_calls_filename = "binary_c_calls.txt"
 
-            print(binary_c_calls_output_dir, binary_c_calls_filename)
+            binary_c_calls_full_filename = os.path.join(binary_c_calls_output_dir, binary_c_calls_filename)
+            print("Writing binary_c calls to {}".format(binary_c_calls_full_filename))
 
+            # Write to file
             with open(
-                os.path.join(binary_c_calls_output_dir, binary_c_calls_filename), "w"
+                binary_c_calls_full_filename, "w"
             ) as f:
-                for system in self.grid_options["system_generator"]:
+                # Get defaults and clean them, then overwrite them with the set values. 
+                if include_defaults:
+                    # TODO: make sure that the defaults here are cleaned up properly
+                    cleaned_up_defaults = self.cleaned_up_defaults()
+                    full_system_dict = cleaned_up_defaults.copy()
+                    full_system_dict.update(self.bse_options.copy())
+                else:
                     full_system_dict = self.bse_options.copy()
+
+                for system in self.grid_options["system_generator"]:
+                    # update values with current system values
                     full_system_dict.update(system)
 
                     binary_cmdline_string = self.return_argline(full_system_dict)
@@ -1259,5 +1278,25 @@ class Population(object):
             )
             print(info_string)
 
+    def cleanup_defaults(self):
+        """
+        Function to clean up the default values: 
+        
+        from a dictionary, removes the entries that have the following values:
+        - "NULL" 
+        - ""
+        - "Function"
+
+        """
+
+        cleaned_dict = {}
+        binary_c_defaults = self.return_binary_c_defaults().copy()
+        for key in binary_c_defaults:
+            if not binary_c_defaults[key] in ['NULL', '', 'Function']:
+                cleaned_dict[key] = binary_c_defaults[key]
+
+        return cleaned_dict
+
+
 
 ################################################################################################
diff --git a/tests/population/grid_tests.py b/tests/population/grid_tests.py
index 57b211596..a5991718a 100644
--- a/tests/population/grid_tests.py
+++ b/tests/population/grid_tests.py
@@ -28,6 +28,13 @@ test_pop.set(
 )
 # print(test_pop.bse_options)
 
+# print(len(test_pop.return_binary_c_defaults()))
+# print('\n\n')
+# print(len(test_pop.cleanup_defaults()))
+
+line = test_pop.return_argline(test_pop.cleanup_defaults())
+print(line)
+quit()
 ## Testing single evolution
 # test_pop.evolve_single()
 # test_pop.test_evolve_single()
-- 
GitLab