From 6d37bce8dae0c511110afe56cf777aa5663fce7a Mon Sep 17 00:00:00 2001
From: Giovanni La Mura <giovanni.lamura@inaf.it>
Date: Tue, 19 Mar 2024 11:38:43 +0100
Subject: [PATCH] Append thread output files to program output

---
 src/cluster/cluster.cpp | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/src/cluster/cluster.cpp b/src/cluster/cluster.cpp
index c0158f91..f0135268 100644
--- a/src/cluster/cluster.cpp
+++ b/src/cluster/cluster.cpp
@@ -604,8 +604,31 @@ void cluster(string config_file, string data_file, string output_path) {
 	delete[] am_2;
 	
       } // jxi488 loop
-      for (int ri=0; ri<ompnumthreads; ri++) {
+      for (int ri = 0; ri < ompnumthreads; ri++) {
       // Giovanni, please add here in this loop code to reopen the temporary files, reread them and append them respectively to the global output and tppoan, before closing them
+	string partial_file_name = output_path + "/c_OCLU_" + to_string(ri);
+	printf("Copying ASCII output of thread %d of %d... ", ri, ompnumthreads);
+	FILE *partial_output = fopen(partial_file_name.c_str(), "r");
+	char c = fgetc(partial_output);
+	while (c != EOF) {
+	  fputc(c, output);
+	  c = fgetc(partial_output);
+	}
+	fclose(partial_output);
+	remove(partial_file_name.c_str());
+	printf("done.\n");
+	partial_file_name = output_path + "/c_TPPOAN_" + to_string(ri);
+	printf("Copying binary output of thread %d of %d... ", ri, ompnumthreads);
+	fstream partial_tppoan;
+	partial_tppoan.open((output_path + "/c_TPPOAN_" + to_string(ri)).c_str(), ios::in | ios::binary);
+	c = partial_tppoan.get();
+	while(!partial_tppoan.eof()) {
+	  tppoan.put(c);
+	  c = partial_tppoan.get();
+	}
+	partial_tppoan.close();
+	remove(partial_file_name.c_str());
+	printf("done.\n");
       }
       tppoan.close();
     } else { // In case TPPOAN could not be opened. Should never happen.
-- 
GitLab