source: MML/trunk/machine/SOLEIL/StorageRing/bpm/measbpmsigma.m @ 4

Last change on this file since 4 was 4, checked in by zhangj, 10 years ago

Initial import--MML version from SOLEIL@2013

File size: 7.9 KB
Line 
1function varargout = measbpmsigma(varargin)
2%MEASBPMSIGMA - Measures the standard deviation of the BPMs
3%  [BPMxSTD, BPMzSTD, BPMx, BPMz, tout, DCCT, FileName] = measbpmsigma(t, BPMxFamily, BPMxList, BPMzFamily, BPMzList, FileName)
4%  [BPMxSTD, BPMzSTD, FileName] = measbpmsigma(... , 'Struct')
5%
6%  INPUTS
7%  1. t = time vector [seconds], or
8%         length of time in seconds to measure data (scalar)
9%         {default: 3 minute at a sample rate of 2 Hz}
10%  2 and 4. BPMxFamily and BPMzFamily are the family names of the BPM's, {default or []: the entire list}
11%  3 and 5. BPMxList and BPMzList are the device list of BPM's, {default or []: the entire list}
12%  6. 'Struct'  will return data structures instead of vectors
13%     'Numeric' will return vector outputs {default}
14%  7.  FileName = Filename (including directory) where the data was saved (if applicable)
15%  8. 'Archive'   - save a data array structure to \<BPMData Directory>\<BPMSigmaFile><Date><Time>.mat {Default}
16%     'NoArchive' - no data will be saved to file
17%
18%  OUTPUTS
19%  For numeric output:
20%  1-2. BPMxSTD and BPMzSTD are standard deviation of the difference orbits
21%  3-4. BPMx and BPMz are the raw orbit data matrices
22%  5. DCCT is a row vector containing the beam current
23%  6. tout is a row vector of times as returned by getam           
24%  7. FileName = Filename (including directory) where the data was saved (if applicable)
25%
26%  For structures:
27%  1-2. BPMxSTD and BPMzSTD - the standard deviations are put the Data field and the
28%     BPMx and BPMz data are put in the RawData field.
29%  3. FileName = Filename (including directory) where the data was saved (if applicable)
30%
31%  NOTE
32%  1. All inputs are optional.  All of the following have the same output: 
33%        measbpmsigma(0:.5:180);
34%        measbpmsigma(0:.5:180, 'BPMx');
35%        measbpmsigma('BPMx');
36%        measbpmsigma('BPMx', 'BPMz');
37%        measbpmsigma('BPMx', [], 'BPMz');
38%  2. To make the measured sigma the default:
39%     First divide by the sqrt(BPMxSigma.NumberOfAverages)
40%     BPMxSigma.Data = BPMxSigma.Data / BPMxSigma.NumberOfAverages;
41%     BPMzSigma.Data = BPMzSigma.Data / BPMzSigma.NumberOfAverages;
42%     [BPMxSigma, BPMzSigma] = measbpmsigma('Struct');
43%     setphysdata(BPMxSigma,'Sigma');
44%     setphysdata(BPMzSigma,'Sigma');
45%  3. Use getsigma to get the default values
46
47%
48% Written by Gregory J. Portmann
49% Modified by Laurent S. Nadolski
50
51% Defaults
52BPMxFamily = 'BPMx';
53BPMzFamily = 'BPMz';
54BPMxList = [];   
55BPMzList = [];   
56T = 3*60; %total monitoring time
57Tsample = .5; % 2Hz by default
58t = []; % t= 0:Tsample:T
59FileName = []; %Filename for archiving
60ArchiveFlag = 1; %Archive by default
61StructOutputFlag = 0;
62OutputFileName = '';
63Navg = getbpmaverages; % number of averages
64
65% Look if 'struct' or 'numeric' in on the input line
66for i = length(varargin):-1:1
67    if strcmpi(varargin{i},'Struct')
68        StructOutputFlag = 1;
69        varargin(i) = [];
70    elseif strcmpi(varargin{i},'Numeric')
71        StructOutputFlag = 0;
72        varargin(i) = [];
73    elseif strcmpi(varargin{i},'Archive')
74        ArchiveFlag = 1;
75        varargin(i) = [];
76    elseif strcmpi(varargin{i},'NoArchive')
77        ArchiveFlag = 0;
78        varargin(i) = [];
79    end
80end
81
82% t input
83if length(varargin) >= 1
84    if isnumeric(varargin{1})
85        t = varargin{1};
86        varargin(1) = [];
87    end
88end
89
90% Look for BPMx family info
91if length(varargin) >= 1
92    if ischar(varargin{1})
93        BPMxFamily = varargin{1};
94        varargin(1) = [];
95        if length(varargin) >= 1
96            if isnumeric(varargin{1})
97                BPMxList = varargin{1};
98                varargin(1) = [];
99            end
100        end
101    else
102        if isnumeric(varargin{1})
103            BPMxList = varargin{1};
104            varargin(1) = [];
105        end
106    end
107end
108
109% Look for BPMz family info
110if length(varargin) >= 1
111    if ischar(varargin{1})
112        BPMzFamily = varargin{1};
113        varargin(1) = [];
114        if length(varargin) >= 1
115            if isnumeric(varargin{1})
116                BPMzList = varargin{1};
117                varargin(1) = [];
118            end
119        end
120    else
121        if isnumeric(varargin{1})
122            BPMzList = varargin{1};
123            varargin(1) = [];
124        end
125    end
126end
127
128% Look for FileName info
129if length(varargin) >= 1
130    if ischar(varargin{1})
131        FileName = varargin{1};
132    end
133end
134
135if isempty(t)
136    t = 0:Tsample:T;
137end
138
139% Make a row vector
140t = t(:)';
141
142% If scalar, create a vector
143if length(t) == 1
144    t = 0:Tsample:t;
145end
146
147%---------------------------Start of process
148fprintf('   Monitoring orbit and current for %.1f seconds\n', t(end)); drawnow;
149
150if isfamily('DCCT')
151    AM = getam({BPMxFamily, BPMzFamily, 'DCCT'}, {BPMxList, BPMzList,[]}, t, 'struct');
152    DCCT = AM{3};
153else
154    AM = getam({BPMxFamily, BPMzFamily}, {BPMxList, BPMzList}, t, 'struct');
155    DCCT = [];
156end
157BPM(1) = AM{1};
158BPM(2) = AM{2};
159
160BPM(1).NumberOfAverages = Navg;
161BPM(1).RawData = BPM(1).Data;
162BPM(1).DCCT = DCCT;
163BPM(1).CreatedBy = 'measbpmsigma';
164BPM(1).DataDescriptor = 'Standard Deviation';
165
166BPM(2).NumberOfAverages = Navg;
167BPM(2).RawData = BPM(2).Data;
168BPM(2).DCCT = DCCT;
169BPM(2).CreatedBy = 'measbpmsigma';
170BPM(2).DataDescriptor = 'Standard Deviation';
171
172
173tout = BPM(1).tout;
174Mx = BPM(1).Data;
175% retrieve first value
176for i = 1:size(Mx,2)
177    Mx(:,i) = Mx(:,i) - BPM(1).Data(:,1);
178end
179
180clf reset
181subplot(2,2,1);
182plot(tout, Mx);
183grid on;
184%title(sprintf('BPM Data (%s)', datestr(BPM(1).TimeStamp)))
185xlabel('Time [Seconds]');
186ylabel('Horizontal Position [mm]');
187
188tout = BPM(2).tout;
189My = BPM(2).Data;
190for i = 1:size(My,2)
191    My(:,i) = My(:,i) -  BPM(2).Data(:,1);
192end
193
194subplot(2,2,3);
195plot(tout, My);
196grid on;
197xlabel('Time [Seconds]');
198ylabel('Vertical Position [mm]');
199
200% Warn if the measurement did not keep in time step with t
201tmeas = tout-t;
202if any(tmeas(1:end-1) > 1.05*diff(t))
203    fprintf('   WARNING: The time allotted for getting data is too small\n');
204end
205
206% Change the definition of .data to the standard deviation
207BPM(1).t = BPM(1).t(1);
208BPM(1).tout = BPM(1).tout(1);
209BPMx = BPM(1).Data;
210BPMz = BPM(2).Data;
211
212% Compute the standard deviation
213if 1   
214    % Definition of standard deviations
215    BPM(1).Data = std(BPM(1).Data,0,2);
216    BPM(2).Data = std(BPM(2).Data,0,2);   
217else   
218    % Low frequency drifting increases the STD.  For many purposes, like LOCO,
219    % this is not desireable.  Using difference orbits mitigates the drift problem.
220    Mx = BPMx;
221    for i = 1:size(Mx,2)-1
222        Mx(:,i) = Mx(:,i+1) - Mx(:,i);
223    end
224    Mx(:,end) = [];
225     
226    My = BPMz;
227    for i = 1:size(My,2)-1
228        My(:,i) = My(:,i+1) - My(:,i);
229    end
230    My(:,end) = [];
231     
232    BPM(1).Data = std(Mx,0,2) / sqrt(2);   % sqrt(2) comes from substracting 2 random variables
233    BPM(2).Data = std(My,0,2) / sqrt(2);
234   
235end
236
237subplot(2,2,2);
238plot(BPM(1).Data);
239grid on;
240xlabel('BPM Number');
241ylabel('Horizontal STD [mm]');
242
243subplot(2,2,4);
244plot(BPM(2).Data);
245grid on;
246xlabel('BPM Number');
247ylabel('Vertical STD [mm]');
248addlabel(.5,1,sprintf('BPM Data (%s)', datestr(BPM(1).TimeStamp)), 10);
249orient landscape
250
251if ArchiveFlag | ~isempty(FileName)
252    if ~isempty(FileName)
253        FileNameArchive = FileName;
254    else
255        FileNameArchive = appendtimestamp('BPMSigma', BPM(1).TimeStamp);
256        DirectoryName = getfamilydata('Directory','BPMData');
257        FileNameArchive = [DirectoryName FileNameArchive];
258    end
259    fprintf('   BPM sigma data structure saved to %s.mat\n', FileNameArchive);
260    BPMxSigma = BPM(1);
261    BPMzSigma = BPM(2);
262    save(FileNameArchive, 'BPMxSigma', 'BPMzSigma');
263    %save(FileNameArchive, 'BPM');
264else
265    FileNameArchive = [];
266end
267
268if StructOutputFlag
269    % Output variables
270    varargout{1} = BPM(1);   
271    varargout{2} = BPM(2);   
272    varargout{3} = FileNameArchive;
273else
274    % Output variables
275    varargout{1} = BPM(1).Data;
276    varargout{2} = BPM(2).Data;
277    varargout{3} = BPMx;
278    varargout{4} = BPMz;
279    varargout{5} = tout;
280    varargout{6} = DCCT;
281    varargout{7} = FileNameArchive;
282end
Note: See TracBrowser for help on using the repository browser.