-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
181 lines (157 loc) · 6.02 KB
/
main.py
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
from settings import *
# ---------------------------------------------------------------------------- #
# Libraries
# ---------------------------------------------------------------------------- #
import os
from pathlib import Path
#import time
import sys
from datetime import datetime
from TSL550 import TSL550
import serial.tools.list_ports
from laser_control import *
from RTO.controller import RTO as connectScope
from data_processing import WavelengthAnalyzer, VisualizeData
# ---------------------------------------------------------------------------- #
# Check Input
# ---------------------------------------------------------------------------- #
print("Checking inputs.")
#Get command line arguments.
args = sys.argv[1:]
#Check laser settings.
laser_sweep_rate = (lambda_stop - lambda_start) / duration
checkSweepRate(laser_sweep_rate)
checkWavelength(lambda_start)
checkWavelength(lambda_stop)
# ---------------------------------------------------------------------------- #
# Initialize Save Directory
# ---------------------------------------------------------------------------- #
today = datetime.now()
datePrefix = "{}_{}_{}_{}_{}_".format(today.year, today.month, today.day, today.hour, today.minute)
prefix = datePrefix if append_date else ""
folderName = prefix + data_directory
folderPath = Path(Path.cwd(), "DATA", folderName)
print("Saving data to {} in current directory.".format(folderPath))
if not os.path.exists(folderPath):
print("Creating {} directory.".format(folderName))
os.makedirs(folderPath)
# ---------------------------------------------------------------------------- #
# Initialize Devices
# ---------------------------------------------------------------------------- #
print("Initializing devices.")
#Initialize Laser
print("Initializing laser.")
laser = initLaser()
laser.on()
laser.power_dBm(power_dBm)
laser.openShutter()
laser.sweep_set_mode(
continuous=True,
twoway=True,
trigger=False,
const_freq_step=False
)
print("Enabling laser's trigger output.")
laser.trigger_enable_output()
triggerMode = laser.trigger_set_mode("Step")
triggerStep = laser.trigger_set_step(trigger_step)
print("Setting trigger to: {} and step to {}".format(triggerMode, triggerStep))
#Get number of samples to record. Add buffer just in case.
acquireTime = duration + buffer
numSamples = int((acquireTime) * sample_rate)
print("Set for {:.2E} Samples @ {:.2E} Sa/s.".format(numSamples, sample_rate))
#Oscilloscope Settings
print("Initializing Oscilloscope")
scope = connectScope(scope_IP)
scope.acquisition_settings(
sample_rate = sample_rate,
duration = acquireTime + buffer,
force_realtime = False
)
for channel in active_channels:
channelMode = "Trigger" if (channel == trigger_channel) else "Data"
print("Adding Channel {} - {}".format(channel, channelMode))
scope.add_channel(
channel_num = channel,
**channel_setting[channel]
)
#Add trigger.
print("Adding Edge Trigger @ {} Volt(s).".format(trigger_level))
scope.edge_trigger(
source_channel = trigger_channel,
trigger_level = trigger_level
)
# ---------------------------------------------------------------------------- #
# Settings Check / Details
# ---------------------------------------------------------------------------- #
check_values = {
"Acquire Mode": "ACQ:MODE?",
"ADC Rate": "ACQ:POIN:ARATe?", #Query only.
"Sample Rate": "ACQ:SRATe?",
"Real Sample Rate": "ACQ:SRR?",
"Max Samples": "ACQ:POIN:MAX?",
}
#for value, command in check_values.items():
# print("Scope {} is {:.2e}.".format(value, scope.query(command)))
# ---------------------------------------------------------------------------- #
# Collect Data
# ---------------------------------------------------------------------------- #
print('Starting Acquisition')
scope.start_acquisition(
timeout = duration*3
)
#Sweep Laser
print('Sweeping Laser')
laser.sweep_wavelength(
start=lambda_start,
stop=lambda_stop,
duration=duration,
number=1
)
#Wait for Measurement Completion
print('Waiting for acquisition to complete.')
scope.wait_for_device()
#Take Screenshot
if take_screenshot:
scope.take_screenshot(folderPath / "screenshot.png")
#Acquire Data
#HACK: In the future, build a class to hold the data instead.
rawData = [None, None, None, None, None] #Ugly hack to make the numbers line up nicely.
for channel in active_channels:
rawData[channel] = scope.get_data_ascii(channel)
wavelengthLog = laser.wavelength_logging()
wavelengthLogSize = laser.wavelength_logging_number()
#Optional Save Raw Data
if save_raw_data:
print("Saving raw data.")
for channel in active_channels:
with open(folderPath / "CHAN{}_Raw.txt".format(channel), "w") as out:
out.write(str(rawData[channel]))
with open(folderPath / "Wavelength_Log.txt", "w") as out:
out.write(str(wavelengthLog))
# ---------------------------------------------------------------------------- #
# Process Data
# ---------------------------------------------------------------------------- #
print("Processing Data")
analysis = WavelengthAnalyzer(
sample_rate = sample_rate,
wavelength_log = wavelengthLog,
trigger_data = rawData[trigger_channel]
)
print('=' * 30)
print("Expected number of wavelength points: " + str(int(wavelengthLogSize)))
print("Measured number of wavelength points: " + str(analysis.num_peaks()))
print('=' * 30)
#HACK: In the future, build a class to hold the data instead.
data = [None, None, None, None, None] #Really ugly hack to make index numbers line up.
for channel in active_channels:
data[channel] = analysis.process_data(rawData[channel])
print("Raw Datasets: {}".format(len(rawData)))
print("Datasets Returned: {}".format((len(data))))
# ---------------------------------------------------------------------------- #
# Generate Visuals & Save Data
# ---------------------------------------------------------------------------- #
for channel in active_channels:
if (channel != trigger_channel):
print("Displaying data for channel " + str(channel))
VisualizeData(folderPath, channel, **(data[channel]))