source: MML/trunk/machine/SOLEIL/StorageRing/orbit/bpmgui.m @ 17

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

To have a stable version on the server.

  • Property svn:executable set to *
File size: 36.1 KB
Line 
1function bpmgui(action, varargin)
2% function varargout = bpmgui(action, varargin)
3%  bpmgui controls orbit manipulation and plotting
4%  graphics handles
5%
6% INPUT
7% 1. action possible among
8%
9% GetRef
10% UpdateRef
11% GetAct
12% UpdateAct
13% ylimits
14% PlotRef_Init
15% PlotRef
16% PlotAct_Init
17% PlotAct
18% PlotFit_Init
19% PlotFit
20% PlotDes_Init
21% PlotDes
22% ClearOffsets
23% PlotIcons_Init
24% PlotBPMs
25% PlotResp_Init
26% PlotResp
27% PlotEig_Init
28% PlotEig
29% BPMSelect
30% BPMBar
31% BPMDown
32% ProcessBPM
33% MoveBPM
34% Up
35% EditDesOrb
36% EditBPMWeight
37% SetBPMOff
38% UpdateBPMBox
39% BPMBox
40% RePlot
41% ClearPlots
42% SelectAll
43% SelectNone
44% ToggleMode
45% ToggleMode
46% DragMode
47% ShowBPMState
48%
49%
50
51%
52% Written By Jeff Corbett
53% Modified by Laurent S. Nadolski, Soleil, April 2004
54
55global BPM COR RSP SYS
56orbfig = findobj(0,'tag','orbfig');
57plane  = SYS.plane;
58
59% BPMfamilies = {'BPMx','BPMz'};
60
61switch action
62    %...GetRef//YLimits
63    %...PlotRef//PlotAct_Init//PlotAct//PlotFit_Init//PlotFit
64    %...PlotDes_Init//Plot_Des//Plot_BPMs_Init//PlotBPMs
65    %...PlotResp_Init//PlotResp//PlotEig_Init//PlotEig
66    %...BPMDown//ProcessBPM//MoveBPMUp
67    %...EditBPM//SetBPMOff//BPMBox//Up
68    %...RePlot//ClearPlot
69    %...SelectAll//SelectNone
70    %...ToggleMode//DragMode//ShowBPMState
71
72    %=============================================================
73    case 'GetRef'                                 % *** GetRef ***
74        %=============================================================
75        %load present orbit into .ref field of BPM structure
76
77        % H-plane
78        BPM(1).ref = BPM(1).act;
79        % V-plane
80        BPM(2).ref = BPM(2).act;
81       
82        setappdata(0,'BPM',BPM);
83
84        %=============================================================
85    case 'UpdateRef'                           % *** UpdateRef ***
86        %=============================================================
87        %load a simulated or measured reference orbit into BPM structure
88        %load a copy into the .des field
89        bpmgui('GetRef'); 
90        %BPM=getappdata(0,'BPM');
91
92        % H-plane
93        BPM(1).act = BPM(1).ref;
94        BPM(1).des = BPM(1).ref;
95        BPM(1).abs = BPM(1).ref;
96        % V-plane
97        BPM(2).act = BPM(2).ref;
98        BPM(2).des = BPM(2).ref;
99        BPM(2).abs = BPM(2).ref;
100
101        if SYS.relative == 1   %absolute mode
102            BPM(1).abs = BPM(1).abs*0;
103            BPM(2).abs = BPM(2).abs*0;
104        end
105
106        setappdata(0,'BPM',BPM);
107
108        orbgui('LBox',' Refresh Reference Orbit: ');
109
110        orbgui('RefreshOrbGUI');
111
112        %=============================================================
113    case 'GetAct'                             % *** GetAct ***
114        %=============================================================
115        %acquire orbit, load into .act field
116
117        % H-plane
118        val = getam(BPM(1).AOFamily,[],SYS.mode);
119        BPM(1).act(BPM(1).status) = val;
120        % V-plane
121        val = getam(BPM(2).AOFamily,[],SYS.mode);
122        BPM(2).act(BPM(2).status) = val;
123
124        %% Variable stored in the workspace memory
125        setappdata(0,'BPM',BPM);     
126
127        %=============================================================
128    case 'UpdateAct'                           % *** UpdateAct ***
129        %=============================================================
130        %read new orbit, update graphics
131
132        orbgui('LBox',' Refresh Orbit ');
133        %         disp('bpm acquisition');
134        %         tic;
135        if ~strcmp(SYS.mode,'Simulator')
136            pause(2);
137        end
138        bpmgui('GetAct');          %loads both planes (300 ms in simulator)
139        %         toc;
140
141        %         disp('Corrector acquisition');
142        %         tic;
143        corgui('GetAct');          %loads both planes (200 ms in simulator)
144        %         toc;
145
146        orbgui('RefreshOrbGUI');
147
148        %=============================================================
149    case 'ylimits'                             % *** ylimits ***
150        %=============================================================
151       
152        %set vertical axis limits for BPM plot
153        if BPM(plane).scalemode == 0
154            return;
155        end  %manual mode
156
157        orbgui('AutoScaleBPMAxis');
158%         mxref = max(abs(BPM(plane).ref(BPM(plane).status)-BPM(plane).abs(BPM(plane).status)));        %for ylimit calculation
159%         mxdes = max(abs(BPM(plane).des(BPM(plane).status)-BPM(plane).abs(BPM(plane).status)));        %for ylimit calculation
160%         mxact = max(abs(BPM(plane).act(BPM(plane).status)-BPM(plane).abs(BPM(plane).status)));        %for ylimit calculation
161%         mxfit = max(abs((BPM(plane).act(BPM(plane).avail)-BPM(plane).abs(BPM(plane).avail)+...
162%             BPM(plane).fit(BPM(plane).avail))));
163%               
164%         ylim = max([mxref mxdes mxact mxfit])*1.1;
165%
166%         % TODO if limimit y-scale wanted
167%         %units are mm
168% %         if ylim < 0.01 ylim = 0.01; end
169%
170%         ylim = ylim *BPM(plane).scale;
171%         
172%         set(SYS.ahbpm,'YLim',ylim*[-1, 1]);
173
174        %==========================================================
175    case 'PlotRef_Init'                        %...PlotRef_Init
176        %==========================================================
177        %red, solid line for reference orbit.
178        %use .iref field
179        set(SYS.ahbpm,'Color',[1 1 1],'NextPlot','add');
180        set(orbfig,'currentaxes',SYS.ahbpm)
181
182        yd = BPM(plane).ref(BPM(plane).iref)-BPM(plane).abs(BPM(plane).iref);
183        yd = yd *BPM(plane).scale;
184       
185        xd = orbgui('GetAbscissa',SYS,'BPM');  %horizontal coordinates for plotting
186        xd = xd(BPM(plane).iref);
187
188        SYS.lhref=      line('parent',SYS.ahbpm,'XData',xd, ...
189            'YData',yd,'Color','r','Tag', 'Ref');
190        set(SYS.lhref,'ButtonDownFcn','bpmgui(''BPMSelect'');');
191
192        setappdata(0,'SYS',SYS);
193
194        %==========================================================
195    case 'PlotRef'                        %...PlotRef
196        %==========================================================
197        %red, solid, static line for reference orbit.
198        %use .iref field
199        bpmgui('ylimits');
200        set(orbfig,'currentaxes',SYS.ahbpm)
201
202        xd = orbgui('GetAbscissa',SYS,'BPM');  %horizontal coordinates for plotting
203        xd = xd(BPM(plane).iref);
204
205        yd = BPM(plane).ref(BPM(plane).iref) - BPM(plane).abs(BPM(plane).iref);
206        yd = yd*BPM(plane).scale;
207       
208        set(SYS.lhref,'LineWidth',1.25,'XData',xd,'YData',yd);
209
210        setappdata(0,'SYS',SYS);
211
212        %==========================================================
213    case 'PlotAct_Init'                        %...PlotAct_Init
214        %==========================================================
215        %blue, solid dynamic line for actual orbit.
216        %use .status field
217        bpmgui('ylimits');
218        set(SYS.ahbpm,'Color',[1 1 1],'NextPlot','add');
219        set(orbfig,'currentaxes',SYS.ahbpm)
220
221        SYS.lhact = line('parent',SYS.ahbpm,'XData',[], ...
222            'YData',[],'Color','b','ButtonDownFcn', ...,
223            'bpmgui(''BPMSelect'');','Tag', 'Act');
224
225        setappdata(0,'SYS',SYS);
226
227        %==========================================================
228    case 'PlotAct'                             %...PlotAct
229        %==========================================================
230        %blue, solid dynamic line for actual orbit.
231        %use .status field
232        bpmgui('ylimits');
233        set(orbfig,'currentaxes',SYS.ahbpm)
234        xd = orbgui('GetAbscissa',SYS,'BPM');  %horizontal coordinates for plotting
235        xd = xd(BPM(plane).status);
236
237        yd = BPM(plane).act(BPM(plane).status)-BPM(plane).abs(BPM(plane).status);
238        yd = yd *BPM(plane).scale;
239       
240        set(SYS.lhact,'LineWidth',1.2,'XData',xd,'YData',yd);
241
242        %==========================================================
243    case 'PlotFit_Init'                        %...PlotFit_Init
244        %==========================================================
245        %blue, dashed line for orbit fit
246        %use .avail field
247        bpmgui('ylimits');
248        set(SYS.ahbpm,'Color',[1,1,1],'NextPlot','add');
249        set(orbfig,'currentaxes',SYS.ahbpm)
250        SYS.lhfit=      line('parent',SYS.ahbpm,'XData',[], ...
251            'YData',[],'Color','b','LineStyle',':','Tag', 'Fit');
252
253        setappdata(0,'SYS',SYS);
254
255        %==========================================================
256    case 'PlotFit'                             %...PlotFit
257        %==========================================================
258        %blue, dashed line for orbit fit
259        %use .avail field
260        set(orbfig,'currentaxes',SYS.ahbpm)
261
262        %actual orbit plus the fitted solution from corrector change
263        %yields the predicted orbit
264        yd = (BPM(plane).act(BPM(plane).avail) - ...
265              BPM(plane).abs(BPM(plane).avail) + ... %zero if absolute, ref else
266              BPM(plane).fit(BPM(plane).avail) + ...
267              BPM(plane).rffit(BPM(plane).avail));
268        yd = yd*BPM(plane).scale;
269       
270        xd = orbgui('GetAbscissa',SYS,'BPM');  %horizontal coordinates for plotting
271        xd = xd(BPM(plane).avail);
272               
273        %% Update graph
274        set(SYS.lhfit,'XData',xd,'YData',yd);
275        set(SYS.lhfit,'ButtonDownFcn','bpmgui(''BPMSelect'');');
276        bpmgui('ylimits');
277
278        %==========================================================
279    case 'PlotDes_Init'                        %...PlotDes_Init
280        %==========================================================
281        %red, dashed dynamic line for offset orbit
282        %use .avail field
283
284        yd = BPM(plane).des(BPM(plane).avail) - BPM(plane).abs(BPM(plane).avail);
285        yd = yd*BPM(plane).scale;
286       
287        xd = orbgui('GetAbscissa',SYS,'BPM');  %horizontal coordinates for plotting
288        xd = xd(BPM(plane).avail);
289       
290        SYS.lhdes = line('parent',SYS.ahbpm,'XData',xd, ...
291            'YData',yd,'Color','r','LineStyle',':','Tag', 'Des');
292        set(SYS.lhdes,'ButtonDownFcn','bpmgui(''BPMSelect'');');
293
294        setappdata(0,'SYS',SYS);
295
296        bpmgui('ylimits');
297
298        %==========================================================
299    case 'PlotDes'                                  %...PlotDes
300        %==========================================================
301        % %red, dashed dynamic line for offset orbit
302        %use .avail field
303
304        yd = BPM(plane).des(BPM(plane).avail) - BPM(plane).abs(BPM(plane).avail);
305        yd = yd *BPM(plane).scale;
306       
307        xd = orbgui('GetAbscissa',SYS,'BPM');  %horizontal coordinates for plotting
308        xd = xd(BPM(plane).avail);
309     
310        set(SYS.lhdes,'XData',xd,'YData',yd);
311        bpmgui('ylimits');
312
313        %%==========================================================
314    case 'ClearOffsets'                        %...ClearOffsets
315        %==========================================================
316        %remove offsets in plane of interest
317        %use .avail field
318
319        id = BPM(plane).avail(1:length(BPM(plane).avail));
320        BPM(plane).des(id) = BPM(plane).ref(id)-BPM(plane).abs(id);
321       
322        %moves the icon on screen
323        set(BPM(plane).hicon(id),{'YData'},num2cell(BPM(plane).des));
324
325%         for ii = 1:length(BPM(plane).avail)
326%             id                 = BPM(plane).avail(ii);
327%             BPM(plane).des(id) = BPM(plane).ref(id)-BPM(plane).abs(id);
328%             b                  = BPM(plane).hicon(id);
329%             set(b,'YData',BPM(plane).des(id));   %moves the icon on screen
330%         end
331
332        setappdata(0,'BPM',BPM);
333
334        orbgui('RefreshOrbGUI');
335
336        %==========================================================
337    case 'PlotIcons_Init'                       %...PlotIcons_Init
338        %==========================================================
339        %draw initial 'hot' circles for each BPM - need to vectorize
340        selectbpm = 'bpmgui(''BPMDown'');';
341
342        %present plane
343        plane = SYS.plane;
344        xd    = orbgui('GetAbscissa',SYS,'BPM');  %horizontal coordinates for plotting
345
346        %black if not in status vector
347        for kk = 1:size(BPM(plane).name,1)
348            BPM(plane).hicon(kk) = line('parent',SYS.ahbpm, ...
349                'Tag', ['b' num2str(kk)],...
350                'XData',xd(kk),...
351                'YData',0,...
352                'Marker','o','MarkerSize',5,'MarkerFaceColor','k',...
353                'ButtonDownFcn',selectbpm);
354        end
355        BPM(plane).hicon          = BPM(plane).hicon(:);
356        BPM(1+mod(plane,2)).hicon = BPM(plane).hicon;
357
358        setappdata(0,'BPM',BPM);
359
360        %draw a vertical black line at selected BPM
361        ylim = get(SYS.ahbpm,'YLim');
362        SYS.lhbid =     line('parent',SYS.ahbpm,...
363            'XData',[xd(BPM(plane).id),xd(BPM(plane).id) + 0.001],...
364            'YData',[ylim(1),ylim(2)],'Color','k','Tag', 'Icons');
365        set(SYS.lhbid, 'ButtonDownFcn','bpmgui(''BPMSelect'');');
366
367        %==========================================================
368    case 'PlotBPMs'                             %...PlotBPMs
369        %==========================================================
370        %green if BPM contained in BPM(plane).ifit, otherwise yellow
371        %red if not available (no response matrix entry or no reference orbit)
372        %default hot bpms to black
373%         yd = BPM(plane).des - BPM(plane).abs;
374        yd = BPM(plane).act(BPM(plane).status) - BPM(plane).abs(BPM(plane).status);
375        yd = yd*BPM(plane).scale;
376       
377        xd = orbgui('GetAbscissa',SYS,'BPM');  %horizontal coordinates for plotting
378
379%         for kk = 1:size(BPM(plane).name,1)
380%             set(BPM(plane).hicon(kk),'Xdata',xd(kk),...
381%                 'YData',0,'MarkerSize',5,'MarkerFaceColor','k');
382%         end
383%         set(BPM(plane).hicon,'YData',0,'MarkerFaceColor','k');
384       
385        nstat = length(BPM(plane).status);
386        kk = (1:nstat)';
387        colorcell = repmat({'g'},nstat,1); %green for fit
388        colorcell(setdiff(kk,BPM(plane).ifit)) = {'y'};  %yellow for no fit
389        colorcell(setdiff(kk,BPM(plane).avail)) = {'r'};  %red for not available
390       
391        % set everything using cell structure instead of for-loop     
392        set(BPM(plane).hicon,{'XData'},num2cell(xd),{'YData'},num2cell(yd), ...
393            {'MarkerFaceColor'},colorcell);
394               
395%         for kk = 1:nstat
396%             k     = BPM(plane).status(kk);   %double index, BPM.status is compressed
397%             color = 'r';                 %red for not available
398%             if ~isempty(find(BPM(plane).avail == k)), color = 'y'; end   %yellow for available
399%             if isempty(BPM(plane).ifit)
400%                 color = 'y'; %yellow for no fit
401%             elseif ~isempty(find(BPM(plane).ifit == k))
402%                 color = 'g'; %green for fit
403%             end
404%             set(BPM(plane).hicon(k),...
405%                 'XData',xd(k),'YData',yd(k),...
406%                 'MarkerSize',5,'MarkerEdgeColor','k','MarkerFaceColor',color);
407%         end
408
409        %==========================================================
410    case 'PlotResp_Init'                      %...PlotResp_Init
411        %==========================================================
412        %black, solid dynamic line for response orbit
413        %use .avail field
414        bpmgui('ylimits');
415        set(SYS.ahbpm,'Color',[1,1,1],'NextPlot','add');
416        set(orbfig,'currentaxes',SYS.ahbpm);
417        SYS.lhrsp =     line('parent',SYS.ahbpm,'XData',[], ...
418            'YData',[],'Color','k','Tag', 'Resp');
419
420        setappdata(0,'SYS',SYS);
421
422        %==========================================================
423    case 'PlotResp'                             %...PlotResp
424        %==========================================================
425        %black, solid static line for column of response matrix
426        %use .avail field
427
428        id = COR(plane).id;
429
430        set(orbfig,'currentaxes',SYS.ahbpm)
431
432        ylim = get(SYS.ahbpm,'YLim');
433       
434        %scale column of response matrix
435        val  = 0.5*ylim(2)/max(abs(RSP(plane).Data(BPM(plane).avail,id)));
436                                 
437        yd   = val*RSP(plane).Data(BPM(plane).avail,id);
438       
439        xd   = orbgui('GetAbscissa',SYS,'BPM');  %horizontal coordinates for plotting
440
441        set(SYS.lhrsp,'LineWidth',1.5,'XData',xd,'YData',yd);
442
443        %==========================================================
444    case 'PlotEig_Init'                        %...PlotEig_Init
445        %==========================================================
446        %black, solid dynamic line for eigenvector orbit
447        %use .iavail field for default
448        bpmgui('ylimits');
449        set(SYS.ahbpm,'Color',[1,1,1],'NextPlot','add');
450        set(orbfig,'currentaxes',SYS.ahbpm)
451        SYS.lheig=      line('parent',SYS.ahbpm,'XData',[], ...
452            'YData',[],'Color','k','Tag', 'Eig');
453
454        setappdata(0,'SYS',SYS);
455
456        %==========================================================
457    case 'PlotEig'                                  %...PlotEig
458        %==========================================================
459        %black, solid static line for eigenvector orbit
460        %use .ifit field
461
462        switch SYS.algo
463
464            case 'SVD'
465                ieig = RSP(plane).nsvd;
466
467                if ieig == 0, return; end
468
469                set(orbfig,'currentaxes',SYS.ahbpm);
470
471                ylim = get(SYS.ahbpm,'YLim');
472                val  = 0.5*ylim(2)/max(abs(RSP(plane).U(ieig,:)));   %columns are .ifit bpms
473                yd   = val*RSP(plane).U(ieig,:);                               
474                xd   = orbgui('GetAbscissa',SYS,'BPM');  %horizontal coordinates for plotting
475                set(SYS.lheig,'LineWidth',1.5,'XData',xd,'YData',yd);               
476
477        end   %end case
478
479        %=============================================================
480    case 'BPMSelect'                          % *** BPMSelect ***
481        %=============================================================
482        %used if mouse clicks anywhere in BPM window
483        % Get mouse position
484        cpa = get(SYS.ahbpm,'CurrentPoint');
485        pos = cpa(1);
486
487        %% Searchs closest BPM
488       
489        switch SYS.xscale
490            case 'meter'
491                id = find(min(abs(BPM(plane).s-pos)) == abs(BPM(plane).s-pos));
492            case 'phase'
493                id = find(min(abs(BPM(plane).phi-pos)) == abs(BPM(plane).phi-pos));
494        end
495
496        BPM(plane).id = id;
497
498        %% Stores updated BPM structure
499        setappdata(0,'BPM',BPM);
500
501        b = BPM(plane).hicon(id); % handle for round symbol
502
503        % Set black cross in the BPM window
504        bpmgui('BPMBar');
505
506        bpmgui('ProcessBPM',id,b);
507
508        %=============================================================
509    case 'BPMBar'                              % *** BPMBar ***
510        %=============================================================
511        %move black line in window to indicate selection
512        id   = BPM(plane).id;
513        ylim = get(SYS.ahbpm,'YLim');
514
515        if isempty(find(BPM(plane).status == id,1))
516            yd = ylim/4*BPM(plane).scale;
517        else
518            yd = (BPM(plane).des(id) - BPM(plane).abs(id))*BPM(plane).scale ...
519                + ylim/4;
520        end
521
522        xd = orbgui('GetAbscissa',SYS,'BPM');  %horizontal coordinates for plotting
523        xd = [xd(id) xd(id) + 0.001];
524       
525        set(SYS.lhbid,'XData',xd,'YData',yd);
526
527        %=============================================================
528    case 'BPMDown'                              % *** BPMDown ***
529        %=============================================================
530        %used if mouse clicks directly on BPM
531        b     = gcbo;
532        bptag = get(b,'tag');
533        id    = str2double(bptag(2:length(bptag))); %strip off 'b' to get the bpm index
534        BPM(plane).id = id;
535
536        setappdata(0,'BPM',BPM);
537
538        bpmgui('ProcessBPM',id,b);
539
540        %=============================================================
541    case 'ProcessBPM'                            % *** ProcessBPM ***
542        %=============================================================
543        %put BPM data into text fields.
544        %if BPM.mode=1, display, toggle
545        %if BPM.mode=2, display, drag
546
547        %identify BPM handle and number
548        id = varargin{1}; %bpm number 'id'
549        b  = varargin{2}; %bpm graphics handle 'b'
550
551        %check to see if bpm available
552        if isempty(find(BPM(plane).avail == id,1)) %put fit value in offset box and in request box
553            bpmgui('UpdateBPMBox');
554            return;
555        end
556
557        %====================
558        %   TOGGLE MODE
559        %====================
560        if BPM(plane).mode == 1                                %BPMs in toggle mode
561
562            if isempty(BPM(plane).ifit)     %no BPMs chosen yet, this is the first one
563                BPM(plane).ifit(1) = id;
564                setappdata(0,'BPM',BPM);
565                set(b,'MarkerFaceColor','g');
566                set(orbfig,'WindowButtonUpFcn','bpmgui(''Up'')');
567                return
568            end
569
570            if ~isempty(find(BPM(plane).ifit == id,1))                   %BPM presently on for fit
571                set(b,'MarkerFaceColor','y');
572                BPM(plane).ifit(find(BPM(plane).ifit==id)) = 0;          %set element in list to zero
573                BPM(plane).ifit=BPM(plane).ifit(find(BPM(plane).ifit));  %compress list
574                BPM(plane).ifit=BPM(plane).ifit(:);                      %create column vector
575                setappdata(0,'BPM',BPM);
576
577            elseif isempty(find(BPM(plane).ifit == id, 1)) && ...
578                    ~isempty(find(BPM(plane).avail == id, 1))     %BPM presently not on for fit
579                set(b,'MarkerFaceColor','g');
580                t=BPM(plane).ifit(:);
581
582                %disp(['ID = ' num2str(id)]);
583                t=sort([t; id]);
584                BPM(plane).ifit=t;
585                setappdata(0,'BPM',BPM);
586            end
587            set(orbfig,'WindowButtonUpFcn','bpmgui(''Up'')')
588
589
590            %====================
591            %   DRAG MODE
592            %====================
593        elseif BPM(plane).mode == 2                                    %BPMs in drag mode
594            selectbpm = ['bpmgui(''MoveBPM'',' num2str(b) ', ' num2str(id) ');'];
595            set(orbfig,'WindowButtonMotionFcn',selectbpm);
596            set(orbfig,'WindowButtonUpFcn','bpmgui(''Up'')');
597        end    %...end mode=2 (drag) condition
598
599        bpmgui('UpdateBPMBox');
600
601        %=============================================================
602    case 'MoveBPM'                              % *** MoveBPM ***
603        %=============================================================
604        %This function is called each time the user moves the mouse in 'down' mode
605
606        %reset the y coordinate of the data point on the dashed plot.
607        %...plot_ydata = get(SYS.desorb, 'YData');
608        %plot_ydata(ind) = pos(1,2);
609        %the catch: note bpm tags have index progressing
610        %linearly from start to finish independent of status.
611        %But plot_ydata has index that skips bpms with bad status.
612        %must look for position in status vector of the
613        %bpm with index ind as chosen by the drag command.
614
615%         b  = varargin{1};
616        id = varargin{2}; 
617        b  = BPM(plane).hicon(id);    %doesn't work without this line - round-off?
618
619
620        %reposition icon for BPM
621        pos = get(SYS.ahbpm, 'CurrentPoint');
622        yd  = pos(1,2);
623     
624        % Update ans save desired orbit
625        BPM(plane).des(id) = yd/BPM(plane).scale + BPM(plane).abs(id);
626        setappdata(0,'BPM',BPM);
627
628        set(b, 'YData', yd);  %move the BPM icon to mouse position
629
630        %re-plot red dotted line (desired orbit)
631        bpmgui('PlotDes');
632        bpmgui('BPMBar');
633        bpmgui('UpdateBPMBox');
634
635
636        %=============================================================
637    case 'Up'                                  % *** Up ***
638        %=============================================================
639        %mouse button is let up, don't respond to motion
640        set(orbfig,'WindowButtonMotionFcn','',...
641            'WindowButtonUpFcn','');
642        respgui('SolveSystem');
643        respgui('UpdateFit');
644        bpmgui('UpdateBPMBox');
645
646        %=============================================================
647    case 'EditDesOrb'                         %***      EditDesOrb ***
648        %=============================================================
649        h1 = SYS.bpmedit;   %handle of BPM edit box
650        if isempty(get(SYS.bpmname,'String'))
651            set(SYS.bpmedit,'String',num2str(0.0));
652            return;
653        end
654        id = BPM(plane).id;
655        b  = findobj(orbfig,'tag',['b' num2str(id)]);  %handle of bpm
656        offset = str2double(get(h1,'String'));
657
658        if isnan(offset) || ~isnumeric(offset) || ~length(offset) == 1
659            % flush the bad string out of the edit; replace with current value
660            offset = 0.0;
661            set(h1,'String',num2str(offset));
662            disp('Warning: Invalid offset entry.');
663            orbgui('LBox','Warning: Invalid offset entry');
664        end
665
666        bpmgui('SetBPMOff',id,b,offset);
667
668        orbgui('RefreshOrbGUI');
669
670        %=============================================================
671    case 'EditBPMWeight'                 %***   EditBPMWeight ***
672        %=============================================================
673        %check if no BPM selected
674        h1 = SYS.editbpmweight;   %handle of BPM weight edit box
675        if isempty(get(SYS.bpmname,'String'))
676            set(SYS.editbpmweight,'String',num2str(1.0));
677            return;
678        end
679
680        id     = BPM(plane).id;
681%         b      = findobj(orbfig,'tag',['b' num2str(id)]);  %handle of bpm
682        weight = str2double(get(h1,'String'));
683
684        if isnan(weight) || ~isnumeric(weight) || ~length(weight) == 1
685            % flush the bad string out of the edit; replace with current value
686            weight = 1.0;
687            set(h1,'String',num2str(weight));
688            disp('Warning: Invalid weight entry.');
689            orbgui('LBox','Warning: Invalid weight entry');
690        end
691
692        BPM(plane).wt(id) = weight;
693        setappdata(0,'BPM',BPM);
694
695        orbgui('RefreshOrbGUI');
696
697        %=============================================================
698    case 'SetBPMOff'                         %***       SetBPMOff ***
699        %=============================================================
700        %Callback of the BPM offset edit box.
701        %resets the dashed red line and BPM icon position.
702        %For details, see "BPMmove" above.
703        %calling sequence:bpmgui('SetBPMOff',id,b,offset);
704
705        %identify BPM handle and number
706        id     = varargin{1}; %bpm number 'id' comes through varargin
707        b      = varargin{2}; %bpm handle 'b' comes through varargin
708        offset = varargin{3}; % offset value
709       
710        % update and save BPM
711        BPM(plane).des(id) = BPM(plane).ref(id)-BPM(plane).abs(id) ...
712            + offset/BPM(plane).scale;     
713        setappdata(0,'BPM',BPM);
714
715        %moves the icon on screen
716        yd = BPM(plane).des(id);       
717        set(b,'YData',yd);   
718
719        % replot Desired orbit
720        bpmgui('PlotDes');
721
722        %=============================================================
723    case 'UpdateBPMBox'                     % *** UpdateBPMBox ***
724        %=============================================================
725        %update BPMBox
726        %called from respgui(SVDSlide),respgui(SVDEdit)
727        id = BPM(plane).id;
728        %check to see if bpm available
729        if isempty(find(BPM(plane).avail == id,1)) %put fit value in offset box and in request box
730            bpmgui('BPMBox',BPM(plane).name(id,:),BPM(plane).act(id),...
731                0,0,0,0,0,0,id);
732            return;
733        else
734            %compute desired offset from reference orbit
735            offset       = BPM(plane).des(id) - BPM(plane).ref(id);
736            %compute predicted fitted value
737            predictedval = BPM(plane).fit(id) + ...
738                (BPM(plane).act(id) - BPM(plane).abs(id));
739            %compute orbit mean
740            meanV = mean(BPM(plane).act(BPM(plane).status)-BPM(plane).abs(BPM(plane).status));
741            %compute orbit rms
742            rms   = std(BPM(plane).act(BPM(plane).status)-BPM(plane).abs(BPM(plane).status));
743            bpmgui('BPMBox',BPM(plane).name(id,:),BPM(plane).act(id),...
744                BPM(plane).ref(id),offset,BPM(plane).wt(id),BPM(plane).des(id),...
745                predictedval, meanV, rms, id)
746        end
747
748        %=============================================================
749    case 'BPMBox'                            % *** BPMBox ***
750        %=============================================================
751        %bpmgui('BPMBox',BPM(plane).name(id,:),BPM(plane).act(id),...
752        %                BPM(plane).ref(id),BPM(plane).des(id),fitval,id);
753        bpmname   = SYS.bpmname;
754        actual    = SYS.bpmact;
755        reference = SYS.bpmref;
756        offedit   = SYS.bpmedit;
757        wtedit    = SYS.editbpmweight;
758        desire    = SYS.bpmdes;
759        measval   = SYS.bpmmeas;
760        rmsval    = SYS.bpmrms;
761        meanval   = SYS.bpmmean;
762
763        name = varargin{1};
764        act  = varargin{2};
765        ref  = varargin{3};
766        off  = varargin{4};
767        wt   = varargin{5};
768        des  = varargin{6};
769        meas = varargin{7};
770        meanV= varargin{8};
771        rms  = varargin{9};
772        id   = varargin{10};
773
774        % conversion in device list
775        dev = elem2dev(BPM(plane).AOFamily,id);
776       
777        set(bpmname,  'String',[name '  [',num2str(dev(1)),' ', ...
778            num2str(dev(2)),']']);
779        set(actual,   'String',num2str(act*BPM(plane).scale,  '%6.3f'));
780        set(reference,'String',num2str(ref*BPM(plane).scale,  '%6.3f'));
781        set(offedit,  'String',num2str(off*BPM(plane).scale,  '%6.3f'));
782        set(wtedit,   'String',num2str(wt,                    '%6.3f'));
783        set(desire,   'String',num2str(des*BPM(plane).scale,  '%6.3f'));
784        set(measval,  'String',num2str(meas*BPM(plane).scale, '%6.3f'));
785        set(rmsval,   'String',num2str(rms*BPM(plane).scale,  '%6.3f'));
786        set(meanval,  'String',num2str(meanV*BPM(plane).scale,'%6.3f'));
787        set(offedit,  'UserData',id);
788
789        %=============================================================
790    case 'RePlot'                                 % *** RePlot ***
791        %=============================================================
792        %plot reference, desired, actual, icons
793        set(orbfig,'currentaxes',SYS.ahbpm)
794        %could replace these calls with PlotRef, PlotAct, etc
795
796        bpmgui('PlotRef');
797        bpmgui('PlotDes');
798        bpmgui('PlotAct');
799        bpmgui('PlotFit');
800        bpmgui('PlotBPMs');  %plots icons
801        bpmgui('ylimits');
802        bpmgui('BPMBar');
803
804        %==========================================================
805    case 'ClearPlots'                        %...ClearPlots
806        %==========================================================
807        %clear columns of response matrix, orbit-eigenvectors and fit plots.
808
809        set(orbfig,'currentaxes',SYS.ahbpm)
810        set(SYS.lhrsp,'XData',[],'YData',[]);
811        set(SYS.lheig,'XData',[],'YData',[]);
812        set(SYS.lhfit,'XData',[],'YData',[]);
813
814        %=============================================================
815    case 'SelectAll'                           % *** SelectAll ***
816        %============================================================
817        BPM(plane).ifit=[];
818        navail = length(BPM(plane).avail);
819       
820        set(BPM(plane).hicon(BPM(plane).avail),'MarkerFaceColor','g');
821       
822        BPM(plane).ifit(1:navail) = BPM(plane).avail(1:navail);
823       
824       
825%         for kk = 1:navail
826%             k    = BPM(plane).avail(kk);
827%             hbpm = BPM(plane).hicon(k);;
828%             set(hbpm,'MarkerFaceColor','g');
829%             BPM(plane).ifit(kk) = k;
830%         end
831
832        setappdata(0,'BPM',BPM);
833
834        orbgui('RefreshOrbGUI');
835
836        %=============================================================
837    case 'SelectNone'                           % *** SelectNone ***
838        %=============================================================
839        BPM(plane).ifit = [];
840       
841        set(BPM(plane).hicon(BPM(plane).avail),'MarkerFaceColor','y');
842               
843%         navail          = length(BPM(plane).avail);
844%         for kk = 1:navail
845%             k    = BPM(plane).avail(kk);
846%             hbpm = BPM(plane).hicon(k);
847%             set(hbpm,'MarkerFaceColor','y');
848%         end
849
850        setappdata(0,'BPM',BPM);
851
852        orbgui('RefreshOrbGUI');
853
854        %respgui('FitOff',num2str(plane));
855
856        %==========================================================
857    case 'ToggleMode'                      % *** ToggleMode ***
858        %==========================================================
859        %callback of the bpm toggle radio button
860        %radio button toggles state and then executes callback
861        %hence, this routine finds the new state
862
863        if get(SYS.togglebpm,'Value') == 0 &&  ...
864                get(SYS.dragbpm,'Value') == 0
865            BPM(plane).mode = 0;           %'0' for display only
866        else
867            set(SYS.dragbpm,'Value',0);
868            BPM(plane).mode = 1;           %'1' for toggle mode
869        end
870
871        setappdata(0,'BPM',BPM);
872
873        %==========================================================
874    case 'DragMode'                          % *** DragMode ***
875        %==========================================================
876        %callback of the bpm drag radio button
877        %radio button toggles state and then executes callback
878        %hence, this routine finds the new state
879
880        if get(SYS.togglebpm,'Value') == 0 &&  ...
881                get(SYS.dragbpm,'Value') == 0
882            BPM(plane).mode = 0;           %'0' for display only
883        else
884            set(SYS.togglebpm,'Value',0);
885            BPM(plane).mode = 2;           %'2' for drag mode
886        end
887
888        setappdata(0,'BPM',BPM);
889
890        %===========================================================
891    case 'ShowBPMState'                    %*** ShowBPMState ***
892        %===========================================================
893        % Show bpm settings
894        len = size(BPM(plane).name,1);
895        tavail  = zeros(len,1);
896        tfit    = zeros(len,1);
897        tstatus = zeros(len,1);
898     
899        idx = intersect(BPM(plane).avail,1:len);
900        tavail(idx) = idx;
901        idx = intersect(BPM(plane).ifit,1:len);
902        tfit(idx) = idx;
903        idx = intersect(BPM(plane).status,1:len);
904        tstatus(idx) = idx;
905     
906%         tic
907%         for ii = 1:size(BPM(plane).name,1)
908%             if ~isempty(find(BPM(plane).avail == ii))  tavail(ii)  = ii;  end
909%             if ~isempty(find(BPM(plane).ifit == ii))   tfit(ii)    = ii;  end
910%             if ~isempty(find(BPM(plane).status == ii)) tstatus(ii) = ii;  end
911%         end
912%         toc
913
914        ref  = BPM(plane).ref;
915        des  = BPM(plane).des;
916        babs = BPM(plane).abs;
917        act  = BPM(plane).act;
918        fit  = BPM(plane).fit;
919        wt   = BPM(plane).wt;
920
921        %%% BUILD UP CELL TO DISPLAY
922        ivec = (1:len)';       
923        Strcell = cell(11,len);
924        Strcell(1,:) = num2cell(ivec);
925        Strcell(2,:) = cellstr(BPM(plane).name(ivec,:))';
926        Strcell(3:end,:) = num2cell([tstatus(ivec),tavail(ivec),tfit(ivec),ref(ivec), ...
927            des(ivec),babs(ivec),act(ivec),fit(ivec),wt(ivec) ])';
928       
929        %% Speed issue
930        % in a workspace : 2.3s
931        % in a file : 0.04s
932        % in a file and edit in matlab : 0.5s first time then 0.1
933        % in a file and edit in nedit  : 0.06s
934       
935        filename = [SYS.localdata 'bpmdata.txt'];
936        fid = fopen(filename,'w');
937        fprintf(fid,'%s\n','index    name    stat  avail  ifit     ref        des          abs       act         fit       weight');
938        fprintf(fid,'%3d %10s %5d %5d %5d %10.3f %10.3f %12.3f %10.3f %10.3f %10.3f\n',...
939            Strcell{:});
940        fclose(fid)
941        % edit 'bpmdata.txt'       
942        system(['nedit ', filename, ' &']);  % much faster
943           
944%             tic
945%         for ii = 1:size(BPM(plane).name,1)
946%             fprintf(0,'%3d %10s %5d %5d %5d %10.3f %10.3f %12.3f %10.3f %10.3f %10.3f\n',...
947%                 ii, BPM(plane).name(ii,:),tstatus(ii),tavail(ii),tfit(ii),ref(ii), ...
948%                 des(ii),babs(ii),act(ii),fit(ii),wt(ii));
949%         end
950%         toc
951        %===========================================================
952    otherwise
953        disp(['Warning: no CASE found in bpmgui: ' action]);
954end  %end switchyard
Note: See TracBrowser for help on using the repository browser.