source: MML/trunk/applications/orbit/orbgui.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: 126.4 KB
Line 
1function varargout = orbgui(action, varargin)
2%ORBGUI -- Contains routines to set up the main orbit control panel
3%  for the 'orbit' program
4% function varargout = orbgui(action, varargin)
5%  orbgui contains routines to set up the main orbit control panel
6%  for the 'orbit' program
7%  routines contained in orbgui switchyard are:
8%
9% OrbFig
10% BPMAxes
11% LatticeAxes
12% CorAxes
13% MachineParameters
14% UpdateParameters
15% SVDAxes
16% PlotMenu
17% BPMChangeUnits
18% CORChangeUnits
19% LoadRaw2Real
20% AlgorithmMenu
21% SVDAlgo
22% SuperperiodMenu
23% BPMMenu
24% CORMenu
25% EtaMenu
26% SYSMenu
27% Switch2online
28% Switch2sim
29% OptMenu
30% SimMenu
31% Cor2Zero
32% Quad2Zero
33% QuadOffset
34% MakeNewRef
35% PlotAlign
36% SetTune
37% UpdateOptics
38% DisplayOpticsEXCEL
39% DisplayOptics
40% PlotBetaFunctions
41% PlotRingElements
42% RespMenu
43% UIControls
44% BPMbox
45% CorBox
46% SVDBox
47% KnobBox
48% Plane
49% TogglePlane
50% RefreshOrbGUI
51% Relative
52% Abort
53% ScaleBPMAxis
54% AutoScaleBPMAxis
55% AutoScaleCorrAxis
56% ScaleCORAxis
57% BPMPlotScale
58% ProcessBPMScale
59% PlotSPosition
60% PlotPhase
61% BPMScaleType
62% CorPlotScale
63% CORScaleType
64% ProcessCORScale
65% LstBox
66% LBox
67% GetAbscissa
68% InitialSaveSet
69% SaveSet2Initial
70% SaveSet
71% MoveSaveSet
72% RestoreSaveSet
73% UnDrawSaveSet
74% ReDrawSaveSet
75% DeleteSaveSet
76% SelectSaveSet
77% LoadSaveSet
78% RestoreSystem
79% SaveSystem
80% SaveBump
81% CloseMainFigure
82%
83
84%
85% Written by J. Corbett
86% Adapted by Laurent S. Nadolski
87
88% graphics handles
89
90% globals
91global BPM COR RSP SYS THERING
92orbfig = findobj(0,'tag','orbfig');
93plane  = SYS.plane; % 1 for H and 2 for V
94
95[BPMxFamily, BPMzFamily] = BPM.AOFamily;
96[HCORFamily, VCORFamily] = COR.AOFamily;
97
98switch action
99
100    %==========================================================
101    case 'OrbFig'                               %OrbFig
102    %==========================================================
103        %orbfig sets up the main figure for the Orbit program.
104        %orbit and corrector plots, uicontrols and uimenus fit into orbfig.
105        %to print graphics activate menubar
106        %PaperPosition sets orientation of figure on print page
107
108        %screen size call
109        [screen_wide, screen_high]=screensizecm;
110
111        %fig_start = [0.13*screen_wide 0.16*screen_high];
112        fig_start = [0.1*screen_wide 0.1*screen_high];
113        %fig_size  = [0.78125*screen_wide 0.78125*screen_high];
114        fig_size  = [0.8125*screen_wide 0.8*screen_high];
115        SYS.orbfig = figure('Visible','on',...
116            'units','centimeters',...
117            'NumberTitle','off',...
118            'Doublebuffer','on',...
119            'Tag','orbfig',...
120            'Name','SOLEIL Storage Ring Orbit Correction Interface v.1',...
121            'PaperPositionMode','Auto',...
122            'MenuBar','None', 'Position',[fig_start fig_size], ...
123            'Resize','off', 'CloseRequestFcn', ...
124            'orbgui(''CloseMainFigure'')');
125        %set(SYS.orbfig,'DeleteFcn',['st=fclose(''all'');' 'CloseCAQuery'],'Resize','off');
126
127%         %==========================================================
128%     case 'Axes_1'                               %Axes_1
129%         %==========================================================
130%         %Establish top axes
131%
132%         [screen_wide, screen_high] = screensizecm;
133%
134%         x0=0.0478*screen_wide; y0=0.5*screen_high; dx=0.7383*screen_wide; dy=0.2*screen_high;
135%
136%         % axe handle 1 = ah1
137%         SYS.ahbpm = axes('Units','centimeters',...
138%             'Color', [1 1 1], ...
139%             'Box','on',...
140%             'YGrid','on',...
141%             'Position',[x0 y0 dx dy]);
142%
143%         set(SYS.ahbpm,'xticklabelmode','manual');
144%         set(SYS.ahbpm,'xticklabel',[]);
145%         varargout{1} = SYS.ahbpm;
146
147        %==========================================================
148    case 'BPMAxes'                               %BPMAxes
149        %==========================================================
150        %BPMAxes is the axes to plot: reference, desired, actual, predicted
151        %columns of response matrix and svd orbit eigenvectors (columns of U: R=UWVt)
152        %need NextPlot add to prevent PLOT from resetting fields to default (incl tag)
153        %DrawMode
154
155        [screen_wide, screen_high] = screensizecm;
156
157        x0=0.0478*screen_wide; y0=0.5*screen_high;
158        dx=0.7383*screen_wide; dy=0.2*screen_high;
159
160        % axe handle 1 = ah1
161        SYS.ahbpm = axes('Units','centimeters',...
162            'Color', [1 1 1], ...
163            'Box','on',...
164            'YGrid','on',...
165            'Position',[x0 y0 dx dy]);
166
167        set(SYS.ahbpm,'xticklabelmode','manual');
168        set(SYS.ahbpm,'xticklabel',[]);
169
170        set(SYS.ahbpm,'NextPlot','add');
171        set(SYS.ahbpm,'ButtonDownFcn','bpmgui(''BPMSelect'')');
172        set(SYS.ahbpm,'xticklabelmode','manual');
173        set(SYS.ahbpm,'xticklabel',[]);
174
175        label = ['BPM Value  (',  getfamilydata(BPMxFamily,'Monitor','HWUnits'), ')'];
176
177        set(get(SYS.ahbpm,'Ylabel'),'string',label);  %handle for xlabel
178
179%         set(SYS.ahbpm,'NextPlot','add');
180%         set(SYS.ahbpm,'ButtonDownFcn','bpmgui(''BPMSelect'')');
181%         set(SYS.ahbpm,'xticklabelmode','manual');
182%         set(SYS.ahbpm,'xticklabel',[]);
183%
184%         label = ['BPM Value  (',  getfamilydata(BPMxFamily,'Monitor','HWUnits'), ')'];
185%
186%         set(get(SYS.ahbpm,'Ylabel'),'string',label);  %handle for xlabel
187
188       %==========================================================
189    case 'LatticeAxes'                                   %ZoomAxes
190        %==========================================================
191        %Position Axes is for slider that controls zoom of display windows
192
193        [screen_wide, screen_high] = screensizecm;
194
195        x0 = 0.0478*screen_wide; y0 = 0.473*screen_high;
196        dx = 0.7383*screen_wide; dy = 0.025*screen_high;
197
198        SYS.ahpos = axes('Units','centimeters',...
199            'Color', [1 1 1],...
200            'Box','on',...
201            'Position',[x0 y0 dx dy],...
202            'xticklabelmode','manual',...
203            'yticklabelmode','manual',...
204            'xtickmode','manual',...
205            'ytickmode','manual');
206
207        drawlattice;
208       
209        set(SYS.ahpos,'XTick',[],'YTick',[]); % to erase tick labels
210       
211        setappdata(0,'SYS',SYS);
212
213        %==========================================================
214    case 'CorAxes'                               %CorAxes
215        %==========================================================
216        %CorAxes are the axes to plot: actual, fit correctors
217        %and svd corrector eigenvectors (columns of V: R=UWVt)
218
219        [screen_wide, screen_high]=screensizecm;
220
221        x0=0.0478*screen_wide; y0=0.27*screen_high;
222        dx=0.7383*screen_wide; dy=0.2*screen_high;
223
224        %axe handle for correctors
225        SYS.ahcor = axes('Units','centimeters',...
226            'NextPlot','add',...
227            'Color', [1 1 1], ...
228            'Box','on',...
229            'YGrid','on',...
230            'Position',[x0 y0 dx dy],...
231            'ButtonDownFcn','corgui(''CorSelect'')');
232
233        label=['Corrector Value  (',  getfamilydata(HCORFamily,'Setpoint','HWUnits'), ')'];
234
235        set(get(SYS.ahcor,'Ylabel'),'string',label);  %handle for xlabel
236        set(get(SYS.ahcor,'Xlabel'),'string','Position in Storage Ring (m)');
237
238        setappdata(0,'SYS',SYS);
239
240        %==========================================================
241    case 'MachineParameters'              %...MachineParameters
242        %==========================================================
243        %Create field boxes to display beam energy and current
244        [screen_wide, screen_high] = screensizecm;
245
246        x0=0.4*screen_wide; y0=0.76*screen_high; dx=0.08*screen_wide; dy=0.02*screen_high;
247
248        uicontrol('Style','text',...                            %Current Text
249            'units', 'centimeters','Position',[x0+0*dx y0 dx dy],...
250            'HorizontalAlignment','right',...
251            'ToolTipString','Electron Beam Current (A)',...
252            'String','Current (mA)');
253
254        SYS.hcurrent=uicontrol('Style','PushButton',...  %Current Display
255            'units', 'centimeters','Position',[x0+1.1*dx y0+0.005*screen_high dx/1.5 dy],...
256            'BackGroundColor',[0.77 0.91 1.00],...
257            'ToolTipString','Electron Beam Current (A)',...
258            'CallBack','orbgui(''UpdateParameters'')',...
259            'String',num2str(SYS.current, '%6.1f'));
260
261        uicontrol('Style','text',...                           %Energy Text
262            'units', 'centimeters','Position',[x0+0*dx y0-dy dx dy],...
263            'HorizontalAlignment','right',...
264            'ToolTipString','Electron Beam Energy (GeV)',...
265            'String','Energy (GeV)');
266
267        SYS.henergy=uicontrol('Style','PushButton',...   %Energy Display
268            'units', 'centimeters','Position',[x0+1.1*dx y0-dy+0.005*screen_high dx/1.5 dy],...
269            'BackGroundColor',[0.77 0.91 1.00],...
270            'ToolTipString','Electron Beam Energy (GeV)',...
271            'CallBack','orbgui(''UpdateParameters'')',...
272            'String',num2str(SYS.energy, '%6.4f'));
273
274        uicontrol('Style','text',...                            %Lifetime Text
275            'units', 'centimeters','Position',[x0+1.8*dx y0 dx dy],...
276            'HorizontalAlignment','right',...
277            'ToolTipString','Electron Beam Lifetime (h)',...
278            'String','Lifetime (h)');
279
280        SYS.hlifetime=uicontrol('Style','PushButton',... %Lifetime Display
281            'units', 'centimeters','Position',[x0+3.0*dx y0+0.005*screen_high dx/1.5 dy],...
282            'BackGroundColor',[0.77 0.91 1.00],...
283            'ToolTipString','Electron Beam Lifetime (h)',...
284            'CallBack','orbgui(''UpdateParameters'')',...
285            'String',num2str(SYS.lifetime, '%6.1f'));
286
287        uicontrol('Style','text',...                            %Amp-hr Text
288            'units', 'centimeters','Position',[x0+1.8*dx y0-dy dx dy],...
289            'HorizontalAlignment','right',...
290            'ToolTipString','Running mode: online or simulator',...
291            'String','Mode');
292
293        SYS.modecolor = uicontrol('Style','PushButton',...                            %Amp-hr Text
294            'units', 'centimeters','Position',[x0 + 3*dx y0 - dy dx/1.5 dy],...
295            'HorizontalAlignment','center');
296             
297%         uicontrol('Style','text',...                            %Amp-hr Text
298%             'units', 'centimeters','Position',[x0+1.8*dx y0-dy dx dy],...
299%             'HorizontalAlignment','right',...
300%             'ToolTipString','Electron beam amp-hours',...
301%             'String','Amp-hr:');
302%
303%         SYS.amphr=uicontrol('Style','PushButton',...     %Amp-hr Display
304%             'units', 'centimeters','Position',[x0+3.0*dx y0-dy+0.005*screen_high dx/1.5 dy],...
305%             'BackGroundColor',[0.77 0.91 1.00],...
306%             'ToolTipString','Electron beam amp-hours',...
307%             'CallBack','orbgui(''UpdateParameters'')',...
308%             'String',num2str(SYS.ahr, '%6.1f'));
309
310        setappdata(0,'SYS',SYS);
311
312        %==========================================================
313    case 'UpdateParameters'              %...UpdateParameters
314        %==========================================================
315        %Update field boxes that display machine parameters
316
317       
318        if strcmpi(SYS.machine,'Ring')
319            [SYS.energy,SYS.current,SYS.lifetime] = getringparams;
320            setappdata(0,'SYS',SYS);
321        end
322        set(SYS.henergy,  'String',num2str(SYS.energy,  '%6.4f'));
323        set(SYS.hcurrent, 'String',num2str(SYS.current, '%6.3f'));       
324        set(SYS.hlifetime,'String',num2str(SYS.lifetime,'%6.3f'));
325
326        %==========================================================
327    case 'SVDAxes'                               %SVDAxes
328        %==========================================================
329        %SVDAxes are the axes to plot singular value spectrum/logarithmic
330
331        %screen size call
332        [screen_wide, screen_high]=screensizecm;
333
334        x0=0.6*screen_wide; dx=0.19*screen_wide;
335        y0=0.0495*screen_high; dy=0.14*screen_high;
336
337        % axe handle for SVD
338        SYS.ahsvd = axes('Units','centimeters','YScale','log','NextPlot', ...
339            'add','Color', [1 1 1],'Box','On','Position',[x0 y0 dx dy], ...
340            'XminorTick','On','YminorTick','On');
341
342        setappdata(0,'SYS',SYS);
343
344        set(get(SYS.ahsvd,'Ylabel'),'string','Singular Value');
345        set(get(SYS.ahsvd,'Xlabel'),'string','Singular Value Index');
346       
347        %==========================================================
348    case 'PlotMenu'                               %  PlotMenu
349        %==========================================================
350        %*** PLOT MENU ***
351        mh.plot = uimenu('Label','Plot    ');
352        uimenu(mh.plot,'Label','BPM Plot Scale', ...
353            'Callback','orbgui(''BPMPlotScale'')');
354        uimenu(mh.plot,'Label','Corrector Plot Scale', ...
355            'Callback','orbgui(''CorPlotScale'')');
356        mh.r2=uimenu(mh.plot,'Label','BPM Unit Data Display');
357        SYS.BPMPhysicsUnits = ...
358            uimenu(mh.r2,'Label','Physics Units','Callback', ...
359            'orbgui(''BPMChangeUnits'',''Physics'')');
360        SYS.BPMHardwareUnits = ...
361            uimenu(mh.r2,'Label','Hardware Units','Callback', ...
362            'orbgui(''BPMChangeUnits'',''Hardware'')');
363        SYS.BPM1000HardwareUnits = ...
364            uimenu(mh.r2,'Label','1000xHardware Units','Callback', ...
365            'orbgui(''BPMChangeUnits'',''1000xHardware'')');
366        mh.r3=uimenu(mh.plot,'Label','COR Unit Data Display');
367        SYS.CORPhysicsUnits = ...
368            uimenu(mh.r3,'Label','Physics Units','Callback', ...
369            'orbgui(''CORChangeUnits'',''Physics'')');
370        SYS.CORHardwareUnits = ...
371            uimenu(mh.r3,'Label','Hardware Units','Callback', ...
372            'orbgui(''CORChangeUnits'',''Hardware'')');
373%         uimenu(mh.plot,'Label',' ');
374%         mh.r2r=uimenu(mh.plot,'Label','Raw/Real Data Display');
375%         uimenu(mh.r2r,'Label','Display Raw Data','Callback','orbgui('''')');
376%         uimenu(mh.r2r,'Label','Display Real Data','Callback','orbgui('''')');
377%         uimenu(mh.r2r,'Label','Update Raw2Real Coefficients','Callback','orbgui('''')');       
378%         uimenu(mh.plot,'Label','Cross-Hair: Axes Start Location','Callback','orbgui(''StartLoc'')');
379%         uimenu(mh.plot,'Label','Cross-Hair: Axes Stop Location', 'Callback','orbgui(''StopLoc'')');
380%         uimenu(mh.plot,'Label','Reset Horizontal Scale',        'Callback','orbgui(''ResetAxes'')');
381        setappdata(0,'SYS',SYS);
382   
383        %==========================================================
384    case 'BPMChangeUnits'  % BPM Unit data Display
385        %==========================================================
386       
387        BPM(1).units = varargin{1};
388        BPM(2).units = varargin{1};
389       
390        setappdata(0,'BPM',BPM);
391       
392        %% BPM y-axis label
393        label = get(get(SYS.ahbpm,'Ylabel'),'string');       
394        switch varargin{1}
395            case 'Hardware'
396                Unit = 'mm';
397                set(SYS.BPMPhysicsUnits,'Checked','Off');
398                set(SYS.BPMHardwareUnits,'Checked','On');
399                set(SYS.BPM1000HardwareUnits,'Checked','Off');
400                BPM(1).scale = 1;
401                BPM(2).scale = 1;
402            case '1000xHardware'
403                Unit = 'um';
404                set(SYS.BPMPhysicsUnits,'Checked','Off');
405                set(SYS.BPMHardwareUnits,'Checked','Off');
406                set(SYS.BPM1000HardwareUnits,'Checked','On');
407                BPM(1).scale = 1000;
408                BPM(2).scale = 1000;
409            case 'Physics'
410                Unit = 'm';
411                set(SYS.BPMPhysicsUnits,'Checked','On');
412                set(SYS.BPMHardwareUnits,'Checked','Off');
413                set(SYS.BPM1000HardwareUnits,'Checked','Off');
414                BPM(1).scale = 0.001;
415                BPM(2).scale = 0.001;
416        end
417
418        label = [label(1:strfind(label,'(')-1) '(' Unit ')'];
419        set(get(SYS.ahbpm,'Ylabel'),'string',label);
420       
421        setappdata(0,'SYS',SYS);
422
423        bpmgui('RePlot');
424        bpmgui('UpdateBPMBox');
425       
426        %==========================================================
427    case 'CORChangeUnits'  % BPM Unit data Display
428        %==========================================================
429       
430        for plane = 1:2
431            COR(plane).units = varargin{1};
432        end
433       
434        setappdata(0,'COR',COR);
435       
436        %% HCOR y-axis label
437        label = get(get(SYS.ahcor,'Ylabel'),'string');       
438        switch varargin{1}
439            case {'Hardware','1000xHardware'}
440                Unit = 'A';
441                set(SYS.CORPhysicsUnits,'Checked','Off');
442                set(SYS.CORHardwareUnits,'Checked','On');
443            case 'Physics'
444                Unit = 'mrad';
445                set(SYS.CORPhysicsUnits,'Checked','On');
446                set(SYS.CORHardwareUnits,'Checked','Off');
447        end
448        label = [label(1:strfind(label,'(')-1) '(' Unit ')'];
449        set(get(SYS.ahcor,'Ylabel'),'string',label);
450
451        corgui('RePlot')
452        corgui('UpdateCorBox');
453
454        %==========================================================
455    case 'LoadRaw2Real'                         %  LoadRaw2Real
456        %==========================================================
457        %Load Raw2Real Coefficients into AppData
458
459        Families = {BPMxFamily, BPMzFamily, HCORFamily, VCORFamily};
460
461        for k = 1:length(Families)
462            Family     = Families{k};
463            DeviceList = getlist(Family,0);   %0 gives all entries
464            raw2realdata.(Family).Gain    = getphysdata(Family, 'Gain',   DeviceList);
465            raw2realdata.(Family).Offset  = getphysdata(Family, 'Offset', DeviceList);
466        end
467
468        setappdata(0,'raw2realdata',raw2realdata);
469
470%         %==========================================================
471%     case 'AlgorithmMenu'                       %  AlgorithmMenu
472%         %==========================================================
473%         %*** Algorithn MENU ***
474%         mh.algo = uimenu('Label','Fit Algorithm');
475%         uimenu(mh.algo,'Label','SVD','Callback',...
476%             'orbgui(''SVDAlgo'');');
477%         uimenu(mh.algo,'Label','Micado','Callback',...
478%             'orbgui(''MicadoAlgo'');');
479
480        %==========================================================
481    case 'SVDAlgo'                       %  SVDAlgo
482        %==========================================================
483        SYS.algo='SVD';
484        setappdata(0,'SYS',SYS);
485
486        %==========================================================
487%     case 'MicadoAlgo'                       %  MicadoAlgo
488%         %==========================================================
489%         SYS.algo='Micado';
490%         setappdata(0,'SYS',SYS);
491
492        %==========================================================
493    case 'SuperperiodMenu'                       %  SuperperiodMenu
494        %==========================================================
495        %*** Algorithn MENU ***
496        mh.superperiod = uimenu('Label','Super-period');
497        SYS.allmachine = ...
498            uimenu(mh.superperiod,'Label','All storage ring','Callback',...
499            'orbgui(''plotxaxis'',''allmachine'');','Tag','allmachine');
500        SYS.superperiod1 = ...
501            uimenu(mh.superperiod,'Label','Super period #1','Callback',...
502            'orbgui(''plotxaxis'',''superperiod1'');','Tag','superperiod1');
503        SYS.superperiod2 = ...
504            uimenu(mh.superperiod,'Label','Super period #2','Callback',...
505            'orbgui(''plotxaxis'',''superperiod2'');','Tag','superperiod2');
506        SYS.superperiod3 = ...
507            uimenu(mh.superperiod,'Label','Super period #3','Callback',...
508            'orbgui(''plotxaxis'',''superperiod3'');','Tag','superperiod3');
509        SYS.superperiod4 = ...
510            uimenu(mh.superperiod,'Label','Super period #4','Callback',...
511            'orbgui(''plotxaxis'',''superperiod4'');','Tag','superperiod4');
512        setappdata(0,'SYS',SYS);
513
514        %==========================================================
515    case 'plotxaxis'
516        %==========================================================
517       
518        action = varargin{1};
519       
520        bpm_axe     = axis(SYS.ahbpm);
521        cor_axe     = axis(SYS.ahcor);
522        lattice_axe = axis(SYS.ahpos);
523     
524        switch action
525            case 'allmachine'
526                AxisRange1X = [0 1]*SYS.xlimax;
527            case 'superperiod1'
528                AxisRange1X = [0 0.25]*SYS.xlimax;
529            case 'superperiod2'
530                AxisRange1X = [0.25 0.5]*SYS.xlimax;
531            case 'superperiod3'
532                AxisRange1X = [0.5 0.75]*SYS.xlimax;
533            case 'superperiod4'
534                AxisRange1X = [0.75 1]*SYS.xlimax;
535        end
536
537        axis(SYS.ahbpm,   [AxisRange1X bpm_axe(3:4)]);
538        axis(SYS.ahcor, [AxisRange1X cor_axe(3:4)]);
539        axis(SYS.ahpos, [AxisRange1X lattice_axe(3:4)]);
540
541        setappdata(0, 'xaxe', AxisRange1X);
542
543        set(SYS.allmachine,'Checked','Off');
544        set(SYS.superperiod1,'Checked','Off');
545        set(SYS.superperiod2,'Checked','Off');
546        set(SYS.superperiod3,'Checked','Off');
547        set(SYS.superperiod4,'Checked','Off');
548        set(SYS.(action),'Checked','On');
549
550
551        %==========================================================
552    case 'BPMMenu'                               %  BPMMenu
553        %==========================================================
554        %*** BPM MENU ***
555        mh.bpms = uimenu('Label','BPMs');
556        uimenu(mh.bpms,'Label','Select All BPMs','Callback','bpmgui(''SelectAll'');');
557        uimenu(mh.bpms,'Label','Select No BPM','Callback','bpmgui(''SelectNone'');');
558        uimenu(mh.bpms,'Label','Remove BPM Drag Changes','Callback','bpmgui(''ClearOffsets'');');
559        uimenu(mh.bpms,'Label','Show BPM State','Callback','bpmgui(''ShowBPMState'');');
560        uimenu(mh.bpms,'Label','Archive X-Orbit',          'Callback', ...
561            'readwrite(''ArchiveBPMOrbit'',''X'');','Separator','on');
562        uimenu(mh.bpms,'Label','Archive Z-Orbit',          'Callback', ...
563            'readwrite(''ArchiveBPMOrbit'',''Z'');');
564        uimenu(mh.bpms,'Label','Archive X/Z-Orbit',        'Callback', ...
565            'readwrite(''ArchiveBPMOrbit'',''XZ'');');
566        uimenu(mh.bpms,'Label','Load X-Reference Orbit',   'Callback', ...
567            'readwrite(''ReadBPMReference'',''X'');','Separator','on');
568        uimenu(mh.bpms,'Label','Load Z-Reference Orbit',   'Callback', ...
569            'readwrite(''ReadBPMReference'',''Z'');');
570        uimenu(mh.bpms,'Label','Load X/Z-Reference Orbit', 'Callback', ...
571            'readwrite(''ReadBPMReference'',''XZ'');');
572        uimenu(mh.bpms,'Label','Archive Golden X-Orbit',   'Callback', ...
573            'readwrite(''ArchiveBPMOrbit'',''X'',''Golden'');','Separator','on');
574        uimenu(mh.bpms,'Label','Archive Golden Z-Orbit',   'Callback', ...
575            'readwrite(''ArchiveBPMOrbit'',''Z'',''Golden'');');
576        uimenu(mh.bpms,'Label','Archive Golden X/Z-Orbit', 'Callback', ...
577            'readwrite(''ArchiveBPMOrbit'',''XZ'',''Golden'');');
578        uimenu(mh.bpms,'Label','Load Golden X-Reference',  'Callback', ...
579            'readwrite(''ReadBPMReference'',''X'',''Golden'');','Separator','on');
580        uimenu(mh.bpms,'Label','Load Golden Z-Reference',  'Callback', ...
581            'readwrite(''ReadBPMReference'',''Z'',''Golden'');');
582        uimenu(mh.bpms,'Label','Load Golden X/Z-Reference','Callback', ...
583            'readwrite(''ReadBPMReference'',''XZ'',''Golden'');');
584
585        %Select horizontal BPM weights
586        cback='rload(''GenFig'', BPM(1).name,BPM(1).avail,BPM(1).ifit,BPM(1).wt,';
587        cback=[cback '''Horizontal BPM Weights'',''xbwt'');'];
588        %instructions are used during 'load' procedure of rload window
589        instructions=[...
590            '   global BPM;',...
591            '   tlist = get(gcf,''UserData'');',...
592            '   BPM(1).wt=tlist{4};',...
593            '   setappdata(0,''BPM'',BPM);',...
594            '   orbgui(''RefreshOrbGUI'');'];
595        uimenu(mh.bpms,'Label','Select X-BPM Weights','Callback', ...
596            cback,'Tag','xbwt','Userdata',instructions,'Separator','on');
597
598        %Select vertical BPM weights
599        cback='rload(''GenFig'', BPM(2).name,BPM(2).avail,BPM(2).ifit,BPM(2).wt,';
600        cback=[cback '''Vertical BPM Weights'',''zbwt'');'];
601        %instructions are used during 'load' procedure of rload window
602        instructions=[...
603            '   global BPM;',...
604            '   tlist = get(gcf,''UserData'');',...
605            '   BPM(2).wt=tlist{4};',...
606            '   setappdata(0,''BPM'',BPM);',...
607            '   orbgui(''RefreshOrbGUI'');'];
608
609        uimenu(mh.bpms,'Label','Select Z-BPM Weights','Callback', ...
610            cback,'Tag','ybwt','Userdata',instructions);
611%         uimenu(mh.bpms,'Label','   ');
612%         uimenu(mh.bpms,'Label','Help with BPM Functions','Callback','readwrite(''OpenHelp'');');
613
614        %==========================================================
615    case 'CORMenu'                               %  CORMenu
616        %==========================================================
617        %*** CORRECTOR MENU ***
618        mh.cors = uimenu('Label','Correctors');
619        uimenu(mh.cors,'Label','Update Correctors','Callback', ...
620            'corgui(''UpdateCorrs'');');
621        uimenu(mh.cors,'Label','Corrector strengths to zero (this plane)', ...
622            'Callback','orbgui(''Cor2Zero'')');
623        uimenu(mh.cors,'Label','Select All Correctors','Callback', ...
624            'corgui(''SelectAll'');','Separator','on');
625        uimenu(mh.cors,'Label','Select No Correctors','Callback', ...
626            'corgui(''SelectNone'');');
627        uimenu(mh.cors,'Label','Load Correctors','Callback', ...
628            'readwrite(''DialogBox'',''Read Correctors'',''ReadCorrectors'');','Separator','on');
629        uimenu(mh.cors,'Label','Archive Correctors','Callback', ...
630            'readwrite(''DialogBox'',''Write Correctors'',''WriteCorrectors'');');
631
632        %Select horizontal corrector limits
633        cback='rload(''GenFig'', COR(1).name,COR(1).status,COR(1).avail,COR(1).lim,';
634        cback=[cback '''Horizontal Corrector Limits'',''xclim'');'];
635        %instructions are used during 'load' procedure of rload window
636        instructions=[...
637            '   global COR;',...
638            '   tlist = get(gcf,''UserData'');',...
639            '   COR(1).lim=tlist{4};',...
640            '   setappdata(0,''COR'',COR);'];
641        uimenu(mh.cors,'Label','Select X-Corrector Limits','Callback', ...
642            cback,'Tag','xclim','Userdata',instructions,'Separator','on');
643
644        %Select horizontal corrector weights
645        cback='rload(''GenFig'', COR(1).name,COR(1).status,COR(1).ifit,COR(1).wt,';
646        cback=[cback '''Horizontal Corrector Weights'',''xcwt'');'];
647        %instructions are used during 'load' procedure of rload window
648        instructions=[...
649            '   global COR;',...
650            '   tlist = get(gcf,''UserData'');',...
651            '   COR(1).wt=tlist{4};',...
652            '   setappdata(0,''COR'',COR);',...
653            '   orbgui(''RefreshOrbGUI'');'];
654
655        uimenu(mh.cors,'Label','Select X-Corrector Weights','Callback', ...
656            cback,'Tag','xcwt','Userdata',instructions);
657
658        %Select vertical corrector limits
659        cback='rload(''GenFig'', COR(2).name,COR(2).status,COR(2).avail,COR(2).lim,';
660        cback=[cback '''Vertical Corrector Limits'',''zclim'');'];
661        %instructions are used during 'load' procedure of rload window
662        instructions=[...
663            '   global COR;',...
664            '   tlist = get(gcf,''UserData'');',...
665            '   COR(2).lim=tlist{4};',...
666            '   setappdata(0,''COR'',COR);'];
667        uimenu(mh.cors,'Label','Select Z-Corrector Limits','Callback',cback, ...
668            'Tag','zclim','Userdata',instructions,'Separator','on');
669
670        %Select vertical corrector weights
671        cback='rload(''GenFig'', COR(2).name,COR(2).status,COR(2).ifit,COR(2).wt,';
672        cback=[cback '''Vertical Corrector Weights'',''zcwt'');'];
673        %instructions are used during 'load' procedure of rload window
674        instructions=[...
675            '   global COR;',...
676            '   tlist = get(gcf,''UserData'');',...
677            '   COR(2).wt=tlist{4};',...
678            '   setappdata(0,''COR'',COR);',...
679            '   orbgui(''RefreshOrbGUI'');'];
680
681        uimenu(mh.cors,'Label','Select Z-Corrector Weights','Callback',cback, ...
682            'Tag','zcwt','Userdata',instructions);
683        uimenu(mh.cors,'Label','Show Corrector State','Callback', ...
684            'corgui(''ShowCORState'');','Separator','on');
685%         uimenu(mh.cors,'Label','Help with Corrector Functions','Callback','readwrite(''OpenHelp'');');
686
687        %==========================================================
688    case 'EtaMenu'                               %  EtaMenu
689        %==========================================================
690        %*** Dispersion control MENU ***
691        mh.eta = uimenu('Label','Eta Fit');
692        uimenu(mh.eta,'Label','Load Dispersion Orbit','Callback', ...
693            'readwrite(''DialogBox'',''Load Reference Orbit'',''ReadDispersion'');');
694        uimenu(mh.eta,'Label','Display Dispersion Control Panel','Callback', ...
695            'respgui(''DispersionPanel'')');
696        uimenu(mh.eta,'Label','Display Dispersion Correction','Callback', ...
697            'respgui(''DispersionPlot'')');
698
699        %==========================================================
700    case 'SYSMenu'                               %  SYSMenu
701        %==========================================================
702        %*** SYSTEM MENU ***
703        mh.sys = uimenu('Label','Session');
704        menuh = uimenu(mh.sys,'Label','Mode');
705        SYS.online = uimenu(menuh,'Label','Online','Callback', ...
706            'orbgui(''Switch2online'')');       
707        SYS.sim   = uimenu(menuh,'Label','Simulator','Callback', ...
708            'orbgui(''Switch2sim'')');
709
710        menuh = uimenu(mh.sys,'Label','Do not use this menu', ...
711            'ForegroundColor', 'r', 'Separator','On');
712        uimenu(menuh,'Label','Save Program Parameters','Callback', ...
713            'readwrite(''DialogBox'',''Save System Parameters'',''SaveSystem'');');
714        uimenu(menuh,'Label','Restore Program Parameters','Callback', ...
715            'readwrite(''DialogBox'',''Restore System Parameters'',''RestoreSystem'')');
716       
717        setappdata(0,'SYS',SYS);
718
719        %==========================================================
720    case 'Switch2online'                         %  Switch2online
721        %==========================================================
722               
723        if strcmpi(SYS.mode,'Simulator') % was Simulator turned Online
724            set(SYS.online, 'Checked', 'On');
725            set(SYS.sim,    'Checked', 'Off');
726            set(SYS.orbfig,'Name', ...
727                'SOLEIL Storage Ring Orbit Correction Interface (Online)');
728            set(SYS.modecolor,'BackGroundColor','r','String','Online');
729            orbgui('LBox','Warning: switched to online');
730            SYS.mode = 'Online';
731            setappdata(0,'SYS',SYS);
732        end
733
734        %==========================================================
735    case 'Switch2sim'                         %  Switch2sim
736        %==========================================================
737       
738        if strcmpi(SYS.mode,'Online')
739            set(SYS.online, 'Checked', 'Off');
740            set(SYS.sim,    'Checked', 'On');
741            set(SYS.orbfig,'Name', ...
742                'SOLEIL Storage Ring Orbit Correction Interface (Simulator)');
743            set(SYS.modecolor,'BackGroundColor','g','String','Simulator');
744            orbgui('LBox','Warning: switched to simulator');
745            SYS.mode = 'Simulator';
746            setappdata(0,'SYS',SYS);
747        end
748
749        %==========================================================
750    case 'OptMenu'                               %  OptMenu
751        %==========================================================
752        %*** OPTICS MENU ***
753        mh.opt = uimenu('Label','Optics');
754        uimenu(mh.opt,'Label','Display Optics Parameters','Callback',...
755            'orbgui(''DisplayOptics'')');
756        if ispc
757            uimenu(mh.opt,'Label','Display Optics Parameters in EXCEL', ...
758                'Callback','orbgui(''DisplayOpticsEXCEL'')');
759        end
760        uimenu(mh.opt,'Label','Plot Betafunctions','Callback', ...
761            'orbgui(''PlotBetaFunctions'')');
762        uimenu(mh.opt,'Label','Plot Ring Elements','Callback', ...
763            'orbgui(''PlotRingElements'')');
764
765        %==========================================================
766    case 'SimMenu'                               %  SimMenu
767        %==========================================================
768        % %*** SIMULATION MENU ***
769        mh.sim = uimenu('Label','Simulator');
770        uimenu(mh.sim,'Label','New Quad Alignment (both plane)', ...
771            'Callback', 'orbgui(''QuadOffset'')');
772        uimenu(mh.sim,'Label','Make New Reference Orbit (this plane)', ...
773            'Callback','orbgui(''MakeNewRef'')');
774        uimenu(mh.sim,'Label','Quad alignment to zero (both planes)', ...
775            'Callback','orbgui(''Quad2Zero'')');
776        uimenu(mh.sim,'Label','Update Optics (LinOpt)', ...
777            'Callback','orbgui(''UpdateOptics'')','Separator','on');
778        t   = getphysdata;   
779        str = sprintf('Fit Tunes to %3.2f/%3.2f',t.TUNE.Golden(1),t.TUNE.Golden(2));
780        uimenu(mh.sim,'Label',str,'Callback','orbgui(''SetTune'')');
781
782        %==========================================================
783    case 'Cor2Zero'                             %...Quad2Zero
784        %==========================================================
785        %Callback of Simulator/'Corrector Strengths to Zero'
786
787        %set correctors to zero
788        if plane == 1
789            setsp(HCORFamily,0.0,SYS.mode);   %only sets correctors with valid status
790        elseif plane == 2
791            setsp(VCORFamily,0.0,SYS.mode);
792        end
793
794        bpmgui('GetAct');
795        corgui('GetAct');
796        orbgui('RefreshOrbGUI');
797
798        %==========================================================
799    case 'Quad2Zero'                             %...Quad2Zero
800        %==========================================================
801        %Callback of Simulator/'Quadrupole Strenths to Zero'
802
803        %set quadrupole offsets to zero
804        quadalign(0.0,0.0);
805        bpmgui('GetAct');
806        corgui('GetAct');
807        orbgui('RefreshOrbGUI');
808
809        %==========================================================
810    case 'QuadOffset'                            %...QuadOffset
811        %==========================================================
812        %Callback of Simulator/'New Quad Alignment'
813
814        %apply quadrupole offsets
815        quadalign(0.0001,0.0001);  %units are meters
816        bpmgui('GetAct');
817        corgui('GetAct');
818        orbgui('RefreshOrbGUI')
819
820        %==========================================================
821    case 'MakeNewRef'                            %...MakeNewRef
822        %==========================================================
823        %callback of Simulation 'Make New Reference Orbit'
824
825        %record present quad positions for reset
826
827        AO = getappdata(0,'AcceleratorObjects');
828
829        ATindx = [] ;
830       
831        aofields = fieldnames(AO);
832       
833        %% look for quads in lattice
834        for ii = 1:length(aofields)
835            if strcmpi(AO.(aofields{ii}).FamilyType,'quad')
836                ATindx = [AO.(aofields{ii}).AT.ATIndex];
837            end
838        end
839       
840        ATindx = unique(ATindx)';
841
842        mx0 = getcellstruct(THERING,'T1',ATindx,1);
843        my0 = getcellstruct(THERING,'T1',ATindx,3);
844
845        %set quads to zero
846        quadalign(0.0,0.0);  %units are meters
847
848        %produce a new random orbit
849        quadalign(0.00001,0.00001);  %units are meters, 10 um rms
850
851        %load new reference orbit
852        bpmgui('GetRef');
853        BPM(1).des = BPM(1).ref;
854        BPM(1).abs = BPM(1).ref;
855        BPM(2).des = BPM(2).ref;
856        BPM(2).abs = BPM(2).ref;
857
858        if SYS.relative == 1    %Absolute orbit mode
859            BPM(1).abs = zeros(size(BPM(1).name,1),1);
860            BPM(2).abs = zeros(size(BPM(2).name,1),1);
861        end
862
863        setappdata(0,'BPM',BPM);
864
865        %put quads back to original position
866        setshift(ATindx,-mx0,-my0);
867
868        bpmgui('GetAct');
869        corgui('GetAct');
870        orbgui('RefreshOrbGUI');
871
872        %==========================================================
873    case 'PlotAlign'                            %...PlotAlign
874        %==========================================================
875        %update graphics after alignments changed
876        bpmgui('GetRef');
877        BPM(1).des = BPM(1).ref;
878        BPM(1).abs = BPM(1).ref;
879        BPM(2).des = BPM(2).ref;
880        BPM(2).abs = BPM(2).ref;
881
882        if SYS.relative == 1
883            BPM(1).abs = zeros(size(BPM(1).name,1),1);
884            BPM(2).abs = zeros(size(BPM(2).name,1),1);
885        end
886
887        setappdata(0,'BPM',BPM);
888
889        orbgui('RefreshOrbGUI');
890
891        %==========================================================
892    case 'SetTune'                    %...SetTune
893        %==========================================================
894        %set tune back to nominal values
895        t = getphysdata;
896       
897        TuneFitFamily1 = 'Q7';
898        TuneFitFamily2 = 'Q9';
899       
900        fittune2([t.TUNE.Golden(1),t.TUNE.Golden(2)], TuneFitFamily1, TuneFitFamily2);
901       
902        bpmgui('UpdateAct');
903
904        %==========================================================
905    case 'UpdateOptics'                    %...UpdateOptics
906        %==========================================================
907        %recompute linear optics
908        [LinData, Nu, Ksi] = linopt(THERING ,0.0, 1:length(THERING));
909        disp(['Nux: ' num2str(Nu(1)) '       Nuz: ' num2str(Nu(2))])
910
911        %==========================================================
912    case 'DisplayOpticsEXCEL'            %...DisplayOpticsEXCEL
913        %==========================================================
914        ring2excel
915
916        %==========================================================
917    case 'DisplayOptics'                      %...DisplayOptics
918        %==========================================================
919        %Display Optics to command window
920        NR = length(THERING);
921        orbgui('LBox','Calculating Accelerator Optics...');
922        optics = gettwiss(THERING,0.0);
923        orbit  = findorbit4(THERING,0.0,1:NR);
924        orbgui('LBox','Begin Writing Twiss Parameters');
925 
926        ivec = (1:NR)';       
927        Strcell = cell(18,NR);
928        Strcell(1,:) = num2cell(ivec);
929        Strcell(2,:) = cellstr(optics.name)';
930        Strcell(3:end,:) = num2cell([optics.len,optics.s, ...
931            optics.betax,optics.alfax, optics.phix, optics.etax, optics.etapx, ...
932            optics.betay,optics.alfay, optics.phiy, optics.etay, optics.etapy, ...
933            orbit(1,:)',orbit(2,:)',orbit(3,:)',orbit(4,:)'])';
934
935        filename = [SYS.localdata 'twissdata.txt'];
936        fid = fopen(filename,'w');
937        fprintf(fid,[' index   name  length   s     betx   alfx   phix' ...
938            'etax   etaxp  betz   alfz   phiz   etaz   etazp    x' ...
939            '      xp     z      zp\n']);
940        fprintf(fid,['%4d %8s %6.2f %6.2f %6.2f %6.2f %6.2f % 6.2f %' ...
941            '6.2f %6.2f %6.2f %6.2f % 6.2f % 6.2f %6.2f %6.2f %6.2f ' ...
942            '%6.2f\n'],...
943            Strcell{:});
944        fclose(fid);
945        system(['nedit ', filename, ' &']);  % much faster
946       
947%         for ii = 1:NR
948%             name = optics.name(ii,:);
949%             len  = optics.len(ii);
950%             s    = optics.s(ii);
951%             betx = optics.betax(ii);
952%             alfx = optics.alfax(ii);
953%             phix = optics.phix(ii);
954%             etax = optics.etax(ii);
955%             etapx= optics.etapx(ii);
956%
957%             bety = optics.betay(ii);
958%             alfy = optics.alfay(ii);
959%             phiy = optics.phiy(ii);
960%             etay = optics.etay(ii);
961%             etapy= optics.etapy(ii);
962%
963%             x = orbit(1,ii);  xp = orbit(2,ii);
964%             y = orbit(3,ii);  yp = orbit(4,ii);
965%
966%             fprintf('%6d %8s %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f\n',...
967%                 ii,name,s,len,betx,alfx,phix,etax,etapx,bety,alfy,phiy,etay,etapy,x,xp,y,yp)
968%             %     'one',1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
969%         end
970%         disp(['Horizontal Tune ' num2str(phix) 'Vertical Tune ' num2str(phiy)])
971        orbgui('LBox','Finished Writing Twiss Parameters');
972
973        %==========================================================
974    case 'PlotBetaFunctions'               % PlotBetaFunctions
975        %==========================================================
976        %plot betafunctions
977        figure;
978        plotbeta;
979
980        %==========================================================
981    case 'PlotRingElements'                % PlotRingElements
982        %==========================================================
983        %call intlat
984        intlat;
985
986        %==========================================================
987    case 'RespMenu'                               %  RespMenu
988        %==========================================================
989        %response matrix
990        mh.rsp = uimenu('Label','R-Matrix');
991        uimenu(mh.rsp,'Label','Read Response Matrix','Callback', ...
992            'readwrite(''ReadResponse'')');
993
994        % COULD BE NOT LATER
995%         uimenu(mh.rsp,'Label','Write Response Matrix', ...
996%             'Callback','readwrite(''DialogBox'',''Write Response Matrix'',''WriteResponse'')');
997
998%         uimenu(mh.rsp,'Label','    ');
999        SYS.showeig  = uimenu(mh.rsp,'Label','Show Eigenvectors', ...
1000            'UserData',0,'Callback','corgui(''ShowEig'')','Separator','on');
1001        SYS.showresp = uimenu(mh.rsp,'Label','Show Response', ...
1002            'UserData',0,'Callback','corgui(''ShowResp'')');
1003
1004%         uimenu(mh.rsp,'Label','    ');
1005        %Select corrector currents for horizontal response to eBPMs
1006%         cback = ['rload(''GenFig'', COR(1).name,COR(1).status,COR(1).ifit,COR(1).ebpm,'];
1007%         cback = [cback '''Horizontal Correctors to Electron BPMs'',''hbpm'');'];
1008%         %instructions are used during 'load' procedure of rload window
1009%         instructions = [...
1010%             '   global COR;',...
1011%             '   tlist = get(gcf,''UserData'');',...
1012%             '   COR(1).ebpm=tlist{4};',...
1013%             '   setappdata(0,''COR'',COR);'];
1014%         uimenu(mh.rsp,'Label','Select X-Corrector Strengths (BPM)',...
1015%             'Callback',cback,...
1016%             'Tag','hbpm',...
1017%             'Userdata',instructions,'Separator','on');
1018
1019%         %Measure Horizontal
1020%         uimenu(mh.rsp,'Label','Measure Horizontal to BPMs (all valid correctors)',...
1021%             'Callback','corgui(''MeasureXResp'')');
1022        %==============
1023        %VERTICAL PLANE
1024        %==============
1025%         uimenu(mh.rsp,'Label','    ');
1026        %Select corrector currents for vertical response to eBPMs
1027%         cback = ['rload(''GenFig'', COR(2).name,COR(2).status,COR(2).ifit,COR(2).ebpm,'];
1028%         cback = [cback '''Vertical Correctors to Electron BPMs'',''vbpm'');'];
1029%         instructions = [...
1030%             '   global COR;',...
1031%             '   tlist = get(gcf,''UserData'');',...
1032%             '   COR(2).ebpm=tlist{4};',...
1033%             '   setappdata(0,''COR'',COR);'];
1034%         uimenu(mh.rsp,'Label','Select Z-corrector Strengths (BPM)',...
1035%             'Callback',cback,...
1036%             'Tag','vbpm',...
1037%             'Userdata',instructions,'Separator','on');
1038%         %Measure Vertical
1039%         uimenu(mh.rsp,'Label','Measure Vertical to BPMs (all valid correctors)',...
1040%             'Callback','corgui(''MeasureYResp'')');
1041
1042        %==========================================================
1043    case 'UIControls'                           %  UIControls
1044        %==========================================================
1045        %pushbuttons located above main orbit display
1046        %other pushbuttons in routines OrbBox, CorBox
1047
1048        [screen_wide, screen_high]=screensizecm;
1049
1050        x0=0.047*screen_wide; y0=0.7318*screen_high; dx=0.74*screen_wide; dy=0.06378*screen_high;
1051        SYS.display = uicontrol('Style','frame',...
1052            'units', 'centimeters', ...
1053            'Position',[x0 y0 dx dy]);            %main frame
1054
1055        x0=0.057*screen_wide; y0=0.739*screen_high; dx=0.117*screen_wide; dy=0.021*screen_high;
1056        dely=0.03*screen_high;   delx=0.15*screen_wide;
1057
1058        uicontrol('Style','pushbutton',...
1059            'Units', 'centimeters', ...
1060            'Position', [x0+0*delx y0+1*dely dx dy], ...
1061            'String','Update Display','FontSize',9,'FontWeight','demi',...
1062            'ToolTipString','Update Measured Orbit (blue line)',...
1063            'Callback','bpmgui(''UpdateAct'')');     %Update Orbit
1064        uicontrol('Style','pushbutton',...
1065            'units', 'centimeters', ...
1066            'Position', [x0+0*delx y0+0*dely dx dy], ...
1067            'String','Update Reference','FontSize',9,'FontWeight','demi',...
1068            'ToolTipString','Update Reference Orbit, both planes (red line)',...
1069            'Callback','bpmgui(''UpdateRef'')');        %Update Reference Orbit
1070
1071        if plane == 1, val=1; else val=0; end
1072        SYS.xplane=uicontrol('Style','checkbox',...
1073            'Units', 'centimeters', ...
1074            'Position', [0.25*screen_wide y0+1*dely 0.08*screen_wide dy], ...
1075            'String','Horizontal','Value',val,...
1076            'FontSize',9,'FontWeight','demi',...
1077            'ToolTipString','Horizontal Display and Control',...
1078            'Callback','orbgui(''Plane'')');  %Toggle Plane-Horizontal
1079
1080        if plane == 2, val=1; else val=0; end
1081        SYS.yplane=uicontrol('Style','checkbox',...
1082            'Units', 'centimeters', ...
1083            'Position', [0.32*screen_wide y0+1*dely 0.08*screen_wide dy], ...
1084            'String','Vertical','Value',val,...
1085            'FontSize',9,'FontWeight','demi',...
1086            'ToolTipString','Vertical Display and Control',...
1087            'Callback','orbgui(''Plane'')');  %Toggle Plane-Vertical
1088
1089        if SYS.relative==1, val=1; else val=0; end
1090        SYS.abs=uicontrol('Style','checkbox','units', 'centimeters', ...
1091            'Position', [0.25*screen_wide y0+0*dely 0.06*screen_wide dy], ...
1092            'String','Absolute','FontSize',9,'FontWeight','demi','Value',val,...
1093            'ToolTipString','Absolute BPM Display',...
1094            'Callback','orbgui(''Relative'')');   %Relative/Absolute-Absolute
1095
1096        if SYS.relative==2, val=1; else val=0; end
1097        SYS.rel=uicontrol('Style','checkbox','units', 'centimeters', ...
1098            'Position', [0.32*screen_wide y0+0*dely 0.06*screen_wide dy], ...
1099            'String','Relative','FontSize',9,'FontWeight','demi','Value',val,...
1100            'ToolTipString','Relative BPM Display',...
1101            'Callback','orbgui(''Relative'')');   %Relative/Absolute-Relative
1102
1103        uicontrol('Style','pushbutton',...
1104            'units', 'centimeters','ForeGroundColor','k',...
1105            'Position', [0.41*screen_wide 0.09*screen_high 0.45*dx dy], ...
1106            'String','Apply',...
1107            'ToolTipString','Apply Correction: Blue Dash Line = Predicted Orbit',...
1108            'FontSize',9,'FontWeight','demi',...
1109            'Callback','corgui(''ApplyCorrection'');');                  %Apply Correction
1110
1111        uicontrol('Style','pushbutton',...
1112            'units', 'centimeters','ForeGroundColor','k',...
1113            'Position', [0.47*screen_wide 0.09*screen_high 0.45*dx dy], ...
1114            'String','Remove',...
1115            'ToolTipString','Remove Previous Corrector Solution',...
1116            'FontSize',9,'FontWeight','demi',...
1117            'Callback','corgui(''RemoveCorrection'');');                 %Remove Correction
1118
1119        uicontrol('Style','pushbutton',...
1120            'units', 'centimeters','ForeGroundColor','k',...
1121            'Position', [0.41*screen_wide 0.06*screen_high 0.97*dx dy], ...
1122            'String','Refresh Fit',...
1123            'ToolTipString','Acquire Orbit, Correctors, Fit, Refresh all Plots & Fields',...
1124            'FontSize',9,'FontWeight','demi',...
1125            'Callback','orbgui(''RefreshOrbGUI'');');                    %Refresh Fit
1126
1127        uicontrol('Style','pushbutton',...
1128            'units', 'centimeters', ...
1129            'Position', [x0+4*delx+0.53*dx        y0+1*dely 0.47*dx dy], ...
1130            'ForeGroundColor','k','String','Save',...
1131            'FontSize',9,'FontWeight','demi',...
1132            'ToolTipString','Save Set - Use to Save Present Working Parameters',...
1133            'Callback','orbgui(''SaveSet'');');      %SaveSet
1134
1135        uicontrol('Style','pushbutton',...
1136            'units', 'centimeters', ...
1137            'Position', [x0+4*delx+0.53*dx        y0+0*dely 0.47*dx dy], ...
1138            'ForeGroundColor','k','String','Restore',...
1139            'FontSize',9,'FontWeight','demi',...
1140            'ToolTipString','Restore System to Save Set Parameters',...
1141            'Callback','orbgui(''RestoreSaveSet'');');     %RestoreSaveSet
1142
1143        %% Scaling of the BPMs
1144           
1145        x0=0.788*screen_wide; dx=0.015*screen_wide; dy=0.031*screen_high; y0=0.583*screen_high;
1146        SYS.BPMScale=uicontrol('Style','slider','Units','centimeters',...
1147            'Position',[x0 y0 dx dy],'Callback','orbgui(''ScaleBPMAxis'');',...
1148            'Max',1,'Min',-1,'SliderStep',[5e-1 2e-1],'Value',0);
1149
1150        SYS.BPMAutoScale=uicontrol('Style','pushbutton', ...
1151            'Units','centimeters',...
1152            'Position',[x0 y0 - 1.5  dx dy], ...
1153            'ForeGroundColor','k','String','A',...
1154            'FontSize',9,'FontWeight','demi',...
1155            'ToolTipString','Autoscales BPM axis',...
1156            'Callback','orbgui(''AutoScaleBPMAxis'');');  %autoscale
1157           
1158
1159        %% Scaling of the correctors
1160       
1161        y0=0.355*screen_high;
1162        SYS.CORScale=uicontrol('Style','slider','Units','centimeters',...
1163            'Position',[x0 y0 dx dy],'Callback','orbgui(''ScaleCORAxis'');',...
1164            'Max',1,'Min',-1,'SliderStep',[5e-1 5e-1],'Value',0);
1165
1166        SYS.BPMAutoScale=uicontrol('Style','pushbutton', ...
1167            'Units','centimeters',...
1168            'Position',[x0 y0 - 1.5  dx dy], ...
1169            'ForeGroundColor','k','String','A',...
1170            'FontSize',9,'FontWeight','demi',...
1171            'ToolTipString','Autoscales Corrector axis',...
1172            'Callback','orbgui(''AutoScaleCorrAxis'');');  %autoscale
1173
1174        setappdata(0,'SYS',SYS);
1175
1176        %==========================================================
1177    case 'BPMbox'                               %  BPMbox
1178        %==========================================================
1179        %BPM Dialog Box
1180        %located at bottom of main frame
1181        [screen_wide, screen_high]=screensizecm;
1182
1183        x0=0.013*screen_wide; dx=0.1640*screen_wide; y0=0.0158*screen_high; dy=0.17*screen_high;
1184        uicontrol('Style','frame','units', 'centimeters','Position',[x0 y0 dx dy]);     %main frame
1185
1186        %Static BPM text fields (labels)
1187%         dx2=0.0888*screen_wide;
1188        dy2=0.017*screen_high; dely=0.0175*screen_high;
1189
1190        x0=0.7*screen_wide; dx=0.085*screen_wide; y0=0.705*screen_high;
1191       
1192        uicontrol('Style','Frame','Units','centimeters','Position', ...
1193            [x0-0.05 y0-0.05 dx+0.075 dy2+0.075]);  %frame around toggle
1194
1195        SYS.togglebpm=uicontrol('Style','checkbox',...
1196            'units', 'centimeters', ...
1197            'Position',[x0 y0 dx dy2],...
1198            'String','Toggle BPMs',...
1199            'ToolTipString','Toggle BPMs between fit (green) and no-fit (yellow)',...
1200            'Callback','bpmgui(''ToggleMode'')');     %Radio BPM: Toggle Mode
1201
1202        uicontrol('Style','Frame','Units','centimeters','Position', ...
1203            [x0-1.5*dx-0.05 y0-0.05 dx+0.075 dy2+0.075]);  %frame around drag
1204
1205        SYS.dragbpm=uicontrol('Style','checkbox',...
1206            'units', 'centimeters', ...
1207            'Position', [x0 - 1.5*dx  y0 dx dy2], ...
1208            'String','Drag BPMs',...
1209            'ToolTipString','Enable BPMs for interactive drag',...
1210            'Callback','bpmgui(''DragMode'')');      %Radio BPM: Drag Mode
1211
1212
1213        x0=0.01856*screen_wide; dx=0.0576*screen_wide; y0=0.0247*screen_high; dy=0.01562*screen_high;
1214        uicontrol('Style','text',...
1215            'units', 'centimeters',...
1216            'Position',[x0 y0+1.7*dely dx dy],...
1217            'HorizontalAlignment','left',...
1218            'ToolTipString','BPM weight',...
1219            'String','Weight:');
1220
1221        uicontrol('Style','text',...
1222            'units', 'centimeters',...
1223            'Position',[x0 y0+2.7*dely dx dy],...
1224            'HorizontalAlignment','left',...
1225            'ToolTipString','Predicted Orbit (blue dash line value at BPM site)',...
1226            'String','Prediction:');
1227
1228        uicontrol('Style','text',...
1229            'units', 'centimeters',...
1230            'Position',[x0 y0+3.7*dely dx dy],...
1231            'HorizontalAlignment','left',...
1232            'ToolTipString','Requested orbit (BPM icon value at BPM site)',...
1233            'String','Desired:');
1234
1235        uicontrol('Style','text',...
1236            'units', 'centimeters',...
1237            'Position',[x0 y0+4.7*dely dx dy],...
1238            'HorizontalAlignment','left',...
1239            'ToolTipString','Offset from BPM icon to red line at BPM site',...
1240            'String','Offset:');
1241
1242        uicontrol('Style','text',...
1243            'units', 'centimeters',...
1244            'Position',[x0 y0+6*dely dx dy],...
1245            'HorizontalAlignment','left',...
1246            'ToolTipString','Reference orbit (red line value at BPM site)',...
1247            'String','Reference:');
1248
1249        uicontrol('Style','text',...
1250            'units', 'centimeters',...
1251            'Position',[x0 y0+7*dely dx dy],...
1252            'HorizontalAlignment','left',...
1253            'ToolTipString','Measured orbit (blue line value at BPM site)',...
1254            'String','Measured:');
1255
1256        uicontrol('Style','text',...
1257            'units', 'centimeters',...
1258            'Position',[x0 y0+8*dely dx dy],...
1259            'HorizontalAlignment','left',...
1260            'ToolTipString','Selected BPM name',...
1261            'String','BPM Name:');
1262
1263        %Dynamic BPM text fields (data)
1264        x0 = 0.09647*screen_wide; dx=0.075*screen_wide; %0.065
1265        y0 = 0.0247*screen_high; dy=0.0156*screen_high;
1266%         dx2 = 0.00037*screen_wide;
1267        dely=0.0175*screen_high;
1268
1269        SYS.bpmmeas =uicontrol('Style','text',...
1270            'units', 'centimeters',...
1271            'Position',[x0 y0+2.7*dely dx dy],...
1272            'BackGroundColor',[0.77 0.91 1.00],...
1273            'String','');                 %BPMmeas
1274
1275        SYS.bpmdes=uicontrol('Style','text',...
1276            'units', 'centimeters',...
1277            'Position',[x0 y0+3.7*dely dx dy],...
1278            'BackGroundColor',[0.77 0.91 1.00],...
1279            'String','');                 %BPMdes
1280
1281        %offset edit box
1282        SYS.bpmedit=uicontrol('Style','edit',...
1283            'units', 'centimeters',...
1284            'Position',[x0 y0+4.7*dely dx 1.2*dy],...
1285            'FontName','times','FontSize',8,...
1286            'Callback','bpmgui(''EditDesOrb'')',...
1287            'String','');                 %EditDesOrb
1288
1289        SYS.editbpmweight=uicontrol('Style','edit',...
1290            'units', 'centimeters',...
1291            'Position',[x0 y0+1.7*dely dx 1.2*dy],...
1292            'FontName','times','FontSize',8,...
1293            'Callback','bpmgui(''EditBPMWeight'')',...
1294            'String','');                 %EditBPMWeight
1295
1296        SYS.bpmref=uicontrol('Style','text',...
1297            'units', 'centimeters',...
1298            'Position',[x0 y0+6*dely dx dy],...
1299            'BackGroundColor',[0.77 0.91 1.00],...
1300            'String','');                 %BPMref
1301
1302
1303        SYS.bpmact=uicontrol('Style','text',...
1304            'units', 'centimeters',...
1305            'Position',[x0 y0+7*dely dx dy],...
1306            'BackGroundColor',[0.77 0.91 1.00],...
1307            'String','');                 %BPMact
1308
1309        SYS.bpmname=uicontrol('Style','text',...
1310            'units', 'centimeters',...
1311            'Position',[x0 y0+8*dely dx dy],...
1312            'BackGroundColor',[0.77 0.91 1.00],...
1313            'String','');                 %bpmname
1314
1315        %RMS Display
1316        x0=0.1*screen_wide; dx=0.035*screen_wide; y0=0.705*screen_high; dy=0.0175*screen_high;
1317
1318        uicontrol('Style','frame','Units','centimeters','Position', ...
1319            [x0-0.025 y0-0.025 2.2*dx+0.05 dy+0.05]); %frame around RMS
1320
1321        uicontrol('Style','text',...
1322            'Units','centimeters',...
1323            'Position',[x0 y0 dx dy],...
1324            'HorizontalAlignment','left',...
1325            'ToolTipString','RMS deviation of actual orbit from desired orbit',...
1326            'String',' rms:');  %Display RMS static
1327
1328        x0 = 0.135*screen_wide; dx = 0.04*screen_wide;
1329        SYS.bpmrms = uicontrol('Style','text',...
1330            'units','centimeters',...
1331            'Position',[x0 y0 dx dy],...
1332            'String','');       %RMS dynamic
1333
1334        x0=0.23*screen_wide; dx=0.035*screen_wide; y0=0.705*screen_high; dy=0.0175*screen_high;
1335
1336        uicontrol('Style','frame','Units','centimeters','Position', ...
1337            [x0-0.025 y0-0.025 2.4*dx+0.05 dy+0.05]); %frame around mean
1338
1339        uicontrol('Style','text',...
1340            'Units','centimeters',...
1341            'Position',[x0 y0 dx dy],...
1342            'HorizontalAlignment','left',...
1343            'ToolTipString','Mean deviation of actual orbit from desired orbit',...
1344            'String',' mean:');    %Display mean static
1345
1346        x0 = 0.27*screen_wide; dx = 0.04*screen_wide;
1347        SYS.bpmmean = uicontrol('Style','text',...
1348            'units','centimeters',...
1349            'Position',[x0 y0 dx dy],...
1350            'String',''); %mean dynamic
1351
1352        setappdata(0,'SYS',SYS);
1353
1354        %==========================================================
1355    case 'CorBox'                      % *** CorrectBox ***
1356        %==========================================================
1357        %frame for corrector uicontrols
1358        %located at bottom of main frame
1359        [screen_wide, screen_high]=screensizecm;
1360
1361        x0 = 0.2*screen_wide; dx=0.1650*screen_wide;
1362        y0=0.016*screen_high; dy=0.17*screen_high;
1363        uicontrol('Style','frame','Units','centimeters','Position',[x0 y0 dx dy]);  %main frame
1364
1365        %Static Corrector text fields (labels)
1366        x0=0.21*screen_wide;
1367        dx=0.06*screen_wide; y0=0.025*screen_high; dy=0.015*screen_high;
1368        dx2=0.1*screen_wide; dely=0.0175*screen_high;
1369        %dy2=0.017*screen_high;
1370
1371        %% Construction from bottom to up
1372        uicontrol('Style','pushbutton',...                  %Restore Correctors
1373            'Units','centimeters',...
1374            'Position',[1.2*x0 y0+0*dely dx2 dy],...
1375            'ForeGroundColor','k',...
1376            'String','Restore Correctors',...
1377            'ToolTipString','Restore corrector strengths in active plane',...
1378            'Callback','corgui(''RestoreCors'');');   %Restore Correctors
1379
1380        uicontrol('Style','pushbutton',...                 %Save Correctors
1381            'Units','centimeters',...
1382            'Position',[1.2*x0 y0+1.0*dely dx2 dy],...
1383            'ForeGroundColor','k',...
1384            'String','Save Correctors',...
1385            'ToolTipString','Save corrector strengths in active plane',...
1386            'Callback','corgui(''SaveCors'',SYS.plane);');   %save correctors for bump generation and restore
1387
1388       %display rf offset
1389        SYS.rftoggle = uicontrol('Style','radio',...
1390            'units', 'centimeters', ...
1391            'Position', [x0-dy*0.5 y0+dely 0.6*dx 1.2*dy], ...
1392            'Callback','respgui(''RFToggle'')','Value',0,...
1393            'ToolTipString','Toggle fitting for RF orbit component', ...
1394            'String','RF');
1395       
1396%         uicontrol('Style','pushbutton',...                %MakeOrbitSlider
1397%             'Units','centimeters',...
1398%             'Position',[1.1*x0 y0+0.4*dely dx2 dy],...
1399%             'ForeGroundColor','k',...
1400%             'String','Make Slider',...
1401%             'ToolTipString','Create Orbit Slider/Save File to Disk',...
1402%             'Callback','corgui(''MakeOrbitSlider'');'); %MakeOrbitSlider
1403       
1404        uicontrol('Style','text',...
1405            'Units','centimeters',...
1406            'Position',[x0 y0+2*dely dx dy],...
1407            'HorizontalAlignment','left',...
1408            'ToolTipString','RF value updated if RF correction',...
1409            'String','rf (MHz):');
1410
1411        uicontrol('Style','text',...
1412            'Units','centimeters',...
1413            'Position',[x0 y0+3*dely dx dy],...
1414            'HorizontalAlignment','left',...
1415            'ToolTipString','RF shift if correction',...
1416            'String','drf (MHz):');
1417
1418        uicontrol('Style','text',...
1419            'Units','centimeters',...
1420            'Position',[x0 y0+4*dely dx dy],...
1421            'HorizontalAlignment','left',...
1422            'ToolTipString','Requested corrector strength (red bar at corrector site)',...
1423            'String','Desired:');
1424
1425        uicontrol('Style','text',...
1426            'Units','centimeters',...
1427            'Position',[x0 y0+5*dely dx dy],...
1428            'HorizontalAlignment','left',...
1429            'ToolTipString','Incremental corrector strength (red bar - green bar)',...
1430            'String','Fit Increment:');
1431
1432        uicontrol('Style','text',...
1433            'Units','centimeters',...
1434            'Position',[x0 y0+6*dely dx dy],...
1435            'HorizontalAlignment','left',...
1436            'ToolTipString','Saved corrector value',...
1437            'String','Reference:');
1438
1439        uicontrol('Style','text',...
1440            'Units','centimeters',...
1441            'Position',[x0 y0+7*dely dx dy],...
1442            'HorizontalAlignment','left',...
1443            'ToolTipString','Measured corrector value (green/yellow bar at corrector site)',...
1444            'String','Measured:');
1445
1446       uicontrol('Style','text',...
1447            'Units','centimeters',...
1448            'Position',[x0 y0+8*dely dx dy],...
1449            'HorizontalAlignment','left',...
1450            'ToolTipString','Selected corrector name',...
1451            'String','Corr Name:');
1452         
1453
1454        %Dynamic corrector text fields (data)
1455        x0=0.28*screen_wide; dx=0.075*screen_wide; %0.065
1456
1457        SYS.hrf=uicontrol('Style','text',...
1458            'units','centimeters',...
1459            'Position',[x0 y0+2*dely dx dy],...
1460            'BackGroundColor',[0.77 0.91 1.00],...
1461            'String','');                 %CORreq
1462
1463        SYS.hdrf=uicontrol('Style','text',...
1464            'units','centimeters',...
1465            'Position',[x0 y0+3*dely dx dy],...
1466            'BackGroundColor',[0.77 0.91 1.00],...
1467            'String','');                 %CORreq
1468
1469        SYS.correq=uicontrol('Style','text',...
1470            'units','centimeters',...
1471            'Position',[x0 y0+4*dely dx dy],...
1472            'BackGroundColor',[0.77 0.91 1.00],...
1473            'String','');                 %CORreq
1474
1475        SYS.coroffset=uicontrol('Style','text',...
1476            'units','centimeters',...
1477            'Position',[x0 y0+5*dely dx dy],...
1478            'BackGroundColor',[0.77 0.91 1.00],...
1479            'String','');                 %CORoffset
1480
1481        SYS.corref=uicontrol('Style','text',...
1482            'units','centimeters',...
1483            'Position',[x0 y0+6*dely dx dy],...
1484            'BackGroundColor',[0.77 0.91 1.00],...
1485            'String','');                 %CORref
1486
1487        SYS.coract=uicontrol('Style','text',...
1488            'units','centimeters',...
1489            'Position',[x0 y0+7*dely dx dy],...
1490            'BackGroundColor',[0.77 0.91 1.00],...
1491            'String','');                 %CORact
1492
1493        SYS.corname=uicontrol('Style','text',...
1494            'units','centimeters',...
1495            'Position',[x0 y0+8*dely dx dy],...
1496            'BackGroundColor',[0.77 0.91 1.00],...
1497            'String','');               %CORname
1498
1499        x0=0.7*screen_wide; dx=0.07*screen_wide; y0=0.445*screen_high; dy=0.0175*screen_high;
1500        uicontrol('Style','Frame','Units','centimeters','Position', ...
1501            [x0-0.05 y0-0.05 dx+0.075 dy+0.075]);  %frame
1502
1503        SYS.togglecor=uicontrol('Style','checkbox','units', 'centimeters','Position', [x0 y0 dx dy],...
1504            'String','Toggle Corrs',...
1505            'ToolTipString','Toggle correctors for fit (green)/nofit (yellow)',...
1506            'Callback','corgui(''ToggleCor'')');        %Radio Corr: Toggle
1507
1508
1509        %RMS Display
1510        x0=0.055*screen_wide; dx=0.035*screen_wide;
1511        uicontrol('Style','frame','Units','centimeters','Position', ...
1512            [x0-0.025 y0-0.025 2.2*dx+0.05 dy+0.05]); %frame around RMS
1513
1514        uicontrol('Style','text','Units','centimeters','Position',[x0 y0 dx dy],...
1515            'HorizontalAlignment','left',...
1516            'ToolTipString','RMS value of correctors used for fit',...
1517            'String',' rms:');                          %rms static
1518
1519        SYS.corrms = uicontrol('Style','text','units','centimeters', ...
1520            'Position',[x0+dx y0 dx dy],'String','');   %rms dynamic
1521
1522        %Mean Display
1523        x0=0.16*screen_wide; dx=0.035*screen_wide;
1524        uicontrol('Style','frame','Units','centimeters','Position', ...
1525            [x0-0.025 y0-0.025 2.4*dx+0.05 dy+0.05]); %frame around mean
1526
1527        uicontrol('Style','text','Units','centimeters','Position',[x0 y0 dx dy],...
1528            'HorizontalAlignment','left',...
1529            'ToolTipString','RMS value of correctors used for fit',...
1530            'String',' mean:');                          %mean static
1531
1532        SYS.cormean = uicontrol('Style','text','units','centimeters', ...
1533            'Position',[x0+dx y0 dx dy],'String','');   %mean dynamic
1534        setappdata(0,'SYS',SYS);
1535
1536        %==========================================================
1537    case 'SVDBox'                               %  SVDBox
1538        %==========================================================
1539        %SVD Dialog Box
1540        %located at bottom of main frame
1541        [screen_wide, screen_high] = screensizecm;
1542
1543        x0=0.385*screen_wide; dx=0.16*screen_wide;
1544        y0=0.0158*screen_high; dy=0.17*screen_high;
1545
1546        uicontrol('Style','frame','Units','centimeters','Position',[x0 y0 dx dy]);     %main frame
1547
1548        x0=0.39*screen_wide; dx=0.09375*screen_wide;
1549        y0=0.018*screen_high; dy=0.02*screen_high;
1550%         dy2=0.022*screen_high;
1551        dely=0.02353*screen_high;
1552
1553        uicontrol('Style','text',...
1554            'units','centimeters',...
1555            'Position',[x0 y0+6*dely dx dy],...
1556            'HorizontalAlignment','left',...
1557            'ToolTipString','Choose number of singular values for fit',...
1558            'String','# Singular Values:');
1559
1560        uicontrol('Style','text',...
1561            'units','centimeters',...
1562            'Position',[x0 y0+5*dely dx*1.2 dy],...
1563            'HorizontalAlignment','left',...
1564            'ToolTipString','Fractional multiplier applied to all correctors (but not shown in fit)',...
1565            'String','Fraction of Correction:');
1566
1567
1568        x0 = 0.50*screen_wide; dx=0.032*screen_wide;
1569        default_nsvd = 56;
1570        % Edit box for number of singular values
1571        SYS.svdedit = uicontrol('Style','edit',...
1572            'Units','centimeters',...
1573            'Position',[x0+dx/2 y0+6*dely dx/1.5 dy],...
1574            'Callback','respgui(''SVDEdit'');',...
1575            'String',num2str(default_nsvd));   %default to one singular value
1576
1577        %Slider for number of singlar values  see %matlab help/slider
1578%         SYS.svdslide=uicontrol('Style','slider',...
1579%             'Units','centimeters',...
1580%             'Position',[x0-dx/2 y0+6*dely dx/1.2 dy],...
1581%             'Callback','respgui(''SVDSlider'');',...
1582%             'Max',default_nsvd,'Min',1,...
1583%             'SliderStep',[1/(default_nsvd-1),1/(default_nsvd-1)],...
1584%             'Value',round(default_nsvd/2));
1585
1586        %Edit box for fraction of correction
1587        SYS.fract=uicontrol('Style','edit',...
1588            'Units','centimeters',...
1589            'Position',[x0+dx/2 y0+5*dely dx/1.5 dy],...
1590            'Callback','corgui(''Fract'');',...
1591            'String',num2str(1));
1592
1593        setappdata(0,'SYS',SYS);
1594
1595        %==========================================================
1596    case 'KnobBox'                               %  KnobBox
1597        %==========================================================
1598
1599        [screen_wide, screen_high]=screensizecm;
1600
1601        x0=0.4170*screen_wide; y0=0.006*screen_high;
1602        dy=0.021*screen_high; dely=0.02353*screen_high;
1603
1604        dx=0.14*screen_wide;
1605        SYS.addignknob=uicontrol('Style','pushbutton',...
1606            'Units','centimeters',...
1607            'Position',[x0 y0+3*dely dx dy],...
1608            'ForeGroundColor','k',...
1609            'String','Assign Knob',...
1610            'ToolTipString','Save knob corrector strengths in active plane',...
1611            'Callback','corgui(''AssignKnob'');');   %Assign Knob
1612
1613        uicontrol('Style','text',...
1614            'units','centimeters',...
1615            'Position',[1.03*x0 y0+1.8*dely dx/2 dy],...
1616            'HorizontalAlignment','left',...
1617            'ToolTipString','Edit corrector knob strength',...
1618            'String','Knob Amplitude:');
1619
1620        SYS.knobedit=uicontrol('Style','edit',...
1621            'Units','centimeters',...
1622            'Position',[1.25*x0 y0+1.9*dely dx/5 dy],...
1623            'Callback','corgui(''KnobEdit'');',...
1624            'String',num2str(0));
1625
1626        setappdata(0,'SYS',SYS);
1627
1628        %=============================================================
1629    case 'Plane'                             % *** Plane ***
1630        %=============================================================
1631        %Toggle the x/y fitting displays
1632        corgui('HidePlots');        %Note: separate handles for corrector icons in each plane
1633        %hide existing icons before switching planes (visible/off)
1634
1635        if  get(SYS.xplane,'Value')==0 && SYS.plane==1     %was in horizontal mode, stay in horizontal
1636            set(SYS.xplane,'Value',1);
1637        elseif get(SYS.yplane,'Value')==0 && SYS.plane==2     %%was in vertical mode, stay in vertical
1638            set(SYS.yplane,'Value',1);
1639        elseif get(SYS.yplane,'Value')==1 && SYS.plane==1     %%was in horizontal mode, switch to vertical
1640            SYS.plane=2;           %horizontal
1641            set(SYS.xplane,'Value',0);
1642        elseif get(SYS.xplane,'Value')==1 && SYS.plane==2     %%was in vertical mode, switch to horizontal
1643            SYS.plane=1;           %horizontal
1644            set(SYS.yplane,'Value',0);
1645        end
1646
1647        setappdata(0,'SYS',SYS);
1648
1649        if RSP(2).rfflag  % disable RF correction for vertical plane
1650            respgui('RFToggle');           
1651        end
1652
1653        corgui('ShowPlots');        %visible/on
1654        orbgui('RefreshOrbGUI');
1655
1656        %=============================================================
1657    case 'TogglePlane'                       % *** TogglePlane ***
1658        %=============================================================
1659        %Toggle plane flag
1660        SYS.plane=1+mod(SYS.plane,2);
1661        setappdata(0,'SYS',SYS);
1662
1663        %=============================================================
1664    case 'RefreshOrbGUI'                   % *** RefreshOrbGUI ***
1665        %=============================================================
1666
1667        respgui('SolveSystem');    %75 ms in simulator
1668        respgui('UpdateFit');
1669
1670        %disp('update fields');    %15 ms in simulator
1671        %Set Defaults
1672        set(SYS.togglebpm,'Value',0);        %default to display only
1673        set(SYS.dragbpm,  'Value',0);        %default to no drag
1674        set(SYS.togglecor,'Value',0);        %default to no toggle
1675        set(SYS.showresp, 'Checked', 'Off'); %default to no resp display
1676        set(SYS.showeig,  'Checked', 'Off'); %default to no eig display
1677       
1678        %default to no rf orbit subtraction
1679        set(SYS.rftoggle, 'Value', 0);
1680        set(SYS.hdrf,'String', '0.0');
1681        set(SYS.fract,'String', num2str(COR(plane).fract));
1682       
1683        %re-write text fields
1684        if BPM(plane).mode == 1, set(SYS.togglebpm,'Value',1); end;     %toggle
1685        if BPM(plane).mode == 2, set(SYS.dragbpm,'Value',1); end;       %drag
1686        if COR(plane).mode == 1, set(SYS.togglecor,'Value',1); end;     %toggle
1687        if strcmpi(RSP(plane).disp(1:2),'on'), set(SYS.showresp,'Checked', 'on'); end;
1688        if strcmpi(RSP(plane).eig(1:2),'on'), set(SYS.showeig, 'Checked', 'on'); end;
1689        if RSP(plane).rfflag  == 1 % RF part
1690            set(SYS.rftoggle,'Value',1);
1691            set(SYS.hdrf,'String', num2str(SYS.drf));
1692        end;
1693
1694        %units for bpm and corrector plot abscissa
1695        if strcmp(SYS.xscale,'meter')
1696            SYS.xlimax = SYS.maxs;
1697        elseif strcmp(SYS.xscale,'phase')
1698            SYS.xlimax = SYS.maxphi(plane);
1699        end
1700
1701        setappdata(0,'SYS',SYS);
1702
1703        %Update BPM Displays (100 ms in simulator)
1704        bpmgui('ClearPlots');            %remove eigenvector, response and fit plots
1705        if BPM(plane).scalemode == 0     %manual
1706            set(SYS.ahbpm,'YLim',[-BPM(plane).ylim,BPM(plane).ylim]);
1707        end
1708        bpmgui('RePlot');                %ref, des, act, fit, icons, limits, bar
1709        bpmgui('UpdateBPMBox');
1710
1711        %Update Corrector Displays (100 ms in simulator)
1712        if COR(plane).scalemode == 0     %manual
1713            set(SYS.ahcor,'YLim',[-COR(plane).ylim,COR(plane).ylim]);   end
1714        corgui('RePlot');                %clear, actual, fit, bar, limits
1715        corgui('UpdateCorBox');
1716
1717        switch SYS.algo
1718            case 'SVD'
1719                %Update SVD Displays
1720                respgui('PlotSVD');
1721                set(SYS.svdedit,'String',num2str(RSP(plane).nsvd));
1722
1723%                 %slider_step=[arrow_step, trough_step] normalized to range
1724%                 if RSP(plane).nsvdmax > 1
1725%                     slider_step = [1/(RSP(plane).nsvdmax-1),1/(RSP(plane).nsvdmax-1)];
1726%                     set(SYS.svdslide,'Visible','On','Value',RSP(plane).nsvd,'Max',RSP(plane).nsvdmax,'Min',1,'SliderStep',slider_step);
1727%                     set(SYS.svdedit,'Value',RSP(plane).nsvd);
1728%                 else
1729%                     set(SYS.svdslide,'Visible','Off');
1730%                 end
1731
1732        end   %end algo case
1733
1734        orbgui('UpdateParameters');
1735
1736        orbgui('LBox',' Finished acquisition and display refresh');
1737
1738        %=============================================================
1739    case 'Relative'                             % *** Relative ***
1740        %=============================================================
1741        %Toggles orbit display absolute/relative
1742        %If reference orbit read from file then subtract
1743        %Otherwise reference orbit is zero
1744
1745        if  get(SYS.abs,'Value') == 0 && SYS.relative == 1     %was in absolute mode, stay in absolute
1746            set(SYS.abs,'Value',1);
1747        elseif get(SYS.rel,'Value') == 0 && SYS.relative == 2  %was in relative mode, stay in relative
1748            set(SYS.rel,'Value',1);
1749        elseif get(SYS.rel,'Value') == 1 && SYS.relative == 1  %was in absolute mode, switch to relative
1750            SYS.relative = 2;           %relative mode
1751            set(SYS.abs,'Value',0);
1752            BPM(1).abs = BPM(1).ref;    %BPM.act and BPM.des have BPM.abs subtracted off at plot time
1753            BPM(2).abs = BPM(2).ref;
1754        elseif get(SYS.abs,'Value') == 1 && SYS.relative == 2  %was in relative mode, switch to absolute
1755            SYS.relative = 1;           %absolute mode
1756            set(SYS.rel,'Value',0);
1757            BPM(1).abs = zeros(size(BPM(1).name,1),1);
1758            BPM(2).abs = zeros(size(BPM(2).name,1),1);
1759        end
1760
1761        setappdata(0,'SYS',SYS);
1762        setappdata(0,'BPM',BPM);
1763
1764        bpmgui('RePlot');     %do not replot correctors for relative
1765        bpmgui('BPMBar');
1766        bpmgui('UpdateBPMBox');
1767       
1768        %==========================================================
1769    case 'Abort'                                % *** Abort ***
1770        %==========================================================
1771        %callback of corrector menu
1772        %in case user aborts response matrix measurement or feedback
1773        SYS.abort = 1;
1774        setappdata(0,'SYS',SYS);
1775
1776        %==========================================================
1777    case 'ScaleBPMAxis'                  % *** ScaleBPMAxis ***
1778        %==========================================================
1779        axes(SYS.ahbpm);
1780        a = axis;
1781        ChangeFactor = 2;
1782       
1783        if get(SYS.BPMScale,'Value') < 0
1784            del = (ChangeFactor-1)*(a(4)-a(3));
1785        else
1786            del = (1/ChangeFactor-1)*(a(4)-a(3));
1787        end
1788        axis([a(1) a(2) a(3)-del/2 a(4)+del/2]);
1789        set(SYS.BPMScale, 'Value', 0);
1790
1791        BPM(plane).ylim = a(4)+del/2;
1792        setappdata(0,'BPM',BPM);
1793
1794        %rescale BPMBar
1795        yd = [-BPM(plane).ylim/4,BPM(plane).ylim/4];
1796        set(SYS.lhbid,'YData',yd);
1797
1798        %==========================================================
1799    case 'AutoScaleBPMAxis'   %  Autoscale BPMaxis
1800        %==========================================================
1801       
1802        % switchoff black cross
1803        set(SYS.lhbid,'XData',[],'YData',[]);
1804       
1805        % Auto-scale y-axis of BPM plot
1806        val0 = axis(SYS.ahbpm);
1807        axis(SYS.ahbpm, 'tight');
1808        val  = axis(SYS.ahbpm);
1809        BPM(plane).ylim = max(abs(val(3:4)))*1.2;
1810        axis(SYS.ahbpm, [val0(1:2) BPM(plane).ylim*[-1 1]]);
1811        setappdata(0,'BPM',BPM);
1812       
1813        % Set back black cross in the BPM window
1814        bpmgui('BPMBar');
1815       
1816        %==================================================================
1817    case 'AutoScaleCorrAxis'                  % *** AutoScaleCORAxis ***
1818        %==================================================================
1819       
1820        % switchoff black cross
1821        set(SYS.lhcid,'XData',[],'YData',[]);
1822       
1823        % Auto-scale y-axis of COR plot
1824        val0 = axis(SYS.ahcor);
1825        axis(SYS.ahcor, 'tight');
1826        val  = axis(SYS.ahcor);
1827        COR(plane).ylim = max(abs(val(3:4)))*1.2;
1828        axis(SYS.ahcor, [val0(1:2) COR(plane).ylim*[-1 1]]);
1829        setappdata(0,'COR',COR);
1830         
1831        % Set back black cross in the BPM window
1832        corgui('CorBar');
1833       
1834        %==========================================================
1835    case 'ScaleCORAxis'                  % *** ScaleCORAxis ***
1836        %==========================================================
1837        axes(SYS.ahcor);
1838        a = axis;
1839        ChangeFactor = 2;
1840       
1841        if get(SYS.CORScale,'Value') < 0
1842            del = (ChangeFactor-1)*(a(4)-a(3));
1843        else
1844            del = (1/ChangeFactor-1)*(a(4)-a(3));
1845        end
1846       
1847        axis([a(1) a(2) a(3)-del/2 a(4)+del/2]);
1848        set(SYS.CORScale, 'Value', 0);
1849
1850        COR(plane).ylim = a(4)+del/2;
1851        setappdata(0,'COR',COR);
1852
1853        %rescale CORBar
1854        yd = [-COR(plane).ylim/4, COR(plane).ylim/4];
1855        set(SYS.lhcid,'YData',yd);
1856
1857        %===========================================================
1858%     case 'StartLoc'              %*** StartLoc ***
1859%         %===========================================================
1860%         %cross cursor to select a new start location on Z axis.
1861%         %must put bpms into 'Select' mode (display only) so
1862%         %bpm nearest cursor will not get toggled or dragged
1863%         %NOTE: tried setting mode to zero and then reset after sizing plot but still toggles bpm state.
1864%
1865%         mode=BPM(plane).mode;     %save mode for after NewStart
1866%         BPM(plane).mode=0;        %put in select mode
1867%         setappdata(0,'BPM',BPM);
1868%
1869%         set(orbfig,'Pointer','fullcross',...
1870%             'WindowButtonDownFcn','orbgui(''NewStart'')');
1871%         BPM(plane).mode=mode;     %restore mode
1872%         setappdata(0,'BPM',BPM);
1873%
1874%         %===========================================================
1875%     case 'NewStart'              %*** NewStart ***
1876%         %===========================================================
1877%         %sets the current cursor position as the new Start Location of the plot.
1878%         cpa = get(SYS.ahbpm,'CurrentPoint');
1879%         limits = get(SYS.ahbpm,'XLim');
1880%         set(SYS.ahbpm,'XLim',[cpa(1,1) limits(1,2)]);   %Change start, leave stop alone
1881%         set(SYS.ahcor,'XLim',[cpa(1,1) limits(1,2)]);   %Change start, leave stop alone
1882%         set(orbfig, 'Pointer','arrow',...
1883%             'WindowButtonDownFcn','');
1884%
1885%         %===========================================================
1886%     case 'StopLoc'              %*** StopLoc ***
1887%         %===========================================================
1888%         %creates a cross cursor for the user to select a new stopping location on Z axis.
1889%         mode=BPM(plane).mode;     %save mode for after NewStart
1890%         BPM(plane).mode=0;        %put in select mode
1891%         setappdata(0,'BPM',BPM);
1892%
1893%         set(orbfig,'Pointer','fullcross',...
1894%             'WindowButtonDownFcn','orbgui(''NewStop'')');
1895%         BPM(plane).mode=mode;     %restore mode
1896%         setappdata(0,'BPM',BPM);
1897%
1898%         %===========================================================
1899%     case 'NewStop'              %*** NewStop ***
1900%         %===========================================================
1901%         %set the current cursor position as the new Stop Location of the plot.
1902%         cpa = get(SYS.ahbpm,'CurrentPoint');
1903%         limits = get(SYS.ahbpm,'XLim');
1904%         set(SYS.ahbpm,'XLim',[limits(1,1) cpa(1,1)]);    %Change stop, leave start alone
1905%         set(SYS.ahcor,'XLim',[limits(1,1) cpa(1,1)]);    %Change stop, leave start alone
1906%         set(orbfig, 'Pointer','arrow',...
1907%             'WindowButtonDownFcn','');
1908%
1909        %===========================================================
1910    case 'BPMPlotScale'                    %*** BPMPlotScale ***
1911        %===========================================================
1912        %Select vertical limit for BPM plot
1913
1914        %Clear previous  figure
1915        bpmscalefig = findobj(0,'tag','bpmplotscale');
1916        if ~isempty(bpmscalefig), delete(bpmscalefig); end
1917
1918        figure('Position',[600 600 400 200],...
1919            'NumberTitle','off',...
1920            'Name','BPM Plot Control',...
1921            'Tag','bpmplotscale',...
1922            'MenuBar','none');
1923
1924        val=0;
1925        if BPM(plane).scalemode==0; val=1; end              %manual
1926        uicontrol('Style','radio',...
1927            'Position',[20 150 120 20],...
1928            'String','Manual Scale',...
1929            'Tag','bpmmanual',...
1930            'Value',val,...
1931            'ToolTipString','BPM plot scale Manual',...
1932            'Callback','orbgui(''BPMScaleType'', ''0'')');
1933
1934        uicontrol('Style','text',...
1935            'String','Vertical Axis Limit: ','HorizontalAlignment','left',...
1936            'Position',[150 150 100 20]);
1937
1938        uicontrol('Style','edit',...
1939            'tag','bpmscale',...
1940            'String',num2str(BPM(plane).ylim),'HorizontalAlignment','left',...
1941            'Position',[250 150 50 20]);
1942
1943
1944        val=0;
1945        if BPM(plane).scalemode==1; val=1; end              %auto
1946        uicontrol('Style','radio',...
1947            'Position',[20 120 120 20],...
1948            'String','Auto Scale','HorizontalAlignment','left',...
1949            'Tag','bpmauto',...
1950            'Value',val,...
1951            'ToolTipString','BPM plot scale Auto',...
1952            'Callback','orbgui(''BPMScaleType'', ''1'')');
1953
1954        val=0;
1955        if strcmp(SYS.xscale,'meter'); val=1; end           %s-position
1956        uicontrol('Style','radio',...
1957            'Position',[20 90 150 20],...
1958            'String','Plot vs. s-position',...
1959            'Tag','plotspos',...
1960            'Value',val,...
1961            'ToolTipString','Plot BPM vs. the s-position',...
1962            'Callback','orbgui(''PlotSPosition'')');
1963
1964        val=0;
1965        if strcmp(SYS.xscale,'phase'); val=1; end          %betatron phase
1966        uicontrol('Style','radio',...
1967            'Position',[20 60 150 20],...
1968            'String','Plot vs. Phase',...
1969            'Tag','plotphase',...
1970            'Value',val,...
1971            'ToolTipString','Plot BPMs vs. the phase',...
1972            'Callback','orbgui(''PlotPhase'')');
1973
1974        uicontrol('Style','pushbutton',...                  %apply
1975            'String','Apply',...
1976            'Position',[150 20 50 20],...
1977            'Callback','orbgui(''ProcessBPMScale'');');
1978
1979        uicontrol('Style','pushbutton',...                  %cancel
1980            'String','Cancel',...
1981            'Callback','delete(gcf)',...
1982            'Position',[210 20 50 20]);
1983
1984        %===========================================================
1985    case 'ProcessBPMScale'              %*** ProcessBPMScale ***
1986        %===========================================================
1987        %select vertical limits for BPM axis (symetric limits)
1988
1989        h    = findobj(0,'Tag','bpmscale');
1990        ylim = str2double(get(h,'String'));
1991
1992        if isempty(ylim)
1993            ylim = BPM(plane).ylim;
1994        end
1995
1996        hmanual = findobj(0,'Tag','bpmmanual');
1997        set(h,'Value',1);
1998        hauto = findobj(0,'Tag','bpmauto');
1999       
2000        if BPM(plane).scalemode == 0  %manual mode
2001            BPM(plane).ylim = ylim;
2002
2003            set(SYS.ahbpm,'YLim', ylim*[-1, 1]);
2004
2005            set(hmanual,'Value',1);
2006            set(hauto,'Value',0);
2007        else                        %auto mode
2008            BPM(plane).ylim = ylim;
2009
2010            set(hmanual,'Value',0);
2011            set(hauto,'Value',1);
2012        end
2013
2014        setappdata(0,'BPM',BPM);
2015
2016        bpmgui('BPMBar');
2017
2018        delete(gcf);
2019
2020
2021        %===========================================================
2022    case 'PlotSPosition'                    %*** PlotSPosition ***
2023        %===========================================================
2024        %plot BPMs and correctors in terms of s-position
2025        h1 = findobj(0,'tag','plotspos');
2026        h2 = findobj(0,'tag','plotphase');
2027
2028        if get(h1,'Value') == 0 && get(h2,'Value') == 0   %...default to meters
2029            set(h2,'Value',1);
2030            SYS.xscale = 'phase';
2031            SYS.xlimax = SYS.maxphi(plane);
2032            set(get(SYS.ahcor,'Xlabel'),'string','Betatron Phase (rad/(2pi))');
2033        else
2034            SYS.xscale = 'meter';
2035            set(h1,'Value',1);
2036            set(h2,'Value',0);
2037            SYS.xlimax = SYS.maxs;
2038            set(get(SYS.ahcor,'Xlabel'),'string','Position in Storage Ring (m)');
2039        end
2040
2041        setappdata(0,'SYS',SYS);
2042
2043        h  = allchild(get(SYS.allmachine,'Parent'));
2044        id = strcmpi(get(h,'Checked'),'on');
2045        orbgui('plotxaxis',get(h(id),'Tag'));
2046       
2047        bpmgui('RePlot');
2048        corgui('RePlot');
2049
2050        %===========================================================
2051    case 'PlotPhase'                    %*** PlotPhase ***
2052        %===========================================================
2053        %plot BPMs and correctors in terms of phase
2054        h1 = findobj(0,'tag','plotspos');
2055        h2 = findobj(0,'tag','plotphase');
2056
2057        if get(h1,'Value') == 0 && get(h2,'Value') == 0   %...default to meters
2058            set(h1,'Value',1);
2059            SYS.xscale = 'meter';
2060            SYS.xlimax = SYS.maxs;
2061            set(get(SYS.ahcor,'Xlabel'),'string','Position in Storage Ring (m)');
2062        else % Phase
2063            SYS.xscale = 'phase';
2064            set(h1,'Value',0);
2065            set(h2,'Value',1);
2066            SYS.xlimax   = SYS.maxphi(plane);
2067            set(get(SYS.ahcor,'Xlabel'),'string','Betatron Phase (rad)');
2068        end
2069       
2070        setappdata(0,'SYS',SYS);
2071
2072        h  = allchild(get(SYS.allmachine,'Parent'));
2073        id = strcmpi(get(h,'Checked'),'on');
2074        orbgui('plotxaxis',get(h(id),'Tag'));
2075       
2076        bpmgui('RePlot');
2077        corgui('RePlot');
2078
2079        %===========================================================
2080    case 'BPMScaleType'                    %*** BPMScaleType ***
2081        %===========================================================
2082        %toggle to manual mode for BPM vertical axis
2083        stype = str2double(varargin{1});    % stype=0 for manual,   stype=1 for auto
2084
2085        %presently in manual mode, toggle to auto
2086        if (BPM(plane).scalemode == 0 && stype==0) ||...
2087                (BPM(plane).scalemode==0 && stype==1)
2088            h=findobj(0,'Tag','bpmmanual');
2089            set(h,'Value',0);
2090            h=findobj(0,'Tag','bpmauto');
2091            set(h,'Value',1);
2092            BPM(plane).scalemode = 1;
2093            setappdata(0,'BPM',BPM);
2094            bpmgui('ylimits');
2095            bpmgui('RePlot');
2096            return
2097        end
2098
2099        %presently in auto mode, toggle to manual
2100        if (BPM(plane).scalemode==1 && stype==0) ||...
2101                (BPM(plane).scalemode==1 && stype==1)
2102            h=findobj(0,'Tag','bpmmanual');
2103            set(h,'Value',1);
2104            h=findobj(0,'Tag','bpmauto');
2105            set(h,'Value',0);
2106            BPM(plane).scalemode=0;
2107            setappdata(0,'BPM',BPM);
2108            ylim=BPM(plane).ylim;
2109            set(SYS.ahbpm,'YLim',[-ylim,ylim]);
2110            bpmgui('RePlot');
2111            return
2112        end
2113
2114        %===========================================================
2115    case 'CorPlotScale'                    %*** CorPlotScale ***
2116        %===========================================================
2117        %Select vertical limit for Corrector plot
2118
2119        %Clear previous  figure
2120        corscalefig = findobj(0,'tag','corplotscale');
2121        if ~isempty(corscalefig), delete(corscalefig); end
2122
2123        figure('Position',[600 600 400 200],...
2124            'NumberTitle','off','Tag','corplotscale',...
2125            'Name','COR Vertical Axis Limits',...
2126            'MenuBar','none');
2127
2128        val = 0;
2129        if COR(plane).scalemode == 0, val = 1; end   %manual
2130        uicontrol('Style','radio',...
2131            'Position',[20 150 120 20],...
2132            'String','Manual Scale',...
2133            'Tag','cormanual',...
2134            'Value',val,...
2135            'ToolTipString','Corrector plot scale Manual',...
2136            'Callback','orbgui(''CORScaleType'', ''0'')');
2137
2138        val = 0;
2139        if COR(plane).scalemode == 1, val = 1; end
2140        uicontrol('Style','radio',...
2141            'Position',[20 120 120 20],...
2142            'String','Auto Scale','HorizontalAlignment','left',...
2143            'Tag','corauto',...
2144            'Value',val,...
2145            'ToolTipString','Corrector plot scale Auto',...
2146            'Callback','orbgui(''CORScaleType'', ''1'')');     %auto
2147
2148        uicontrol('Style','text',...
2149            'String','Vertical Axis Limit: ','HorizontalAlignment','left',...
2150            'Position',[150 150 100 20]);
2151
2152        uicontrol('Style','edit',...
2153            'tag','corscale',...
2154            'String',num2str(COR(plane).ylim),'HorizontalAlignment','left',...
2155            'Position',[250 150 50 20]);
2156
2157        uicontrol('Style','pushbutton',...                    %apply
2158            'String','Apply',...
2159            'Position',[150 20 50 20],...
2160            'Callback','orbgui(''ProcessCORScale'');');
2161
2162        uicontrol('Style','pushbutton',...                    %cancel
2163            'String','Cancel',...
2164            'Callback','delete(gcf)',...
2165            'Position',[210 20 50 20]);
2166
2167        %===========================================================
2168    case 'CORScaleType'                    %*** CORScaleType ***
2169        %===========================================================
2170        %toggle to manual mode for COR vertical axis       
2171       
2172        % stype=0 for manual, stype=1 for auto
2173        stype = str2double(varargin{1});   
2174
2175        hmanual = findobj(0,'Tag','cormanual');
2176        hauto   = findobj(0,'Tag','corauto');
2177
2178        %presently in manual mode, toggle to auto
2179        if (COR(plane).scalemode == 0 && stype == 0) ||...
2180                (COR(plane).scalemode == 0 && stype == 1)
2181            set(hmanual,'Value',0);
2182            set(hauto,  'Value',1);
2183            COR(plane).scalemode=1;
2184            setappdata(0,'COR',COR);
2185            corgui('ylimits');
2186            corgui('RePlot');
2187            return
2188        end
2189
2190        %presently in auto mode, toggle to manual
2191        if (COR(plane).scalemode==1 && stype==0) ||...
2192                (COR(plane).scalemode==1 && stype==1)
2193            set(hmanual,'Value',1);
2194            set(hauto,'Value',0);
2195            COR(plane).scalemode=0;
2196            setappdata(0,'COR',COR);
2197            ylim=COR(plane).ylim;
2198            set(SYS.ahcor,'YLim',[-ylim,ylim]);
2199            corgui('RePlot');
2200            return
2201        end
2202
2203        %===========================================================
2204    case 'ProcessCORScale'              %*** ProcessCORScale ***
2205        %===========================================================
2206        h = findobj(0,'Tag','corscale');
2207        ylim = str2double(get(h,'String'));
2208        if isempty(ylim), ylim = COR(plane).ylim; end
2209
2210        if COR(plane).scalemode == 0  %manual mode
2211            COR(plane).ylim=ylim;
2212            setappdata(0,'COR',COR);
2213            set(SYS.ahcor,'YLim',[-ylim,ylim]);
2214
2215            h=findobj(0,'Tag','cormanual');
2216            set(h,'Value',1);
2217            h=findobj(0,'Tag','corauto');
2218            set(h,'Value',0);
2219
2220
2221        else                        %auto mode
2222            COR(plane).ylim=ylim;
2223            setappdata(0,'COR',COR);
2224            h=findobj(0,'Tag','cormanual');
2225            set(h,'Value',0);
2226            h=findobj(0,'Tag','corauto');
2227            set(h,'Value',1);
2228        end
2229
2230        corgui('CorBar');
2231
2232        delete(gcf);
2233
2234        %=============================================================
2235    case 'LstBox'                               %  ListBox
2236        %==========================================================
2237        %create list box to display program output dialog
2238        ts = ['Program Start-Up: ' datestr(now,0)];
2239        [screen_wide, screen_high]=screensizecm;
2240
2241        x0=0.013*screen_wide; y0=0.2000*screen_high;
2242        dx=0.3061*screen_wide; dy=0.04817*screen_high;
2243
2244        SYS.lstbox = uicontrol('Style','list','Units','centimeters', ...
2245            'Position',[x0 y0 dx dy],'String',{ts});
2246
2247%         %=============================================================
2248%     case 'RestoreFileBox'                        %  RestoreFileBox
2249%         %=============================================================
2250%         %create list box to display program output dialog
2251%         [screen_wide, screen_high]=screensizecm;
2252%
2253%         x0=0.63*screen_wide; y0=0.210*screen_high; dx=0.15*screen_wide; dy=0.025*screen_high;
2254%
2255%         uicontrol('Style','Text',...
2256%             'Units','centimeters',...
2257%             'Position',[x0 y0 dx dy],...
2258%             'String',' ',...
2259%             'tag','restorefile');
2260
2261        %===========================================================
2262    case 'LBox'                          %*** LBox ***
2263        %===========================================================
2264        %load latest sequence of strings into graphical display listbox
2265        comment = varargin{1};
2266        ts      = datestr(now,0);
2267        addstr  = {[ts  ': ' comment]};
2268        h       = SYS.lstbox;
2269        str     = get(h,'String');
2270        str     = [str; addstr];
2271        ione    = size(str,1);
2272        nentry  = 50;
2273       
2274        if ione >= nentry                %keep only top entries
2275            str = str(ione-nentry+1:ione,1);
2276            ione = size(str,1);
2277        end
2278       
2279        set(h,'String',str,'listboxtop',ione);
2280
2281        % % % if ~isempty(SYS.SYSLogfid)              %write to log file
2282        % % %    str=char(addstr{1});
2283        % % %    fprintf(SYS.SYSLogfid,'%s\n',str);
2284        % % % end
2285
2286%         %==========================================================
2287%     case 'StartPatchActive'                  % StartPatchActive
2288%         %==========================================================
2289%         %activate start patch in element icon/zoom bar
2290%         set(orbfig,'WindowButtonMotionFcn','orbgui(''MoveStartPatch'')',...
2291%             'WindowButtonUpFcn',    'orbgui(''StartPatchUp'')');
2292%
2293%         %==========================================================
2294%     case 'MoveStartPatch'                    % MoveStartPatch
2295%         %==========================================================
2296%         xpos=orbgui('GetStartPos',0.02);
2297%         orbgui('SetStartPatch',xpos);
2298
2299        %==========================================================
2300%     case 'GetStartPos'                    % GetStartPos
2301%         %==========================================================
2302%         %find starting position and check agains stop position
2303%         stoplim=varargin(1); stoplim=stoplim{1};
2304%         %check requested point to right of zero
2305%         cpa = get(SYS.ahpos, 'CurrentPoint');
2306%         xpos = cpa(1);
2307%         if xpos<=0.0055
2308%             xpos=0.0055;
2309%         end
2310%
2311%         %check requested point to left of stop patch
2312%         h=findobj(0,'Tag','stoppatch');
2313%         xlim=get(h,'XData');   %x-positions of three patch corners
2314%         if xpos>=xlim(1)-stoplim;
2315%             xpos =xlim(1)-stoplim;
2316%         end
2317%         varargout{1}=xpos;
2318%
2319%         %==========================================================
2320%     case 'SetStartPatch'                     % SetStartPatch
2321%         %==========================================================
2322%         %move the patch to mouse position
2323%         xpos=varargin{1};
2324%         h=findobj(orbfig,'tag','startpatch');
2325%         xdata = [xpos-0.005 xpos+0.005 xpos-0.005];
2326%         set(h, 'XData', xdata);
2327%
2328%         %==========================================================
2329%     case 'StartPatchUp'                       %StartPatchUp
2330%         %==========================================================
2331%         %sequence to execute when startpatch let up
2332%         xpos=orbgui('GetStartPos',0.01);
2333%         orbgui('SetStartPatch',xpos);
2334%
2335%         %change BPM and corrector plot limits
2336%         limits = get(SYS.ahbpm,'XLim');
2337%         set(SYS.ahbpm,'XLim',[SYS.xlimax*xpos limits(1,2)]);   %Change start, leave stop alone
2338%         set(SYS.ahcor,'XLim',[SYS.xlimax*xpos limits(1,2)]);
2339%         corgui('PlotAct');
2340%         corgui('PlotFit');
2341%         set(orbfig, 'Pointer','arrow',...
2342%             'WindowButtonMotionFcn','','WindowButtonUpFcn','');
2343%
2344%         %==========================================================
2345%     case 'StopPatchActive'                  % StopPatchActive
2346%         %==========================================================
2347%         set(orbfig,'WindowButtonMotionFcn','orbgui(''MoveStopPatch'')',...
2348%             'WindowButtonUpFcn',    'orbgui(''StopPatchUp'')');
2349%
2350%         %==========================================================
2351%     case 'MoveStopPatch'                    % MoveStopPatch
2352%         %==========================================================
2353%         xpos=orbgui('GetStopPos',0.01);
2354%         orbgui('SetStopPatch',xpos);
2355%
2356%         %==========================================================
2357%     case 'GetStopPos'                    % GetStopPos
2358%         %==========================================================
2359%         stoplim=varargin(1); stoplim=stoplim{1};
2360%         %check requested point to left of the x-limit
2361%         cpa = get(SYS.ahpos, 'CurrentPoint');
2362%         xpos = cpa(1);
2363%         if xpos>=(1.0 - 0.005)
2364%             xpos =(1.0 - 0.005);
2365%         end
2366%
2367%         %check requested point to right of start patch
2368%         h=findobj(orbfig,'tag','startpatch');
2369%         xlim=get(h,'XData');   %x-positions of three patch corners
2370%         if xpos<=xlim(1)+stoplim
2371%             xpos=xlim(1)+stoplim+0.01;
2372%         end
2373%         varargout{1}=xpos;
2374%
2375%         % %check requested point to left of stop patch
2376%         % h=findobj(orbfig,'tag','stoppatch');
2377%         % xlim=get(h,'XData');   %x-positions of four patch corners
2378%         % if xpos>=xlim(1)-stoplim;
2379%         %    xpos=xlim(1)-stoplim;
2380%         % end
2381%         % varargout{1}=xpos;
2382%
2383%         %==========================================================
2384%     case 'SetStopPatch'                     % SetStopPatch
2385%         %==========================================================
2386%         xpos=varargin{1};
2387%         h=findobj(orbfig,'tag','stoppatch');
2388%         xdata = [xpos+0.005 xpos-0.005 xpos+0.005];
2389%         set(h, 'XData', xdata);  %move the patch to mouse position
2390%
2391%         %==========================================================
2392%     case 'StopPatchUp'                       %StopPatchUp
2393%         %==========================================================
2394%         xpos=orbgui('GetStopPos',0.01);
2395%         orbgui('SetStopPatch',xpos);
2396%
2397%         %change BPM and corrector plot limits
2398%         limits = get(SYS.ahbpm,'XLim');
2399%         set(SYS.ahbpm,'XLim',[limits(1,1) SYS.xlimax*xpos]);   %Change stop, leave start alone
2400%         set(SYS.ahcor,'XLim',[limits(1,1) SYS.xlimax*xpos]);
2401%         corgui('PlotAct');
2402%         corgui('PlotFit');
2403%         set(orbfig, 'Pointer','arrow',...
2404%             'WindowButtonMotionFcn','','WindowButtonUpFcn','');
2405
2406        %==========================================================
2407    case 'GetAbscissa'                          %...GetAbscissa
2408        %==========================================================
2409        %return coordinates for abscissa (position or phase)
2410        SYS   = varargin{1};
2411        elem  = varargin{2};
2412        plane = SYS.plane;
2413       
2414        switch SYS.xscale
2415            case 'meter'
2416                if strcmpi(elem,'BPM'), xd = BPM(plane).s; end
2417                if strcmpi(elem,'COR'), xd = COR(plane).s; end
2418            case 'phase'
2419                if strcmpi(elem,'BPM'), xd = BPM(plane).phi; end
2420                if strcmpi(elem,'COR'), xd = COR(plane).phi; end
2421        end
2422
2423        varargout = {xd};
2424
2425        %=============================================================
2426    case 'InitialSaveSet'                 % *** InitialSaveSet ***
2427        %=============================================================
2428        %used during startup
2429        %generate a Save Set and copy to Initial slot
2430        SYS.save.max = 5;     %1 slot for Initial, 4 slot for Save Sets
2431        setappdata(0,'SYS',SYS);
2432        orbgui('SaveSet');
2433        SYS.save.ptr = 2;
2434        setappdata(0,'SYS',SYS);
2435        orbgui('SaveSet2Initial');
2436        SYS.save.ptr = 1;
2437        setappdata(0,'SYS',SYS);
2438
2439        %=============================================================
2440    case 'SaveSet2Initial'               % *** SaveSet2Initial ***
2441        %=============================================================
2442        %load selected Save Set into Initial Slot
2443        ptr = SYS.save.ptr;
2444        orbgui('MoveSaveSet',ptr,1);    %move Save Set from ptr to 1
2445        %update figure if it exists
2446        if ~isempty(findobj('Tag','savesetfigure'))
2447            orbgui('UnDrawSaveSet');
2448            orbgui('ReDrawSaveSet');
2449        end
2450
2451        %=============================================================
2452    case 'SaveSet'                             % *** SaveSet ***
2453        %=============================================================
2454        %generate SaveSet of parameters for both planes
2455        %bpm selection (ifit,wt)
2456        %beamline selection (ifit,wt)
2457        %corrector selection (ifit,wt)
2458        %bpm offsets (des)
2459        %system parameters: mode, bpmode, plane, relative, algorithm, bpmslp
2460        %fitting parameters: nsvd, etc
2461
2462        %initialize buffer depth if necessary
2463        if ~isfield(SYS.save,'len'), SYS.save.len = 1;  end 
2464
2465        %system parameters
2466        sys.mode       = SYS.mode;
2467        sys.bpmode     = SYS.bpmode;
2468        sys.plane      = SYS.plane;
2469        SYS.relative   = SYS.relative;
2470        sys.machine    = SYS.machine;
2471        sys.bpmslp     = SYS.bpmslp;
2472        sys.algo       = SYS.algo;
2473        sys.maxs       = SYS.maxs;
2474        sys.maxphi     = SYS.maxphi;
2475        sys.xscale     = SYS.xscale;
2476
2477        %bpm, corrector and response matrix parameters
2478        for ip = 1:2,
2479            %BPM
2480            bpm(ip).mode     = BPM(ip).mode;
2481            bpm(ip).scalemode= BPM(ip).scalemode;
2482%             bpm(ip).dev      = BPM(ip).dev;
2483            bpm(ip).id       = BPM(ip).id;
2484            bpm(ip).ifit     = BPM(ip).ifit;
2485            bpm(ip).wt       = BPM(ip).wt;
2486            bpm(ip).des      = BPM(ip).des;
2487            %Correctors
2488            cor(ip).mode     = COR(ip).mode;
2489            cor(ip).scalemode= COR(ip).scalemode;
2490            cor(ip).fract    = COR(ip).fract;
2491            cor(ip).id       = COR(ip).id;
2492            cor(ip).ifit     = COR(ip).ifit;
2493            cor(ip).wt       = COR(ip).wt;
2494            cor(ip).ebpm     = COR(ip).ebpm;
2495            %Orbit Response Matrix
2496            rsp(ip).nsvd     = RSP(ip).nsvd;
2497            rsp(ip).disp     = RSP(ip).disp;
2498            rsp(ip).eig      = RSP(ip).eig;
2499        end
2500
2501        %move Save Sets down
2502        len = SYS.save.len;    %present length of buffer
2503        for ii = len:-1:2      %'Initial' occupies first slot
2504            orbgui('MoveSaveSet',ii,ii+1);
2505        end
2506
2507        %load new Save Set slot #2 (slot #1 = Initial)
2508        slot = 2;
2509        setappdata(orbfig,['sys' num2str(slot)],sys);
2510        setappdata(orbfig,['bpm' num2str(slot)],bpm);
2511        setappdata(orbfig,['cor' num2str(slot)],cor);
2512        setappdata(orbfig,['rsp' num2str(slot)],rsp);
2513       
2514        %create time stamp
2515        SYS.save.ts(slot,:) = datestr(now,0);
2516
2517        %increment Save Set buffer length
2518        SYS.save.len = SYS.save.len+1;                     
2519       
2520        if SYS.save.len > SYS.save.max    %stop at maximum
2521            SYS.save.len = SYS.save.max;
2522            setappdata(0,'SYS',SYS);
2523        end
2524
2525        %update figure if it exists
2526        if ~isempty(findobj('Tag','savesetfigure'))
2527            orbgui('UnDrawSaveSet');
2528            SYS.save.ptr = 2;
2529            setappdata(0,'SYS',SYS);
2530            %point to most recent selection
2531            orbgui('ReDrawSaveSet');
2532        end
2533
2534        orbgui('LBox',' Saved parameter Save Set');
2535
2536        %=============================================================
2537    case 'MoveSaveSet'                       % *** MoveSaveSet ***
2538        %=============================================================
2539        %moves a Save Set from one location in appdata to another
2540        start = varargin{1};  %initial SaveSet location
2541        stop  = varargin{2};  %final SaveSet location
2542
2543        sys = getappdata(orbfig,['sys' num2str(start)] );
2544        bpm = getappdata(orbfig,['bpm' num2str(start)] );
2545        cor = getappdata(orbfig,['cor' num2str(start)] );
2546        rsp = getappdata(orbfig,['rsp' num2str(start)] );
2547
2548        setappdata(orbfig,['sys' num2str(stop)],sys);
2549        setappdata(orbfig,['bpm' num2str(stop)],bpm);
2550        setappdata(orbfig,['cor' num2str(stop)],cor);
2551        setappdata(orbfig,['rsp' num2str(stop)],rsp);
2552
2553        SYS.save.ts(stop,:) = SYS.save.ts(start,:);    %move time stamp
2554        setappdata(0,'SYS',SYS);
2555
2556        %===================================================================
2557    case 'RestoreSaveSet'                       % *** RestoreSaveSet ***
2558        %===================================================================
2559        %pop up screen with choices for SaveSet
2560
2561        %test if Save Set panel already exists
2562        h = findobj(0,'tag','savesetfigure');
2563        if ~isempty(h)
2564            delete(h);
2565        end
2566
2567        [screen_wide, screen_high]=screensizecm;
2568
2569        % Draw the outside box of the figure
2570        x0=0.3545*screen_wide; y0=0.4297*screen_high; dx=0.4014*screen_wide; dy=0.2500*screen_high;
2571        figure('Units','centimeters',...
2572            'Position',[x0 y0 dx dy],...
2573            'NumberTitle','off',...
2574            'Name','Save Set Selection',...
2575            'Tag','savesetfigure',...
2576            'MenuBar','none');
2577
2578        %pushbuttons for Save Set commands
2579        x0=0.025*screen_wide; y0=0.02*screen_high; dx=0.08*screen_wide; dy=0.025*screen_high;
2580        uicontrol('Style','pushbutton',...
2581            'Units','centimeters',...
2582            'String','Load Save Set',...
2583            'Position',[x0+0*dx y0 dx dy],...
2584            'Callback','orbgui(''LoadSaveSet'');');
2585
2586        uicontrol('Style','pushbutton',...
2587            'Units','centimeters',...
2588            'String','Delete Save Set',...
2589            'Callback','orbgui(''DeleteSaveSet'');',...
2590            'Position',[x0+1.1*dx y0 dx dy]);
2591
2592        uicontrol('Style','pushbutton',...
2593            'Units','centimeters',...
2594            'String','Load to Initial',...
2595            'Callback','orbgui(''SaveSet2Initial'');',...
2596            'Position',[x0+2.2*dx y0 dx dy]);
2597
2598        uicontrol('Style','pushbutton',...
2599            'Units','centimeters',...
2600            'String','Cancel',...
2601            'Callback','delete(gcf)',...
2602            'Position',[x0+3.3*dx y0 dx dy]);
2603
2604        orbgui('ReDrawSaveSet')
2605
2606        %=============================================================
2607    case 'UnDrawSaveSet'                   % *** UnDrawSaveSet ***
2608        %=============================================================
2609        %remove radio buttons
2610        for ptr = 1:SYS.save.len
2611            h=findobj(0,'Tag',['ss' num2str(ptr)]);
2612            delete(h);
2613        end
2614
2615        %=============================================================
2616    case 'ReDrawSaveSet'                   % *** ReDrawSaveSet ***
2617        %=============================================================
2618        [screen_wide, screen_high]=screensizecm;
2619        x0=0.025*screen_wide; y0=0.02*screen_high; dx=0.08*screen_wide; dy=0.025*screen_high;
2620
2621        h=findobj('Tag','savesetfigure');
2622        figure(h);
2623        %List Save Set choices
2624        %radio button for initial Save Set
2625        uicontrol('Style','radio',...
2626            'Units','centimeters',...
2627            'Position',[x0 y0+7*dy 4*dx 0.9*dy],...
2628            'String',['Initial Save Set  :  ' SYS.save.ts(1,:)],...
2629            'Value',0,...
2630            'Tag','ss1',...
2631            'Callback','orbgui(''SelectSaveSet'');',...
2632            'ToolTipString','Restore to Initial Save Set');
2633
2634        %radio buttons for Save Sets
2635        len = SYS.save.len;                      %length of buffer
2636        for ii = 2:len
2637            tag = ['ss' num2str(ii)];          %add unity since initial in '1' slot
2638            ts  = SYS.save.ts(ii,:);
2639            uicontrol('Style','radio',...
2640                'Units','centimeters',...
2641                'Position',[x0 y0+(7-ii)*dy 4*dx 0.9*dy],...
2642                'String',['Save Set  ',num2str(ii-1),'         :  ' ts],...
2643                'Value',0,...
2644                'Tag',tag,...
2645                'Callback','orbgui(''SelectSaveSet'');',...
2646                'ToolTipString','Restore Save Set');
2647        end
2648
2649        if isfield(SYS.save,'ptr')
2650            if ~isempty(SYS.save.ptr)
2651                set(findobj('Tag',['ss' num2str(SYS.save.ptr)]),'Value',1);
2652            end
2653        end
2654
2655        %=============================================================
2656    case 'DeleteSaveSet'               % *** DeleteSaveSet ***
2657        %=============================================================
2658        %Delete the selected Save Set
2659        %first locate Save Set of choice
2660        if ~isfield(SYS.save,'ptr') || ~isfield(SYS.save,'len'),return; end
2661
2662        ptr = SYS.save.ptr;    %pointer to selected Save Set
2663        len = SYS.save.len;    %total length of Save Set ring buffer
2664
2665        if SYS.save.ptr == 1, return; end   %don'td delete Initial
2666
2667        %shift higher order Save Sets down one slot each
2668        for ii = ptr:len-1,
2669            sys = getappdata(orbfig,['sys' num2str(ii+1)] );
2670            bpm = getappdata(orbfig,['bpm' num2str(ii+1)] );
2671            cor = getappdata(orbfig,['cor' num2str(ii+1)] );
2672            rsp = getappdata(orbfig,['rsp' num2str(ii+1)] );
2673
2674            setappdata(orbfig,['sys' num2str(ii)],sys);   %initial in '1' slot
2675            setappdata(orbfig,['bpm' num2str(ii)],bpm);
2676            setappdata(orbfig,['cor' num2str(ii)],cor);
2677            setappdata(orbfig,['rsp' num2str(ii)],rsp);
2678            SYS.save.ts(ii,:) = SYS.save.ts(ii+1,:);
2679        end
2680        orbgui('UnDrawSaveSet');                      %clear drawing before reducing len
2681        SYS.save.ptr=[];
2682
2683        if SYS.save.len>=2
2684            SYS.save.len=len-1;
2685        end
2686
2687        setappdata(0,'SYS',SYS);
2688
2689        orbgui('ReDrawSaveSet')
2690
2691        %=============================================================
2692    case 'SelectSaveSet'               % *** SelectSaveSet ***
2693        %=============================================================
2694        imax=SYS.save.max+1;   %add '1' to include Initial
2695
2696        %first turn off all choices
2697        for ii=1:imax,
2698            set(findobj(0,'Tag',['ss' num2str(ii)]),'Value',0);
2699        end
2700
2701        %turn on selection
2702        set(gcbo,'Value',1);
2703        ptr=get(gcbo,'Tag');
2704        ptr=str2double(ptr(3));
2705
2706        %load selection into global
2707        SYS.save.ptr=ptr;
2708        setappdata(0,'SYS',SYS);
2709
2710        %=============================================================
2711    case 'LoadSaveSet'                       % *** LoadSaveSet ***
2712        %=============================================================
2713        %load parameters
2714        %update orbit
2715        %update correctors
2716        %solvesystem, backsub
2717        %replot
2718
2719        if ~isfield(SYS.save,'ptr')
2720            disp('Warning: no Save Set Selected');
2721            orbgui('LBox','Warning: no Save Set Selected');
2722            return
2723        end
2724
2725        ptr = SYS.save.ptr;
2726
2727        sys = getappdata(orbfig,['sys' num2str(ptr)] );
2728        bpm = getappdata(orbfig,['bpm' num2str(ptr)] );
2729        cor = getappdata(orbfig,['cor' num2str(ptr)] );
2730        rsp = getappdata(orbfig,['rsp' num2str(ptr)] );
2731
2732        SYS.mode       = sys.mode;
2733        SYS.bpmode     = sys.bpmode;
2734        SYS.plane      = sys.plane;
2735        SYS.relative   = SYS.relative;
2736        SYS.machine    = sys.machine;
2737        SYS.bpmslp     = sys.bpmslp;
2738        SYS.algo       = sys.algo;
2739        SYS.maxs        = sys.maxs;
2740        SYS.maxphi      = sys.maxphi;
2741        SYS.xscale     = sys.xscale;
2742
2743        for ip = 1:2
2744            % BPM
2745            BPM(ip).mode     = bpm(ip).mode;
2746            BPM(ip).scalemode= bpm(ip).scalemode;
2747%             BPM(ip).dev      = bpm(ip).dev;
2748            BPM(ip).id       = bpm(ip).id;
2749            BPM(ip).ifit     = bpm(ip).ifit;
2750            BPM(ip).wt       = bpm(ip).wt;
2751            BPM(ip).des      = bpm(ip).des;
2752            %Correctors
2753            COR(ip).mode     =cor(ip).mode;
2754            COR(ip).scalemode=cor(ip).scalemode;
2755            COR(ip).fract    =cor(ip).fract;
2756            COR(ip).id       =cor(ip).id;
2757            COR(ip).ifit     =cor(ip).ifit;
2758            COR(ip).wt       =cor(ip).wt;
2759            COR(ip).ebpm     =cor(ip).ebpm;
2760            %Orbit response matrix
2761            RSP(ip).nsvd     =rsp(ip).nsvd;
2762            RSP(ip).disp     =rsp(ip).disp;
2763            RSP(ip).eig      =rsp(ip).eig;
2764        end
2765       
2766        setappdata(0,'SYS',SYS);
2767        setappdata(0,'BPM',BPM);
2768        setappdata(0,'COR',COR);
2769        setappdata(0,'RSP',RSP);
2770
2771        orbgui('RefreshOrbGUI');
2772
2773        COR(1).rst = COR(1).act;
2774        COR(2).rst = COR(2).act;
2775        setappdata(0,'COR',COR);
2776
2777        orbgui('LBox',' Finished restoring system parameters ');
2778        %disp(['Save Set Loaded ' num2str(ptr) '  ' SYS.save.ts(ptr,:)]);
2779
2780        %=============================================================
2781    case 'RestoreSystem'                   % *** RestoreSystem ***
2782        %=============================================================
2783        %restore ORBIT program from file
2784        pathname = char(varargin(1)); %file directory
2785        filename = char(varargin(2)); %SaveSystem file name
2786        auto     = char(varargin(3)); %automatic load flag
2787        [SYS BPM COR RSP] = RestoreOrbit(pathname,filename,auto,SYS,BPM,COR,RSP);  %no graphics commands
2788
2789        %Some code below this line is done in RestoreOrbit
2790
2791        %Get BPM status, sort for avail, ifit
2792        % TODO
2793        [BPM(1).status BPM(2).status] = SPEAR2BPMCheck999(BPM(1).hndl,BPM(2).hndl);
2794        BPM(1).avail = BPM(1).status;  %...if status o.k. default to available
2795        BPM(2).avail = BPM(2).status;
2796        [BPM]        = SortBPMs(BPM,RSP);
2797        setappdata(0,'BPM',BPM);
2798
2799        %Get COR status, sort for avail, ifit
2800        [COR] = SortCORs(COR,RSP);
2801        setappdata(0,'COR',COR);
2802
2803
2804        %Set up program parameters: both planes
2805        for ip = 1:2
2806            orbgui('TogglePlane');
2807            orbgui('RefreshOrbGUI');        %acquires new orbit, correctors, fits, updates plots
2808            %COR=getappdata(0,'COR');
2809            COR(ip).ref = COR(ip).act;
2810            COR(ip).rst = COR(ip).act;       %for save/restore
2811        end
2812
2813        orbgui('InitialSaveSet');   
2814        SYS.save.ptr = 1;   
2815        SYS.save.len = 2;   %make save set, clear others
2816
2817        orbgui('LBox',[' Finished loading Restore File: ' filename]);
2818        set(findobj('tag','restorefile'),'String',filename);
2819
2820        %=============================================================
2821    case 'SaveSystem'                         % *** SaveSystem ***
2822        %=============================================================
2823        %write ORBIT parameters to *.m file
2824       
2825        filename = char(varargin(1));    %cell array containing file name
2826        comment  = char(varargin(2));    %cell array containing file comment
2827       
2828        [fid,message] = fopen(filename,'w');
2829       
2830        if fid == -1
2831            disp(message);
2832            return
2833        end
2834       
2835        % Laurent asks %s\n for windows compatibility ???
2836       
2837        fprintf(fid,'%s\n','%system parameter save file');
2838        ts = datestr(now,0);
2839        fprintf(fid,'%s\n',['%timestamp: ' ts]);
2840        fprintf(fid,'%s\n',['%comment: ' comment]);
2841        %
2842        fprintf(fid,'%s',['sys.machine='  ,'''',SYS.machine,''';']);
2843        fprintf(fid,'%s\n', '   %machine for control');
2844        %
2845        fprintf(fid,'%s',['sys.mode='  ,'''',SYS.mode,''';']);
2846        fprintf(fid,'%s\n', '      %online or simulator');
2847        %
2848        fprintf(fid,'%s',['sys.bpmode=' ,'''',SYS.bpmode,''';']);
2849        fprintf(fid,'%s\n', '     %BPM system mode');
2850        %
2851        fprintf(fid,'%s',['sys.bpmslp= ',num2str(SYS.bpmslp),';']);
2852        fprintf(fid,'%s\n', '          %BPM sleep time in sec');
2853        %
2854        fprintf(fid,'%s',['sys.plane='  ,     num2str(SYS.plane),';']);
2855        fprintf(fid,'%s\n', '            %plane (1=horizontal 2=vertical)');
2856        %
2857        fprintf(fid,'%s',['sys.algo=', '''SVD'';']);
2858        fprintf(fid,'%s\n', '         %fitting algorithm');
2859        %
2860%         fprintf(fid,'%s',['sys.pbpm= ' ,num2str(SYS.pbpm),';']);
2861%         fprintf(fid,'%s\n', '            %acquisition of photon BPMs');
2862        %
2863        fprintf(fid,'%s',['sys.filepath=  ' ,'''',SYS.filepath,''';']);
2864        fprintf(fid,'%s\n', '     %file path in MATLAB');
2865        %
2866        fprintf(fid,'%s',['sys.reffile=  ' ,'''',SYS.reffile,''';']);
2867        fprintf(fid,'%s\n', '     %reference orbit file');
2868        %
2869%         fprintf(fid,'%s',['sys.rspfile=  ' ,'''',SYS.rspfile,''';']);
2870%         fprintf(fid,'%s\n', '     %response matrix file');
2871        %
2872%         fprintf(fid,'%s',['sys.brspfile= ','''',SYS.brspfile,''';']);
2873%         fprintf(fid,'%s\n', '     %bump response matrix file');
2874        %
2875        fprintf(fid,'%s',['sys.etafile= ' ,'''',SYS.etafile,''';']);
2876        fprintf(fid,'%s\n', '     %dispersion file');
2877        %
2878        fprintf(fid,'%s',['SYS.relative= ' ,num2str(SYS.relative),';']);
2879        fprintf(fid,'%s\n', '        %relative or absolute BPM plot 1=absolute, 2=relative');
2880        %
2881%         fprintf(fid,'%s',['sys.fdbk= 0;']);
2882%         fprintf(fid,'%s\n', '            %no feedback');
2883%         %
2884%         fprintf(fid,'%s',['sys.abort=0;']);
2885%         fprintf(fid,'%s\n', '            %reset abort flag');
2886        %
2887        fprintf(fid,'%s',['sys.maxs= ',num2str(SYS.maxs),';']);
2888        fprintf(fid,'%s\n', '           %maximum ring circumference');
2889        %
2890        fprintf(fid,'%s',['sys.xlimax= ' ,num2str(SYS.xlimax),';']);
2891        fprintf(fid,'%s\n', '        %abcissa plot limit');
2892        %
2893        fprintf(fid,'%s',['sys.maxphi(1)= ',num2str(SYS.maxphi(1)),';']);
2894        fprintf(fid,'%s\n', '        %maximum horizontal phase advance');
2895        %
2896        fprintf(fid,'%s',['sys.maxphi(2)= ',num2str(SYS.maxphi(2)),';']);
2897        fprintf(fid,'%s\n', '        %maximum vertical phase advance');
2898        %
2899        fprintf(fid,'%s',['sys.xscale=', '''meter'';']);
2900        fprintf(fid,'%s\n', '        %abcissa plotting mode (meter or phase)');
2901
2902        %==============================================================
2903        %********   WRITE HORIZONTAL & VERTICAL CONTROL DATA  *********
2904        %==============================================================
2905        header = ['%*=== HORIZONTAL DATA ===*'; '%*===   VERTICAL DATA ===*';];
2906        xy = ['x';'z';];
2907        for ip = 1:2,
2908            p = num2str(ip);
2909            fprintf(fid,'%s\n',' ');
2910            fprintf(fid,'%s\n',header(ip,:));
2911
2912            %BPM
2913            fprintf(fid,'%s',[['bpm(',p,').dev=']  ,num2str(10),          ';']);
2914            fprintf(fid,'%s\n','          %maximum orbit deviation');
2915%             fprintf(fid,'%s',['bpm(',p,').drf=0;']);
2916%             fprintf(fid,'%s\n','           %dispersion component zero');
2917            fprintf(fid,'%s',[['bpm(',p,').id=']  ,num2str(BPM(ip).id),          ';']);
2918            fprintf(fid,'%s\n','          %BPM selection');
2919            fprintf(fid,'%s',[['bpm(',p,').scalemode=']  ,num2str(BPM(ip).scalemode),          ';']);
2920            fprintf(fid,'%s\n','     %BPM scale mode 0=manual mode, 1=autoscale');
2921            fprintf(fid,'%s',[['bpm(',p,').ylim=']  ,num2str(BPM(ip).ylim),          ';']);
2922            fprintf(fid,'%s\n','            %BPM vertical axis scale');
2923
2924            %COR
2925            fprintf(fid,'%15s %6.3f',[['cor(',p,').fract='],num2str(COR(ip).fract),';']);
2926            fprintf(fid,'%s\n','      %fraction of correctors');
2927            fprintf(fid,'%s','cor(',p,').savfrac =1.0;');
2928            fprintf(fid,'%s\n' ,'    %saved fraction of corrector solution (unity)');
2929
2930            %RSP
2931            fprintf(fid,'%s',['rsp(',p,').disp =''off'';']);
2932            fprintf(fid,'%s\n' ,'     %mode for matrix column display');
2933            fprintf(fid,'%s',['rsp(',p,').eig  =''off'';']);
2934            fprintf(fid,'%s\n' ,'     %mode for eigenvector display');
2935            fprintf(fid,'%s',['rsp(',p,').fit  =0;']);
2936            fprintf(fid,'%s\n' ,'         %valid fit flag');
2937            fprintf(fid,'%s %d',['rsp(',p,').rfflag=', num2str(RSP(ip).rfflag),';']);
2938            fprintf(fid,'%s\n' ,'           %rf fitting flag');
2939            fprintf(fid,'%s %d',['rsp(',p,').savflag=0;']);
2940            fprintf(fid,'%s\n' ,'      %save solution flag');
2941            fprintf(fid,'%s',[['rsp(',p,').nsvd=' ],num2str(RSP(ip).nsvd), ';']);
2942            fprintf(fid,'%s\n','         %number of singular values');
2943%             fprintf(fid,'%s',[['rsp(',p,').savnsvd=' ],num2str(RSP(ip).savnsvd), ';']);
2944%             fprintf(fid,'%s\n','       %saved number of singular values');
2945            fprintf(fid,'%s',['rsp(',p,').nsvdmax=1;']);
2946            fprintf(fid,'%s\n','        %default maximum number of singular values');
2947%             fprintf(fid,'%s',[['rsp(',p,').bmp=' ],num2str(RSP(ip).bmp), ';']);
2948%             fprintf(fid,'%s\n','           %bump subtraction flag');
2949            fprintf(fid,'%s\n',' ');
2950
2951
2952            %BPM fitting indices, weights
2953            fprintf(fid,'%s\n','%BPM data: name, index, fit,  weight, etaweight');
2954            fprintf(fid,'%6s\n' ,['bpm',xy(ip,:),'={']);
2955
2956            for ii = 1:size(BPM(ip).name,1)
2957                ifit = ii;
2958                if isempty(find(BPM(ip).ifit == ii,1))
2959                    ifit = 0;
2960                end
2961                fprintf(fid,'%1s %10s %5d %5d %10.3f %10.3f %1s\n' ,...
2962                    '{',['''',BPM(ip).name(ii,:),''''], ii, ifit, BPM(ip).wt(ii), 0.0, '}');
2963            end
2964            fprintf(fid,'%2s\n' ,'};');
2965            fprintf(fid,'%s\n',' ');
2966
2967            %Corrector fitting indices, weights, limits, ebpm matrix, pbpm matrix
2968            fprintf(fid,'%s\n','%COR data: name, index, fit,  weight,   limit,      ebpm,      pbpm');
2969            fprintf(fid,'%6s\n' ,['cor',xy(ip,:),'={']);
2970            for ii = 1:size(COR(ip).name,1)
2971                ifit = ii;
2972                if isempty(find(COR(ip).ifit == ii,1))
2973                    ifit = 0;
2974                end
2975                fprintf(fid,'%1s %10s %5d %5d %10.3f %10.3f %10.3f  %1s\n' ,...
2976                    '{',['''',COR(ip).name(ii,:),''''], ii, ifit,  ...
2977                    COR(ip).wt(ii), COR(ip).lim(ii), COR(ip).ebpm(ii),'}');
2978            end
2979            fprintf(fid,'%2s\n' ,'};');
2980            fprintf(fid,'%s\n',' ');
2981
2982        end    %end of plane loop
2983
2984        %close file, produce binary backup
2985        fclose(fid);
2986        disp(['Saved system parameters to file... ' filename ]);
2987        temp = [filename '_binary'];
2988        save(temp, 'BPM', 'COR', 'RSP', 'SYS');
2989        disp(['Backup file... ' temp ]);
2990
2991        %==========================================================
2992    case 'SaveBump'                               %  SaveBump
2993        %==========================================================
2994        %*** Save Bump File ***
2995        extype='*.dat';
2996        [filename,pathname]=uiputfile(extype,'Select Bump File Name');
2997        if isempty(findstr(filename,'.m'))
2998            filename = [filename,'.m'];
2999        end
3000
3001        if isequal(filename,0) || isequal(pathname,0)
3002            disp('File not open');
3003            return
3004        else
3005            disp(['File ', [pathname filename], ' open']);
3006        end
3007
3008        [fid,message]=fopen([pathname filename],'w');
3009        if fid==-1
3010            disp(['WARNING: Unable to open file to write bump :' pathname filename]);
3011            disp(message);
3012            return
3013        end
3014
3015        ncor = length(COR(plane).ifit);
3016        fprintf(fid,'%s\n','new bump');
3017        fprintf(fid,'%d\n',ncor);
3018        for ii = 1:ncor
3019            id = COR(plane).ifit(ii);
3020            %.name is not compressed   .fit vector is compressed
3021            fprintf(fid,'%s %10.3f\n' ,...
3022                [COR(plane).name(id,:) '     '], COR(plane).fit(ii));
3023        end
3024        fclose(fid);
3025
3026        %==========================================================
3027    case 'CloseMainFigure'                    %CloseMainFigure
3028        %==========================================================
3029
3030        answer = questdlg('Close Orbit GUI?',...
3031            'Exit Orbit Control Program',...
3032            'Yes','No','Yes');
3033        switch answer
3034
3035            case 'Yes'
3036
3037                %     answer = questdlg('Clear channel access handles?',...
3038                %                        'Channel Access handles',...
3039                %                        'Yes','No','Yes');
3040                %     if strcmp(answer,'Yes')
3041                %         disp('   Closing Channel Access SYS...');
3042                %         clearmcahandles;
3043                %     end
3044                delete(findobj('tag','makeorbitslider'));
3045                delete(findobj('tag','bpmplotscale'));
3046                delete(findobj('tag','corplotscale'));
3047
3048                delete(findobj('tag','rload_xbwt'));
3049                delete(findobj('tag','rload_xclim'));
3050                delete(findobj('tag','rload_xcwt'));
3051                delete(findobj('tag','rload_hbpm'));
3052                delete(findobj('tag','rload_etawtx'));
3053
3054                delete(findobj('tag','rload_zbwt'));
3055                delete(findobj('tag','rload_zclim'));
3056                delete(findobj('tag','rload_zcwt'));
3057                delete(findobj('tag','rload_ypwt'));
3058                delete(findobj('tag','rload_vbpm'));
3059                delete(findobj('tag','rload_etawtz'));
3060
3061                delete(findobj('tag','rload_vbl'));
3062                delete(findobj('tag','toggle_blsel'));
3063
3064                delete(findobj('tag','dispersionpanel'));
3065                delete(findobj('tag','orbfig'));
3066                return
3067
3068            case 'No'
3069                return
3070            otherwise
3071                return
3072        end
3073
3074        %===========================================================
3075    otherwise
3076        disp(['   Warning: CASE not found in ORBGUI: ' action]);
3077
3078end  %end switchyard
3079
Note: See TracBrowser for help on using the repository browser.