1 | function monbpmhistory(varargin) |
---|
2 | % monbpmhistory - Read history buffer of all the BPMs |
---|
3 | % |
---|
4 | % INPUTS |
---|
5 | % 2 and 4. BPMxFamily and BPMyFamily are the family names of the BPM's, {Default or []: the entire list} |
---|
6 | % 3 and 5. BPMxList and BPMyList are the device list of BPM's, {Default or []: the entire list} |
---|
7 | % 6. 'Struct' will return data structures instead of vectors |
---|
8 | % 'Numeric' will return vector outputs {Default} |
---|
9 | % 7. FileName = Filename (including directory) where the data was saved (if applicable) |
---|
10 | % 8. 'Archive' - save a data array structure to \<BPMData Directory>\<BPMData><Date><Time>.mat {Default} |
---|
11 | % 'NoArchive' - no data will be saved to file |
---|
12 | % 'Summary' - Sort BPM by decreasind STD value |
---|
13 | % |
---|
14 | % OUTPUTS |
---|
15 | % For numeric output: |
---|
16 | % 1-2. BPMx and BPMy are the raw orbit data matrices or structures |
---|
17 | % 3. DCCT is a row vector containing the beam current |
---|
18 | % 4. tout is a row vector of times as returned by getam |
---|
19 | % 5-6. BPMxSTD and BPMySTD are standard deviation of the difference orbits |
---|
20 | % 7. FileName = Filename (including directory) where the data was saved (if applicable) |
---|
21 | % |
---|
22 | % For structures: |
---|
23 | % BPMxSTD and BPMySTD are the .Sigma field |
---|
24 | |
---|
25 | % Written by Laurent S. Nadolski |
---|
26 | % TODO Not all options are operational |
---|
27 | |
---|
28 | FileName = -1; |
---|
29 | ArchiveFlag = 0; |
---|
30 | StructOutputFlag = 0; |
---|
31 | DisplayFlag=1; |
---|
32 | SummaryFlag = 1; |
---|
33 | |
---|
34 | % Look if 'struct' or 'numeric' in on the input line |
---|
35 | for i = length(varargin):-1:1 |
---|
36 | if strcmpi(varargin{i},'Struct') |
---|
37 | StructOutputFlag = 1; |
---|
38 | varargin(i) = []; |
---|
39 | elseif strcmpi(varargin{i},'Numeric') |
---|
40 | StructOutputFlag = 0; |
---|
41 | varargin(i) = []; |
---|
42 | elseif strcmpi(varargin{i},'Archive') |
---|
43 | ArchiveFlag = 1; |
---|
44 | if length(varargin) > i |
---|
45 | % Look for a filename as the next input |
---|
46 | if ischar(varargin{i+1}) |
---|
47 | FileName = varargin{i+1}; |
---|
48 | varargin(i+1) = []; |
---|
49 | end |
---|
50 | end |
---|
51 | varargin(i) = []; |
---|
52 | elseif strcmpi(varargin{i},'NoArchive') |
---|
53 | ArchiveFlag = 0; |
---|
54 | varargin(i) = []; |
---|
55 | elseif strcmpi(varargin{i},'NoDisplay') |
---|
56 | DisplayFlag = 0; |
---|
57 | varargin(i) = []; |
---|
58 | elseif strcmpi(varargin{i},'Summary') |
---|
59 | SummaryFlag = 1; |
---|
60 | varargin(i) = []; |
---|
61 | elseif strcmpi(varargin{i},'NoSummary') |
---|
62 | SummaryFlag = 0; |
---|
63 | varargin(i) = []; |
---|
64 | elseif strcmpi(varargin{i},'Display') |
---|
65 | DisplayFlag = 1; |
---|
66 | varargin(i) = []; |
---|
67 | end |
---|
68 | end |
---|
69 | |
---|
70 | %% |
---|
71 | if isempty(varargin) |
---|
72 | BPM(1).DeviceList = family2dev('BPMx'); |
---|
73 | BPM(2).DeviceList = family2dev('BPMz'); |
---|
74 | devName = family2tangodev('BPMx'); |
---|
75 | else |
---|
76 | BPM(1).DeviceList = varargin{:}; |
---|
77 | BPM(2).DeviceList = varargin{:}; |
---|
78 | devName = family2tangodev('BPMx',varargin{:}); |
---|
79 | end |
---|
80 | |
---|
81 | %% |
---|
82 | Xpos = []; |
---|
83 | Zpos = []; |
---|
84 | Xrmspos = []; |
---|
85 | Zrmspos = []; |
---|
86 | |
---|
87 | TimeStart = gettime; |
---|
88 | |
---|
89 | for k = 1:length(devName), |
---|
90 | %rep = tango_read_attributes(devName{k},{'XPosSAHistory','ZPosSAHistory','XRMSPosSA','ZRMSPosSA'}); |
---|
91 | rep = tango_read_attributes(devName{k},{'XPosSAHistory','ZPosSAHistory'}); |
---|
92 | Xpos(:,k) = rep(1).value; |
---|
93 | Zpos(:,k) = rep(2).value; |
---|
94 | end |
---|
95 | |
---|
96 | BPM(1).Data = Xpos; |
---|
97 | BPM(2).Data = Zpos; |
---|
98 | BPM(1).Sigma = std(Xpos); |
---|
99 | BPM(2).Sigma = std(Zpos); |
---|
100 | BPM(1).UnitsString = 'mm'; |
---|
101 | BPM(2).UnitsString = 'mm'; |
---|
102 | |
---|
103 | xtime = (1:size(Xpos,1))/10; |
---|
104 | |
---|
105 | if ArchiveFlag |
---|
106 | if isempty(FileName) |
---|
107 | FileName = appendtimestamp('BPMDataHistory'); |
---|
108 | DirectoryName = getfamilydata('Directory','BPMData'); |
---|
109 | if isempty(DirectoryName) |
---|
110 | DirectoryName = [getfamilydata('Directory','DataRoot'), filesep, 'BPM', filesep]; |
---|
111 | else |
---|
112 | % Make sure default directory exists |
---|
113 | DirStart = pwd; |
---|
114 | [DirectoryName, ErrorFlag] = gotodirectory(DirectoryName); |
---|
115 | cd(DirStart); |
---|
116 | end |
---|
117 | [FileName, DirectoryName] = uiputfile('*.mat', 'Select a BPM Monitor File', [DirectoryName FileName]); |
---|
118 | if FileName == 0 |
---|
119 | ArchiveFlag = 0; |
---|
120 | fprintf(' monbpm canceled\n'); |
---|
121 | varargout{1} = []; |
---|
122 | return |
---|
123 | end |
---|
124 | FileName = [DirectoryName, FileName]; |
---|
125 | elseif FileName == -1 |
---|
126 | FileName = appendtimestamp('BPMDataHistory'); |
---|
127 | DirectoryName = getfamilydata('Directory','BPMData'); |
---|
128 | if isempty(DirectoryName) |
---|
129 | DirectoryName = [getfamilydata('Directory','DataRoot'), filesep, 'BPM', filesep]; |
---|
130 | end |
---|
131 | FileName = [DirectoryName, FileName]; |
---|
132 | end |
---|
133 | end |
---|
134 | |
---|
135 | |
---|
136 | % |
---|
137 | |
---|
138 | if DisplayFlag |
---|
139 | List = BPM(1).DeviceList; |
---|
140 | Nsectors = max(List(:,1)); |
---|
141 | Ndevices = max(List(:,2));Sector = List(:,1) + List(:,2)/Ndevices + 1/Ndevices/2; |
---|
142 | [Sector Idx] = sort(Sector); |
---|
143 | figure |
---|
144 | subplot(2,2,1) |
---|
145 | plot(xtime, BPM(1).Data-repmat(BPM(1).Data(1,:),size(BPM(1).Data,1),1)) |
---|
146 | xaxis([1 size(BPM(1).Data,1)/10]) |
---|
147 | ylabel('DX (mm)') |
---|
148 | xlabel('time [s]') |
---|
149 | grid on |
---|
150 | subplot(2,2,2) |
---|
151 | plot(Sector,BPM(1).Sigma) |
---|
152 | xaxis([1 Nsectors+1]) |
---|
153 | set(gca,'XTick',1:Nsectors); |
---|
154 | xlabel('Sector Number'); |
---|
155 | ylabel(sprintf('Horizontal STD [%s]', BPM(1).UnitsString)); |
---|
156 | grid on |
---|
157 | subplot(2,2,3) |
---|
158 | plot(xtime, BPM(2).Data-repmat(BPM(2).Data(1,:),size(BPM(2).Data,1),1)) |
---|
159 | xaxis([1 size(BPM(2).Data,1)/10]) |
---|
160 | ylabel('DZ (mm)') |
---|
161 | xlabel('time [s]') |
---|
162 | grid on |
---|
163 | subplot(2,2,4) |
---|
164 | plot(Sector,BPM(2).Sigma) |
---|
165 | xaxis([1 Nsectors+1]) |
---|
166 | set(gca,'XTick',1:Nsectors); |
---|
167 | xlabel('Sector Number'); |
---|
168 | ylabel(sprintf('Horizontal STD [%s]', BPM(1).UnitsString)); |
---|
169 | grid on |
---|
170 | |
---|
171 | addlabel(datestr(tango_shift_time(rep(2).time))); |
---|
172 | end |
---|
173 | |
---|
174 | % Save data in the proper directory |
---|
175 | if ArchiveFlag | ischar(FileName) |
---|
176 | [DirectoryName, FileName, Ext] = fileparts(FileName); |
---|
177 | DirStart = pwd; |
---|
178 | [DirectoryName, ErrorFlag] = gotodirectory(DirectoryName); |
---|
179 | if ErrorFlag |
---|
180 | fprintf('\n There was a problem getting to the proper directory!\n\n'); |
---|
181 | end |
---|
182 | BPMxData = BPM(1); |
---|
183 | BPMyData = BPM(2); |
---|
184 | save(FileName, 'BPMxData', 'BPMyData'); |
---|
185 | %save(FileName, 'BPM'); |
---|
186 | cd(DirStart); |
---|
187 | FileName = [DirectoryName, FileName, '.mat']; |
---|
188 | |
---|
189 | if DisplayFlag |
---|
190 | fprintf(' BPM data saved to %s\n', FileName); |
---|
191 | fprintf(' The total measurement time was %.2f minutes.\n', (gettime-TimeStart)/60); |
---|
192 | end |
---|
193 | else |
---|
194 | FileName = ''; |
---|
195 | end |
---|
196 | |
---|
197 | |
---|
198 | if SummaryFlag |
---|
199 | % sort by sigmas |
---|
200 | [SortedData1 DataIdx1] = sort(BPM(1).Sigma,'descend'); |
---|
201 | SortedDeviceList1 = BPM(1).DeviceList(DataIdx1,:); |
---|
202 | [SortedData2 DataIdx2] = sort(BPM(2).Sigma,'descend'); |
---|
203 | SortedDeviceList2 = BPM(2).DeviceList(DataIdx2,:); |
---|
204 | |
---|
205 | % sort by drift |
---|
206 | maxBPM1 = max(abs(BPM(1).Data-repmat(BPM(1).Data(1,:),size(BPM(1).Data,1),1))); |
---|
207 | [SortedData3 DataIdx3] = sort(maxBPM1,'descend'); |
---|
208 | SortedDeviceList3 = BPM(1).DeviceList(DataIdx1,:); |
---|
209 | maxBPM2 = max(abs(BPM(2).Data-repmat(BPM(2).Data(1,:),size(BPM(2).Data,1),1))); |
---|
210 | [SortedData4 DataIdx4] = sort(maxBPM2,'descend'); |
---|
211 | SortedDeviceList4 = BPM(2).DeviceList(DataIdx1,:); |
---|
212 | |
---|
213 | fprintf('\n\n BPMxname DevList Max drift BPMzname DevList Max drift\n'); |
---|
214 | for k=1:size(BPM(1).DeviceList,1), |
---|
215 | fprintf('%s [%2d %2d] %6.2e [%s] %s [%2d %2d] %6.2e [%s] \n', ... |
---|
216 | 'BPMx', SortedDeviceList3(k,1), SortedDeviceList3(k,2), SortedData3(k), BPM(1).UnitsString, ... |
---|
217 | 'BPMz', SortedDeviceList4(k,1), SortedDeviceList4(k,2), SortedData4(k), BPM(2).UnitsString); |
---|
218 | end |
---|
219 | |
---|
220 | fprintf('\n\n BPMxname DevList STD value BPMzname DevList STD value\n'); |
---|
221 | for k=1:size(BPM(1).DeviceList,1), |
---|
222 | fprintf('%s [%2d %2d] %6.2e [%s] %s [%2d %2d] %6.2e [%s] \n', ... |
---|
223 | 'BPMx', SortedDeviceList1(k,1), SortedDeviceList1(k,2), SortedData1(k), BPM(1).UnitsString, ... |
---|
224 | 'BPMz', SortedDeviceList2(k,1), SortedDeviceList2(k,2), SortedData2(k), BPM(2).UnitsString); |
---|
225 | end |
---|
226 | end |
---|
227 | |
---|
228 | if StructOutputFlag |
---|
229 | % Output variables |
---|
230 | varargout{1} = BPM(1); |
---|
231 | varargout{2} = BPM(2); |
---|
232 | varargout{3} = FileName; |
---|
233 | else |
---|
234 | % Output variables |
---|
235 | varargout{1} = BPM(1).Data; |
---|
236 | varargout{2} = BPM(2).Data; |
---|
237 | varargout{3} = 0; % tout |
---|
238 | varargout{4} = 0; %DCCT; |
---|
239 | varargout{5} = BPM(1).Sigma; |
---|
240 | varargout{6} = BPM(2).Sigma; |
---|
241 | varargout{7} = FileName; |
---|
242 | end |
---|
243 | |
---|