From 842fdefd9cfb42b9e79016aab13f6f253ec05c8e Mon Sep 17 00:00:00 2001
From: Olivier Coulaud <olivier.coulaud@inria.fr>
Date: Thu, 23 Jan 2025 18:35:31 +0100
Subject: [PATCH] add ascii writer

---
 python_interface/fmmTools.py | 52 ++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

diff --git a/python_interface/fmmTools.py b/python_interface/fmmTools.py
index de7c4855..b106b824 100644
--- a/python_interface/fmmTools.py
+++ b/python_interface/fmmTools.py
@@ -102,6 +102,57 @@ def fmareader(filename,verbose=False):
         sys.exit("Wrong extention. only .fma (ascii) or .bfma (binary) files are avalaible")
 
 
+def fmawriter_ascii(filename,particles, centre, width,verbose=False):
+    '''
+        write particles in  FMA file of scalfmmm 
+
+    '''
+
+    # write the two first line of the header
+    file = open(filename, "w")
+    #   DatatypeSize  Number_of_record_per_line dimension Number_of_input_data
+    #   NB_particles  half_Box_width  Center (dim values)
+    #   Particle_values
+    p_shape = particles.shape
+    print(type(p_shape),p_shape)
+    if particles[0].dtype == 'float64':
+        line1 = '8'
+    else:
+        line1 = '4'
+    n = 0
+    for i in range(p_shape[0]):
+        n += particles[i].shape[1]
+    dimension = particles[0].shape[1]
+    line1 += ' ' + str(n) + ' ' + str(dimension) 
+    if p_shape[0] > 1:
+        nb_inputs = particles[1].shape[1]
+        line1 += ' ' + str(nb_inputs) #nb_inputs
+    else:
+        nb_inputs = 0 
+        line1 += ' 0 '
+    file.write(line1+'\n')
+    nb_outputs = n -  dimension - nb_inputs
+    #
+    nb_part = particles[0].shape[0]
+    line2 = str(nb_part)
+    line2 += ' ' + str(width/2 )
+    for i in range(dimension):
+        line2 += ' ' + str(centre[i])
+    file.write(line2+'\n')
+    pos = particles[0]
+    for p in range(nb_part):
+        linep = ''
+        for i in range(p_shape[0]):
+            current = particles[i]
+            for j in range(current.shape[1]):
+                linep += str(current[p, j]) + ' '
+
+        file.write(linep+'\n')
+
+    #
+
+    file.close()
+
 ########################################################
 if __name__ == '__main__':
     filename = "../data/cubeSorted_10_PF.fma"
@@ -118,3 +169,4 @@ if __name__ == '__main__':
     print("size", size)
 
     print(particles)
+    fmawriter_ascii('toto.fma',particles, centre, size)
\ No newline at end of file
-- 
GitLab