-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20220615_SHB_data_processing.py
230 lines (203 loc) · 12.4 KB
/
20220615_SHB_data_processing.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
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
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 15 14:33:38 2022
@author: Wei Jiang
The program is used to deal with the data of spectral hole burning experiments;
A excel file including the parameters used for the generation of the pulse sequence
from a HDAWG is necessary. If you want to know the layout of the excel file, please
contact Wei.
"""
import os
import numpy as np
import pandas as pd
from natsort import natsorted
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import sys
# Directory and folder for data
dir = 'C:\\Users\\wj2002\\Dropbox (Heriot-Watt University Team)\\RES_EPS_Quantum_Photonics_Lab\\Experiments\\Current Experiments\\Broadband telecom quantum memories\\Pr_YSO_spectroscopy_HWU\\20220622_Pr_YSO_SHB_pit'
folder = '20220622_burnback'
path = dir + '\\' + folder + '\\'
file_list = os.listdir(path)
# Listing all .csv files but not all data fies will be used in some cases
list_csv=natsorted([i for i in file_list if i.endswith('.CSV')])
# Finding the excel file to know experimental conditions and variables
list_xlsx=natsorted([i for i in file_list if i.endswith('.xlsx')])
if not list_xlsx:
sys.exit('Excel file does not exist or must be of format ".xlsx"')
xlsx = pd.ExcelFile(path + list_xlsx[0])
print(list_xlsx[0])
# Finding out references
# ***IMPORTANT***
# Flag=0 --> Burning data;
# Flag=1 --> Reference data; (without the crystal or laser polarisation along b-axis)
# Flag=2 --> Data when switching OFF burning pulses
read_xlsx= pd.read_excel(xlsx, sheet_name='Sheet1')
read_flag= pd.read_excel(xlsx, sheet_name='Sheet1', usecols=['Flag'])
flagNum = np.reshape(read_flag.to_numpy(), len(read_flag))
xlsx_size=read_xlsx.shape
# Setting some conditions when doing the measurements
#variable='Central frequency of burnback pulse (MHz)'
#variable='Amplitude of second chirp sine wave (V)'
variable='Amplitude of burnback pulse (V)'
index_of_variable=read_xlsx.columns.get_loc(variable)
index_of_FileName=read_xlsx.columns.get_loc('File name')
# Listing all data files when switching on the burning pulse
find_Burning=np.where(flagNum == 0) # flagNum is 0 when switching on burning
Burning_index=np.transpose(np.array(find_Burning))
Burning_index_len=len(Burning_index)
Burning_csv_list=np.empty([Burning_index_len,xlsx_size[1]],dtype='U25')
for index in range(Burning_index_len):
Burning_csv_list0=read_xlsx.loc[int(Burning_index[index])]
Burning_csv_list0=Burning_csv_list0.to_frame()
Burning_csv_list0=Burning_csv_list0.T
Burning_csv_list1=Burning_csv_list0.to_numpy()
for ii in range(len(Burning_csv_list1.T)-1):
Burning_csv_list[index,ii]=Burning_csv_list1[0,ii]
# Listing all reference files
find_references=np.where(flagNum == 1) #flagNum is 1, the data are the references(without crystal or along b(or D1) axis)
ref_index=np.transpose(np.array(find_references))
ref_index_len=len(ref_index)
ref_csv_list=np.empty([ref_index_len,xlsx_size[1]],dtype='U25')
for index in range(ref_index_len):
ref_csv_list0=read_xlsx.loc[int(ref_index[index])]
ref_csv_list0=ref_csv_list0.to_frame()
ref_csv_list0=ref_csv_list0.T
ref_csv_list1=ref_csv_list0.to_numpy()
for ii in range(len(ref_csv_list1.T)-1):
ref_csv_list[index,ii]=ref_csv_list1[0,ii]
# Reading a random .csv file to achieve the time scale
read_example=pd.read_csv(os.path.join(path,Burning_csv_list[0,0] +'.'+'csv'))
read_example=read_example.to_numpy()
row_of_data, column_of_data=np.shape(read_example)
time_scale_example=read_example[:,0]
time_scale_len=len(time_scale_example)
# Initialising arrays
Burning_csv=np.zeros([Burning_index_len,xlsx_size[1]],dtype=np.int64)
Burning_data=np.empty([Burning_index_len,time_scale_len,column_of_data])
Burning_csv_variable_num=np.empty([Burning_index_len,1])
Burning_data_corrected=np.empty([Burning_index_len,time_scale_len,1])
ref_data=np.empty([ref_index_len,time_scale_len,column_of_data])
ref_data_corrected=np.empty([ref_index_len,time_scale_len,1])
noBurning_data=np.empty([ref_index_len,time_scale_len,column_of_data])
noBurning_data_corrected=np.empty([ref_index_len,time_scale_len,1])
absorption_Burning=np.empty([Burning_index_len,time_scale_len,1])
absorption_Burning_corrected=np.empty([Burning_index_len,time_scale_len,1])
absorption_noBurning=np.empty([Burning_index_len,time_scale_len,1])
absorption_noBurning_corrected=np.empty([Burning_index_len,time_scale_len,1])
time_scale=np.empty([Burning_index_len,time_scale_len,1])
frequency_scale=np.empty([Burning_index_len,time_scale_len,1])
# Identifing whether there are data when switching off the burning pulse (flagNum is 2 when switching off the burning pulse)
read_flag_1=read_flag.to_numpy()
read_flag_1=str(read_flag_1[:,:])
if '2' not in read_flag_1:
print('No data when switching off burning pulse')
# Finding out the different experimental conditions (based on variable set above) and recording them
column_of_ref_variable_0=ref_csv_list[:,index_of_variable]
column_of_ref_variable=np.empty([ref_index_len,1])
for index in range(ref_index_len):
column_of_ref_variable[index,0]=float(column_of_ref_variable_0[index])
for index in range(Burning_index_len):
# Find the data when switching on the burning pulse
Burning_csv=read_xlsx.loc[Burning_index[index]]
Burning_csv_name=pd.DataFrame(Burning_csv, columns= ['File name'])
Burning_csv_name_1=Burning_csv_name.to_numpy()
Burning_csv_name=str(Burning_csv_name_1[0,0])
Burning_csv_variable=pd.DataFrame(Burning_csv, columns= [variable])
Burning_csv_variable_1=Burning_csv_variable.to_numpy()
Burning_csv_variable=Burning_csv_variable_1[0,0]
Burning_csv_variable_num[index,0]=Burning_csv_variable_1[0,0]
read_Burning_data=pd.read_csv(os.path.join(path,Burning_csv_name +'.'+'csv'))
Burning_data[index,:,:]=read_Burning_data.to_numpy()
Burning_data_corrected[index,:,0]=Burning_data[index,:,2]-min(Burning_data[index,:,2])+0.0000001
time_scale[index,:,0]=Burning_data[index,:,0]
# find the corresponding reference data
loc_ref=np.where(column_of_ref_variable == Burning_csv_variable)
corresponding_ref_name=ref_csv_list[loc_ref[0],index_of_FileName]
read_ref_data=pd.read_csv(os.path.join(path,corresponding_ref_name[0] +'.'+'csv'))
ref_data[index,:,:]=read_ref_data.to_numpy()
ref_data_corrected[index,:,0]=ref_data[index,:,2]-min(ref_data[index,:,2])+0.0000001
absorption_Burning[index,:,0]=np.log(ref_data[index,:,2]/Burning_data[index,:,2])
absorption_Burning_corrected[index,:,0]=np.log(ref_data_corrected[index,:,0]/Burning_data_corrected[index,:,0])
reading_pulse_duation=pd.DataFrame(Burning_csv, columns= ['Pulse width of second chirp sine wave (ms)'])
reading_pulse_freq_detuning=pd.DataFrame(Burning_csv, columns= ['Frequency detuning of second chirp sine wave (MHz)'])
reading_pulse_duation=reading_pulse_duation.to_numpy()
reading_pulse_freq_detuning=reading_pulse_freq_detuning.to_numpy()
frequency_scale[index,:,0]=time_scale[index,:,0]/(reading_pulse_duation*1e-3)*reading_pulse_freq_detuning
fig, ax=plt.subplots()
for index in range(Burning_index_len-1):
ax.plot(frequency_scale[index,:,0],absorption_Burning_corrected[index,:,0],label="{}".format(Burning_csv_variable_num[index,0]))
#ax.plot(time_scale,absorption_Burning_corrected[index,:,0])
#ax.set_xlabel('Time(s)')
ax.set_xlabel('Frequency detuning (MHz)')
ax.set_ylabel('OD')
ax.legend(loc=1,title='Amplitude of reading-out pulse',fontsize='x-small',title_fontsize='x-small')
plt.show()
if '2' in read_flag_1:
print('There are data when switching off burning pulse')
find_noBurning=np.where(flagNum == 2) # flagNum is 0 when switching OFF burning
noBurning_index=np.transpose(np.array(find_noBurning))
noBurning_index_len=len(noBurning_index)
noBurning_csv_list=np.empty([noBurning_index_len,xlsx_size[1]],dtype='U25')
for index in range(noBurning_index_len):
noBurning_csv_list0=read_xlsx.loc[int(noBurning_index[index])]
noBurning_csv_list0=noBurning_csv_list0.to_frame()
noBurning_csv_list0=noBurning_csv_list0.T
noBurning_csv_list1=noBurning_csv_list0.to_numpy()
for ii in range(len(noBurning_csv_list1.T)-1):
noBurning_csv_list[index,ii]=noBurning_csv_list1[0,ii]
column_of_ref_variable_0=ref_csv_list[:,index_of_variable]
column_of_ref_variable=np.empty([ref_index_len,1])
for index in range(ref_index_len):
column_of_ref_variable[index,0]=float(column_of_ref_variable_0[index])
column_of_noBurning_variable_0=ref_csv_list[:,index_of_variable]
column_of_noBurning_variable=np.empty([noBurning_index_len,1])
for index in range(noBurning_index_len):
column_of_noBurning_variable[index,0]=float(column_of_noBurning_variable_0[index])
for index in range(Burning_index_len):
# find the data when switching on the burning pulse
Burning_csv=read_xlsx.loc[Burning_index[index]]
Burning_csv_name=pd.DataFrame(Burning_csv, columns= ['File name'])
Burning_csv_name_1=Burning_csv_name.to_numpy()
Burning_csv_name=str(Burning_csv_name_1[0,0])
Burning_csv_variable=pd.DataFrame(Burning_csv, columns= [variable])
Burning_csv_variable_1=Burning_csv_variable.to_numpy()
Burning_csv_variable=Burning_csv_variable_1[0,0]
Burning_csv_variable_num[index,0]=Burning_csv_variable_1[0,0]
read_Burning_data=pd.read_csv(os.path.join(path,Burning_csv_name +'.'+'csv'))
Burning_data[index,:,:]=read_Burning_data.to_numpy()
Burning_data_corrected[index,:,0]=Burning_data[index,:,2]-min(Burning_data[index,:,2])+0.0000001
time_scale[index,:,0]=Burning_data[index,:,0]
# find the corresponding reference data
loc_ref=np.where(column_of_ref_variable == Burning_csv_variable)
corresponding_ref_name=ref_csv_list[loc_ref[0],index_of_FileName]
read_ref_data=pd.read_csv(os.path.join(path,corresponding_ref_name[0] +'.'+'csv'))
ref_data[index,:,:]=read_ref_data.to_numpy()
ref_data_corrected[index,:,0]=ref_data[index,:,2]-min(ref_data[index,:,2])+0.0000001
# find the corresponding noBurning data
loc_noBurning=np.where(column_of_noBurning_variable == Burning_csv_variable)
corresponding_noBurning_name=ref_csv_list[loc_noBurning[0],index_of_FileName]
read_noBurning_data=pd.read_csv(os.path.join(path,corresponding_noBurning_name[0] +'.'+'csv'))
noBurning_data[index,:,:]=read_noBurning_data.to_numpy()
noBurning_data_corrected[index,:,0]=noBurning_data[index,:,2]-min(noBurning_data[index,:,2])+0.0000001
absorption_Burning[index,:,0]=np.log(ref_data[index,:,2]/Burning_data[index,:,2])
absorption_Burning_corrected[index,:,0]=np.log(ref_data_corrected[index,:,0]/Burning_data_corrected[index,:,0])
absorption_noBurning[index,:,0]=np.log(ref_data[index,:,2]/noBurning_data[index,:,2])
absorption_noBurning_corrected[index,:,0]=np.log(ref_data_corrected[index,:,0]/noBurning_data_corrected[index,:,0])
#reading_pulse_duation=pd.DataFrame(Burning_csv, columns= ['Pulse duration of read-out pulse (ms)'])
#reading_pulse_freq_detuning=pd.DataFrame(Burning_csv, columns= ['Frequency detuning of read-out pulse (MHz)'])
reading_pulse_duation=pd.DataFrame(Burning_csv, columns= ['Pulse duration of read-out pulse (ms)'])
reading_pulse_freq_detuning=pd.DataFrame(Burning_csv, columns= ['Read-out pulse frequency sweeping (MHz)'])
reading_pulse_duation=reading_pulse_duation.to_numpy()
reading_pulse_freq_detuning=reading_pulse_freq_detuning.to_numpy()
frequency_scale[index,:,0]=time_scale[index,:,0]/(reading_pulse_duation*1e-3)*reading_pulse_freq_detuning
for index in range(Burning_index_len):
fig, ax=plt.subplots()
ax.plot(frequency_scale[index,:,0],absorption_Burning_corrected[index,:,0],label="{}".format(Burning_csv_variable_num[index,0]))
ax.plot(frequency_scale[index,:,0],absorption_noBurning[index,:,0])
#ax.plot(time_scale[index,:,0],absorption_Burning_corrected[index,:,0])
#ax.set_xlabel('Time(s)')
ax.set_xlabel('Frequency detuning (MHz)')
ax.set_ylabel('OD')
ax.legend(loc=1,title='Amplitude of reading-out pulse',fontsize='x-small',title_fontsize='x-small')
plt.show()