-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.py
More file actions
323 lines (280 loc) · 12.3 KB
/
logging.py
File metadata and controls
323 lines (280 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env python3
"""
Ramp data logging for Lakeshore 625 Superconducting Magnet Power Supply
Continuously records ramp parameters every minute and saves to timestamped CSV
"""
import time
import csv
import os
import sys
from datetime import datetime
# Add the lakeshore625 module to the path
sys.path.append('/home/kids/lakeshore625-python')
from lakeshore625.power_controller import PowerController
class RampLogger:
def __init__(self):
# Initialize power controller
self.power_controller = PowerController()
# Data storage
self.ramp_data = []
# CSV file setup with automatic numbering
self.start_date = datetime.now().strftime("%Y-%m-%d")
# Create ramps directory if it doesn't exist
os.makedirs("ramps", exist_ok=True)
# Find next available filename
self.csv_filename = self._get_next_csv_filename()
# CSV headers
self.headers = [
"Timestamp",
"Date",
"Time",
"Elapsed_Seconds",
"Ramp_Rate_A_per_s",
"Current_A",
"Voltage_V",
"Field_T",
"Error_Status"
]
self.column_widths = [28, 12, 12, 16, 18, 14, 14, 14, 20]
def _parse_error_status(self, response):
"""Parse ERSTR? response and return human-readable error or 'None'"""
if not response or response == "NO_RESPONSE":
return "None"
try:
hardware_errors, operational_errors, psh_errors = response.split(',')
operational_val = int(operational_errors.strip())
# Check for specific errors we care about
errors = []
if operational_val & 64: errors.append("Magnet Crowbar")
if operational_val & 32: errors.append("Magnet Quench")
if operational_val & 16: errors.append("Remote Inhibit")
if operational_val & 8: errors.append("Temp High")
if operational_val & 4: errors.append("High Line Voltage")
if operational_val & 2: errors.append("Ext Program Error")
if operational_val & 1: errors.append("Calibration Error")
# Also check hardware errors
hardware_val = int(hardware_errors.strip())
if hardware_val & 32: errors.append("DAC Error")
if hardware_val & 16: errors.append("Output Control Fail")
if hardware_val & 8: errors.append("Output Over Voltage")
if hardware_val & 4: errors.append("Output Over Current")
if hardware_val & 2: errors.append("Low Line Voltage")
if hardware_val & 1: errors.append("Temperature Fault")
# PSH errors
psh_val = int(psh_errors.strip())
if psh_val & 2: errors.append("PSH Short")
if psh_val & 1: errors.append("PSH Open")
return "; ".join(errors) if errors else "None"
except (ValueError, IndexError):
return "Parse Error"
def _get_next_csv_filename(self):
"""Find the next available CSV filename with automatic numbering"""
base_filename = f"ramps/{self.start_date}_ramp_log.csv"
# If the base filename doesn't exist, use it
if not os.path.exists(base_filename):
return base_filename
# Otherwise, find the next numbered version
counter = 1
while True:
numbered_filename = f"ramps/{self.start_date}_ramp_log_{counter}.csv"
if not os.path.exists(numbered_filename):
return numbered_filename
counter += 1
def print_formatted_header(self):
"""Print nicely formatted header to terminal"""
# Print header row
header_line = ""
for i, header in enumerate(self.headers):
header_line += f"{header:<{self.column_widths[i]}}"
print(header_line)
# Print separator line
separator_line = ""
for width in self.column_widths:
separator_line += "-" * width
print(separator_line)
def print_formatted_row(self, data_row):
"""Print nicely formatted data row to terminal"""
formatted_line = ""
for i, value in enumerate(data_row):
# Special formatting for different columns
if i == 3 and isinstance(value, float):
# Elapsed seconds (1 decimal place)
formatted_value = f"{value:.1f}"
elif i in [4, 5, 6, 7] and isinstance(value, float):
# Ramp rate, current, voltage, field (4 decimal places)
formatted_value = f"{value:.4f}"
elif isinstance(value, float):
# Other numbers (2 decimal places)
formatted_value = f"{value:.2f}"
else:
formatted_value = str(value)
formatted_line += f"{formatted_value:<{self.column_widths[i]}}"
print(formatted_line)
def get_ramp_data(self, start_time):
"""Get all required ramp data"""
current_time = datetime.now()
timestamp = current_time.isoformat()
date_str = current_time.strftime("%Y-%m-%d")
time_str = current_time.strftime("%H:%M:%S")
elapsed = time.time() - start_time
try:
# Get ramp rate
ramp_rate_str = self.power_controller.get_ramp_rate()
try:
ramp_rate = float(ramp_rate_str.replace('+', '')) if ramp_rate_str != "NO_RESPONSE" else None
except:
ramp_rate = None
# Get current
current_str = self.power_controller.get_current()
try:
current = float(current_str.replace('+', '')) if current_str != "NO_RESPONSE" else None
except:
current = None
# Get voltage
voltage_str = self.power_controller.get_voltage()
try:
voltage = float(voltage_str.replace('+', '')) if voltage_str != "NO_RESPONSE" else None
except:
voltage = None
# Get field
field_str = self.power_controller.get_field()
try:
field = float(field_str.replace('+', '').replace('E+00', '')) if field_str != "NO_RESPONSE" else None
except:
field = None
# Get error status
error_response = self.power_controller.send_command("ERSTR?")
error_status = self._parse_error_status(error_response)
return {
"timestamp": timestamp,
"date": date_str,
"time": time_str,
"elapsed": elapsed,
"ramp_rate": ramp_rate,
"current": current,
"voltage": voltage,
"field": field,
"error_status": error_status
}
except Exception as e:
print(f"Error reading ramp data: {e}")
return None
def format_csv_row(self, data):
"""Format data dictionary as CSV row"""
try:
row = [
data["timestamp"],
data["date"],
data["time"],
data["elapsed"],
data["ramp_rate"],
data["current"],
data["voltage"],
data["field"],
data["error_status"]
]
return row
except Exception as e:
print(f"Error formatting CSV row: {e}")
return None
def save_to_csv(self):
"""Save all recorded data to CSV file"""
try:
with open(self.csv_filename, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(self.headers)
writer.writerows(self.ramp_data)
except Exception as e:
print(f"Error saving CSV file: {e}")
def create_formatted_csv(self):
"""Create CSV file with nicely formatted columns"""
try:
with open(self.csv_filename, 'w', newline='') as csvfile:
# Write formatted header
header_line = ""
for i, header in enumerate(self.headers):
if i == len(self.headers) - 1: # Last column
header_line += header
else:
header_line += f"{header:<{self.column_widths[i]}}"
csvfile.write(header_line + "\n")
except Exception as e:
print(f"Error creating CSV file: {e}")
def append_to_formatted_csv(self, data_row):
"""Append a formatted row to the CSV file"""
try:
with open(self.csv_filename, 'a', newline='') as csvfile:
# Write formatted data row
formatted_line = ""
for i, value in enumerate(data_row):
# Special formatting for different columns
if i == 3 and isinstance(value, float):
# Elapsed seconds (1 decimal place)
formatted_value = f"{value:.1f}"
elif i in [4, 5, 6, 7] and isinstance(value, float):
# Ramp rate, current, voltage, field (4 decimal places)
formatted_value = f"{value:.4f}"
elif i == 8: # Error status column
formatted_value = str(value) if value is not None else "None"
elif isinstance(value, float):
# Other numbers (2 decimal places)
formatted_value = f"{value:.2f}"
else:
formatted_value = str(value)
if i == len(data_row) - 1: # Last column
formatted_line += formatted_value
else:
formatted_line += f"{formatted_value:<{self.column_widths[i]}}"
csvfile.write(formatted_line + "\n")
except Exception as e:
print(f"Error appending to CSV file: {e}")
def run(self):
"""Main logging loop"""
# Print startup information first
print(f"Starting Lakeshore 625 ramp data logging...")
print(f"Recording interval: 60 seconds")
print(f"CSV file will be saved as: {self.csv_filename}")
print(f"Press Ctrl+C to stop recording and save data")
print("-" * 120)
print() # Empty line
# Now print the formatted header
self.print_formatted_header()
# Create the CSV file immediately with formatted header
self.create_formatted_csv()
start_time = time.time()
try:
while True:
# Get current readings
data = self.get_ramp_data(start_time)
if data:
# Format as CSV row
csv_row = self.format_csv_row(data)
if csv_row:
# Store data
self.ramp_data.append(csv_row)
# Print to terminal with nice formatting
self.print_formatted_row(csv_row)
# Append to CSV file immediately
self.append_to_formatted_csv(csv_row)
# Wait 60 seconds
time.sleep(60)
except KeyboardInterrupt:
# Handle Ctrl+C gracefully
print(f"\n\nStopping ramp data logging.")
print(f"Ramp log saved successfully!")
print(f"Total readings recorded: {len(self.ramp_data)}")
print(f"File location: {self.csv_filename}")
except Exception as e:
print(f"Unexpected error: {e}")
# Still save using the formatted method as backup
self.save_to_csv()
finally:
# Close the power controller connection
if hasattr(self, 'power_controller'):
self.power_controller.close()
def main():
"""Main function"""
logger = RampLogger()
logger.run()
if __name__ == "__main__":
main()