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