From 8aa6567c4f57e786f276deaab97f8154208e1d1b Mon Sep 17 00:00:00 2001
From: Olivier Coulaud <olivier.coulaud@inria.fr>
Date: Fri, 24 Jan 2025 10:12:23 +0100
Subject: [PATCH] improvement in fmawriter for Python

---
 python_interface/README.md     | 10 +++++++++
 python_interface/fmmTools.py   | 23 +++++++++++++++++++++
 python_interface/read-write.py | 37 ++++++++++++++++++++++++++++++++++
 3 files changed, 70 insertions(+)
 create mode 100644 python_interface/read-write.py

diff --git a/python_interface/README.md b/python_interface/README.md
index 90ad0472..c39f5f54 100644
--- a/python_interface/README.md
+++ b/python_interface/README.md
@@ -1,5 +1,8 @@
 # Python binding for ScalFMM (experimental)
 
+
+## Build thz python module
+
 To build the python module:
 ``` bash
 cd ScalFMM
@@ -13,6 +16,10 @@ Do not forget to position the `PYTHONPATH` environment variable:
 export PYTHONPATH=build/python_interface/
 ```
 
+## Tutorial
+
+
+
 Finally, you can run the tutorial (this is a translation of `examples/tutorial.cpp` which is a simple minimalist 2D toy example):
   - Run the sequential version (example with 50000)
 ``` bash
@@ -28,3 +35,6 @@ export OMP_MAX_TASK_PRIORITY=11
 python3 python_interface/tutorial.py --group-size 1 --order 5 --tree-height 3 --number-particles 50000 --random --center 0 0 --width 2 -vv --open-mp
 ```
 
+## Examples
+
+### Read/write particles
diff --git a/python_interface/fmmTools.py b/python_interface/fmmTools.py
index b106b824..31a0b687 100644
--- a/python_interface/fmmTools.py
+++ b/python_interface/fmmTools.py
@@ -153,6 +153,29 @@ def fmawriter_ascii(filename,particles, centre, width,verbose=False):
 
     file.close()
 
+
+
+def fmawriter(filename, particles, centre, width, verbose=False):
+    '''
+        Read an FMA file of scalfmmm 
+
+        Return 
+            particules an array of three objects (position, inputs, ou tputs)
+            centre of the box
+            the size of the box
+    '''
+    # Chemin du fichier
+    print("read file ",filename)
+    # Extraction de l'extension
+    extension = os.path.splitext(filename)
+    if  extension[-1] == '.fma':
+        fmawriter_ascii(filename,particles, centre, width,verbose)
+    # elif  extension[-1] == '.bfma':
+    #     return fmareader_binary(filename,verbose)
+    else:
+        sys.exit("Wrong extention. only .fma (ascii) files are avalaible")
+
+
 ########################################################
 if __name__ == '__main__':
     filename = "../data/cubeSorted_10_PF.fma"
diff --git a/python_interface/read-write.py b/python_interface/read-write.py
new file mode 100644
index 00000000..4cf6fd45
--- /dev/null
+++ b/python_interface/read-write.py
@@ -0,0 +1,37 @@
+import argparse
+import numpy as np 
+
+from pyfmm import fmaloader
+from fmmTools import fmareader, fmawriter
+
+
+
+parser = argparse.ArgumentParser(prog="read-write")
+
+parser.add_argument("-if", "--input_file", type=str,  required=True, help="Particles file to read in fma/bfma format")
+parser.add_argument("-of", "--output_file",default="output.fma",  type=str, help="Particles file to write in fma format")
+
+# Create the parser
+
+args = parser.parse_args()
+
+parser.print_help()
+
+filename = args.input_file
+
+
+particles, centre, width = fmareader(filename)
+
+print("centre", centre)
+print("size", width)
+
+filename = args.output_file
+
+print(particles)
+
+# remove z axis
+pos = particles[0]
+particles[0] = pos[:,0:2]
+#
+#. Write the new particles
+fmawriter(filename,particles, centre, width)
\ No newline at end of file
-- 
GitLab