From f8f2b07e2a8fd8ac56e131063303003d26247998 Mon Sep 17 00:00:00 2001
From: David Keitel <david.keitel@ligo.org>
Date: Wed, 31 Jan 2018 13:53:23 +0000
Subject: [PATCH] writer function for atoms (TransientGridSearch)

---
 pyfstat/core.py                | 12 ++++++++++++
 pyfstat/grid_based_searches.py |  5 ++++-
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/pyfstat/core.py b/pyfstat/core.py
index 1ffda24..4482121 100755
--- a/pyfstat/core.py
+++ b/pyfstat/core.py
@@ -907,6 +907,18 @@ class ComputeFstat(BaseSearchClass):
         else:
             return ax
 
+    def write_atoms_to_file(self, fnamebase=''):
+        multiFatoms = getattr(self.FstatResults, 'multiFatoms', None)
+        if multiFatoms and multiFatoms[0]:
+            dopplerName = lalpulsar.PulsarDopplerParams2String ( self.PulsarDopplerParams )
+            #fnameAtoms = os.path.join(self.outdir,'Fstatatoms_%s.dat' % dopplerName)
+            fnameAtoms = fnamebase + '_Fstatatoms_%s.dat' % dopplerName
+            fo = lal.FileOpen(fnameAtoms, 'w')
+            lalpulsar.write_MultiFstatAtoms_to_fp ( fo, multiFatoms[0] )
+            del fo # instead of lal.FileClose() which is not SWIG-exported
+        else:
+            raise RuntimeError('Cannot print atoms vector to file: no FstatResults.multiFatoms, or it is None!')
+
 
 class SemiCoherentSearch(ComputeFstat):
     """ A semi-coherent search """
diff --git a/pyfstat/grid_based_searches.py b/pyfstat/grid_based_searches.py
index 20ddd0f..42b3c3e 100644
--- a/pyfstat/grid_based_searches.py
+++ b/pyfstat/grid_based_searches.py
@@ -354,7 +354,8 @@ class TransientGridSearch(GridSearch):
                  input_arrays=False, assumeSqrtSX=None,
                  transientWindowType=None, t0Band=None, tauBand=None,
                  dt0=None, dtau=None,
-                 outputTransientFstatMap=False):
+                 outputTransientFstatMap=False,
+                 outputAtoms=False):
         """
         Parameters
         ----------
@@ -442,6 +443,8 @@ class TransientGridSearch(GridSearch):
                 thisCand += [windowRange.t0+maxidx[0]*windowRange.dt0,
                              windowRange.tau+maxidx[1]*windowRange.dtau]
             data.append(thisCand)
+            if self.outputAtoms:
+                self.search.write_atoms_to_file(os.path.splitext(self.out_file)[0])
 
         data = np.array(data, dtype=np.float)
         if return_data:
-- 
GitLab