source: MML/trunk/mml/plotwaveform.m @ 4

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

Initial import--MML version from SOLEIL@2013

File size: 78.8 KB
Line 
1% PJ BOussina waveform loader September 11, 2006
2% Just for fun nothing serious
3% If you don't like it too bad, make it yourself
4% Have FUN
5
6%==========================
7function plotwaveform()
8%==========================
9    %declare main structures
10    SYS=[];  WAV=[];  AO=[];  AD=[];
11
12    AO=plotwaveform_ao;   %AO for plotwaveform
13    AD=plotwaveform_ad;   %AD for plotwaveform
14   
15    %program parameters
16    traces={'Trace1' 'Trace2'};                %each waveform has Trace1, Trace2
17    channels={'ChannelA' 'ChannelB'};          %each trace has Channel A, Channel B
18    sources={'Monitor' 'Golden' 'Save' 'File'};%each Channel has 4 sources
19    makeSYSstruct;                             %initialize SYS structure
20    nwfm=4;                                    %number of plots per axis
21    makeWAVstruct;                             %initialize WAV structure
22
23    background=[0.86 0.85 0.82];               %graphics color scheme
24    df=1;                                      %number of pixels to extend frame
25    PlotWaveformGUI                            %build the main GUI
26    set(SYS.Handles.Figure,'HandleVisibility','off');
27    make_psm_display                           %pulsed-signal monitor system for SPEAR3
28 
29    %initialize timer
30    pwtimer=timer('TimerFcn',@single_shot, 'Period', 1,'ExecutionMode','fixedRate','Tag','PlotwaveformTimer');
31     
32%========================
33%=== BEGIN FUNCTIONS  ===
34%=========================================
35    function launch_timer_callback(src,evt)        %launch_timer_callback
36%=========================================       
37        if ~SYS.iLoad
38            disp('Load waveform first')
39            return
40        end
41        if strcmpi(get(src,'String'),'Continuous')  %showing 'Continous', turned to 'on'
42          try
43            start(pwtimer);
44            set(SYS.Handles.Timer,'String','Running');
45          catch
46            warndlg(lasterr);
47            a=timerfindall;
48            stop(a);
49            delete(a);
50            return;
51          end
52        elseif strcmpi(get(src,'String'),'Running')
53          try
54            stop(pwtimer);
55            set(SYS.Handles.Timer,'String','Continuous');
56          catch
57            warndlg(lasterr);
58            a=timerfindall;
59            stop(a);
60            delete(a);
61            return;
62          end
63        end
64    end
65
66%======================================       
67   function select_waveform_callback(src,evt)   %%%%%%%%  select_waveform_callback %%%%%%%%%
68%======================================       
69    val=get(src,'Userdata');   %callback of select waveform pushbuttons
70    SYS.iplot=val(1);  iplot=SYS.iplot;
71    SYS.iwfm=val(2);   iwfm =SYS.iwfm;
72   
73    if WAV(iplot,iwfm).iLoad   %waveform already loaded
74        update_display;
75        if strcmpi(WAV(iplot,iwfm).FamilyType,'PSM')
76            psm_display;  %updates psm parameter window
77        end
78        return
79    end
80   
81    %generate figure for loading waveform
82    SYS.Handles.SelectFigure=figure('Numbertitle','off','name','Select Waveform',...
83       'Position', [112 500 920 300],'resize','off','Color',background);
84        %set(FigHandles,'Toolbar','none','menubar','none');
85           
86    %make listboxes
87    for ii=1:length(SYS.Families)
88    allmax=size(AO.(SYS.Families{ii}).Monitor.ChannelNames,1);
89    SYS.Handles.(SYS.Families{ii})=...
90        uicontrol('Style', 'listbox','BackgroundColor',background, 'String', {'Select'},...
91        'Position', [25+150*(ii-1) 60 135 200], 'Enable', 'on','Value',[],'Max',allmax,'Min',0,...
92        'String',AO.(SYS.Families{ii}).Monitor.ChannelNames);
93        uicontrol('Style', 'text','BackgroundColor',background, 'String', SYS.Families{ii},...
94        'Position', [25+150*(ii-1) 260 120 20], 'Enable', 'on','Value',1);
95    end
96       
97    %make 'Select' button
98         uicontrol('Style', 'pushbutton','BackgroundColor',background, 'String', 'Select',...
99     'Callback',@process_selection_callback,'Position', [300 20 80 20], 'Enable', 'on','Value',0);
100     set(SYS.Handles.SelectFigure,'HandleVisibility','off');
101
102   end %end select_waveform_callback
103
104%======================================       
105    function process_selection_callback(src,evt)   %%%%%%%%  process_selection_callback %%%%%%%%%
106%======================================       
107        %callback of the 'Select' button on waveform selection subpanel
108        iplot=SYS.iplot;
109        iwfm=SYS.iwfm;
110       
111        %initialize
112        waveforms=[];
113        ifams=[];
114       
115        %find which waveform selected
116        for ii=1:length(SYS.Families)
117        z=get(SYS.Handles.(SYS.Families{ii}),'String');  %all strings in listbox
118        val=get(SYS.Handles.(SYS.Families{ii}),'value');
119        str=z(val,:);  %specific string if chosen
120        if ~isempty(str)   %found one
121            ival=val;
122            ifams=[ifams; ii];
123            waveforms=[waveforms; str];  %string for selected value
124        end
125        end
126        if size(waveforms,1)>1 
127            waveform=waveforms(end,:);   %later may have multiple waveforms
128            ifam=ifams(end);
129        else
130            waveform=waveforms;
131            ifam=ifams;
132        end
133       
134        %check timebase is consistent with other waveforms in this graph
135        if isempty(SYS.Families(ifam))
136          errordlg('Warning: No waveform selected','plotwaveform')
137        return
138        end
139
140        iok=check_xunits(iplot,SYS.Families(ifam));
141        if ~iok
142          errordlg('Invalid waveform selection - inconsistent time base. Choose again.','Plotwaveform timebase error')
143        return
144        end
145
146        %build the WAV structure
147        clear_plot(iplot,iwfm);
148        wav2zero(iplot,iwfm);
149        WAV(iplot,iwfm).ChannelName=deblank(waveform);  %channel name
150        waveform=AO.(SYS.Families{ifam}).CommonNames(get(SYS.Handles.(SYS.Families{ifam}),'value'),:); %common name from AO
151        WAV(iplot,iwfm).Family=SYS.Families{ifam}; %family
152        WAV(iplot,iwfm).FamilyType=SYS.FamilyTypes{ifam}; %type e.g. PSM, MCORR, etc
153        WAV(iplot,iwfm).CommonName=deblank(AO.(SYS.Families{ifam}).CommonNames(ival,:)); %common name
154        set(SYS.Handles.Select(iplot,iwfm),'String',waveform,'ForegroundColor',  WAV(iplot,iwfm).Color);  %display common name
155        set(SYS.Handles.Display(iplot,iwfm),'Value',1) %checkbox for display 'on'
156        WAV(iplot,iwfm).iLoad=1;       %status bit for waveform load
157        WAV(iplot,iwfm).iDisplay=1;    %status bit for display
158        WAV(iplot,iwfm).Monitor.XDataUnits=AO.(char(SYS.Families(ifam))).XUnits;  %usec, etc
159        WAV(iplot,iwfm).Monitor.RawDataUnits=deblank(AO.(char(SYS.Families(ifam))).RawYUnits(ival,:));  %units for raw signal (e.g. Volts)
160        WAV(iplot,iwfm).Monitor.EngDataUnits=deblank(AO.(char(SYS.Families(ifam))).EngYUnits(ival,:));  %units for Eng signal (e.g. Pwr)
161
162        if strcmpi(WAV(iplot,iwfm).FamilyType,'PSM')   %acquire conversion factors from PV's
163             wavpsm2zero(iplot,iwfm);
164             WAV(iplot,iwfm).Monitor.VoltOffset=getpv([WAV(iplot,iwfm).ChannelName 'Conv.G']);
165             conv={'A'; 'B'; 'C'; 'D'; 'E'; 'F';};
166             for ii=1:6 %A,B,C,D,E,F,G - Polynomial conversion factors from Volt to EGU (G=voltage offset)
167             WAV(iplot,iwfm).Monitor.PolyConv(ii)=getpv([WAV(iplot,iwfm).ChannelName 'Conv.' conv{ii}]);
168             end
169             WAV(iplot,iwfm).Monitor.RawSignal=AO.(char(SYS.Families(ifam))).RawSignal;  %VoltFst
170             WAV(iplot,iwfm).Monitor.EngSignal=AO.(char(SYS.Families(ifam))).EngSignal;  %Fst
171             WAV(iplot,iwfm).Monitor.XDataStep=AO.(WAV(iplot,iwfm).Family).Timebase(ival);
172             NORD=getpv([WAV(iplot,iwfm).ChannelName 'Waveform.NORD']);
173             WAV(iplot,iwfm).Monitor.XData=WAV(iplot,iwfm).Monitor.XDataStep*[1:NORD]';
174        else
175            errordlg(['Generate horizontal time base in select_waveform_callback for ' SYS.Machine])
176            %WAV(iplot,iwfm).Monitor.XData=  %needed for plotting
177            %WAV(iplot,iwfm).Monitor.XDataStep=   %needed for horizontal scroll gui
178        end
179       
180        SYS.iLoad=1;
181        set(SYS.Handles.Source(1,1),'Value',1);      %select radio button for Source(1)= 'Monitor'
182        set(SYS.Handles.Source(2,1),'Value',1);      %select radio button for Source(2)= 'Monitor'
183        set(SYS.Handles.Trace1Channel(1),'Value',1);  %select radio button for channel 'A'
184        set(SYS.Handles.Trace2State,'Value',0)
185        t=SYS.Families(ifam);
186        set(get(SYS.Handles.Plot(iplot),'Xlabel'),'string',['Time (' AO.(t{1}).XUnits ' )'])
187        waveform_name; %displays waveform name
188       
189        golden2memory;  %load Golden file if available
190       
191        if ishandle(SYS.Handles.SelectFigure)
192        close(SYS.Handles.SelectFigure)
193        end
194       
195        single_shot;
196        monitor2save;   %save reference automatically at load time
197        if strcmpi(WAV(iplot,iwfm).FamilyType,'PSM')
198          if ~ishandle(SYS.PSMHandles.Figure)  %if window does not exist make one
199             make_psm_display;
200          end
201
202          psm_display;  %updates psm parameter window, single_shot does not look at psm
203        end
204    end
205
206%======================================       
207   function update_channel_callback(src,evt)   %%%%%%%%  update_channel_callback %%%%%%%%%
208%======================================       
209       %radiobuttion selection of channel A, B or A-B for Trace1 or Trace2
210       val=get(src,'Userdata');
211       tr=val(1);    %1,2   = Trace1 or Trace2
212       ch=val(2);    %1,2,3 = Channel A, B or A-B
213       iplot=SYS.iplot;
214       iwfm=SYS.iwfm;
215       if ~WAV(iplot,iwfm).iLoad;
216           errordlg('Select waveform first','Select Channel'); 
217           set(SYS.Handles.([traces{tr} 'Channel'])(ch),'Value',0);
218           return;
219       end
220   
221       for ii=1:3   %turn off all Channel radiobuttons for selected Trace
222       set(SYS.Handles.([traces{tr} 'Channel'])(ii),'Value',0)
223       end
224       if tr==2 && ~get(SYS.Handles.Trace2State,'Value')    %user chose Trace2 with radio button 'off'
225           val=get(SYS.Handles.([traces{tr} 'Channel'])(ch),'Value');  %find value of radiobutton pushed
226           set(SYS.Handles.([traces{tr} 'Channel'])(ch),'Value',mod(1,val));   %put it back
227           WAV(iplot,iwfm).Trace2Channel=ch;  %update channel selection
228           return
229       end
230
231       set(src,'Value',1)
232       WAV(iplot,iwfm).([traces{tr} 'Channel'])=ch;  %1,2 or 3 for A, B or A-B
233       WAV(iplot,iwfm).([traces{tr} 'State'])=1;
234       single_shot_callback;    %defaults to update
235   end
236
237%========================================       
238   function trace2state_callback(src,evt)  %trace2state_callback
239%========================================       
240        %radio button toggle on/off for Trace2
241        iplot=SYS.iplot;
242        iwfm=SYS.iwfm;
243
244        if WAV(iplot,iwfm).iDisplay==0 || WAV(iplot,iwfm).iLoad==0  %flag to indicate waveform loaded)
245           errordlg('Select or Load waveform first','Load waveform')
246           set(SYS.Handles.Trace2State,'Value',0);
247           return
248        end
249
250        val=get(SYS.Handles.Trace2State,'Value');
251        if val   %user turned it 'on'
252            WAV(iplot,iwfm).Trace2State=1;
253            update_display
254        else     %user turned it 'off'
255          WAV(iplot,iwfm).Trace2State=0;
256          set(SYS.Handles.Trace2(iplot,iwfm),'XData',[],'YData',[]);
257
258        end   
259    end
260
261%======================================       
262   function update_source_callback(src,evt)    %%%%%%%%  update_source_callback %%%%%%%%%
263%======================================       
264       %selects source (Monitor/Save/Golden/File) via radiobutton for Channel A or Channel B
265       val=get(src,'Userdata'); 
266       ch=val{1};    %1,2   = ChannelA or ChannelB
267       isr=val{2};   %index of source radio button
268       iplot=SYS.iplot;
269       iwfm=SYS.iwfm;
270
271       if ~WAV(iplot,iwfm).iLoad;
272           errordlg('Select waveform first','Select Channel');
273           set(src,'Value',0)
274           return;
275       end
276
277
278       switch isr
279           case 2 %Golden
280               if ~WAV(iplot,iwfm).iGolden
281               errordlg('Load golden waveform first','Data from Golden')
282               set(src,'Value',0)  %put radio button back to zero
283               return
284               end
285           case 3  %Save
286               if ~WAV(iplot,iwfm).iSave
287               errordlg('Save reference waveform first','Saved data')
288               set(src,'Value',0)  %put radio button back to zero
289               return
290               end
291           case 4 %File
292               if ~WAV(iplot,iwfm).iFile
293               errordlg('Load waveform from file first','Data from file')
294               set(src,'Value',0)  %put radio button back to zero
295               return
296               end
297
298       end
299       
300       for ii=1:4   %turn off all Source radiobuttons for selected Channel
301       set(SYS.Handles.Source(ch,ii),'Value',0)
302       end
303
304       set(src,'Value',1)
305       %both Trace1 and Trace2 have same SourceA and SourceB
306       if ch==1
307       WAV(iplot,iwfm).ChannelA=isr;
308       elseif ch==2
309       WAV(iplot,iwfm).ChannelB=isr;
310       end
311       single_shot_callback;  %defaults to update
312   end
313
314%======================================       
315   function monitor2save_callback(src,evt)    %%%%%%%%  monitor2save_callback %%%%%%%%%
316%======================================       
317       monitor2save(src,evt);
318   end
319
320%======================================       
321   function monitor2save(src,evt)    %%%%%%%%  monitor2save %%%%%%%%%
322%======================================       
323       iplot=SYS.iplot;
324       iwfm=SYS.iwfm;
325       if ~WAV(iplot,iwfm).iLoad; errordlg('Select waveform first','Save Data');  return; end
326       WAV(iplot,iwfm).Save=WAV(iplot,iwfm).Monitor;
327       WAV(iplot,iwfm).Save.TimeStamp=sprintf('%s', datestr(clock,31));
328       disp([sprintf('%s', datestr(clock,31)) ' Waveform data ' WAV(iplot,iwfm).CommonName ' saved to memory '])
329       WAV(iplot,iwfm).iSave=1;
330       for kk=1:2 set(SYS.Handles.Source(kk,3),'ToolTipString',['Data saved: ' WAV(iplot,iwfm).Save.TimeStamp]); end
331   end
332
333%======================================       
334   function save2file_callback(src,evt)     %%%%%%%%  save2file_callback %%%%%%%%%%
335%======================================       
336       save2file
337   end
338
339%======================================       
340   function save2file(src,evt)     %%%%%%%%  save2file %%%%%%%%%%
341%======================================       
342       iplot=SYS.iplot;
343       iwfm=SYS.iwfm;
344       if ~WAV(iplot,iwfm).iLoad; errordlg('Select waveform first','Save to File');  return; end
345
346       DirStart = pwd;
347       Family=WAV(iplot,iwfm).Family;   %family
348       DirectoryName=AD.Directory.PlotWaveform.(Family); %directory
349       Name=WAV(iplot,iwfm).CommonName;     %signal common name
350       FileName = appendtimestamp(Name, clock);
351       cd(DirectoryName);
352       
353       Waveform.(Name)=WAV(iplot,iwfm).Monitor
354       Waveform.(Name).TimeStamp=sprintf('%s', datestr(clock,31));
355       save(FileName, 'Waveform');
356       disp([sprintf('%s', datestr(clock,31)) ' Waveform File [ ' FileName ' ] saved to directory ' DirectoryName])
357       cd(DirStart);
358   end
359
360%======================================       
361   function save2golden_callback(src,evt)   %%%%%%%%  save2golden_callback %%%%%%%%%%
362%======================================       
363       iplot=SYS.iplot;
364       iwfm=SYS.iwfm;
365       if ~WAV(iplot,iwfm).iLoad; errordlg('Select waveform first','Save to Golden');  return; end
366       save2file
367       
368       DirStart = pwd;
369       Family=WAV(iplot,iwfm).Family;   %family
370       DirectoryName=AD.Directory.PlotWaveform.(Family); %directory
371       cd(DirectoryName)
372       Name=WAV(iplot,iwfm).CommonName;     %signal common name
373       FileName = ['Golden_' Name];
374       
375       iexist=exist([FileName '.mat'],'file');
376       if iexist
377           answer=questdlg(['Golden waveform file [ ' FileName '.mat ] already exists - overwrite?'],'Golden file','Yes','No','Yes');
378           if strcmpi(answer,'no')
379             disp([sprintf('%s', datestr(clock,31)) ' No Golden file written ' ]);
380             return
381           end
382       end
383
384       Golden.(Name)=WAV(iplot,iwfm).Monitor;
385       Golden.(Name).TimeStamp=sprintf('%s', datestr(clock,31));
386       save(FileName, 'Golden');
387       disp([sprintf('%s', datestr(clock,31)) ' Golden File [ ' FileName '.mat ] saved to directory ' DirectoryName])
388       %save a copy to backup
389       DirectoryName=[DirectoryName 'Golden_Backup'];
390       cd([DirectoryName]);
391       FileName=appendtimestamp(FileName, clock);
392       save(FileName,'Golden')
393       disp([sprintf('%s', datestr(clock,31)) ' Golden File [ ' FileName '.mat ] saved to directory ' DirectoryName])
394       cd(DirStart);
395   end
396
397%======================================       
398   function all_checkbox_callback(srv,evt)   %all_checkbox_callback
399%======================================       
400        iplot=SYS.iplot;
401        iwfm=SYS.iwfm;
402        if ~WAV(iplot,iwfm).iLoad;
403            errordlg('Select waveform first','Write All to Workspace');
404            set(SYS.Handles.all2workspace,'Value',0);
405        end
406   end
407
408 %======================================       
409  function all2workspace
410 %======================================       
411       iplot=SYS.iplot;
412        iwfm=SYS.iwfm;
413        for ii=1:2
414            for jj=1:nwfm
415                if WAV(ii,jj).iLoad
416                    SYS.iplot=ii;
417                    SYS.iwfm=jj;
418                    write2workspace
419                end
420            end
421        end
422          SYS.iplot=iplot;
423          SYS.iwfm=iwfm;
424    end
425
426%=============================================       
427    function write2workspace_callback(src,evt)   %write2workspace_callback
428%=============================================
429      write2workspace(src,evt)
430    end
431
432%======================================       
433    function write2workspace(src,evt)     %write2workspace
434%======================================       
435       %write waveform data structure to workspace
436        iplot=SYS.iplot;
437        iwfm=SYS.iwfm;
438        if ~WAV(iplot,iwfm).iLoad; errordlg('Select waveform first','Write to Workspace');  return; end
439
440        if get(SYS.Handles.all2workspace,'Value')
441            set(SYS.Handles.all2workspace,'Value',0)
442            all2workspace;
443            set(SYS.Handles.all2workspace,'Value',1)
444            return
445        end
446       
447        Name=WAV(iplot,iwfm).CommonName;     %signal common name
448        Struct.Family=WAV(iplot,iwfm).Family;
449        Struct.CommonName=Name;
450        source={'Monitor';};
451        if WAV(iplot,iwfm).iGolden; source=[source 'Golden']; end
452        if WAV(iplot,iwfm).iSave; source=[source 'Save']; end
453        if WAV(iplot,iwfm).iFile; source=[source 'File']; end
454        for kk=1:size(source,2)  %loop over available sources (Monitor, Golden, Save, File
455           Struct.(source{kk})=WAV(iplot,iwfm).(source{kk});
456           Struct.TimeStamp=sprintf('%s', datestr(clock,31));
457        end                 
458        assignin('base',Name,Struct);
459        disp([sprintf('%s', datestr(clock,31)) ' Waveform data for [ ' Name ' ] written to MATLAB workspace'])
460    end
461
462%======================================       
463  function file2memory_callback(src,evt)    %%%%%%%%  file2memory_callback %%%%%%%%%
464%======================================       
465       %load the file
466       iplot=SYS.iplot;
467       iwfm=SYS.iwfm;
468       if ~WAV(iplot,iwfm).iLoad; errordlg('Select waveform first','Load Data File');  return; end
469
470       DirStart = pwd;
471       Family=WAV(iplot,iwfm).Family;   %family
472       DirectoryName=AD.Directory.PlotWaveform.(Family);  %directory
473       CommonName=WAV(iplot,iwfm).CommonName;     %signal common name
474       cd(DirectoryName)
475       FileName=uigetfile([CommonName '*.mat'], 'Select file');
476        try
477         load(FileName);
478         WAV(iplot,iwfm).File=Waveform.(CommonName);
479         WAV(iplot,iwfm).File.FileName=FileName;
480         WAV(iplot,iwfm).iFile=1;
481         disp([sprintf('%s', datestr(clock,31)) ' Waveform file [ ' FileName ' ] loaded to memory'])
482         for kk=1:2
483             set(SYS.Handles.Source(kk,4),'ToolTipString',['File loaded: ' WAV(iplot,iwfm).File.FileName]);
484         end
485         if strcmpi(WAV(iplot,iwfm).FamilyType,'PSM')
486           psm_display;  %updates psm parameter window
487         end
488       catch
489         errordlg('Warning: Problem with loading File', 'Load File');
490       end
491       cd(DirStart);
492    end         
493                 
494%======================================       
495    function golden2memory_callback(src,evt)  %%%%%%%%  golden2memory_callback %%%%%%%
496%======================================       
497      golden2memory(src,evt)
498    end
499
500%======================================       
501    function golden2memory(src,evt)  %%%%%%%%  golden2memory %%%%%%%
502%======================================       
503%load  golden file
504       iplot=SYS.iplot;
505       iwfm=SYS.iwfm;
506       if ~WAV(iplot,iwfm).iLoad; errordlg('Select waveform first','Load Golden');  return; end
507
508       DirStart = pwd;
509       Family=WAV(iplot,iwfm).Family;
510       CommonName=WAV(iplot,iwfm).CommonName;
511       DirectoryName=AD.Directory.PlotWaveform.(Family); %directory
512
513       %[DirectoryName, ErrorFlag] = gotodirectory(DirectoryName);
514       cd(DirectoryName)
515       %look for golden file
516       FileName = ['Golden_' CommonName '.mat'];
517       if exist(FileName,'file');
518        try
519         load(FileName);
520         WAV(iplot,iwfm).Golden=Golden.(CommonName);
521         WAV(iplot,iwfm).iGolden=1;
522         for ii=1:4   %turn off all Source radiobuttons for ChannelB
523           set(SYS.Handles.Source(2,ii),'Value',0)
524         end
525         set(SYS.Handles.Source(2,2),'Value',1);      %select radio button for Source(2)= 'Golden'
526         WAV(iplot,iwfm).ChannelB=2;  %default Bhannel B to Golden
527         disp(['Golden Waveform file [ ' FileName ' ] loaded to memory'])
528         for kk=1:2
529             set(SYS.Handles.Source(kk,2),...
530                 'ToolTipString',['Golden Timestamp ' WAV(iplot,iwfm).Golden.TimeStamp]);
531         end
532         if strcmpi(WAV(iplot,iwfm).FamilyType,'PSM')
533           psm_display;  %updates psm parameter window
534         end
535
536         catch
537           errordlg('Warning: Problem with loading Golden File', 'Load Golden File');
538         end
539       else
540           disp([CommonName ': No Golden file available'])
541       end
542       cd(DirStart);
543    end
544 
545%=========================================       
546    function single_shot_callback(src,evt)
547%=========================================       
548%callback of single_shot pushbutton
549      if strcmpi(pwtimer.Running,'on')  %timer running
550          return
551      end
552      single_shot;
553      waveform_name;
554      iplot=SYS.iplot;
555      iwfm=SYS.iwfm;
556      if strcmpi(WAV(iplot,iwfm).FamilyType,'PSM')
557          psm_display;  %updates psm parameter window
558      end
559    end
560
561%=========================================       
562   function single_shot(src,evt,irefresh)   %%%%%%%%  single_shot %%%%%%%%%
563%=========================================       
564%plot selected traces for selected signals on both axes
565       if ~exist('irefresh','var'); irefresh=1; end  %default to acquiring fresh data
566       for ii=1:2   %axes index
567         for jj=1:nwfm  %waveform index
568             if   WAV(ii,jj).iDisplay
569                %check to see if Monitor signal required
570                sourceA=WAV(ii,jj).ChannelA;   %sources are 1,2,3,4 (Monitor, Save, Golden, File)
571                sourceB=WAV(ii,jj).ChannelB;
572
573                if (sourceA==1 || sourceB==1) && irefresh
574                  if strcmpi(WAV(ii,jj).FamilyType,'PSM')
575                    getpv_spear3(ii,jj);
576                  else
577                    WAV(ii,jj).Monitor.RawData=getpv(WAV(ii,jj).ChannelName);
578                    if SYS.Units
579                        %WAV(ii,jj).Monitor.EngData=convert to Engineering Units
580                    end
581                    WAV(ii,jj).Monitor.TimeStamp=sprintf('%s', datestr(clock,31));
582                  end
583                end
584                for kk=1:2;  %trace index
585                 if WAV(ii,jj).([traces{kk} 'State']);   %trace is on for plotting
586                     switch WAV(ii,jj).([traces{kk} 'Channel'])  %which channel(s) for trace
587                         case 1   %A only
588                            if SYS.Units==0   %0 for raw units, 1 for converted/engineering units
589                            yd=WAV(ii,jj).(sources{sourceA}).RawData;
590                            else
591                            yd=WAV(ii,jj).(sources{sourceA}).EngData;
592                            end
593                         case 2   %B only
594                            if SYS.Units==0
595                            yd=WAV(ii,jj).(sources{sourceB}).RawData;
596                            else
597                            yd=WAV(ii,jj).(sources{sourceB}).EngData;
598                            end
599                         case 3   %A-B
600                            if SYS.Units==0
601                            yd=WAV(ii,jj).(sources{sourceA}).RawData-WAV(ii,jj).(sources{sourceB}).RawData;
602                            else
603                            yd=WAV(ii,jj).(sources{sourceA}).EngData-WAV(ii,jj).(sources{sourceB}).EngData;
604                            end
605                     end  %end trace display type switchyard
606                     WAV(ii,jj).(traces{kk}).YData=yd';
607                     WAV(ii,jj).(traces{kk}).XData=WAV(ii,jj).Monitor.XData;
608                     plot_waveform(ii,jj,kk)
609                  end  %end trace display condition
610               end   %end loop on traces
611             end  %end signal signal display condition
612          end  %end loop over signals
613       end  %end loop over axes
614   end  %end single_shot
615
616
617%======================================       
618   function getpv_spear3(iplot,iwfm)    %%%%%%%%  getpv_spear3  %%%%%%%%%
619%======================================       
620%called from single_shot
621     ch=WAV(iplot,iwfm).ChannelName;
622     RawPV=[ch deblank(WAV(iplot,iwfm).Monitor.RawSignal)];
623     EngPV=[ch deblank(WAV(iplot,iwfm).Monitor.EngDataUnits) deblank(WAV(iplot,iwfm).Monitor.EngSignal)];
624     disp([RawPV '    '  EngPV])
625     WAV(iplot,iwfm).Monitor.RawData=getpv(RawPV);     
626     WAV(iplot,iwfm).Monitor.EngData=getpv(EngPV);
627     WAV(iplot,iwfm).Monitor.TimeStamp=sprintf('%s', datestr(clock,31));
628       
629       if strcmpi(WAV(iplot,iwfm).FamilyType,'PSM')
630         if strcmpi(pwtimer.Running,'off')  %timer not running
631           get_psm;
632         end
633       end
634
635   end
636
637%======================================       
638  function plot_waveform(iplot,iwfm,tr)
639%======================================       
640     xd=WAV(iplot,iwfm).(traces{tr}).XData-WAV(iplot,iwfm).(traces{tr}).XOffset;
641     yd=WAV(iplot,iwfm).(traces{tr}).YScale*WAV(iplot,iwfm).(traces{tr}).YData;
642     yd=yd-WAV(iplot,iwfm).(traces{tr}).YOffset;
643     set(SYS.Handles.(traces{tr})(iplot,iwfm),'xdata',xd,'ydata',yd);
644     a=axis(SYS.Handles.Plot(iplot));
645     set(SYS.Handles.Plot(iplot),'XLim',[a(1) a(2)]);
646   end
647   
648%==========================================       
649    function update_display(src,evt)
650%==========================================       
651%updates graphical interface display but not plots
652        iplot=SYS.iplot;
653        iwfm=SYS.iwfm;
654               
655        for kk=1:2      %loop over traces
656            for jj=1:3   %loop over channel selection setting 'A', 'B', 'A-B' to radiobutton 'off'
657            set(SYS.Handles.([traces{kk} 'Channel'])(jj),'Value',0)
658            end
659            ichan=WAV(iplot,iwfm).([traces{kk} 'Channel']);  %locate which radiobutton is active
660
661            if ichan>0   %0 indicates no source selection
662            set(SYS.Handles.([traces{kk} 'Channel'])(ichan),'Value',1)  %set active radiobutton
663            end
664        end
665       
666        for kk=1:2   %loop over channel selections for A and B
667            for jj=1:4   %loop over source selection setting radiobuttons 'off'
668            set(SYS.Handles.Source(kk,jj),'Value',0)
669            end
670            isrc=WAV(iplot,iwfm).(channels{kk});  %value 1,2,3,4 for Monitor,Golden,Save,File
671            %disp(num2str([kk isrc]));
672            set(SYS.Handles.Source(kk,isrc),'Value',1);
673        end
674
675        set(SYS.Handles.Trace2State,'Value',WAV(iplot,iwfm).Trace2State)
676
677        set(SYS.Date,'string',datestr(now,'HH:MM:SS PM'));
678
679        for kk=1:2  %loop over sources for channels A and B
680            if WAV(iplot,iwfm).iGolden
681            set(SYS.Handles.Source(kk,2),...
682                        'ToolTipString',['Golden Timestamp ' WAV(iplot,iwfm).Golden.TimeStamp]);
683            end
684            set(SYS.Handles.Source(kk,3),'ToolTipString',['Data saved: ' WAV(iplot,iwfm).Save.TimeStamp]);
685            if WAV(iplot,iwfm).iFile
686            set(SYS.Handles.Source(kk,4),'ToolTipString',['File loaded: ' WAV(iplot,iwfm).File.FileName]);
687            end
688        end
689       
690        %reset radio buttons for scaling and horizontal offset display
691        if WAV(iplot,iwfm).TraceScale==1
692          set(SYS.Handles.Trace1Scale,'Value',1)
693          set(SYS.Handles.Trace2Scale,'Value',0)
694          set(SYS.Handles.XOffsetDisplay,'String',num2str(WAV(iplot,iwfm).(traces{1}).XOffset, '%6.3f'))
695        elseif WAV(iplot,iwfm).TraceScale==2
696          set(SYS.Handles.Trace1Scale,'Value',0)
697          set(SYS.Handles.Trace2Scale,'Value',1)
698          set(SYS.Handles.XOffsetDisplay,'String',num2str(WAV(iplot,iwfm).(traces{2}).XOffset, '%6.3f'))
699        end
700     
701   
702        waveform_name;
703    end
704
705%==================================================       
706     function display_waveform_callback(src,evt)    %%%%%%%%  display_waveform  %%%%%%%%%
707%==================================================       
708       %response to display checkbox for selected signal
709       %check indicated waveform is displayed, no check not displayed
710       val=get(src,'userdata');
711       SYS.iplot=val(1);   iplot=SYS.iplot;
712       SYS.iwfm=val(2);    iwfm=SYS.iwfm;
713
714       if  ~WAV(iplot,iwfm).iLoad
715           set(src,'value',0);
716           return
717       end
718
719       if get(src,'value')    %toggle 'on'
720         WAV(iplot,iwfm).iDisplay=1;
721         update_display;
722         single_shot(0);  %0=no update
723         if strcmpi(WAV(iplot,iwfm).FamilyType,'PSM')
724           psm_display;  %updates psm parameter window
725         end
726       else                   %toggle 'off'
727         WAV(iplot,iwfm).iDisplay=0;
728         axes(SYS.Handles.Plot(iplot));
729         set(SYS.Handles.Trace1(iplot,iwfm),'xdata',[],'ydata',[]);
730         set(SYS.Handles.Trace2(iplot,iwfm),'xdata',[],'ydata',[]);
731         str=['Plot # ' num2str(SYS.iplot) '  Waveform: '];
732         set(SYS.Handles.Waveform,'String',str,'Foregroundcolor','k');
733         if strcmpi(WAV(iplot,iwfm).FamilyType,'PSM')
734           clear_psm_display
735         end
736         
737         %look for another waveform to use in the same plot
738         ifound=0;
739         for ii=1:4
740             if WAV(SYS.iplot,ii).iDisplay
741                 SYS.iwfm=ii;
742                 ifound=1;
743                 update_display;
744                 single_shot(0);  %0=no update
745                 if strcmpi(WAV(iplot,iwfm).FamilyType,'PSM')
746                   psm_display;  %updates psm parameter window
747                 end
748                 break
749             end
750         end
751         if ~ifound %look for another waveform to use in the other plot
752             SYS.iplot=rem(SYS.iplot,2)+1;
753            for ii=1:4
754             if WAV(SYS.iplot,ii).iDisplay
755                 SYS.iwfm=ii;
756                 ifound=1;
757                 update_display;
758                 single_shot(0);  %0=no update
759                 if strcmpi(WAV(iplot,iwfm).FamilyType,'PSM')
760                    psm_display;  %updates psm parameter window
761                 end
762                 break
763             end
764            end
765         end
766         if ~ifound  %no signals selected in either plot
767             SYS.iplot=1;
768             SYS.iwfm=1;
769         end
770
771       end  %end turn-off
772     end
773
774%==========================================       
775   function waveform_name(src,evt)    %%%%%%%%  waveform name  %%%%%%%%%
776%==========================================       
777       %display waveform name and plot index
778        iplot=SYS.iplot;
779        iwfm=SYS.iwfm;
780        if    ~SYS.Units
781            Units=WAV(iplot,iwfm).Monitor.RawDataUnits;
782        elseif SYS.Units
783            Units=WAV(iplot,iwfm).Monitor.EngDataUnits;
784        end
785        str=['    Plot ' num2str(SYS.iplot) ': ' WAV(iplot,iwfm).CommonName '  [' Units ']'  ];
786        set(SYS.Handles.Waveform,'String',str,'Foregroundcolor',  WAV(iplot,iwfm).Color);
787        set(get(SYS.Handles.Plot(iplot),'Ylabel'),'string',[char(WAV(iplot,iwfm).CommonName) '  [' Units ']'  ])
788
789   end
790 
791%==========================================       
792   function clear_plots_callback(src,evt)                 %%%%%%%%  clear_plots_callback  %%%%%%%%%
793%==========================================       
794       %callback of Clear Plots pushbutton   
795       %clears all waveform loads for selected graph   
796       iplot=get(src,'userdata');   %graph index
797         axes(SYS.Handles.Plot(iplot));
798         for iwfm=1:nwfm
799             if strcmpi(WAV(iplot,iwfm).FamilyType,'PSM')
800                 wavpsm2zero(iplot,iwfm)
801             end
802             wav2zero(iplot,iwfm);
803             clear_plot(iplot,iwfm)
804         end
805         
806         SYS.iGrid(iplot)=0;
807         set(get(SYS.Handles.Plot(iplot),'Xlabel'),'string','Timebase')
808         set(SYS.Handles.Waveform,'String','no waveform selected','Foregroundcolor','k');
809         for kk=1:2
810            set(SYS.Handles.Source(kk,2),...
811                        'ToolTipString',['Golden Timestamp ']);
812            set(SYS.Handles.Source(kk,3),'ToolTipString',['Data save time']);
813            set(SYS.Handles.Source(kk,4),'ToolTipString',['File load']);
814         end
815         SYS.iwfm=1;
816         
817         %check if waveforms loaded in other plot
818         SYS.iLoad=0;
819         ip=rem(iplot,2)+1;  %other plot
820         for iwfm=1:nwfm
821             if WAV(ip,iwfm).iLoad
822                 SYS.iLoad=1;
823             end
824         end
825   end
826
827%==========================================       
828function clear_plot(iplot,iwfm)
829%==========================================       
830%clear graphics for specific plot index, waveform index
831   set(SYS.Handles.Trace1(iplot,iwfm),'xdata',[],'ydata',[]);
832   set(SYS.Handles.Trace2(iplot,iwfm),'xdata',[],'ydata',[]);
833   set(SYS.Handles.Select(iplot,iwfm),'String','Select','ForegroundColor','k');
834   set(SYS.Handles.Display(iplot,iwfm),'Value',0);
835   str=['Plot # ' num2str(SYS.iplot) '  Waveform: '];
836   set(SYS.Handles.Master(iplot,iwfm),'Value',0);  %turn off master button
837   WAV(iplot,iwfm).imaster=0;         %waveform not master
838   set(SYS.Handles.Waveform,'String',str,'Foregroundcolor','k');
839   for ii=1:2   %turn off A,B,A-B radio button, turn on 'A'
840     for jj=1:3
841       set(SYS.Handles.([traces{ii} 'Channel'])(jj),'Value',0);
842     end
843     set(SYS.Handles.([traces{ii} 'Channel'])(1), 'Value',1);  %1=radio button for channel 'A'
844   end
845           
846   for ii=1:2  %turn off Channel A,B radio buttons, turn on 'Monitor
847     for jj=1:4   %Monitor,Golden,Save,File
848       set(SYS.Handles.Source(ii,jj),'Value',0);
849     end
850     set(SYS.Handles.Source(ii,1), 'Value',1);   %Monitor
851   end
852   
853   if strcmpi(WAV(ii,jj).FamilyType,'PSM')
854       clear_psm_display
855   end
856end
857
858%==========================================       
859function delete_waveform_callback(src,evt)   %delete_waveform_callback
860%==========================================       
861  val=get(src,'Userdata');
862  iplot=val(1);
863  iwfm=val(2);
864  wav2zero(iplot,iwfm);
865    if strcmpi(WAV(iplot,iwfm).FamilyType,'PSM')
866    wavpsm2zero(iplot,iwfm);
867    end
868  clear_plot(iplot,iwfm)
869end
870
871%==========================================       
872function wav2zero(ii,jj)
873%==========================================       
874%initialize segment of WAV structure
875%ii=graph index
876%jj=waveform index
877%NOTE: Could also make 'SYS.WAV0' and set WAV(ii,jj)=SYS.WAV0 each time
878  WAV(ii,jj).Family='';
879  WAV(ii,jj).FamilyType='';
880  WAV(ii,jj).ChannelName='';  %ChannelName of waveform
881  WAV(ii,jj).CommonName='';   %CommonName of waveform
882  WAV(ii,jj).iLoad=0;  %flag to indicate waveform loaded
883  for kk=1:4    %Monitor, Save, File, Golden
884    WAV(ii,jj).(sources{kk}).TimeStamp=[];   
885    WAV(ii,jj).(sources{kk}).RawData=[];  WAV(ii,jj).(sources{kk}).RawDataUnits=[];
886    WAV(ii,jj).(sources{kk}).EngData=[];  WAV(ii,jj).(sources{kk}).EngDataUnits=[];
887    WAV(ii,jj).(sources{kk}).XData=[];    WAV(ii,jj).(sources{kk}).XDataUnits=[];
888  end
889  WAV(ii,jj).iDisplay=0;  %flag to indicate waveform displayed
890  WAV(ii,jj).iMaster=0;  %master for plot scale
891  WAV(ii,jj).Color=SYS.colors{jj};
892  WAV(ii,jj).iSave=0;  %waveform saved memoryly
893  WAV(ii,jj).iFile=0;  %waveform loaded from file
894  WAV(ii,jj).iGolden=0;  %waveform loaded from golden
895  WAV(ii,jj).Trace1State=1; %TraceA on for plotting (always)
896  WAV(ii,jj).Trace1Channel=1;  %1,2 or 3 indicates A,B or A-B
897  WAV(ii,jj).Trace2State=0; %TraceB on for plotting (optional)
898  WAV(ii,jj).Trace2Channel=1;
899  WAV(ii,jj).ChannelA=1;  %1,2,3 or 4 indicates Monitor,Save,Golden,File
900  WAV(ii,jj).ChannelB=1;  %1,2,3 or 4 indicates Monitor,Save,Golden,File
901  WAV(ii,jj).TraceScale=1; %Trace1 selected for scaling
902  WAV(ii,jj).Trace1.XOffset=0;  WAV(ii,jj).Trace2.XOffset=0;
903  WAV(ii,jj).Trace1.YOffset=0;  WAV(ii,jj).Trace2.YOffset=0;
904  WAV(ii,jj).Trace1.YScale=1;   WAV(ii,jj).Trace2.YScale=1;
905end
906
907%==========================================       
908   function PlotWaveformGUI(src,evt)    %%%%%%%%  PlotWaveformGUI  %%%%%%%%%
909%==========================================       
910
911    %figure
912        SYS.Handles.Figure=figure('Numbertitle','off','name','PlotWaveform',...
913       'Position', [112 400 900 700],'Color',background,'CloseRequestFcn',@closeplotwaveform);
914    set(SYS.Handles.Figure,'Units','character');
915    set(SYS.Handles.Figure,'resize','off');
916    set(SYS.Handles.Figure,'Toolbar','none','menubar','none');
917   
918    %menus
919    h=uimenu(SYS.Handles.Figure,'Label','Plot #1');
920    uimenu(h,'Label','Toggle Graticule','Callback',@graticule_callback,'UserData',1);
921    uimenu(h,'Label','Pop Plot','Callback',@pop_axes_callback,'UserData',1);
922   
923    h=uimenu(SYS.Handles.Figure,'Label','Plot #2');
924    uimenu(h,'Label','Toggle Graticule','Callback',@graticule_callback,'UserData',2);
925    uimenu(h,'Label','Pop Plot','Callback',@pop_axes_callback,'UserData',2);
926
927    %axes
928    y=[0.66 0.29];
929    for ii=1:2  %two axes
930      SYS.Handles.Plot(ii)=axes('Box','on','Position',[0.1 y(ii) 0.72 0.3],'NextPlot','add');
931      %set(SYS.Handles.Plot(ii),'xticklabelmode','manual','xticklabel',[],'NextPlot','add');
932      set(get(SYS.Handles.Plot(ii),'Xlabel'),'string','Timebase')
933      set(get(SYS.Handles.Plot(ii),'Ylabel'),'Interpreter','None','string','Signal')
934      SYS.iGrid(ii)=0;
935    end; clear y
936 
937    %axes zoom and autoscale
938    uicontrol('Style', 'Frame','Position', [105 12 187 130],'Backgroundcolor',background);
939    uicontrol('Style', 'text','BackgroundColor','w','Position', [110 95  43 27],'String', ['Graph  1'; 'Vertical'],'Backgroundcolor',background)
940    uicontrol('Style', 'text','BackgroundColor','w','Position', [110 50  42 27],'String', ['Graph  2'; 'Vertical'],'Backgroundcolor',background)
941    uicontrol('Style', 'text','BackgroundColor','w','Position', [155 120 32 17],'String', 'Zoom','Backgroundcolor',background)
942    uicontrol('Style', 'text','BackgroundColor','w','Position', [190 120 32 17],'String', 'Offset','Backgroundcolor',background)
943    uicontrol('Style', 'text','BackgroundColor','w','Position', [115 18  30 17],'String', 'Horiz.','Backgroundcolor',background)
944
945    %sliders for zoom
946    slim=2;  %limit for zoom
947    uicontrol('Style','slider','Position',[162 92 20 30],'Callback',@vertical_zoom_callback,...
948    'Max',slim,'Min',-slim,'SliderStep',[0.01 0.01],'Value',0,'UserData',1);
949    uicontrol('Style','slider','Position',[162 50  20 30],'Callback',@vertical_zoom_callback,...
950    'Max',slim,'Min',-slim,'SliderStep',[0.01 0.01],'Value',0,'UserData',2);
951    uicontrol('Style','slider','Position',[195 92 20 30],'Callback',@vertical_offset_callback,...
952    'Max',slim,'Min',-slim,'SliderStep',[0.01 0.01],'Value',0,'UserData',1);
953    uicontrol('Style','slider','Position',[195 50  20 30],'Callback',@vertical_offset_callback,...
954    'Max',slim,'Min',-slim,'SliderStep',[0.01 0.01],'Value',0,'UserData',2);
955    uicontrol('Style','slider','Position',[155 20  30 20],'Callback',@horizontal_zoom_callback,...
956    'Max',slim,'Min',-slim,'SliderStep',[0.01 0.01],'Value',0);
957    uicontrol('Style','slider','Position',[190 20  30 20],'Callback',@horizontal_offset_callback,...
958    'Max',slim,'Min',-slim,'SliderStep',[0.01 0.01],'Value',0);
959
960    %pushbuttons for autoscale
961    uicontrol('Style','pushbutton','Position',[225 95  60 20],'String', 'Autoscale','Callback',@autoscale_callback,'Userdata',1,'Backgroundcolor',background);
962    uicontrol('Style','pushbutton','Position',[225 57  60 20],'String', 'Autoscale','Callback',@autoscale_callback,'Userdata',2,'Backgroundcolor',background);
963    uicontrol('Style','pushbutton','Position',[225 20  60 20],'String', 'Reset','Callback',@reset_horizontal_callback,...
964        'ToolTipString','Reset horizontal scale on both plots','BackgroundColor',background )
965
966    %waveform selection
967    uicontrol('Style', 'text','BackgroundColor','w','Position', [700 670 80 20],'String', 'Master','Backgroundcolor',background)
968    uicontrol('Style', 'text','BackgroundColor','w','Position', [760 670 40 20],'String', 'Display','Backgroundcolor',background)
969    uicontrol('Style', 'text','BackgroundColor','w','Position', [800 670 60 20],'String', 'Waveform','Backgroundcolor',background)
970    uicontrol('Style', 'text','BackgroundColor','w','Position', [860 670 40 20],'String', 'Delete','Backgroundcolor',background)
971
972    uicontrol('Style', 'Frame','Position', [296 12 115 130],'Backgroundcolor',background);
973    uicontrol('Style', 'Frame','Position', [415 12 163 130],'Backgroundcolor',background);
974    uicontrol('Style', 'Frame','Position', [582 12 130 130],'Backgroundcolor',background);
975    uicontrol('Style', 'Frame','Position', [716 12 175 130],'Backgroundcolor',background);
976
977    %trace line Handles, master, display box, edit box,
978    y =[650 395];                %vertical offsets for axes in pixel
979    yc=[545 295];                %vertical offsets for clear plot button
980    for ii=1:2
981      axes(SYS.Handles.Plot(ii));
982      for jj=1:nwfm
983        %master
984        SYS.Handles.Master(ii,jj)=uicontrol('Style', 'radiobutton','BackgroundColor','w',...
985        'Callback',@masterbox_callback,    'Position', [750 y(ii)-25*(jj-1) 17 17], 'Enable', 'on','Userdata',[ii jj],'Backgroundcolor',background);   
986        %display
987         SYS.Handles.Display(ii,jj)=uicontrol('Style', 'checkbox','BackgroundColor','w',...
988         'Callback',@display_waveform_callback,'Position', [775 y(ii)-25*(jj-1) 17 17], 'Enable', 'on','Userdata',[ii jj],'Backgroundcolor',background);
989         %select
990         SYS.Handles.Select(ii,jj)=uicontrol('Style', 'pushbutton','BackgroundColor','w', 'String', 'Select',...
991        'Callback',@select_waveform_callback,'Position', [800 y(ii)-25*(jj-1) 60 20], 'Enable', 'on','Userdata',[ii jj],'Backgroundcolor',background);       
992        %delete
993         SYS.Handles.Delete(ii,jj)=uicontrol('Style', 'pushbutton','BackgroundColor','w',...
994         'Callback',@delete_waveform_callback,'Position', [865 y(ii)-25*(jj-1) 25 20], 'Enable', 'on','Userdata',[ii,jj],'Backgroundcolor',background);
995       
996        %trace initialization (lines, sources, displays)
997         SYS.Handles.Trace1(ii,jj)=line('XData',[],'YData',[],'Color',SYS.colors{jj});  %'Linewidth',linewidth{1}
998         SYS.Handles.Trace2(ii,jj)=line('XData',[],'YData',[],'Color',SYS.colors{jj},'Linestyle',':');
999      end  %loop on waveforms
1000     
1001        %clear plot buttons
1002        uicontrol('Style', 'pushbutton','BackgroundColor','w', 'String', 'Clear Plots',...
1003        'Position', [800  yc(ii)  90 20],'Callback',@clear_plots_callback,'UserData',ii,'Backgroundcolor',background)
1004    end; %loop on axes
1005   
1006    clear linewidth
1007
1008    %trace1 and trace2 (A, B, A-B) radiobuttons
1009    y=[98 37];
1010    for kk=1:2  %loop over traces
1011       uicontrol('Style', 'text','BackgroundColor','w','Position', [297 y(kk)+15 40 20],'String', traces{kk},'Backgroundcolor',background);
1012       SYS.Handles.([traces{kk} 'Channel'])(1)=uicontrol('Style', 'radiobutton','BackgroundColor','w','Position', [300 y(kk)  30 17],...
1013           'Callback',@update_channel_callback,'String', 'A',  'Userdata',[kk 1],'Value',0,'Backgroundcolor',background);
1014       SYS.Handles.([traces{kk} 'Channel'])(2)=uicontrol('Style', 'radiobutton','BackgroundColor','w','Position', [330 y(kk)  30 17],...
1015           'Callback',@update_channel_callback,'String', 'B',  'Userdata',[kk 2],'Value',0,'Backgroundcolor',background);
1016       SYS.Handles.([traces{kk} 'Channel'])(3)=uicontrol('Style', 'radiobutton','BackgroundColor','w','Position', [360 y(kk)  40 17],...
1017           'Callback',@update_channel_callback,'String', 'A-B','Userdata',[kk 3],'Value',0,'Backgroundcolor',background);
1018    end;  clear y
1019   
1020    %trace2 on/off selection   
1021       SYS.Handles.Trace2State=uicontrol('Style', 'radiobutton','BackgroundColor','w',...
1022            'Position', [360 58  50 17],'Callback',@trace2state_callback,'String', 'on/off','Value',0,'Backgroundcolor',background);
1023
1024    %waveform name display
1025       uicontrol('Style','Frame','Position', [298-df 150-df 280+2*df 17+2*df])
1026       SYS.Handles.Waveform=uicontrol('Style', 'text','BackgroundColor','w','Position', [298 150 280 17],'String', 'Waveform Name',...
1027           'Backgroundcolor',background,'fontsize',10,'HorizontalAlignment','Center');   
1028     
1029    %raw/eng units selection
1030       SYS.Handles.RawUnits=uicontrol('Style', 'radiobutton','String', 'Raw Units','Value',0,'UserData',0,...
1031        'Position', [590 150 65 17],'Callback',@unitselect_callback,'Backgroundcolor',background,'FontSize',7);
1032       SYS.Handles.EngUnits=uicontrol('Style', 'radiobutton','String', 'Eng Units','Value',1,'UserData',1,...
1033        'Position', [650 150 65 17],'Callback',@unitselect_callback,'Backgroundcolor',background,'FontSize',7);
1034   
1035    %ChannelA and ChannelB source selection (Monitor/Golden/Saved/File) Userdata contains {Channel #, source}
1036       uicontrol('Style', 'text','BackgroundColor','w','Position', [419 118 50 17],'String', 'Channel A','Backgroundcolor',background);
1037       uicontrol('Style', 'text','BackgroundColor','w','Position', [497 118 50 17],'String', 'Channel B','Backgroundcolor',background);
1038       x=[420 497];
1039       for kk=1:2   %loop over channels A,B
1040        SYS.Handles.Source(kk,1)=uicontrol('Style', 'radiobutton','BackgroundColor','w','Position', [x(kk) 98 75 17],...
1041        'Callback',@update_source_callback,'String','Monitor'   ,'Userdata',{kk 1},'Backgroundcolor',background);
1042        SYS.Handles.Source(kk,2)=uicontrol('Style', 'radiobutton','BackgroundColor','w','Position', [x(kk) 78 75 17],...
1043        'Callback',@update_source_callback,'String','Golden'    ,'Userdata',{kk 2},'Backgroundcolor',background,'ToolTipString','Golden Waveform');
1044        SYS.Handles.Source(kk,3)=uicontrol('Style', 'radiobutton','BackgroundColor','w','Position', [x(kk) 58 75 17],...
1045        'Callback',@update_source_callback,'String','Saved Data','Userdata',{kk 3},'Backgroundcolor',background','ToolTipString','Saved Waveform');
1046        SYS.Handles.Source(kk,4)=uicontrol('Style', 'radiobutton','BackgroundColor','w','Position', [x(kk) 38 75 17],...
1047        'Callback',@update_source_callback,'String','File'      ,'Userdata',{kk 4},'Backgroundcolor',background','ToolTipString','File Waveform');
1048       end; clear x
1049     
1050    %scale single waveform
1051     slim=2;  %limit for sliders
1052     uicontrol('Style', 'text','String', 'Scale','Position',  [615 100 30 17],'Backgroundcolor',background)
1053     uicontrol('Style', 'text','String', 'Offset','Position',[650 100 40 17],'Backgroundcolor',background)
1054     uicontrol('Style', 'text','String', 'Vert.','Position',  [590 80  25 17],'Backgroundcolor',background)
1055     uicontrol('Style', 'text','String', 'Horizontal','Position',  [590 50  55 17],'Backgroundcolor',background)
1056     SYS.Handles.Trace1Scale=uicontrol('Style', 'radiobutton','String', 'Trace1','Value',1,'Userdata',1,...
1057        'Position', [590 120 55 17],'Callback',@tracescale_callback,'Backgroundcolor',background);
1058     SYS.Handles.Trace2Scale=uicontrol('Style', 'radiobutton','String', 'Trace2','Value',0,'Userdata',2,...
1059        'Position', [650 120 55 17],'Callback',@tracescale_callback,'Backgroundcolor',background);
1060     uicontrol('Style', 'pushbutton','String', 'Reset',...
1061        'Position', [625 18  50 17],'Callback',@reset_waveform_scale,...
1062        'ToolTipString','Reset x/y scale - this trace, this waveform only','Backgroundcolor',background)
1063     uicontrol('Style','slider','Position',[622 74 20 30],'Callback',@vertical_scale_single_callback,...
1064    'Max',slim,'Min',-slim,'SliderStep',[0.01 0.01],'Value',0,'UserData',1);
1065     uicontrol('Style','slider','Position',[660 74 20 30],'Callback',@vertical_offset_single_callback,...
1066    'Max',slim,'Min',-slim,'SliderStep',[0.01 0.01],'Value',0,'UserData',1);
1067    uicontrol('Style','slider','Position',[655 50  30 20],'Callback',@horizontal_offset_single_callback,...
1068    'Max',slim,'Min',-slim,'SliderStep',[0.25 0.25],'Value',0);
1069    SYS.Handles.XOffsetDisplay=uicontrol('Style','text','Position',[605 38 35 15],'String','0');
1070
1071    %save and load data
1072     uicontrol('Style', 'pushbutton','String', 'Save Data',...
1073        'Position', [720 110 80 17],'Callback',@monitor2save_callback,'ToolTipString','store waveform in memory','Backgroundcolor',background)
1074     uicontrol('Style', 'pushbutton','String', 'Save to File',...
1075        'Position', [720 80  80 17],'Callback',@save2file_callback,'ToolTipString','write waveform to file','Backgroundcolor',background)
1076     uicontrol('Style', 'pushbutton','String', 'Save to Golden',...
1077        'Position', [720 50  80 17],'Callback',@save2golden_callback,'ToolTipString','write waveform to golden','Backgroundcolor',background)
1078     uicontrol('Style', 'pushbutton','String', 'Load Data File',...
1079        'Position', [805 80  80 17],'Callback',@file2memory_callback,'ToolTipString','read waveform from file','Backgroundcolor',background)
1080     uicontrol('Style', 'pushbutton','String', 'Load Golden',...
1081        'Position', [805 50  80 17],'Callback',@golden2memory_callback,'ToolTipString','golden file to memory','Backgroundcolor',background)
1082   
1083
1084    %data transfer to file, workspace
1085    SYS.Handles.all2workspace=uicontrol('Style', 'checkbox','BackgroundColor','w', 'String', 'All',...
1086        'Position', [830 20 35 17],'Callback',@all_checkbox_callback,'Backgroundcolor',background);
1087    uicontrol('Style', 'pushbutton','BackgroundColor','w', 'String', 'Write to Workspace',...
1088        'Position', [720 20  100 17],'Callback',@write2workspace_callback,'Backgroundcolor',background)
1089   
1090    %trigger controls
1091    uicontrol('Style', 'Frame','Position', [10 35 90 107],'Backgroundcolor',background);
1092    uicontrol('Style', 'text','BackgroundColor','w','Position', [25 118 60 20],'String', 'Trigger','Backgroundcolor',background)
1093    uicontrol('Style', 'pushbutton','BackgroundColor','w', 'String', 'One Shot',...
1094        'Position', [15 100 80 17],'Callback',@single_shot_callback,'Backgroundcolor',background);
1095    SYS.Handles.Timer=uicontrol('Style', 'pushbutton','BackgroundColor','w', 'String', 'Continuous',...
1096        'Position', [15 70 80 17],'Callback',@launch_timer_callback,'Backgroundcolor',background);
1097   
1098    %debug
1099    uicontrol('Style', 'pushbutton','BackgroundColor','w','Position', [375 680 60 20],'String', 'Debug',...
1100        'Callback',@show_state_callback','Backgroundcolor',background,'fontsize',10,'HorizontalAlignment','Left');       
1101   
1102    %update PSM display
1103    if strcmpi(SYS.Machine,'SPEAR3')
1104        uicontrol('Style', 'pushbutton','BackgroundColor','w','Position', [475 680 150 20],'String', 'Update PSM Display',...
1105        'Callback',@psm_display_callback','Backgroundcolor',background,'fontsize',10,'HorizontalAlignment','Left');       
1106    end
1107   
1108    %date
1109    SYS.Date = uicontrol('Style', 'text','FontSize',8,'BackgroundColor','w','string',datestr(now,'HH:MM:SS PM'  ),...
1110        'Position', [15 40 80 17],'Backgroundcolor',background);
1111
1112end %end PlotWaveformGUI
1113
1114%==========================================       
1115   function masterbox_callback(src,evt)    %%%%%%%%  masterbox_callback  %%%%%%%%%
1116%==========================================       
1117   end
1118
1119%==========================================       
1120    function iok=check_xunits(iplot,Family)
1121%==========================================       
1122        %check family has same time base as other waveforms in plot
1123        iok=1;
1124        ii=iplot;
1125        xunits=AO.(Family{1}).XUnits;
1126        for jj=1:nwfm
1127            if   WAV(ii,jj).iLoad
1128                Family=WAV(ii,jj).Family;
1129                if ~(strcmpi(AO.(Family).XUnits,xunits))
1130                    iok=0;   %found a disagreement in units
1131                    for kk=1:length(SYS.Families)
1132                      set(SYS.Handles.(SYS.Families{kk}),'value',[])
1133                    end
1134                    return
1135                end
1136            end
1137        end
1138    end
1139
1140%==========================================       
1141function vertical_zoom_callback(src,evt)    %%%%%%%%  vertical_zoom_callback %%%%%%%%%
1142%==========================================       
1143      h=SYS.Handles.Plot(get(src,'Userdata'));   %plot Handles
1144      xlim=get(h,'XLim');
1145      ylim=get(h,'YLim');
1146      factor = 1.1;
1147      if get(src,'Value') < 0     %increase size
1148        del = (factor-1)*(ylim(2)-ylim(1));
1149      else                        %decrease size
1150        del = (1/factor-1)*(ylim(2)-ylim(1));
1151      end
1152      set(h,'XLim',xlim,'YLim',[ylim(1)-del/2 ylim(2)+del/2]);
1153      set(src, 'Value', 0)
1154   end
1155
1156
1157%==========================================       
1158   function vertical_offset_callback(src,evt)    %%%%%%%%  vertical_offset_callback %%%%%%%%%
1159%==========================================
1160%vertically offsets Plot 1 or Plot 2
1161      h=SYS.Handles.Plot(get(src,'Userdata'));   %plot Handles
1162      xlim=get(h,'XLim');
1163      ylim=get(h,'YLim');
1164      factor = 1.1;
1165      if get(src,'Value') < 0     %raise scale
1166        del = (factor-1)*(ylim(2)-ylim(1));
1167      else                        %lower scale
1168        del = (1/factor-1)*(ylim(2)-ylim(1));
1169      end
1170      set(h,'XLim',xlim,'YLim',[ylim(1)+del/2 ylim(2)+del/2]);
1171      set(src, 'Value', 0)
1172   end
1173%==========================================       
1174  function horizontal_offset_callback(src,evt)    %%%%%%%%  horizontal_offset_callback %%%%%%%%%
1175%==========================================             
1176%horizontal offset Plot 1 and Plot 2
1177      factor = 1.1;
1178      for ii=1:2
1179      h=SYS.Handles.Plot(ii);
1180      xlim=get(h,'XLim');
1181      ylim=get(h,'YLim');
1182      if get(src,'Value') < 0     %raise scale
1183        del = (factor-1)*(xlim(2)-xlim(1));
1184      else                        %lower scale
1185        del = (1/factor-1)*(xlim(2)-xlim(1));
1186      end
1187      set(SYS.Handles.Plot(ii),'XLim',[xlim(1)+del/2 xlim(2)+del/2],'YLim',ylim);
1188      end
1189      set(src, 'Value', 0)
1190   end
1191
1192%==========================================       
1193   function horizontal_zoom_callback(src,evt)    %%%%%%%%  horizontal_zoom_callback %%%%%%%%%
1194%==========================================       
1195%horizontal zoom Plot 1 and Plot 2
1196      factor = 1.1;
1197      for ii=1:2
1198      h=SYS.Handles.Plot(ii);
1199      xlim=get(h,'XLim');
1200      ylim=get(h,'YLim');
1201      if get(src,'Value') < 0     %increase size
1202        del = (factor-1)*(xlim(2)-xlim(1));
1203      else                        %decrease size
1204        del = (1/factor-1)*(xlim(2)-xlim(1));
1205      end
1206      set(SYS.Handles.Plot(ii),'XLim',[xlim(1)-del/2 xlim(2)+del/2],'YLim',ylim);
1207      end
1208      set(src, 'Value', 0)
1209   end
1210
1211%===============================================       
1212    function reset_horizontal_callback(src,evt)   %reset_horizontal_callback
1213%===============================================       
1214      for ii=1:2
1215        for jj=1:nwfm
1216          if WAV(ii,jj).iLoad
1217            for kk=1:2   %loop over traces
1218              if WAV(ii,jj).iLoad
1219                xmin=WAV(ii,jj).(traces{kk}).XData(1);
1220                xmax=WAV(ii,jj).(traces{kk}).XData(end);
1221                break
1222              end
1223            end
1224          break
1225          end
1226        end
1227        set(SYS.Handles.Plot(ii),'XLim',[xmin xmax]);
1228      end
1229     
1230    end
1231
1232%==========================================       
1233    function autoscale_callback(src,evt)    %%%%%%%%  autoscale_callback %%%%%%%%%
1234%==========================================       
1235%autoscales selected plot
1236      ii=get(src,'Userdata');
1237      ymax=-1e6;
1238      ymin=1e6;
1239      for jj=1:nwfm
1240      if   WAV(ii,jj).iDisplay   %check if waveform is displayed
1241          %Trace1
1242            y=get(SYS.Handles.Trace1(ii,jj),'ydata');
1243            ymax1=max(y);
1244            ymin1=min(y);
1245            if ymax1>ymax
1246              ymax=ymax1;
1247            end
1248            if ymin1<ymin
1249              ymin=ymin1;
1250            end
1251           
1252          %Trace2
1253          if WAV(ii,jj).Trace2State
1254            y=get(SYS.Handles.Trace2(ii,jj),'ydata');
1255            ymax1=max(y);
1256            ymin1=min(y);
1257            if ymax1>ymax
1258              ymax=ymax1;
1259            end
1260            if ymin1<ymin
1261              ymin=ymin1;
1262            end
1263          end
1264      end  %end condition on display
1265      end  %end loop on waveforms
1266
1267      ymin=ymin-0.05*abs(ymin);
1268      ymax=ymax+0.05*abs(ymax);
1269      if ymin>ymax
1270          t=ymin;
1271          ymin=ymax;
1272          ymax=t;
1273      end
1274      if ymin==0 && ymax==0
1275          ymax=1;
1276      end
1277%       disp(num2str(ymin))
1278%       disp(num2str(ymax))
1279
1280      a=axis(SYS.Handles.Plot(ii));
1281      set(SYS.Handles.Plot(ii),'xlim',[a(1) a(2)],'ylim',[ymin ymax])
1282      end %end function
1283
1284 %==========================================       
1285    function graticule_callback(src,evt)       %graticule_callback
1286 %==========================================       
1287        iplot=get(gcbo,'Userdata');
1288        if SYS.iGrid(iplot)==0
1289          set(SYS.Handles.Plot(iplot),'XGrid','On', 'YGrid','On')
1290          SYS.iGrid(iplot)=1;
1291        else
1292          set(SYS.Handles.Plot(iplot),'XGrid','Off', 'YGrid','Off')
1293          SYS.iGrid(SYS.iplot)=0;
1294        end
1295        clear iplot
1296    end
1297
1298
1299 %==========================================       
1300   function tracescale_callback(src,evt)      %tracescale_callback
1301 %==========================================       
1302%radio buttons to control scaling of Trace1 or Trace2
1303        tr=get(src,'Userdata');
1304        iplot=SYS.iplot;
1305        iwfm=SYS.iwfm;
1306          if ~get(SYS.Handles.Trace2State,'Value')   %trace2 not selected
1307             set(SYS.Handles.Trace2Scale,'Value',0)  %put back radio button
1308             return
1309          end
1310
1311        if ~WAV(iplot,iwfm).iLoad;
1312            set(SYS.Handles.Trace1Scale,'Value',1)
1313            set(SYS.Handles.Trace2Scale,'Value',0)
1314            errordlg('Select waveform first','Trace Selection'); 
1315            return;
1316        end
1317        %toggle radio buttons
1318        if tr==1
1319          val=get(SYS.Handles.Trace1Scale,'Value')
1320          if val  %user turned Trace1 on
1321             set(SYS.Handles.Trace2Scale,'Value',0)
1322             WAV(iplot,iwfm).TraceScale=1; %Trace1 selected for scaling
1323             set(SYS.Handles.XOffsetDisplay,'String',num2str(WAV(iplot,iwfm).(traces{1}).XOffset, '%6.3f'))
1324          else  %user turned Trace1 off
1325             set(SYS.Handles.Trace2Scale,'Value',1)
1326             WAV(iplot,iwfm).TraceScale=2; %Trace2 selected for scaling
1327             set(SYS.Handles.XOffsetDisplay,'String',num2str(WAV(iplot,iwfm).(traces{2}).XOffset, '%6.3f'))
1328          end
1329        elseif tr==2
1330          val=get(SYS.Handles.Trace1Scale,'Value')
1331          if val  %user turned Trace2 on
1332             set(SYS.Handles.Trace1Scale,'Value',0)
1333             WAV(iplot,iwfm).TraceScale=2; %Trace2 selected for scaling
1334             set(SYS.Handles.XOffsetDisplay,'String',num2str(WAV(iplot,iwfm).(traces{2}).XOffset, '%6.3f'))
1335          else  %user turned Trace2 off
1336             set(SYS.Handles.Trace1Scale,'Value',1)
1337             WAV(iplot,iwfm).TraceScale=1; %Trace1 selected for scaling
1338             set(SYS.Handles.XOffsetDisplay,'String',num2str(WAV(iplot,iwfm).(traces{1}).XOffset, '%6.3f'))
1339          end
1340        end
1341     
1342    end
1343
1344 %==========================================       
1345   function unitselect_callback(src,evt)        %unitselect_callback
1346 %==========================================       
1347      %radio buttons to control display of Raw and Eng Units
1348        iplot=SYS.iplot;
1349        iwfm=SYS.iwfm;
1350
1351        if ~WAV(iplot,iwfm).iLoad;
1352            set(SYS.Handles.RawUnits,'Value',0)
1353            set(SYS.Handles.EngUnits,'Value',1)
1354            errordlg('Select waveform first','Unit Selection'); 
1355            return;
1356        end
1357       
1358        %toggle radio buttons
1359          btn=get(src,'UserData');
1360          val=get(src,'Value');
1361          if     ~btn && val  %Raw turned on
1362             set(SYS.Handles.EngUnits,'Value',0)
1363             SYS.Units=0; %Raw Units
1364          elseif ~btn && ~val  %Raw turned off
1365             set(SYS.Handles.EngUnits,'Value',1)
1366             SYS.Units=1; %Eng Units
1367          elseif btn && val  %Eng turned on
1368             set(SYS.Handles.RawUnits,'Value',0)
1369             SYS.Units=1;
1370          elseif btn && ~val  %Eng turned off
1371             set(SYS.Handles.RawUnits,'Value',1)
1372             SYS.Units=0;
1373          end   
1374          if strcmpi(pwtimer.Running,'off')  %timer not running
1375              single_shot_callback;
1376              waveform_name;
1377          end
1378    end
1379
1380  %==========================================       
1381  function reset_waveform_scale(src,evt)
1382  %==========================================       
1383      %reset scaling of single waveform
1384        iplot=SYS.iplot;
1385        iwfm=SYS.iwfm;
1386        tr=WAV(iplot,iwfm).TraceScale;
1387        if ~WAV(iplot,iwfm).iLoad; errordlg('Select waveform first','Reset');  return; end
1388        WAV(iplot,iwfm).(traces{tr}).YScale=1;
1389        WAV(iplot,iwfm).(traces{tr}).YOffset=0;
1390        WAV(iplot,iwfm).(traces{tr}).XOffset=0;
1391        set(SYS.Handles.XOffsetDisplay,'String',num2str(WAV(iplot,iwfm).(traces{tr}).XOffset, '%6.3f'))
1392        plot_waveform(iplot,iwfm,tr);
1393    end
1394
1395  %===============================================       
1396  function vertical_scale_single_callback(src,evt)   %vertical_scale_single_callback
1397  %===============================================       
1398      %scale single waveform vertical
1399        iplot=SYS.iplot;
1400        iwfm=SYS.iwfm;
1401        tr=WAV(iplot,iwfm).TraceScale;
1402        if ~WAV(iplot,iwfm).iLoad
1403            errordlg('Select waveform first','Vertical Scale')
1404        end
1405        val=get(src,'Value');
1406        if val>0
1407            del=0.05;
1408        elseif val<0
1409            del=-0.05;
1410        end
1411        WAV(iplot,iwfm).(traces{tr}).YScale=WAV(iplot,iwfm).(traces{tr}).YScale+del;
1412        if WAV(iplot,iwfm).(traces{tr}).YScale<0
1413            WAV(iplot,iwfm).(traces{tr}).YScale=0;
1414        end
1415        plot_waveform(iplot,iwfm,tr);
1416        set(src, 'Value', 0)
1417    end
1418
1419 %==========================================       
1420   function vertical_offset_single_callback(src,evt)     %vertical_offset_single_callback
1421 %==========================================       
1422      %vertical offset for single waveform
1423        iplot=SYS.iplot;
1424        iwfm=SYS.iwfm;
1425        if ~WAV(iplot,iwfm).iLoad
1426          errordlg('Select waveform first','Vertical Offset')
1427        end
1428        tr=WAV(iplot,iwfm).TraceScale;
1429        val=get(src,'Value');
1430       
1431        h=SYS.Handles.Plot(iplot);   %plot Handles
1432        xlim=get(h,'XLim');
1433        ylim=get(h,'YLim');
1434        factor = 1.1;
1435        %get(src,'Value')
1436        if get(src,'Value') < 0     %raise scale
1437          del = (factor-1)*(ylim(2)-ylim(1));
1438        else                        %lower scale
1439          del = (1/factor-1)*(ylim(2)-ylim(1));
1440        end
1441        WAV(iplot,iwfm).(traces{tr}).YOffset=WAV(iplot,iwfm).(traces{tr}).YOffset+del;
1442        plot_waveform(iplot,iwfm,tr);
1443        set(h,'XLim',xlim,'YLim',ylim);   %return plot coordinates to original value
1444        set(src, 'Value', 0)
1445
1446    end
1447
1448  %==========================================       
1449  function horizontal_offset_single_callback(src,evt)    %horizontal_offset_single_callback
1450  %==========================================       
1451      %horizontal offset for single waveform
1452        iplot=SYS.iplot;
1453        iwfm=SYS.iwfm;
1454        if ~WAV(iplot,iwfm).iLoad
1455          errordlg('Select waveform first','Horizontal Offset')
1456         end
1457        tr=WAV(iplot,iwfm).TraceScale;
1458        val=get(src,'Value');
1459        nstep=2;
1460        WAV(iplot,iwfm).(traces{tr}).XOffset=...
1461        WAV(iplot,iwfm).(traces{tr}).XOffset+val*nstep*WAV(iplot,iwfm).Monitor.XDataStep;
1462        plot_waveform(iplot,iwfm,tr);
1463        set(src, 'Value', 0)
1464        set(SYS.Handles.XOffsetDisplay,'String',num2str(WAV(iplot,iwfm).(traces{tr}).XOffset, '%6.3f'))
1465    end
1466
1467%==========================================       
1468function makeSYSstruct
1469%==========================================       
1470 SYS=[];
1471 SYS.Families=fieldnames(AO);  %check to see if Families are MemberOf 'PlotWaveform'
1472 for ii=1:size(SYS.Families,1)
1473 SYS.FamilyTypes{ii}=AO.(SYS.Families{ii}).FamilyType; 
1474 end
1475 SYS.Machine='SPEAR3';
1476 SYS.iplot=1;
1477 SYS.iwfm=1;
1478 SYS.colors={'b','r','k','m'};   %colors for plots
1479 SYS.iLoad=0;
1480 SYS.Units=1;  %0 for raw data (.RawData), 1 for converted data (.EngData)
1481end
1482
1483%==========================================       
1484function makeWAVstruct
1485%==========================================       
1486 %initialize WAV structure for program PlotWaveform
1487  WAV=[];
1488  for ii=1:2  %plots
1489    for jj=1:nwfm  %waveforms
1490      wav2zero(ii,jj)
1491    end
1492  end
1493end
1494
1495%==========================================       
1496%     function pop_axes_callback(src,evt)
1497%==========================================       
1498%      SYS.iplot=get(gcbo,'Userdata');
1499%      a=figure;
1500%      myplot=gca;
1501%      b=copyobj(gca, a);
1502%      for ii=1:4
1503%        if WAV(SYS.iplot,ii).iplot==1;
1504%            h=WAV(SYS.iplot,ii).Line.Handles;
1505%            copyobj(h,b);
1506%        end
1507%      end
1508%      clear h
1509%
1510%      t=axis(gca);
1511%      axis(t)
1512%      set(b, 'Position',[0.13 0.11 0.775 0.815]);
1513%      set(b, 'ButtonDownFcn','');
1514%      set(b, 'XAxisLocation','Bottom');
1515%      set(b,'XTick',[]);
1516%      set(b,'XTick',get(SYS.Handles.Plot(iplot),'XTick'));
1517%
1518%
1519% %      set(b,'YTickLabel',[]);
1520% %      axis(b,[get(SYS.Handles.Plot(iplot),'XLim') get(SYS.Handles.Plot(iplot),'YLim')]);
1521%      xlabel('Time [microsec]');
1522%      orient portrait
1523%     end
1524
1525%==========================================       
1526function show_state_callback(src,evt)   %%%%%%%%  show_state_callback %%%
1527%==========================================       
1528disp(['Machine: ' SYS.Machine])
1529disp(['Graph ' num2str(SYS.iplot)])
1530disp(['Waveform ' num2str(SYS.iwfm)])
1531disp(' ')
1532show_string('Family')
1533show_string('ChannelName')
1534show_string('CommonName')
1535show_index('iLoad','waveform loaded')
1536show_index('iDisplay','selected for display')
1537%show_index('iMaster')
1538show_index('iSave','memory save')
1539show_index('iFile','file loaded')
1540show_index('iGolden','Golden loaded')
1541show_index('Trace1State','on or off')
1542show_index('Trace1Channel','A,B or A-B')
1543show_index('Trace2State','on or off')
1544show_index('Trace2Channel','A,B or A-B')
1545show_index('ChannelA','Monitor, Golden, Save, File')
1546show_index('ChannelB','Monitor, Golden, Save, File')
1547show_index('TraceScale','horizontal waveform offset')
1548show_index('Trace1','horizontal waveform offset','XOffset')
1549show_index('Trace2','horizontal waveform offset','XOffset')
1550show_index('Trace1','vertical waveform offset','YOffset')
1551show_index('Trace2','vertical waveform offset','YOffset')
1552show_index('Trace1','vertical waveform scaling','YScale')
1553show_index('Trace2','vertical waveform scaling','YScale')
1554%show_index('YOffset','vertical waveform offset')
1555
1556%==========================================       
1557    function show_string(field)
1558%==========================================       
1559     disp(field)
1560     for ii=1:2
1561      t=[];
1562       for jj=1:nwfm
1563        if isempty(WAV(ii,jj).(field));
1564            txt='None';
1565        else
1566            txt=WAV(ii,jj).(field);
1567        end
1568        t=[t txt '   '];
1569      end
1570      disp(t)
1571     end
1572     disp(' ')
1573    end
1574
1575 %==========================================       
1576   function show_index(field1,comment,field2)
1577 %==========================================       
1578    ifield2=0;
1579     if ~exist('field2','var')
1580       disp([field1   ' [  ' comment ' ]'])
1581     else
1582       ifield2=1;
1583       disp([field1 '.' field2 '   [' comment ' ]'])
1584     end
1585     
1586     for ii=1:2
1587      t=[];
1588       for jj=1:nwfm
1589            if ~ifield2
1590            t=[t WAV(ii,jj).(field1)];
1591            else
1592            t=[t WAV(ii,jj).(field1).(field2)];   
1593            end
1594       end
1595      disp(t)
1596     end
1597      disp(' ')
1598    end
1599  end  %show_state_callback
1600
1601%==========================================       
1602    function closeplotwaveform(src,evt)
1603%==========================================
1604%callback of closewindow
1605        %kill the timer
1606          if exist('pwtimer','var')
1607            stop(pwtimer);
1608            delete(pwtimer);
1609            clear pwtimer
1610          end
1611          t=timerfind;
1612          for k=1:length(t)
1613            if strcmpi(t(k).Tag,'PlotwaveformTimer')
1614              stop(t(k));
1615              delete(t(k));
1616            end
1617          end
1618          delete(SYS.Handles.Figure);
1619          %if  exist('SYS.PSMHandles.Figure')
1620             if  ishandle(SYS.PSMHandles.Figure)
1621              delete(SYS.PSMHandles.Figure);
1622             end
1623          %end
1624      %clear global SYS WAV AO AD;
1625    end
1626
1627%==========================================       
1628function wavpsm2zero(ii,jj)
1629%==========================================       
1630 for kk=1:4
1631  WAV(ii,jj).(sources{kk}).XDataStep=[];
1632  WAV(ii,jj).(sources{kk}).VoltOffset=[]; %Voltage offset
1633  WAV(ii,jj).(sources{kk}).PolyConv=[]; %Polynomial conversion factors
1634  WAV(ii,jj).(sources{kk}).RawSignal='';  WAV(ii,jj).(sources{kk}).EngSignal='';  %VoltFst, Fst
1635  WAV(ii,jj).(sources{kk}).EngAvg=[];        WAV(ii,jj).(sources{kk}).EngFstAvg=[];
1636  WAV(ii,jj).(sources{kk}).EngCtr=[];        WAV(ii,jj).(sources{kk}).EngFstCtr=[];
1637  WAV(ii,jj).(sources{kk}).EngMax=[];        WAV(ii,jj).(sources{kk}).EngFstMax=[];
1638  WAV(ii,jj).(sources{kk}).EngMin=[];        WAV(ii,jj).(sources{kk}).EngFstMin=[];
1639  WAV(ii,jj).(sources{kk}).EngSum=[];        WAV(ii,jj).(sources{kk}).EngFstSum=[];
1640  WAV(ii,jj).(sources{kk}).EngInt=[];        WAV(ii,jj).(sources{kk}).EngFstInt=[];  %<Charge or Ener>  (Int value - "Charge" for Curr, "Ener" for Pwr, "Intgrl" for Load)
1641  WAV(ii,jj).(sources{kk}).RawAvg=[];        WAV(ii,jj).(sources{kk}).RawFstAvg=[];
1642  WAV(ii,jj).(sources{kk}).RawCtr=[];        WAV(ii,jj).(sources{kk}).RawFstCtr=[];
1643  WAV(ii,jj).(sources{kk}).RawMax=[];        WAV(ii,jj).(sources{kk}).RawFstMax=[];
1644  WAV(ii,jj).(sources{kk}).RawMin=[];        WAV(ii,jj).(sources{kk}).RawFstMin=[];
1645  WAV(ii,jj).(sources{kk}).RawSum=[];        WAV(ii,jj).(sources{kk}).RawFstSum=[];
1646  WAV(ii,jj).(sources{kk}).RawInt=[];        WAV(ii,jj).(sources{kk}).RawFstInt=[];  %<Charge or Ener>  (Int value - "Charge" for Curr, "Ener" for Pwr, "Intgrl" for Load)
1647 end
1648end
1649
1650%======================================
1651    function make_psm_display(src,evt)
1652%======================================       
1653    %generate figure to display SPEAR3 psm parameters
1654    SYS.PSMHandles.Figure=figure('Numbertitle','off','name','SPEAR3 PSM Parameters',...
1655       'Position', [110 300 500 200],'resize','off','Color',background,'CloseRequestFcn',@close_psm);
1656     
1657    %Source headers (Monitor, Save, File, Golden)
1658    uicontrol('Style','text','BackgroundColor',background, 'String', 'Monitor','Position', [105 180 50 17]);
1659    uicontrol('Style','text','BackgroundColor',background, 'String', 'Golden', 'Position', [205 180 50 17]);
1660    uicontrol('Style','text','BackgroundColor',background, 'String', 'Save',   'Position', [305 180 50 17]);
1661    uicontrol('Style','text','BackgroundColor',background, 'String', 'File',   'Position', [405 180 50 17]);
1662    calcname={'Avg' 'Ctr' 'Max' 'Min' 'Sum' 'Int'};
1663
1664    dx=100; dy=20;
1665    for ii=1:size(calcname,2)  %calculated number titles
1666      uicontrol('Style','text','BackgroundColor',background, 'String', [calcname{ii} ' / Fast'],...
1667                'Position',[30 180-dy*(ii) 50 17],'HorizontalAlignment','Left');
1668      uicontrol('Style','frame','BackgroundColor',background,'Position',[95 179-dy*(ii) 400 22]);
1669
1670      for jj=1:4  %make text fields for calculated numbers
1671        SYS.PSMHandles.(sources{jj}).(calcname{ii})= uicontrol('Style','text','BackgroundColor',background,...
1672            'Position', [100+dx*(jj-1) 180-dy*(ii) 30 17],'HorizontalAlignment','Left','String', '2Hz');
1673        SYS.PSMHandles.(sources{jj}).([calcname{ii} 'Fst'])= uicontrol('Style', 'text','BackgroundColor',background,...
1674            'Position', [135+dx*(jj-1) 180-dy*(ii) 30 17],'HorizontalAlignment','Left','String', 'Fast');
1675      end
1676    end
1677       SYS.PSMHandles.Units=uicontrol('Style','text','BackgroundColor',background,...
1678                                            'String', 'Units','Position', [50 5 80 17]);
1679       SYS.PSMHandles.Waveform=uicontrol('Style','text','BackgroundColor',background,...
1680                                            'String', 'Waveform Name','Position', [150 5 200 17]);
1681       SYS.PSMHandles.Time=uicontrol('Style','text','BackgroundColor',background,...
1682                                            'String', sprintf('%s', datestr(clock,31)),'Position', [350 5 150 17]);
1683       uicontrol('Style','Pushbutton','BackgroundColor',background,...
1684                 'Position', [220 30 60 17],'HorizontalAlignment','Left','String', 'Update','Callback',@psm_display_callback);
1685   
1686       set(SYS.PSMHandles.Figure,'HandleVisibility','off');
1687
1688end  %end make_psm_display
1689
1690%======================================
1691    function close_psm(src,evt)
1692%======================================
1693        %question whether user wants to close SPEAR3 PSM parameter display window
1694        answer=questdlg('Close parameter display window?','Close display window');
1695        if strcmpi(answer,'yes')
1696            delete(SYS.PSMHandles.Figure)
1697        end
1698    end
1699
1700%======================================
1701    function get_psm
1702%======================================
1703      iplot=SYS.iplot;
1704      iwfm=SYS.iwfm;
1705      if ~WAV(iplot,iwfm).iLoad
1706          errordlg('Select waveform first','SPEAR3 PSM Parameter display')
1707          return
1708      end
1709      calcname={'Avg' 'Ctr' 'Max' 'Min' 'Sum' 'Int'};
1710      ch=WAV(iplot,iwfm).ChannelName;
1711      %tic
1712      WAV(iplot,iwfm).Monitor.EngAvg =    getpv([ch WAV(iplot,iwfm).Monitor.EngDataUnits 'Avg']);       
1713      WAV(iplot,iwfm).Monitor.EngFstAvg = getpv([ch WAV(iplot,iwfm).Monitor.EngDataUnits 'FstAvg']);
1714      WAV(iplot,iwfm).Monitor.EngCtr =    getpv([ch WAV(iplot,iwfm).Monitor.EngDataUnits 'Centroid']);       
1715      WAV(iplot,iwfm).Monitor.EngFstCtr = getpv([ch WAV(iplot,iwfm).Monitor.EngDataUnits 'FstCentroid']);
1716      WAV(iplot,iwfm).Monitor.EngMax =    getpv([ch WAV(iplot,iwfm).Monitor.EngDataUnits 'Max']);       
1717      WAV(iplot,iwfm).Monitor.EngFstMax = getpv([ch WAV(iplot,iwfm).Monitor.EngDataUnits 'FstMax']);
1718      WAV(iplot,iwfm).Monitor.EngMin =    getpv([ch WAV(iplot,iwfm).Monitor.EngDataUnits 'Min']);       
1719      WAV(iplot,iwfm).Monitor.EngFstMin = getpv([ch WAV(iplot,iwfm).Monitor.EngDataUnits 'FstMin']);
1720      WAV(iplot,iwfm).Monitor.EngSum =    getpv([ch WAV(iplot,iwfm).Monitor.EngDataUnits 'Sum']);       
1721      WAV(iplot,iwfm).Monitor.EngFstSum = getpv([ch WAV(iplot,iwfm).Monitor.EngDataUnits 'FstSum']);
1722     %WAV(iplot,iwfm).Monitor.EngInt =    getpv([ch WAV(iplot,iwfm).EngDataUnits 'Avg'];       
1723     %WAV(iplot,iwfm).Monitor.EngFstInt='';  %<Charge or Ener>  (Int value
1724     %- "Charge" for Curr, "Ener" for Pwr, "Intgrl" for Load)      WAV(iplot,iwfm).Monitor.EngAvg =    getpv([ch WAV(iplot,iwfm).Monitor.EngDataUnits 'Avg']);       
1725% % %       WAV(iplot,iwfm).Monitor.RawFstAvg = getpv([ch WAV(iplot,iwfm).Monitor.RawDataUnits 'FstAvg']);
1726% % %       WAV(iplot,iwfm).Monitor.RawCtr =    getpv([ch WAV(iplot,iwfm).Monitor.RawDataUnits 'Centroid']);       
1727% % %       WAV(iplot,iwfm).Monitor.RawFstCtr = getpv([ch WAV(iplot,iwfm).Monitor.RawDataUnits 'FstCentroid']);
1728% % %       WAV(iplot,iwfm).Monitor.RawMax =    getpv([ch WAV(iplot,iwfm).Monitor.RawDataUnits 'Max']);       
1729% % %       WAV(iplot,iwfm).Monitor.RawFstMax = getpv([ch WAV(iplot,iwfm).Monitor.RawDataUnits 'FstMax']);
1730% % %       WAV(iplot,iwfm).Monitor.RawMin =    getpv([ch WAV(iplot,iwfm).Monitor.RawDataUnits 'Min']);       
1731% % %       WAV(iplot,iwfm).Monitor.RawFstMin = getpv([ch WAV(iplot,iwfm).Monitor.RawDataUnits 'FstMin']);
1732% % %       WAV(iplot,iwfm).Monitor.RawSum =    getpv([ch WAV(iplot,iwfm).Monitor.RawDataUnits 'Sum']);       
1733% % %       WAV(iplot,iwfm).Monitor.RawFstSum = getpv([ch WAV(iplot,iwfm).Monitor.RawDataUnits 'FstSum']);
1734% % %      %WAV(iplot,iwfm).Monitor.RawInt =    getpv([ch WAV(iplot,iwfm).RawDataUnits 'Avg'];       
1735% % %      %WAV(iplot,iwfm).Monitor.RawFstInt='';  %<Charge or Ener>  (Int value
1736% % %      %- "Charge" for Curr, "Ener" for Pwr, "Intgrl" for Load)
1737      %load the raw parameters here
1738      %toc
1739
1740    end
1741
1742%======================================       
1743function psm_display_callback(src,evt)     %psm_display_callback
1744%======================================       
1745  get_psm;
1746  psm_display;
1747end
1748
1749%======================================       
1750function psm_display(src,evt)
1751%======================================       
1752      %acquire SPEAR3 data and update display
1753          if ~ishandle(SYS.PSMHandles.Figure)  %if window does not exist make one
1754              make_psm_display;
1755          end
1756
1757      iplot=SYS.iplot;
1758      iwfm=SYS.iwfm;
1759      if ~WAV(iplot,iwfm).iLoad
1760           errordlg('Select waveform first','SPEAR3 PSM Parameter display')
1761           return
1762      end
1763
1764      calcname={'Avg' 'Ctr' 'Max' 'Min' 'Sum'} ; % 'Int'};
1765
1766      for ii=1:4 %loop over sources (Monitor, Golden, Save, File)
1767          %disp(sources{ii})
1768        for jj=1:size(calcname,2)  %loop over calculated values for each source
1769            str=num2str(WAV(iplot,iwfm).(sources{ii}).(['Eng' calcname{jj}]), '%8.2f');
1770            %disp(str)
1771         set(SYS.PSMHandles.(sources{ii}).(calcname{jj}),...       
1772         'String',str);
1773         set(SYS.PSMHandles.(sources{ii}).([calcname{jj} 'Fst']),...
1774         'String',num2str(WAV(iplot,iwfm).(sources{ii}).(['Eng' 'Fst' calcname{jj} ]), '%8.2f'));
1775        end
1776      end
1777        if    ~SYS.Units
1778            Units=WAV(iplot,iwfm).Monitor.RawDataUnits;
1779            set(SYS.PSMHandles.Units,'String','Raw Units','Foregroundcolor',  WAV(iplot,iwfm).Color);
1780        elseif SYS.Units
1781            Units=WAV(iplot,iwfm).Monitor.EngDataUnits;
1782            set(SYS.PSMHandles.Units,'String','Eng Units','Foregroundcolor',  WAV(iplot,iwfm).Color);
1783        end
1784        str=['    Plot ' num2str(SYS.iplot) ': ' WAV(iplot,iwfm).CommonName '  [' Units ']'  ];
1785        set(SYS.PSMHandles.Waveform,'String',str,'Foregroundcolor',  WAV(iplot,iwfm).Color);
1786        set(SYS.PSMHandles.Time,'String',sprintf('%s', datestr(clock,31)));
1787end  %end psm_display
1788
1789%======================================
1790    function clear_psm_display
1791%======================================
1792      calcname={'Avg' 'Ctr' 'Max' 'Min' 'Sum' 'Int'};
1793      for ii=1:4 %loop over sources (Monitor, Golden, Save, File)
1794        for jj=1:size(calcname,2)  %loop over calculated values for each source
1795         set(SYS.PSMHandles.(sources{ii}).(calcname{jj}),        'String',' ');
1796         set(SYS.PSMHandles.(sources{ii}).([calcname{jj} 'Fst']),'String',' ');
1797        end
1798      end
1799      set(SYS.PSMHandles.Units,'String','Units','Foregroundcolor','k');
1800      set(SYS.PSMHandles.Waveform,'String','Waveform','Foregroundcolor','k');
1801      set(SYS.PSMHandles.Time,'String',sprintf('%s', datestr(clock,31)));
1802    end
1803
1804
1805end  %program
Note: See TracBrowser for help on using the repository browser.