From a8c30f9a45e6afd6ad3e4606c6ba54d98f19221a Mon Sep 17 00:00:00 2001
From: Colm Talbot <colm.talbot@ligo.org>
Date: Wed, 26 Sep 2018 21:20:07 -0500
Subject: [PATCH] Allow overwrite results

---
 CHANGELOG.md         |  2 ++
 tupak/core/result.py | 24 ++++++++++++++++++------
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 73ce6290..5a72f151 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,6 +27,8 @@ Changes currently on master, but not under a tag.
 - PowerSpectralDensity structure modified
 - Fixed bug in get_open_data 
 - .prior files are no longer created. The prior is stored in the result object.
+- Users can now choose to overwrite existing result files, rather than creating
+  a .old file.
 
 ### Removed
 - Removes the "--detectors" command line argument (not a general CLI requirement)
diff --git a/tupak/core/result.py b/tupak/core/result.py
index 3b14ca1d..3912a598 100644
--- a/tupak/core/result.py
+++ b/tupak/core/result.py
@@ -167,15 +167,27 @@ class Result(dict):
             item = [Result._standardise_a_string(i) for i in item]
         return item
 
-    def save_to_file(self):
-        """ Writes the Result to a deepdish h5 file """
+    def save_to_file(self, overwrite=False):
+        """
+        Writes the Result to a deepdish h5 file
+
+        Parameters
+        ----------
+        overwrite: bool, optional
+            Whether or not to overwrite an existing result file.
+            default=False
+        """
         file_name = result_file_name(self.outdir, self.label)
         utils.check_directory_exists_and_if_not_mkdir(self.outdir)
         if os.path.isfile(file_name):
-            logger.debug(
-                'Renaming existing file {} to {}.old'.format(file_name,
-                                                             file_name))
-            os.rename(file_name, file_name + '.old')
+            if overwrite:
+                logger.debug('Removing existing file {}'.format(file_name))
+                os.remove(file_name)
+            else:
+                logger.debug(
+                    'Renaming existing file {} to {}.old'.format(file_name,
+                                                                 file_name))
+                os.rename(file_name, file_name + '.old')
 
         logger.debug("Saving result to {}".format(file_name))
 
-- 
GitLab