11import logging
22import os
33from pathlib import Path
4+ from typing import Union
45
56import numpy as np
67
78logger = logging .getLogger (__name__ )
89
910
10- def save_drag_curve (datapoints , data_labels , output_folder ):
11- """Extracts the drag curve from the data and saves it to a csv file.
12-
13- Parameters
14- ----------
15- datapoints : list
16- The datapoints.
17- data_labels : list of str
18- The labels of the data.
19- path : str
20- Path to the folder where the drag curve should be saved. This needs to
21- be a folder that already exists.
22-
23- Returns
24- -------
25- path : str
26- The path to the drag curve.
27- """
28- # Remove the data after apogee
29- altitude_vector = [
11+ def __collect_altitude_vector (
12+ datapoints : list [float ], data_labels : list [str ]
13+ ) -> list [float ]:
14+ return [
3015 float (datapoint .text .split ("," )[data_labels .index ("Altitude" )])
3116 for datapoint in datapoints
3217 ]
33- logger .info ("Collected altitude vector" )
3418
19+
20+ def __remove_data_after_apogee (datapoints : list [float ], altitude_vector : list [float ]):
3521 apogee_index = np .argmax (altitude_vector )
36- datapoints = datapoints [:apogee_index ]
37- logger .info ("Removed data after apogee" )
22+ return datapoints [:apogee_index ]
3823
39- # Extract the drag coefficient and Mach number
24+
25+ def __extract_drag_and_mach (
26+ datapoints : list [float ], data_labels : list [str ]
27+ ) -> np .ndarray :
4028 cd = [
4129 float (datapoint .text .split ("," )[data_labels .index ("Axial drag coefficient" )])
4230 for datapoint in datapoints
@@ -45,24 +33,41 @@ def save_drag_curve(datapoints, data_labels, output_folder):
4533 float (datapoint .text .split ("," )[data_labels .index ("Mach number" )])
4634 for datapoint in datapoints
4735 ]
48- logger .info ("Collected drag coefficient and Mach number" )
36+ return np .array ([mach , cd ]).T
37+
38+
39+ def __process_drag_data (cd : np .ndarray ) -> np .ndarray :
40+ cd = cd [~ np .isnan (cd ).any (axis = 1 ), :] # Remove NaN values
41+ cd = cd [cd [:, 0 ].argsort ()] # Sort by Mach number
42+ cd = np .unique (cd , axis = 0 ) # Remove duplicate Mach numbers
43+ cd = cd [cd [:, 1 ] > 0 , :] # Remove values when the drag is lower than 0
44+ return cd
4945
50- # Convert to numpy array
51- cd = np .array ([mach , cd ]).T
52- # Remove NaN values to avoid errors
53- cd = cd [~ np .isnan (cd ).any (axis = 1 ), :]
54- # Sort by Mach number
55- cd = cd [cd [:, 0 ].argsort ()]
56- # Remove duplicate Mach numbers
57- cd = np .unique (cd , axis = 0 )
58- # Remove values when the drag is lower than 0
59- cd = cd [cd [:, 1 ] > 0 , :]
60- logger .info ("Successfully created the drag curve" )
6146
62- # Save to csv file
47+ def __save_to_csv ( cd : np . ndarray , output_folder : Union [ Path , str ]) -> str :
6348 path = os .path .join (output_folder , "drag_curve.csv" )
6449 np .savetxt (path , cd , delimiter = "," , fmt = "%.6f" )
6550 logger .info (
6651 "Successfully saved the drag curve file to: '%s'" , Path (path ).as_posix ()
6752 )
6853 return path
54+
55+
56+ def save_drag_curve (
57+ datapoints : list [float ], data_labels : list [str ], output_folder : Union [Path , str ]
58+ ) -> str :
59+ """Extracts the drag curve from the data and saves it to a csv file."""
60+
61+ altitude_vector = __collect_altitude_vector (datapoints , data_labels )
62+ logger .info ("Collected altitude vector" )
63+
64+ datapoints = __remove_data_after_apogee (datapoints , altitude_vector )
65+ logger .info ("Removed data after apogee" )
66+
67+ cd = __extract_drag_and_mach (datapoints , data_labels )
68+ logger .info ("Collected drag coefficient and Mach number" )
69+
70+ cd = __process_drag_data (cd )
71+ logger .info ("Successfully created the drag curve" )
72+
73+ return __save_to_csv (cd , output_folder )
0 commit comments