Last active 4 days ago

Revision 1a968307d8ed377734c6832f0075c78d992468dc

SpringClean.py Raw
1import pandas as pd
2import glob
3import os
4
5
6# ---------------------------------------------------------
7# STEP 1 - Ask user which spring to process
8# ---------------------------------------------------------
9spring_name = input("Enter spring number (example: 1040_01): ").strip()
10
11
12# ---------------------------------------------------------
13# STEP 2 - Find all matching test files
14# ---------------------------------------------------------
15file_pattern = f"Spring_{spring_name}_Test_*.csv"
16
17file_list = glob.glob(file_pattern)
18
19# Make sure files are processed in order
20file_list.sort()
21
22if len(file_list) == 0:
23 print(f"No files found matching: {file_pattern}")
24 exit()
25
26
27# ---------------------------------------------------------
28# STEP 3 - Variables used while combining files
29# ---------------------------------------------------------
30combined_data = []
31
32time_offset = 0
33
34
35# ---------------------------------------------------------
36# STEP 4 - Process each test file
37# ---------------------------------------------------------
38for test_number, file in enumerate(file_list, start=1):
39
40 print(f"Processing: {file}")
41
42 # Read CSV
43 df = pd.read_csv(file)
44
45 # Remove rows containing empty data
46 df = df.dropna()
47
48 # Time column name
49 time_column = "Time (s)"
50
51 # Create Test column
52 df["Test"] = test_number
53
54 # Create continuous time column
55 df["Continuous Time (s)"] = df[time_column] + time_offset
56
57 # Determine ending time for next test
58 last_time = df[time_column].iloc[-1]
59
60 time_offset += last_time
61
62 # Store cleaned data
63 combined_data.append(df)
64
65
66# ---------------------------------------------------------
67# STEP 5 - Combine all tests into one dataframe
68# ---------------------------------------------------------
69final_df = pd.concat(combined_data, ignore_index=True)
70
71
72# ---------------------------------------------------------
73# STEP 6 - Create output filename
74# ---------------------------------------------------------
75output_file = f"Spring_{spring_name}_Cleaned.csv"
76
77
78# ---------------------------------------------------------
79# STEP 7 - Save cleaned data
80# ---------------------------------------------------------
81final_df.to_csv(output_file, index=False)
82
83print()
84print(f"Finished!")
85print(f"Output saved as: {output_file}")