1 | function [MagnetSetpoints, MagnetMonitors, BPMMonitors, MagnetSetpointsEnd, FileName] = monmags(varargin) |
---|
2 | %MONMAGS - Monitors all magnet power supplies and plots various statistics |
---|
3 | % |
---|
4 | % [MagnetSetpoints, MagnetMonitors, BPMMonitors, MagnetSetpointsEnd] = monmags(FileName, PlotDataFlag, UnitsFlag) |
---|
5 | % [MagnetSetpoints, MagnetMonitors, BPMMonitors, MagnetSetpointsEnd] = monmags(FileName, t, UnitsFlag) |
---|
6 | % |
---|
7 | % INPUTS |
---|
8 | % 1. FileName - file to load or save data {Default or []: prompt for a filename} |
---|
9 | % 2. PlotDataFlag - 0 -> get, save and plot data {Default: prompt for input} |
---|
10 | % 1 -> plot all families (data coming from a file) |
---|
11 | % 2 -> plot one family (data coming from a file) |
---|
12 | % t - time vector (see help getpv for more details) {Default: prompt for input} |
---|
13 | % 3. UnitsFlag ('Hardware' or 'Physics') - Just for plotting. {Default: default units when the data was taken} |
---|
14 | % 4. 'NoDisplay' - read or save the data file without plotting the result (just for getting data). |
---|
15 | % 5. 'Raw2Real' - if in hardware units, a raw2real conversion is make before plotting (no change is made to the measured data). |
---|
16 | % |
---|
17 | % OUTPUTS |
---|
18 | % 1. MagnetSetpoints - Cell array of magnet setpoints at the start of the test |
---|
19 | % 2. MagnetMonitors - Cell array of magnets monitors sampled according to the vector t |
---|
20 | % 3. BPMMonitors - Cell array of BPMs |
---|
21 | % 4. MagnetSetpointsEnd - Cell array of magnet setpoints at the end of the test |
---|
22 | % |
---|
23 | % NOTE |
---|
24 | % 1. Input order does not matter. All inputs are optional. |
---|
25 | % 2. Make sure the sample period is long enough to acquirer the data. It's |
---|
26 | % best to test on a few samples first (3 is points in the minimum). |
---|
27 | % 3. Orbits are also monitored. Use monbpm to just monitor the orbit. |
---|
28 | % |
---|
29 | % EXAMPLES |
---|
30 | % 1. To plot data in physics units (prompt for the file): |
---|
31 | % monmags('physics', 1); |
---|
32 | % 2. To get data for 3 minutes at 1 sample/second, save to filename=magdata101.mat (in |
---|
33 | % the current directory), and plot in default units: |
---|
34 | % monmags('magdata101',0:1:180); |
---|
35 | % |
---|
36 | % Written by Greg Portmann |
---|
37 | |
---|
38 | |
---|
39 | DisplayFlag = 1; |
---|
40 | FileName = -1; |
---|
41 | ArchiveFlag = -1; |
---|
42 | UnitsFlag = ''; |
---|
43 | PlotDataFlag = []; |
---|
44 | t = []; |
---|
45 | DirectoryName = ''; |
---|
46 | MagnetSetpoints =[]; |
---|
47 | MagnetMonitors = []; |
---|
48 | BPMMonitors = []; |
---|
49 | MagnetSetpointsEnd = []; |
---|
50 | Raw2RealFlag = 0; |
---|
51 | |
---|
52 | for i = length(varargin):-1:1 |
---|
53 | if isstruct(varargin{i}) |
---|
54 | % Remove |
---|
55 | varargin(i) = []; |
---|
56 | elseif iscell(varargin{i}) |
---|
57 | % Remove |
---|
58 | varargin(i) = []; |
---|
59 | elseif strcmpi(varargin{i},'struct') |
---|
60 | % Remove |
---|
61 | varargin(i) = []; |
---|
62 | elseif strcmpi(varargin{i},'numeric') |
---|
63 | % Remove |
---|
64 | varargin(i) = []; |
---|
65 | elseif strcmpi(varargin{i},'Display') |
---|
66 | DisplayFlag = 1; |
---|
67 | varargin(i) = []; |
---|
68 | elseif strcmpi(varargin{i},'NoDisplay') |
---|
69 | DisplayFlag = 0; |
---|
70 | varargin(i) = []; |
---|
71 | elseif strcmpi(varargin{i},'Physics') |
---|
72 | UnitsFlag = varargin{i}; |
---|
73 | varargin(i) = []; |
---|
74 | elseif strcmpi(varargin{i},'Raw2Real') |
---|
75 | Raw2RealFlag = 1; |
---|
76 | varargin(i) = []; |
---|
77 | elseif strcmpi(varargin{i},'Hardware') |
---|
78 | UnitsFlag = varargin{i}; |
---|
79 | varargin(i) = []; |
---|
80 | end |
---|
81 | end |
---|
82 | |
---|
83 | for i = length(varargin):-1:1 |
---|
84 | if ischar(varargin{i}) |
---|
85 | FileName = varargin{i}; |
---|
86 | if strcmpi(FileName,'Archive') |
---|
87 | PlotDataFlag = 0; |
---|
88 | ArchiveFlag = 1; |
---|
89 | if length(varargin) > i |
---|
90 | % Look for a filename as the next input |
---|
91 | if ischar(varargin{i+1}) |
---|
92 | FileName = varargin{i+1}; |
---|
93 | varargin(i+1) = []; |
---|
94 | end |
---|
95 | end |
---|
96 | varargin(i) = []; |
---|
97 | elseif strcmpi(varargin{i},'NoArchive') |
---|
98 | ArchiveFlag = 0; |
---|
99 | varargin(i) = []; |
---|
100 | end |
---|
101 | elseif isnumeric(varargin{i}) |
---|
102 | if length(varargin{i}) == 1 |
---|
103 | PlotDataFlag = varargin{i}; |
---|
104 | else |
---|
105 | PlotDataFlag = 0; |
---|
106 | t = varargin{i}; |
---|
107 | end |
---|
108 | varargin(i) = []; |
---|
109 | end |
---|
110 | end |
---|
111 | |
---|
112 | |
---|
113 | if isempty(PlotDataFlag) |
---|
114 | if FileName == -1 |
---|
115 | ButtonNumber = menu('What would you like to do?', 'Get New Data','Plot Data From a File','Plot 1 Family From a File','Cancel'); |
---|
116 | switch ButtonNumber |
---|
117 | case 1 |
---|
118 | PlotDataFlag = 0; |
---|
119 | case 2 |
---|
120 | PlotDataFlag = 1; |
---|
121 | case 3 |
---|
122 | PlotDataFlag = 2; |
---|
123 | otherwise |
---|
124 | fprintf(' monmags cancelled\n'); |
---|
125 | return |
---|
126 | end |
---|
127 | else |
---|
128 | if exist(FileName) || exist([FileName,'.mat']) |
---|
129 | PlotDataFlag = 1; |
---|
130 | else |
---|
131 | PlotDataFlag = 0; |
---|
132 | end |
---|
133 | end |
---|
134 | end |
---|
135 | |
---|
136 | |
---|
137 | % Get file for plotting |
---|
138 | if (isempty(FileName) || isequal(FileName,-1)) && PlotDataFlag > 0 |
---|
139 | DirectoryName = getfamilydata('Directory','DataRoot'); |
---|
140 | DirectoryName = [DirectoryName, 'Magnets\']; |
---|
141 | [FileName, DirectoryName] = uigetfile('*.mat', 'Select a magnet file', DirectoryName); |
---|
142 | if isnumeric(FileName) |
---|
143 | fprintf(' monmags cancelled'); |
---|
144 | return |
---|
145 | end |
---|
146 | end |
---|
147 | |
---|
148 | |
---|
149 | if PlotDataFlag == 0 |
---|
150 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
151 | % Get Data From The Control System % |
---|
152 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
153 | |
---|
154 | if ArchiveFlag |
---|
155 | if isempty(FileName) |
---|
156 | FileName = appendtimestamp('magnetdata'); |
---|
157 | DirectoryName = [getfamilydata('Directory','DataRoot'), 'Magnets', filesep]; |
---|
158 | |
---|
159 | % Make sure default directory exists |
---|
160 | DirStart = pwd; |
---|
161 | [DirectoryName, ErrorFlag] = gotodirectory(DirectoryName); |
---|
162 | cd(DirStart); |
---|
163 | |
---|
164 | [FileName, DirectoryName] = uiputfile('*.mat', 'Select a Magnet Monitor File', [DirectoryName FileName]); |
---|
165 | if isequal(FileName,0) || isequal(DirectoryName,0) |
---|
166 | disp(' Magnet power supply monitoring canceled.'); |
---|
167 | FileName = ''; |
---|
168 | return |
---|
169 | end |
---|
170 | FileName = [DirectoryName, FileName]; |
---|
171 | elseif FileName == -1 |
---|
172 | FileName = appendtimestamp('magnetdata'); |
---|
173 | DirectoryName = [getfamilydata('Directory','DataRoot'), 'Magnets', filesep]; |
---|
174 | FileName = [DirectoryName, FileName]; |
---|
175 | end |
---|
176 | else |
---|
177 | if nargout == 0 |
---|
178 | fprintf(' No output or filename input, so there is no reason to monitor the power supplies\n'); |
---|
179 | end |
---|
180 | end |
---|
181 | |
---|
182 | |
---|
183 | % Setup the family cell array |
---|
184 | MagnetFamilies = findmemberof('Magnet'); |
---|
185 | MagnetFamilies = MagnetFamilies(:); |
---|
186 | |
---|
187 | % If a family has all zero status remove it |
---|
188 | for i = length(MagnetFamilies):-1:1 |
---|
189 | Status = family2status(MagnetFamilies{i}); |
---|
190 | if all(Status==0) |
---|
191 | MagnetFamilies(i) = []; |
---|
192 | end |
---|
193 | end |
---|
194 | |
---|
195 | MagnetFamilies = editlist(MagnetFamilies); |
---|
196 | MagnetFamilies = MagnetFamilies(:); |
---|
197 | iMagnets = length(MagnetFamilies); |
---|
198 | MagnetDeviceLists = cell(iMagnets,1); |
---|
199 | |
---|
200 | |
---|
201 | % BPM families |
---|
202 | BPMFamilies = findmemberof('BPM'); |
---|
203 | BPMFamilies = BPMFamilies(:); |
---|
204 | |
---|
205 | % If a family has all zero status remove it |
---|
206 | for i = length(BPMFamilies):-1:1 |
---|
207 | Status = family2status(BPMFamilies{i}); |
---|
208 | if all(Status==0) |
---|
209 | BPMFamilies(i) = []; |
---|
210 | end |
---|
211 | end |
---|
212 | |
---|
213 | BPMFamilies = editlist(BPMFamilies(:)); |
---|
214 | BPMFamilies = BPMFamilies(:); |
---|
215 | iBPMs = length(BPMFamilies); |
---|
216 | BPMDeviceLists = cell(iBPMs,1); |
---|
217 | |
---|
218 | if isempty(MagnetFamilies) && isempty(BPMFamilies) |
---|
219 | fprintf(' Nothing to monitor.\n'); |
---|
220 | return; |
---|
221 | end |
---|
222 | |
---|
223 | |
---|
224 | |
---|
225 | disp(' '); |
---|
226 | disp(' This function monitors the storage ring magnets and BPMs then computes various'); |
---|
227 | disp(' accuracy statistics. The sample period should be long enough to acquire '); |
---|
228 | disp(' all the magnets and BPMs in the storage ring. The storage ring must be static'); |
---|
229 | disp(' during this measurement (no magnet or insertion device changes, etc). This '); |
---|
230 | disp(' function open a lot of windows, ">> close all" closes all figure windows.'); |
---|
231 | disp(' '); |
---|
232 | if isempty(t) |
---|
233 | prompt = {'Input the sample period (seconds)', 'Input the total data collection time (seconds)'}; |
---|
234 | answer = inputdlg(prompt,'monmags',1,{'1','180'}); |
---|
235 | if isempty(answer) |
---|
236 | fprintf(' monmags cancelled\n'); |
---|
237 | return |
---|
238 | end |
---|
239 | T = str2num(answer{1}); |
---|
240 | EndTime = str2num(answer{2}); |
---|
241 | t = 0:T:EndTime; |
---|
242 | end |
---|
243 | if length(t) < 3 |
---|
244 | fprintf('\n The input time vector only has %d sample points.\n', length(t)); |
---|
245 | fprintf(' The function will run with 3 but you should have a lot (like 100 or more).\n'); |
---|
246 | fprintf(' monmags cancelled'); |
---|
247 | return |
---|
248 | end |
---|
249 | |
---|
250 | fprintf(' Taking data for about %.1f seconds . . . ', t(end)); |
---|
251 | TimeStart = gettime; |
---|
252 | |
---|
253 | |
---|
254 | % Get the setpoints at the start |
---|
255 | MagnetSetpoints = getsp(MagnetFamilies, MagnetDeviceLists, 'Struct'); |
---|
256 | |
---|
257 | % Get the monitors (first connect to the channels so that the first sample time is not longer than the rest) |
---|
258 | % Forcing units to hardware is necessary to speedup the data taking |
---|
259 | tmp = getam([MagnetFamilies; BPMFamilies], [MagnetDeviceLists; BPMDeviceLists], 'Hardware'); |
---|
260 | Monitors = getam([MagnetFamilies; BPMFamilies], [MagnetDeviceLists; BPMDeviceLists], t, 'Struct', 'Hardware'); |
---|
261 | |
---|
262 | % Get the setpoints at the end (hopefully it's the same as the start) |
---|
263 | MagnetSetpointsEnd = getsp(MagnetFamilies, MagnetDeviceLists, 'Struct'); |
---|
264 | |
---|
265 | % Split the monitors into magnets and BPMs |
---|
266 | MagnetMonitors = Monitors(1:iMagnets); |
---|
267 | BPMMonitors = Monitors(iMagnets+1:iMagnets+iBPMs); |
---|
268 | |
---|
269 | fprintf('finished in %.1f seconds\n', Monitors{1}.tout(end)); |
---|
270 | fprintf(' Average Sampling Period = %.3f seconds (Max= %.3f, Min=%.3f seconds)\n', mean(diff(Monitors{1}.tout)), max(diff(Monitors{1}.tout)), min(diff(Monitors{1}.tout))); |
---|
271 | |
---|
272 | |
---|
273 | % Convert to the default units |
---|
274 | for i = 1:length(Monitors) |
---|
275 | ActualUnits = getunits(Monitors{i}.FamilyName); |
---|
276 | if strcmpi(ActualUnits, 'Physics') |
---|
277 | Monitors{i} = hw2physics(Monitors{i}); |
---|
278 | end |
---|
279 | end |
---|
280 | |
---|
281 | |
---|
282 | % Convert to fields (more readable) |
---|
283 | MagnetMonitors = cell2field(MagnetMonitors); |
---|
284 | MagnetSetpoints = cell2field(MagnetSetpoints); |
---|
285 | BPMMonitors = cell2field(BPMMonitors); |
---|
286 | MagnetSetpointsEnd = cell2field(MagnetSetpointsEnd); |
---|
287 | |
---|
288 | |
---|
289 | % Save data in the proper directory |
---|
290 | if ArchiveFlag || ischar(FileName) |
---|
291 | [DirectoryName, FileName, Ext] = fileparts(FileName); |
---|
292 | DirStart = pwd; |
---|
293 | [DirectoryName, ErrorFlag] = gotodirectory(DirectoryName); |
---|
294 | if ErrorFlag |
---|
295 | fprintf('\n There was a problem getting to the proper directory!\n\n'); |
---|
296 | end |
---|
297 | save(FileName, 'MagnetMonitors', 'MagnetSetpoints', 'BPMMonitors', 'MagnetSetpointsEnd'); |
---|
298 | cd(DirStart); |
---|
299 | FileName = [DirectoryName FileName]; |
---|
300 | |
---|
301 | if DisplayFlag |
---|
302 | fprintf(' Magnet power supply data saved to %s\n', FileName); |
---|
303 | fprintf(' The total measurement time was %.2f minutes.\n', (gettime-TimeStart)/60); |
---|
304 | else |
---|
305 | return; |
---|
306 | end |
---|
307 | else |
---|
308 | FileName = ''; |
---|
309 | end |
---|
310 | |
---|
311 | else |
---|
312 | |
---|
313 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
314 | % Get data from file and plot it % |
---|
315 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
316 | load([DirectoryName, FileName]); |
---|
317 | |
---|
318 | end |
---|
319 | |
---|
320 | |
---|
321 | if ~DisplayFlag |
---|
322 | return |
---|
323 | end |
---|
324 | |
---|
325 | |
---|
326 | |
---|
327 | %%%%%%%%%%%% |
---|
328 | % Plotting % |
---|
329 | %%%%%%%%%%%% |
---|
330 | |
---|
331 | % Convert to cell arrays |
---|
332 | if ~iscell(MagnetMonitors) |
---|
333 | if isempty(MagnetMonitors) |
---|
334 | MagnetMonitors = {}; |
---|
335 | MagnetSetpoints = {}; |
---|
336 | MagnetSetpointsEnd = {}; |
---|
337 | else |
---|
338 | MagnetMonitors = field2cell(MagnetMonitors); |
---|
339 | MagnetSetpoints = field2cell(MagnetSetpoints); |
---|
340 | MagnetSetpointsEnd = field2cell(MagnetSetpointsEnd); |
---|
341 | end |
---|
342 | end |
---|
343 | if ~iscell(BPMMonitors) |
---|
344 | if isempty(BPMMonitors) |
---|
345 | BPMMonitors = {}; |
---|
346 | else |
---|
347 | BPMMonitors = field2cell(BPMMonitors); |
---|
348 | end |
---|
349 | end |
---|
350 | |
---|
351 | if ~isempty(MagnetMonitors) |
---|
352 | tout = MagnetMonitors{1}.tout; |
---|
353 | elseif ~isempty(BPMMonitors) |
---|
354 | tout = BPMMonitors{1}.tout; |
---|
355 | else |
---|
356 | fprintf(' There is not data in this file to plot.\n'); |
---|
357 | return |
---|
358 | end |
---|
359 | |
---|
360 | |
---|
361 | if PlotDataFlag == 2 |
---|
362 | % Plot one family |
---|
363 | for i = 1:length(MagnetMonitors) |
---|
364 | FamilyList{i} = MagnetMonitors{i}.FamilyName; |
---|
365 | end |
---|
366 | for i = 1:length(BPMMonitors) |
---|
367 | FamilyList{i+length(MagnetMonitors)} = BPMMonitors{i}.FamilyName; |
---|
368 | end |
---|
369 | |
---|
370 | FamilyNumber = menu('Choose a family', FamilyList); |
---|
371 | |
---|
372 | if FamilyNumber <= length(MagnetMonitors) |
---|
373 | MagnetMonitors = MagnetMonitors(FamilyNumber); |
---|
374 | MagnetSetpoints = MagnetSetpoints(FamilyNumber); |
---|
375 | BPMMonitors = []; |
---|
376 | else |
---|
377 | BPMMonitors = BPMMonitors(FamilyNumber-length(MagnetMonitors)); |
---|
378 | MagnetMonitors = []; |
---|
379 | MagnetSetpoints = []; |
---|
380 | end |
---|
381 | end |
---|
382 | |
---|
383 | |
---|
384 | % Plot all families |
---|
385 | for i = 1:length(MagnetMonitors) |
---|
386 | if strcmpi(UnitsFlag, 'Physics') |
---|
387 | MagnetMonitors{i} = hw2physics(MagnetMonitors{i}); |
---|
388 | MagnetSetpoints{i} = hw2physics(MagnetSetpoints{i}); |
---|
389 | elseif strcmpi(UnitsFlag, 'Hardware') |
---|
390 | MagnetMonitors{i} = physics2hw(MagnetMonitors{i}); |
---|
391 | MagnetSetpoints{i} = physics2hw(MagnetSetpoints{i}); |
---|
392 | end |
---|
393 | |
---|
394 | if Raw2RealFlag && strcmpi(MagnetMonitors{i}.Units, 'Hardware') |
---|
395 | MagnetMonitors{i} = raw2real(MagnetMonitors{i}); |
---|
396 | MagnetSetpoints{i} = raw2real(MagnetSetpoints{i}); |
---|
397 | end |
---|
398 | |
---|
399 | |
---|
400 | a = unique(MagnetMonitors{i}.Data, 'rows'); |
---|
401 | if size(a,1) == 1 |
---|
402 | % Single power supply or ganged power supply |
---|
403 | figure; |
---|
404 | subplot(2,1,1); |
---|
405 | hist(MagnetMonitors{i}.Data(1,:),200); |
---|
406 | |
---|
407 | title(sprintf('%s: SP=%g, mean(AM)=%g, SP-mean(AM)=%g', ... |
---|
408 | MagnetSetpoints{i}.FamilyName, ... |
---|
409 | MagnetSetpoints{i}.Data(1,1), ... |
---|
410 | mean(MagnetMonitors{i}.Data(1,:)), ... |
---|
411 | MagnetSetpoints{i}.Data(1,1) - mean(MagnetMonitors{i}.Data(1,:))), 'FontSize',8); |
---|
412 | xlabel(MagnetMonitors{i}.UnitsString,'FontSize',8); |
---|
413 | |
---|
414 | subplot(2,1,2); |
---|
415 | plot(MagnetMonitors{i}.tout, MagnetMonitors{i}.Data(1,:)); |
---|
416 | title(sprintf('std(AM)=%.3g, max(AM-mean(AM))=%.3g', ... |
---|
417 | std(MagnetMonitors{i}.Data(1,:)), ... |
---|
418 | max(abs(MagnetMonitors{i}.Data(1,:) - mean(MagnetMonitors{i}.Data(1,:))))), 'FontSize',8); |
---|
419 | ylabel(MagnetMonitors{i}.UnitsString,'FontSize',8); |
---|
420 | xlabel('Time [Seconds]','FontSize',8); |
---|
421 | |
---|
422 | addlabel(1,0,sprintf('%s', datestr(MagnetMonitors{i}.TimeStamp))); |
---|
423 | orient tall |
---|
424 | |
---|
425 | else |
---|
426 | |
---|
427 | % Independent power supplies |
---|
428 | PlotBySector = 1; |
---|
429 | |
---|
430 | ElementList = dev2elem(MagnetMonitors{i}.FamilyName, MagnetMonitors{i}.DeviceList); |
---|
431 | |
---|
432 | [Sector, Nsectors, Ndevices] = sectorticks(MagnetMonitors{i}.DeviceList); |
---|
433 | |
---|
434 | AMmean = mean(MagnetMonitors{i}.Data,2); |
---|
435 | AMstd = std(MagnetMonitors{i}.Data,0,2); |
---|
436 | |
---|
437 | %fprintf(' %s: mean(AM)=%7.3f Amps, SP-mean(AM)=%6.3f, std(AM)=%5.3f Amps\n', MagnetMonitors{i}.FamilyName, AMmean, MagnetSetpoints{i}.Data-AMmean, AMstd); |
---|
438 | |
---|
439 | figure; |
---|
440 | subplot(4,1,1); |
---|
441 | if PlotBySector |
---|
442 | bar(Sector, AMmean); |
---|
443 | xaxis([1 Nsectors+1]) |
---|
444 | set(gca,'XTick',1:Nsectors); |
---|
445 | else |
---|
446 | bar(ElementList, AMmean); |
---|
447 | end |
---|
448 | title(sprintf('%s Family (Units are in %s)', MagnetMonitors{i}.FamilyName, MagnetMonitors{i}.UnitsString)); |
---|
449 | ylabel('mean(AM)','FontSize',8); |
---|
450 | %ylabel(sprintf('mean(AM) [%s]',MagnetMonitors{i}.UnitsString),'FontSize',8); |
---|
451 | grid on; |
---|
452 | |
---|
453 | ymax = max(AMmean); |
---|
454 | ymin = min(AMmean); |
---|
455 | extra = .05*(ymax-ymin); |
---|
456 | if extra < 1e6*eps |
---|
457 | extra = 1e6*eps; |
---|
458 | end |
---|
459 | if ~isnan(extra) |
---|
460 | yaxis([ymin-extra ymax+extra]); |
---|
461 | end |
---|
462 | |
---|
463 | subplot(4,1,2); |
---|
464 | if PlotBySector |
---|
465 | bar(Sector, MagnetSetpoints{i}.Data-AMmean); |
---|
466 | xaxis([1 Nsectors+1]) |
---|
467 | set(gca,'XTick',1:Nsectors); |
---|
468 | else |
---|
469 | bar(ElementList, MagnetSetpoints{i}.Data-AMmean); |
---|
470 | end |
---|
471 | ylabel('SP-mean(AM)','FontSize',8); |
---|
472 | %ylabel(sprintf('SP-mean(AM) [%s]',MagnetMonitors{i}.UnitsString),'FontSize',8); |
---|
473 | grid on; |
---|
474 | |
---|
475 | subplot(4,1,3); |
---|
476 | if PlotBySector |
---|
477 | bar(Sector, AMstd); |
---|
478 | xaxis([1 Nsectors+1]) |
---|
479 | set(gca,'XTick',1:Nsectors); |
---|
480 | else |
---|
481 | bar(ElementList, AMstd); |
---|
482 | end |
---|
483 | ylabel('std(AM)','FontSize',8); |
---|
484 | %ylabel(sprintf('std(AM) [%s]',MagnetMonitors{i}.UnitsString),'FontSize',8); |
---|
485 | grid on; |
---|
486 | |
---|
487 | subplot(4,1,4); |
---|
488 | % Outlier test |
---|
489 | clear AM_AMmeanMax |
---|
490 | for n = 1:size(AMmean,1) |
---|
491 | [tmp,j] = max(abs(MagnetMonitors{i}.Data(n,:)-AMmean(n))); |
---|
492 | AM_AMmeanMax(n,1) = MagnetMonitors{i}.Data(n,j)-AMmean(n); |
---|
493 | end |
---|
494 | if PlotBySector |
---|
495 | bar(Sector, AM_AMmeanMax); |
---|
496 | xaxis([1 Nsectors+1]) |
---|
497 | set(gca,'XTick',1:Nsectors); |
---|
498 | else |
---|
499 | bar(ElementList, AM_AMmeanMax); |
---|
500 | end |
---|
501 | ylabel('max(AM-mean(AM))','FontSize',8); |
---|
502 | % ylabel(sprintf('max(AM-mean(AM)) [%s]',MagnetMonitors{i}.UnitsString),'FontSize',8); |
---|
503 | if PlotBySector |
---|
504 | xlabel('Sector Number'); |
---|
505 | else |
---|
506 | xlabel('Element Number','FontSize',8); |
---|
507 | end |
---|
508 | grid on; |
---|
509 | |
---|
510 | addlabel(1,0,sprintf('%s', datestr(MagnetMonitors{i}.TimeStamp))); |
---|
511 | orient tall |
---|
512 | |
---|
513 | if PlotDataFlag == 2 |
---|
514 | % Plot more stuff |
---|
515 | figure; |
---|
516 | |
---|
517 | % Worst SP-AMmean |
---|
518 | subplot(3,1,1); |
---|
519 | [tmp, k] = max(abs(MagnetSetpoints{i}.Data-AMmean)); |
---|
520 | hist(MagnetMonitors{i}.Data(k,:), 200); |
---|
521 | title(sprintf('Worst SP-mean(AM): %s(%d,%d) SP=%g, mean(AM)=%g, SP-mean(AM)=%g', ... |
---|
522 | MagnetSetpoints{i}.FamilyName, ... |
---|
523 | MagnetSetpoints{i}.DeviceList(k,1), MagnetSetpoints{i}.DeviceList(k,2), ... |
---|
524 | MagnetSetpoints{i}.Data(k,1), ... |
---|
525 | AMmean(k), ... |
---|
526 | MagnetSetpoints{i}.Data(k,1) - AMmean(k)), ... |
---|
527 | 'FontSize',8); |
---|
528 | xlabel(MagnetMonitors{i}.UnitsString,'FontSize',8); |
---|
529 | a1 = axis; |
---|
530 | |
---|
531 | % Worst std(AM) |
---|
532 | subplot(3,1,2); |
---|
533 | [tmp, k] = max(AMstd); |
---|
534 | hist(MagnetMonitors{i}.Data(k,:), 200); |
---|
535 | title(sprintf('Worst std(AM): %s(%d,%d) SP=%g, mean(AM)=%g, std(AM)=%g', ... |
---|
536 | MagnetSetpoints{i}.FamilyName, ... |
---|
537 | MagnetSetpoints{i}.DeviceList(k,1), MagnetSetpoints{i}.DeviceList(k,2), ... |
---|
538 | MagnetSetpoints{i}.Data(k,1), ... |
---|
539 | AMmean(k), ... |
---|
540 | AMstd(k)), ... |
---|
541 | 'FontSize',8); |
---|
542 | xlabel(MagnetMonitors{i}.UnitsString,'FontSize',8); |
---|
543 | a2 = axis; |
---|
544 | |
---|
545 | % Worst max(AM-mean(AM)) |
---|
546 | subplot(3,1,3); |
---|
547 | [tmp, k] = max(abs(AM_AMmeanMax)); |
---|
548 | hist(MagnetMonitors{i}.Data(k,:), 200); |
---|
549 | title(sprintf('Worst max(AM-mean(AM)): %s(%d,%d) SP=%g, mean(AM)=%g, max(AM-mean(AM))=%g', ... |
---|
550 | MagnetSetpoints{i}.FamilyName, ... |
---|
551 | MagnetSetpoints{i}.DeviceList(k,1), MagnetSetpoints{i}.DeviceList(k,2), ... |
---|
552 | MagnetSetpoints{i}.Data(k,1), ... |
---|
553 | AMmean(k), ... |
---|
554 | AM_AMmeanMax(k)), ... |
---|
555 | 'FontSize',8); |
---|
556 | xlabel(MagnetMonitors{i}.UnitsString,'FontSize',8); |
---|
557 | a3 = axis; |
---|
558 | |
---|
559 | % Force all the x-axis to be the same |
---|
560 | %xaxis([min([a1(1) a2(1) a3(1)]) max([a1(2) a2(2) a3(2)])]); |
---|
561 | %subplot(3,1,1); |
---|
562 | %xaxis([min([a1(1) a2(1) a3(1)]) max([a1(2) a2(2) a3(2)])]); |
---|
563 | %subplot(3,1,2); |
---|
564 | %xaxis([min([a1(1) a2(1) a3(1)]) max([a1(2) a2(2) a3(2)])]); |
---|
565 | %addlabel(1,0,sprintf('%s', datestr(MagnetMonitors{i}.TimeStamp))); |
---|
566 | orient tall |
---|
567 | end |
---|
568 | end |
---|
569 | end |
---|
570 | |
---|
571 | |
---|
572 | for i = 1:length(BPMMonitors) |
---|
573 | if ~all(isnan(BPMMonitors{i}.Data)) |
---|
574 | |
---|
575 | if strcmpi(UnitsFlag, 'physics') |
---|
576 | BPMMonitors{i} = hw2physics(BPMMonitors{i}); |
---|
577 | elseif strcmpi(UnitsFlag, 'hardware') |
---|
578 | BPMMonitors{i} = physics2hw(BPMMonitors{i}); |
---|
579 | end |
---|
580 | |
---|
581 | if Raw2RealFlag && strcmpi(BPMMonitors{i}.Units, 'Hardware') |
---|
582 | BPMMonitors{i} = raw2real(BPMMonitors{i}); |
---|
583 | end |
---|
584 | |
---|
585 | ElementList = dev2elem(BPMMonitors{i}.FamilyName, BPMMonitors{i}.DeviceList); |
---|
586 | |
---|
587 | if PlotDataFlag == 2 |
---|
588 | % Plot more stuff |
---|
589 | figure; |
---|
590 | subplot(2,1,1); |
---|
591 | plot(BPMMonitors{i}.tout, BPMMonitors{i}.Data); |
---|
592 | title(sprintf('%s Family (Raw Orbit Data)', BPMMonitors{i}.FamilyName)); |
---|
593 | ylabel(sprintf('%s [%s]', BPMMonitors{i}.FamilyName, BPMMonitors{i}.UnitsString)); |
---|
594 | axis tight; |
---|
595 | grid on; |
---|
596 | end |
---|
597 | |
---|
598 | % Plot difference orbits |
---|
599 | Orbit0 = BPMMonitors{i}.Data(:,1); |
---|
600 | for j = size(BPMMonitors{i}.Data,2):-1:1 |
---|
601 | BPMMonitors{i}.Data(:,j) = BPMMonitors{i}.Data(:,j) - Orbit0; |
---|
602 | end |
---|
603 | |
---|
604 | if PlotDataFlag == 2 |
---|
605 | subplot(2,1,2); |
---|
606 | plot(BPMMonitors{i}.tout, BPMMonitors{i}.Data); |
---|
607 | title(sprintf('%s Family Difference Orbits from %s', BPMMonitors{i}.FamilyName, datestr(BPMMonitors{i}.TimeStamp,13))); |
---|
608 | ylabel(sprintf('%s [%s]', BPMMonitors{i}.FamilyName, BPMMonitors{i}.UnitsString)); |
---|
609 | axis tight; |
---|
610 | grid on; |
---|
611 | |
---|
612 | addlabel(1,0,sprintf('%s', datestr(BPMMonitors{i}.TimeStamp))); |
---|
613 | orient tall |
---|
614 | end |
---|
615 | |
---|
616 | % Difference orbits |
---|
617 | DiffOrbits = diff(BPMMonitors{i}.Data, 1, 2); |
---|
618 | %fprintf(' %s: mean(AM)=%7.3f Amps, SP-mean(AM)=%6.3f, std(AM)=%5.3f Amps\n', BPMMonitors{i}.FamilyName, AMmean, BPMSetpoints{i}.Data-AMmean, AMstd); |
---|
619 | |
---|
620 | AMmean = mean(BPMMonitors{i}.Data,2); |
---|
621 | AMmax = max(BPMMonitors{i}.Data,[],2); |
---|
622 | AMmin = min(BPMMonitors{i}.Data,[],2); |
---|
623 | AMstd = std(BPMMonitors{i}.Data,0,2); |
---|
624 | AMstddetrend = std(detrend(BPMMonitors{i}.Data'))'; |
---|
625 | |
---|
626 | figure; |
---|
627 | subplot(2,1,1); |
---|
628 | stairs(ElementList-.5, AMmean, 'b'); |
---|
629 | hold on |
---|
630 | stairs(ElementList-.5, AMmax, 'r'); |
---|
631 | stairs(ElementList-.5, AMmin, 'g'); |
---|
632 | hold off |
---|
633 | title(sprintf('%s Family (Deviation from the Starting Orbit)', BPMMonitors{i}.FamilyName)); |
---|
634 | ylabel(sprintf('Max/Mean/Min [%s]', BPMMonitors{i}.UnitsString)); |
---|
635 | grid on; |
---|
636 | |
---|
637 | |
---|
638 | subplot(2,1,2); |
---|
639 | stairs(ElementList-.5, AMstd, 'b'); |
---|
640 | hold on; |
---|
641 | stairs(ElementList-.5, AMstddetrend, 'r'); |
---|
642 | stairs(ElementList-.5, std(DiffOrbits,0,2)/sqrt(2), 'g'); |
---|
643 | hold off |
---|
644 | ylabel(sprintf('STD [%s]', BPMMonitors{i}.UnitsString)); |
---|
645 | xlabel('Element Number'); |
---|
646 | legend('Raw data','Linear trend removed','Difference Orbits/sqrt(2)', 0); |
---|
647 | grid on; |
---|
648 | |
---|
649 | addlabel(1,0,sprintf('%s', datestr(BPMMonitors{i}.TimeStamp))); |
---|
650 | orient tall |
---|
651 | end |
---|
652 | end |
---|
653 | |
---|
654 | |
---|
655 | % Convert back to structures |
---|
656 | if ~isstruct(MagnetMonitors) |
---|
657 | MagnetMonitors = cell2field(MagnetMonitors); |
---|
658 | MagnetSetpoints = cell2field(MagnetSetpoints); |
---|
659 | MagnetSetpointsEnd = cell2field(MagnetSetpointsEnd); |
---|
660 | end |
---|
661 | if ~isempty(BPMMonitors) |
---|
662 | if ~isstruct(BPMMonitors) |
---|
663 | BPMMonitors = cell2field(BPMMonitors); |
---|
664 | end |
---|
665 | end |
---|
666 | |
---|