1 | # -*- coding: utf-8 -*- |
---|
2 | """ |
---|
3 | Created on Mon Feb 6 15:47:25 2017 |
---|
4 | |
---|
5 | @author: delerue |
---|
6 | """ |
---|
7 | |
---|
8 | |
---|
9 | import numpy as np |
---|
10 | import matplotlib.pyplot as plt |
---|
11 | #from time import sleep |
---|
12 | from shutil import copyfile |
---|
13 | import os.path |
---|
14 | |
---|
15 | |
---|
16 | def read_plot_data(filename): |
---|
17 | #Definitions |
---|
18 | nchannels=32 |
---|
19 | |
---|
20 | #electron_signal |
---|
21 | electrons_channel=30 |
---|
22 | electrons_signal_scaling=0.05 |
---|
23 | electrons_threshold=-5000 |
---|
24 | electrons_half_width=5 |
---|
25 | |
---|
26 | data_start=1 |
---|
27 | data_length=9035 |
---|
28 | electron_pretrig=5 |
---|
29 | # electron_trig_stop=500 |
---|
30 | electron_trig_stop=20 |
---|
31 | electron_post_trig_integration_start=1 |
---|
32 | electron_post_trig_integration_stop=5 |
---|
33 | |
---|
34 | #position signal |
---|
35 | position_channel=28 |
---|
36 | |
---|
37 | #channels mapping |
---|
38 | #channels_mapping=[17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27] |
---|
39 | data_mapping=[ 0, 0, 0, 0, 0, |
---|
40 | 0, 0, 0, 0, 0, |
---|
41 | 0, 0, 0, 0, 0, |
---|
42 | 30, 40, 50, 60, 70, |
---|
43 | 80, 90, 100, 110, 120, |
---|
44 | 130, 140, 0, 0, 0, |
---|
45 | 0, 0 ] |
---|
46 | |
---|
47 | |
---|
48 | #code |
---|
49 | key_values={} |
---|
50 | if (len(os.path.dirname(filename))==0): |
---|
51 | filename="./"+filename |
---|
52 | |
---|
53 | with open(filename,'r') as fid: |
---|
54 | fileline=fid.readline() |
---|
55 | while fileline[0]=='#': |
---|
56 | entries=fileline.replace('#','').replace(' ','').replace('\n','').split('=') |
---|
57 | key_values[entries[0]]=entries[1] |
---|
58 | fileline=fid.readline() |
---|
59 | fid.close() |
---|
60 | |
---|
61 | print(key_values) |
---|
62 | |
---|
63 | alldata=np.loadtxt(filename, dtype='int32', comments='#', delimiter=' ', usecols=range(0,nchannels), converters={ _:lambda s: int(s , 16) for _ in range(0,nchannels) }) |
---|
64 | |
---|
65 | #converting the data in the correct format |
---|
66 | alldata=alldata%(2**16) |
---|
67 | alldata=(2**15)-alldata |
---|
68 | data_scaling_factor=(float(key_values['amplitude'])*5.)/(2.**15) |
---|
69 | time_scaling_factor=(36./float(key_values['acquisition_rate']))*1.e-3 |
---|
70 | |
---|
71 | #Looking for electrons |
---|
72 | electrons_found=False |
---|
73 | if (min(alldata[:,electrons_channel])<electrons_threshold): |
---|
74 | elec=alldata[:,electrons_channel] |
---|
75 | pos_elec=elec.argmin() |
---|
76 | while ((pos_elec>100)and(elec[pos_elec]<electrons_threshold)): |
---|
77 | pos_elec=pos_elec-1 |
---|
78 | if ((elec[pos_elec+electrons_half_width]<electrons_threshold)or(elec[pos_elec-electrons_half_width]<electrons_threshold)): |
---|
79 | electrons_found=True |
---|
80 | electron_data_start=pos_elec-electron_pretrig |
---|
81 | electron_data_stop=pos_elec+electron_trig_stop |
---|
82 | print('electrons found') |
---|
83 | print elec[pos_elec] |
---|
84 | print pos_elec |
---|
85 | |
---|
86 | |
---|
87 | |
---|
88 | #Mean position value |
---|
89 | mean_position=np.mean(alldata[:,electrons_channel]) |
---|
90 | print('Mean position: ', mean_position) |
---|
91 | filename_position=filename.replace('.txt','.pos') |
---|
92 | fid=open(filename_position,'w') |
---|
93 | fid.write("pos: "+str(round(mean_position,3))) |
---|
94 | fid.close() |
---|
95 | |
---|
96 | |
---|
97 | #prepare the figures |
---|
98 | fig=plt.figure(1,figsize=(12, 8)) |
---|
99 | if electrons_found: |
---|
100 | figzoom=plt.figure(2,figsize=(12, 8)) |
---|
101 | figsignal=plt.figure(3,figsize=(12, 8)) |
---|
102 | datasignal=np.zeros(11) |
---|
103 | filename_zoom_txt=filename.replace('.txt','.zoom_txt') |
---|
104 | fidz=open(filename_zoom_txt,'w') |
---|
105 | for jdata in range(electron_data_start,electron_data_stop): |
---|
106 | fidz.write(str(jdata*time_scaling_factor)) |
---|
107 | fidz.write(" ") |
---|
108 | fidz.write("\n") |
---|
109 | |
---|
110 | |
---|
111 | for idata in range(17,nchannels): |
---|
112 | this_data_scaling_factor=data_scaling_factor*-1 |
---|
113 | txtline='r' |
---|
114 | if idata%6==0: |
---|
115 | txtline='b' |
---|
116 | if idata%6==1: |
---|
117 | txtline='g' |
---|
118 | if idata%6==2: |
---|
119 | txtline='k' |
---|
120 | if idata%6==3: |
---|
121 | txtline='c' |
---|
122 | if idata%6==4: |
---|
123 | txtline='m' |
---|
124 | if idata%6==5: |
---|
125 | txtline='y' |
---|
126 | if idata==17: |
---|
127 | txtline='r' |
---|
128 | if idata==20: |
---|
129 | txtline='r' |
---|
130 | if idata==22: |
---|
131 | txtline='r' |
---|
132 | if idata==30: |
---|
133 | txtline='bs' |
---|
134 | linestyle='-' |
---|
135 | if idata==electrons_channel: |
---|
136 | this_data_scaling_factor=this_data_scaling_factor*electrons_signal_scaling |
---|
137 | |
---|
138 | if ((idata!=position_channel)): |
---|
139 | # data_start=9022 |
---|
140 | # data_length=9035 |
---|
141 | plt.figure(1) |
---|
142 | plt.plot(np.arange(data_start,data_length)*time_scaling_factor,alldata[range(data_start,data_length),idata].astype(np.float)*this_data_scaling_factor, txtline+linestyle) |
---|
143 | # plt.show() |
---|
144 | |
---|
145 | if (electrons_found): |
---|
146 | plt.figure(2) |
---|
147 | this_channel_baseline=0 |
---|
148 | baseline_offset=100 |
---|
149 | baseline_length=100 |
---|
150 | if (pos_elec>1000): |
---|
151 | this_channel_baseline=np.mean(alldata[range(electron_data_start-baseline_offset-baseline_length,electron_data_stop-baseline_offset),idata]) |
---|
152 | this_channel_baseline_rms=np.std(alldata[range(electron_data_start-baseline_offset-baseline_length,electron_data_stop-baseline_offset),idata])*-1 |
---|
153 | else: |
---|
154 | this_channel_baseline=np.mean(alldata[range(electron_data_start+baseline_offset,electron_data_stop+baseline_offset+baseline_length),idata]) |
---|
155 | this_channel_baseline_rms=np.sqrt(np.std(alldata[range(electron_data_start+baseline_offset,electron_data_stop+baseline_offset+baseline_length),idata]))*-1 |
---|
156 | |
---|
157 | plt.plot(np.arange(electron_data_start,electron_data_stop)*time_scaling_factor,(alldata[range(electron_data_start,electron_data_stop),idata].astype(np.float)-this_channel_baseline)*this_data_scaling_factor, txtline+linestyle) |
---|
158 | plt.plot(np.arange(electron_data_start,electron_data_stop)*time_scaling_factor,np.zeros(len(range(electron_data_start,electron_data_stop)))+(this_channel_baseline_rms*this_data_scaling_factor), txtline+":") |
---|
159 | for jdata in range(electron_data_start,electron_data_stop): |
---|
160 | fidz.write('{:06.5e}'.format((alldata[jdata,idata].astype(np.float)-this_channel_baseline)*this_data_scaling_factor)) |
---|
161 | fidz.write(" ") |
---|
162 | fidz.write("\n") |
---|
163 | |
---|
164 | if data_mapping[idata]>0: |
---|
165 | plt.figure(3) |
---|
166 | txtline=txtline+'^' |
---|
167 | datasignal[(data_mapping[idata]/10)-4]=np.mean(alldata[range(pos_elec+electron_post_trig_integration_start,pos_elec+electron_post_trig_integration_stop),idata].astype(np.float)-this_channel_baseline)*this_data_scaling_factor |
---|
168 | plt.plot(data_mapping[idata], datasignal[(data_mapping[idata]/10)-4], txtline+linestyle) |
---|
169 | |
---|
170 | plt.figure(1) |
---|
171 | plt.title(key_values['date']+ ' --- Position: '+str(round(mean_position,2))) |
---|
172 | plt.ylabel('Signal amplitude [V]') |
---|
173 | plt.xlabel('Time [ms]') |
---|
174 | plt.grid(True) |
---|
175 | imagename=filename.replace('.txt','.png') |
---|
176 | print(imagename) |
---|
177 | fig.savefig(imagename) |
---|
178 | plt.close(fig) |
---|
179 | imagename_no_path=os.path.basename(imagename) |
---|
180 | fid=open('list.html','a'); |
---|
181 | fid.write("<A HREF=") |
---|
182 | fid.write(imagename_no_path) |
---|
183 | fid.write("><IMG SRC=") |
---|
184 | fid.write(imagename_no_path) |
---|
185 | fid.write(" width=600>") |
---|
186 | fid.write("</A><BR/>") |
---|
187 | fid.write(imagename_no_path) |
---|
188 | fid.write("<BR/><BR/>\n") |
---|
189 | fid.close() |
---|
190 | copyfile(imagename,os.path.dirname(imagename)+'/last.png') |
---|
191 | |
---|
192 | if (electrons_found): |
---|
193 | fidz.close() |
---|
194 | plt.figure(2) |
---|
195 | plt.title(key_values['date']+ ' --- Position: '+str(round(mean_position,2))) |
---|
196 | plt.ylabel('Signal amplitude [V] - pedestal removed') |
---|
197 | plt.xlabel('Time [ms]') |
---|
198 | plt.grid(True) |
---|
199 | imagenamezoom=filename.replace('.txt','_zoom.png') |
---|
200 | figzoom.savefig(imagenamezoom) |
---|
201 | plt.close(figzoom) |
---|
202 | copyfile(imagenamezoom,os.path.dirname(imagename)+'/last_zoom.png') |
---|
203 | |
---|
204 | imagenamezoom_no_path=os.path.basename(imagenamezoom) |
---|
205 | fid=open('zoom_list.html','a'); |
---|
206 | fid.write("<A HREF=") |
---|
207 | fid.write(imagenamezoom_no_path) |
---|
208 | fid.write("><IMG SRC=") |
---|
209 | fid.write(imagenamezoom_no_path) |
---|
210 | fid.write(" width=600>") |
---|
211 | fid.write("</A><BR/>") |
---|
212 | fid.write(imagenamezoom_no_path) |
---|
213 | fid.write("<BR/><BR/>\n") |
---|
214 | fid.close() |
---|
215 | |
---|
216 | plt.figure(3) |
---|
217 | plt.plot(range(40,150,10),datasignal,'b--') |
---|
218 | plt.title(key_values['date']+ ' --- Position: '+str(round(mean_position,2))) |
---|
219 | plt.ylabel('Signal amplitude [V]') |
---|
220 | plt.xlabel('Detector angle (degree)') |
---|
221 | plt.grid(True) |
---|
222 | imagenamesignal=filename.replace('.txt','_signal.png') |
---|
223 | figsignal.savefig(imagenamesignal) |
---|
224 | plt.close(figsignal) |
---|
225 | copyfile(imagenamesignal,os.path.dirname(imagename)+'/last_signal.png') |
---|
226 | |
---|
227 | filenamesig=filename.replace('.txt','.sig') |
---|
228 | fid=open(filenamesig,'w'); |
---|
229 | fid.write(str(datasignal)) |
---|
230 | fid.close() |
---|
231 | |
---|
232 | imagenamesignal_no_path=os.path.basename(imagenamesignal) |
---|
233 | fid=open('signal_list.html','a'); |
---|
234 | fid.write("<A HREF=") |
---|
235 | fid.write(imagenamesignal_no_path) |
---|
236 | fid.write("><IMG SRC=") |
---|
237 | fid.write(imagenamesignal_no_path) |
---|
238 | fid.write(" width=600>") |
---|
239 | fid.write("</A><BR/>") |
---|
240 | fid.write(imagenamesignal_no_path) |
---|
241 | fid.write("<BR/><BR/>\n") |
---|
242 | fid.close() |
---|
243 | |
---|
244 | return; |
---|
245 | |
---|
246 | |
---|
247 | #read_plot_data('/Users/delerue/Downloads/CLIO/data_CLIO_2017-02-08_17-26-53.txt') |
---|
248 | #read_plot_data('/Users/delerue/Downloads/CLIO_tmp/20170227/data_CLIO_2017-02-27_12-01-14.txt') |
---|
249 | #read_plot_data('/Users/delerue/Downloads/CLIO_tmp/20170227/data_CLIO_2017-02-27_12-43-46.txt') |
---|
250 | #HWaddr 64:00:6a:3a:6f:1c |
---|