Skip to content

Commit e51b221

Browse files
GMinoruyGui-FernandesBR
authored andcommitted
ENH: trying to add Monte Carlo output to csv and json see issue #242
1 parent 45a891e commit e51b221

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

rocketpy/simulation/monte_carlo.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"""
1515

1616
import json
17+
import csv
1718
import os
1819
import traceback
1920
import warnings
@@ -588,6 +589,25 @@ def __evaluate_flight_outputs(self, flight, sim_idx):
588589
json.dumps(outputs_dict, cls=RocketPyEncoder, **self._export_config) + "\n"
589590
)
590591

592+
def export_results(self, output_filename, output_format):
593+
txt_data = []
594+
with open(self.filename, "r", encoding="utf-8") as f:
595+
for line in enumerate(f):
596+
line = line.strip()
597+
data = json.loads(line)
598+
txt_data.append(data)
599+
600+
output_format = output_format.strip().lower()
601+
if output_format == "json":
602+
with open(f"{output_filename}.json", "w", encoding="utf-8") as f:
603+
json.dump(txt_data, f, indent=4)
604+
elif output_format == "csv":
605+
output_csv_header = txt_data[0].keys()
606+
with open(f"{output_filename}.csv", "w", newLine='', encoding="utf-8") as f:
607+
output_writer = csv.DictWriter(f, fieldnames=output_csv_header)
608+
output_writer.writeheader()
609+
output_writer.writerows(txt_data)
610+
591611
def __terminate_simulation(self):
592612
"""
593613
Terminates the simulation, closes the files and prints the results.

0 commit comments

Comments
 (0)