Skip to content

Commit b8997ec

Browse files
Fix CSV column header spacing in FlightDataExporter (Issue #864)
Co-authored-by: Gui-FernandesBR <[email protected]>
1 parent 3deb5b8 commit b8997ec

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

rocketpy/simulation/flight_data_exporter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class attributes which are instances of the Function class. Usage
156156
) from exc
157157
variable_points = variable_function(time_points)
158158
exported_matrix += [variable_points]
159-
exported_header += f", {variable_function.__outputs__[0]}"
159+
exported_header += f",{variable_function.__outputs__[0]}"
160160

161161
exported_matrix = np.array(exported_matrix).T # Fix matrix orientation
162162

tests/unit/test_flight_data_exporter.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,63 @@ def test_export_kml_trajectory(flight_calisto_robust, tmp_path):
192192
assert np.allclose(flight_calisto_robust.latitude[:, 1], lat, atol=1e-3)
193193
assert np.allclose(flight_calisto_robust.longitude[:, 1], lon, atol=1e-3)
194194
assert np.allclose(flight_calisto_robust.z[:, 1], z, atol=1e-3)
195+
196+
197+
def test_export_data_csv_column_names_no_leading_spaces(flight_calisto, tmp_path):
198+
"""Test that CSV column headers have no leading spaces after commas.
199+
200+
This validates that exported CSV files can be easily read with pandas
201+
without requiring leading spaces in column names (Issue #864).
202+
203+
When reading CSVs with pandas, column names should be accessible as
204+
'Vz (m/s)' not ' Vz (m/s)' (with leading space).
205+
206+
Parameters
207+
----------
208+
flight_calisto : rocketpy.Flight
209+
Flight object to be tested.
210+
tmp_path : pathlib.Path
211+
Pytest fixture for temporary directories.
212+
"""
213+
file_name = tmp_path / "flight_data_columns.csv"
214+
FlightDataExporter(flight_calisto).export_data(
215+
str(file_name), "z", "vz", "altitude"
216+
)
217+
218+
# Read the header line directly
219+
with open(file_name, "r") as f:
220+
header_line = f.readline().strip()
221+
222+
# Verify header format - should have no spaces after commas
223+
# Format should be: # Time (s),Z (m),Vz (m/s),Altitude AGL (m)
224+
assert header_line.startswith("# Time (s),")
225+
assert ", " not in header_line, "Header should not contain ', ' (comma-space)"
226+
227+
# Verify with pandas that columns are accessible without leading spaces
228+
try:
229+
import pandas as pd
230+
231+
df = pd.read_csv(file_name)
232+
columns = df.columns.tolist()
233+
234+
# First column should be '# Time (s)'
235+
assert columns[0] == "# Time (s)"
236+
237+
# Other columns should NOT have leading spaces
238+
for col in columns[1:]:
239+
assert not col.startswith(" "), f"Column '{col}' has leading space"
240+
241+
# Verify columns are accessible with expected names (no leading spaces)
242+
assert "Z (m)" in columns
243+
assert "Vz (m/s)" in columns
244+
assert "Altitude AGL (m)" in columns
245+
246+
# Verify we can access data using column names without spaces
247+
_ = df["# Time (s)"]
248+
_ = df["Z (m)"]
249+
_ = df["Vz (m/s)"]
250+
_ = df["Altitude AGL (m)"]
251+
252+
except ImportError:
253+
# pandas not available, skip pandas-specific test
254+
pass

0 commit comments

Comments
 (0)