source: MML/trunk/machine/SOLEIL/common/getturnbyturnposition.m

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

To have a stable version on the server.

  • Property svn:executable set to *
File size: 14.1 KB
Line 
1function varargout = getturnbyturnposition(varargin)
2% getturnbyturnposition - Gets turn by turn data for  BPM
3%
4%  INPUTS
5%  1. Device List bpm number (scalar or vector) list ([] means all valid BPM)
6%
7%  Optional input arguments
8%  2. Optional display
9%     'Display'   - Plot BPM data X,Z, Sum, Q
10%     'NoDisplay' - No plotting
11%  3. 'NoArchive' - No file archive {Default}
12%     'Archive'   - Save a dispersion data structure to \<Directory.BPMData>\<DispArchiveFile><Date><Time>.mat
13%                   To change the filename, included the filename after the 'Archive', '' to browse
14%                   Structure output  is forced
15%  4. 'Struct'    - Return out as a structure
16%  5. 'Freezing'  - Buffer freezing mechanism
17%     'NoFreezing'
18%  6. 'Group'  - TAngo group mecanism
19%     'NoGroup'
20%
21%  7. 'XZSumFlag' - Select only X Z and Sum signals
22%
23%  OUTPUTS
24%  structure output if 'Struct' precised
25%  AM
26%
27%  vector output
28%  1. X - Horizontal data
29%  2. Z - Vertical data
30%
31%  EXAMPLES
32%  1. Display BPM 18
33%      getturnbyturnposition(18)
34%  2. Display all valid BPM and output data as a structure
35%      getturnbyturnposition([],'Struct');
36%  3. Output all valid BPM data
37%      [X Z Sum Q Va Vb Vc Vd] = getturnbyturnposition([],'NoDisplay');
38%  4. Archives BPM 17 and 18 w/o displaying
39%     getturnbyturnposition([17; 18],'Archive','NoDisplay');
40%  5. Archives BPM 17 and 18 w/o displaying w/ buffer freezing mechanism
41%     getturnbyturnposition([17; 18],'Archive','NoDisplay','Freezing');
42%  6. Idem via devicelist
43%     getturnbyturnposition([17 1; 18 1],'Archive','NoDisplay','Freezing');
44%
45% See Also anabpmfirstturn
46
47%
48% Written by Laurent S. Nadolski
49% 17 May 2006: group added
50
51% TODO freezing mechanism
52OldLiberaFlag = 0; % Booster version
53DisplayFlag   = 1;
54ArchiveFlag   = 0;
55StructureFlag = 0;
56FreezingFlag  = 0;
57GroupFlag     = 1;
58XZSumFlag     = 0;
59FileName      = '';
60varargin2     = {};
61
62if ~exist('DeviceName','var')
63    DeviceName = [];
64end
65
66for i = length(varargin):-1:1
67    if strcmpi(varargin{i},'Display')
68        DisplayFlag = 1;
69        varargin2 = {varargin2{:} varargin{i}};
70        varargin(i) = [];
71    elseif strcmpi(varargin{i},'NoDisplay')
72        DisplayFlag = 0;
73        varargin2 = {varargin2{:} varargin{i}};
74        varargin(i) = [];
75    elseif strcmpi(varargin{i},'Group')
76        GroupFlag = 1;
77        varargin(i) = [];
78    elseif strcmpi(varargin{i},'NoGroup')
79        GroupFlag = 0;
80        % Marie-Agnes modification 23 mai 2006
81        varargin2 = {varargin2{:} varargin{i}};
82        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83        varargin(i) = [];
84    elseif strcmpi(varargin{i},'Archive')
85        ArchiveFlag = 1;
86        StructureFlag = 1;
87%        varargin2 = {varargin2{:} varargin{i}};
88        varargin(i) = [];
89    elseif strcmpi(varargin{i},'NoArchive')
90        ArchiveFlag = 0;
91%        varargin2 = {varargin2{:} varargin{i}};
92        varargin(i) = [];
93    elseif strcmpi(varargin{i},'Struct')
94        StructureFlag = 1;
95        varargin2 = {varargin2{:} varargin{i}};
96        varargin(i) = [];
97    elseif strcmpi(varargin{i},'Freezing')
98        FreezingFlag = 1;
99        varargin(i) = [];
100    elseif strcmpi(varargin{i},'NoFreezing')
101        FreezingFlag = 0;
102        varargin(i) = [];
103    elseif strcmpi(varargin{i},'XZSum')
104        XZSumFlag = 1;
105        varargin(i) = [];
106    end
107end
108
109Machine = getfamilydata('SubMachine');
110
111switch Machine
112    case 'OldBooster'  % Modif Alex shuntant l'ancien mode BPM booster 28-08-06
113        OldLiberaFlag = 1;
114    otherwise
115        OldLiberaFlag = 0;
116end
117
118AO = getfamilydata('BPMx');
119% if empty select all valid BPM
120
121if isempty(varargin)
122    num = 1:length(AO.DeviceName);
123    DeviceList = family2dev('BPMx');
124else
125    DeviceList = varargin{1};
126    if size(DeviceList,2) == 2 % DeviceList
127        %%
128    else %% Element list       
129        DeviceList = elem2dev('BPMx',DeviceList);
130    end   
131end
132
133% Status one devices
134Status     = family2status('BPMx',DeviceList);
135DeviceList = DeviceList(find(Status),:);
136
137if isempty(DeviceList)
138    disp('All BPM not valid')
139    AM = -1;
140    return;
141end
142
143%
144% % read BPM number of samples
145% % If all not the same stop program and ask action to user
146% for k = 1:length(num)
147%     modestr{k} = char(readattribute([AO.DeviceName{num(k)} '/Mode']));
148% end
149%
150% for k = 1:length(num)
151%     sampleNumber(k) = readattribute([AO.DeviceName{num(k)} '/NumSamples']);
152% end
153%
154% notTheSame = sampleNumber-sampleNumber(1);
155%
156% if ~all(strcmp(modestr,modestr{1}))
157%    disp([mfilename ' STOP!']);
158%    error('Not all BPM in the same mode')
159%    return;
160% end
161%
162% if (sum(notTheSame) ~= 0)
163%    disp([mfilename ' STOP!']);
164%    error('Not all BPM with the same number of samples')
165%    return;
166% end
167
168%% Buffer freezing
169% Enable freezing mechanism
170
171
172if GroupFlag
173    if ~XZSumFlag
174        attr_name = ...
175            {'XPosDD','ZPosDD', 'QuadDD', 'SumDD', ...
176            'VaDD', 'VbDD', 'VcDD', 'VdDD'};
177    else
178        attr_name = {'XPosDD','ZPosDD', 'SumDD'};
179    end
180
181    GroupId = tango_group_id('BPM');
182
183    DeviceListFull = family2dev('BPMx',0);
184    tango_group_disable_device2(GroupId, dev2tangodev('BPMx',DeviceListFull));
185    tango_group_enable_device2(GroupId, dev2tangodev('BPMx',DeviceList));
186    %rep = tango_group_read_attributes(GroupId,attr_name,0);
187
188    % Stimulate connexion TANGO bug
189    tango_group_ping(GroupId);
190    rep = tango_group_read_attributes(GroupId,attr_name,0);
191   
192    if tango_error == -1
193        tango_print_error_stack
194        return,
195    end
196   
197    if rep.has_failed
198        disp('Error when reading data for BPM');
199        for k = 1:length(rep.dev_replies),
200            if rep.dev_replies(k).has_failed
201                fprintf('Error with device %s\n',rep.dev_replies(k).dev_name)
202                tango_print_error_stack_as_it_is(rep.dev_replies(k).attr_values(1).error)
203            end
204        end       
205        error('Programme %s Stopped', mfilename);
206    else
207        vect = [];
208        for k = 1:length(rep.dev_replies),
209            % modification Marie-Agnes 23 mai 2006 c'est moche bien sur
210            vect = [vect ; rep.dev_replies(k).is_enabled];
211           
212            if rep.dev_replies(k).is_enabled
213                %rep.dev_replies(k).attr_values(1).dev_name;
214                AM.DeviceName{k} = rep.dev_replies(k).attr_values(1).dev_name;
215                AM.Data.X(k,:)   = rep.dev_replies(k).attr_values(1).value;
216                AM.Data.Z(k,:)   = rep.dev_replies(k).attr_values(2).value;
217                AM.Data.Sum(k,:) = rep.dev_replies(k).attr_values(4).value;
218                if ~XZSumFlag
219                    AM.Data.Q(k,:)   = rep.dev_replies(k).attr_values(3).value;
220                    AM.Data.Va(k,:)  = rep.dev_replies(k).attr_values(5).value;
221                    AM.Data.Vb(k,:)  = rep.dev_replies(k).attr_values(6).value;
222                    AM.Data.Vc(k,:)  = rep.dev_replies(k).attr_values(7).value;
223                    AM.Data.Vd(k,:)  = rep.dev_replies(k).attr_values(8).value;
224                end
225                %%%%%%%%%%% modification
226                klast = k;
227                %%%%%%%%%%%%%%%%%%%%%%%%
228           
229                if DisplayFlag
230                    figure
231                    subplot(3,1,1)
232                    plot(AM.Data.X(k,:))
233                    ylabel('X (mm)')
234                    grid on
235
236                    subplot(3,1,2)
237                    plot(AM.Data.Z(k,:))
238                    ylabel('Z (mm)')
239                    grid on
240
241                    subplot(3,1,3)
242                    plot(AM.Data.Sum(k,:))
243                    ylabel('SUM')
244                    xlabel('turn number')
245                    grid on
246                    addlabel(1,0,datestr(clock));
247                    suptitle(sprintf('Turn by turn data for %s',AM.DeviceName{k}))
248                end
249               
250            end
251        end
252        % modification
253        vect = vect(1:klast);
254        AM.DeviceName(find(1 - vect)) = [];
255        AM.Data.X   = AM.Data.X(find(vect),:);
256        AM.Data.Z   = AM.Data.Z(find(vect),:);
257        AM.Data.Sum = AM.Data.Sum(find(vect),:);
258        if ~XZSumFlag
259            AM.Data.Q = AM.Data.Q(find(vect),:);
260            AM.Data.Va = AM.Data.Va(find(vect),:);
261            AM.Data.Vb = AM.Data.Vb(find(vect),:);
262            AM.Data.Vc = AM.Data.Vc(find(vect),:);
263            AM.Data.Vd = AM.Data.Vd(find(vect),:);
264        end
265        %%%%%%%%%%%%%%
266        AM.TimeStamp = datestr(now);
267        AM.DataDescriptor = ['Turn by turn data for ' getsubmachinename];
268        AM.CreatedBy = mfilename;
269        AM.DeviceList = DeviceList;
270    end
271else
272    %% loop of bpm list
273    if size(DeviceList,1) > 1
274        AM.DeviceList=[];
275        for k = 1:length(DeviceList)
276            AM0 = getturnbyturnposition(DeviceList(k,:),varargin2{:},'Struct');
277            try
278                AM.DeviceName{k} = AM0.DeviceName{:};
279                AM.Data.X(k,:) = AM0.Data.X(:); AM.Data.Z(k,:) = AM0.Data.Z(:);
280                AM.Data.Sum(k,:) = AM0.Data.Sum(:);
281                if ~XZSumFlag
282                    AM.Data.Q(k,:)  = AM0.Data.Q(:);
283                    AM.Data.Va(k,:) = AM0.Data.Va(:); AM.Data.Vb(k,:) = AM0.Data.Vb(:);
284                    AM.Data.Vc(k,:) = AM0.Data.Vc(:); AM.Data.Vd(k,:) = AM0.Data.Vd(:);
285                end
286                AM.TimeStamp = datestr(now);
287                AM.DataDescriptor = ['Turn by turn data for ' getsubmachinename];
288                AM.CreatedBy = mfilename;
289                AM.DeviceList = [AM.DeviceList; AM0.DeviceList];
290            catch
291                switch lasterr
292                    case 'Subscripted assignment dimension mismatch.'
293                        error('BPM do not have the same number of samples !!!\n see BPM %s', AM.DeviceName{k})
294                    otherwise
295                        fprintf('error %s\n', lasterr)
296                end
297            end
298        end
299
300        if (FreezingFlag)
301
302            % Disable freezing mechanism
303            if OldLiberaFlag
304                Disablecmd = 'DisableBufferFreezing';
305            else
306                Disablecmd = 'DisableDDBufferFreezing';
307            end
308
309            disp([mfilename ': disabling buffer freezing for BPM'])
310            for k = 1:length(DeviceList)
311                tango_command_inout2(DeviceName{k}, Disablecmd);
312            end
313        end
314
315    else
316        %% Loop for one BPM
317        AO = getfamilydata('BPMx');
318        DeviceName = family2tangodev('BPMx',DeviceList);
319
320        if OldLiberaFlag
321            attr_name = ...
322                {'XPosVector','ZPosVector', 'QuadVector', 'SumVector', ...
323                'VaVector', 'VbVector', 'VcVector', 'VdVector'};
324        else
325            if ~XZSumFlag
326                attr_name = ...
327                    {'XPosDD','ZPosDD', 'QuadDD', 'SumDD', ...
328                    'VaDD', 'VbDD', 'VcDD', 'VdDD'};
329            else
330                attr_name = {'XPosDD','ZPosDD', 'SumDD'};
331            end
332        end
333
334
335
336        rep = tango_read_attributes2(DeviceName{:},attr_name);
337
338        X   = rep(1).value;
339        Z   = rep(2).value;
340        Sum = rep(3).value;
341        if ~XZSumFlag
342            Q   = rep(4).value;
343            Va  = rep(5).value;
344            Vb  = rep(6).value;
345            Vc  = rep(7).value;
346            Vd  = rep(8).value;
347        end
348        %% Display part
349
350        if DisplayFlag
351            figure
352            subplot(3,1,1)
353            plot(X)
354            ylabel('X (mm)')
355            grid on
356
357            subplot(3,1,2)
358            plot(Z)
359            ylabel('Z (mm)')
360            grid on
361
362            subplot(3,1,3)
363            plot(Sum)
364            ylabel('SUM')
365            xlabel('turn number')
366            grid on
367
368            addlabel(1,0,datestr(clock));
369            suptitle(sprintf('Turn by turn data for %s',DeviceName{:}))
370        end
371
372        if StructureFlag
373           
374            AM.DeviceList = DeviceList;           
375            AM.DeviceName = DeviceName;
376            AM.Data.X   = X;
377            AM.Data.Z   = Z;
378            AM.Data.Sum = Sum;
379            if ~XZSumFlag
380                AM.Data.Q   = Q;
381                AM.Data.Va  = Va;
382                AM.Data.Vb  = Vb;
383                AM.Data.Vc  = Vc;
384                AM.Data.Vd  = Vd;
385            end
386            %time stamp of recording
387            AM.TimeStamp = datestr(now);
388            AM.DataDescriptor = ['Turn by turn data for ' getfamilydata('Machine')];
389            AM.CreatedBy  = mfilename;
390            AM.DeviceList = DeviceList;
391        end
392    end
393end
394if ArchiveFlag
395    % filling up data
396    % Archive data structure
397    if isempty(FileName)
398        FileName = appendtimestamp('BPMTurnByTurn');
399        DirectoryName = getfamilydata('Directory','BPMData');
400        if isempty(DirectoryName)
401            DirectoryName = [getfamilydata('Directory','DataRoot') 'BPM', filesep];
402        else
403            % Make sure default directory exists
404            DirStart = pwd;
405            [DirectoryName, ErrorFlag] = gotodirectory(DirectoryName);
406            cd(DirStart);
407        end
408        [DirectoryName FileName]
409        [FileName, DirectoryName] = uiputfile('*.mat', 'Select Dispersion File', [DirectoryName FileName]);
410        if FileName == 0
411            ArchiveFlag = 0;
412            disp('   BPM measurement canceled.');
413            FileName='';
414            return
415        end
416        FileName = [DirectoryName, FileName];
417    elseif FileName == -1
418        FileName = appendtimestamp(getfamilydata('Default', 'BPMArchiveFile'));
419        DirectoryName = getfamilydata('Directory','BPMData');
420        if isempty(DirectoryName)
421            DirectoryName = [getfamilydata('Directory','DataRoot') 'BPM', filesep];
422        end
423        FileName = [DirectoryName, FileName];
424    end
425
426    save(FileName,'AM');
427
428end
429
430if StructureFlag
431    varargout{1} = AM;
432else
433    if exist('AM','var') % not nice but it works
434        varargout{1} = AM.Data.X;   varargout{2}  =  AM.Data.Z;
435        varargout{3} = AM.Data.Sum;
436        if ~XZSumFlag
437            varargout{4}  =  AM.Data.Q;
438            varargout{5} = AM.Data.Va;  varargout{6}  =  AM.Data.Vb;
439            varargout{7} = AM.Data.Vc;  varargout{8}  =  AM.Data.Vd;
440        end
441    else
442        varargout{1} = X;   varargout{2} =   Z;
443        varargout{3} = Sum;
444        if ~XZSumFlag
445            varargout{4} =  Q;
446            varargout{5} = Va;  varargout{6} =  Vb;
447            varargout{7} = Vc;  varargout{8} =  Vd;
448        end
449    end
450end
Note: See TracBrowser for help on using the repository browser.