%   Name: plottool
%
%   Usage: plottool
%
%   Description: Creates the palette of plot attribute tools 
%     
%   The following variables must be defined in the workspace
%	prior to using the plot tools:
%
%       haxes - handle of the current plot axes
%       hlegax - handle of the current plot legend axes
%       hline - handle of the plot lines
%		(first row - primary line)
%		(second row - secondary line)
%
%  Version SWD961003
%
%  Author(s): Scott W. Doebling, doebling@lanl.gov
%             Erik G. Straser, estraser@lanl.gov
%
%  Institution: Los Alamos National Laboratory,
%		ESA-EA
%
%  Copyright (c) 1996, The Regents of the University
%     	of California. (See top level of code for
%		full copyright statement.)

hplttl(1) = figure('units','pixels','position',[30 30 300 200],...
			'menubar','none','numbertitle','off','resize','off',...
			'name','Diamond: Plot Tools','visible','off');

top=.95;
left=.05;
btnwd=.25;
btnht=.1;
spacing=.01;


%  =================
%  Axis Limits Boxes
%  =================

btnnum=0;
ypos=top-(btnnum)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd/2 btnht];

%
% fmin button
%
hplttl(2)=uicontrol(hplttl(1), ...
	'style','text', ...
	'units','normal', ...
	'pos',btnpos, ...
    	'string','fmin');
btnpos = btnpos + [btnwd/2 0 btnwd/2 0];
hplttl(3)=uicontrol(hplttl(1), ...
     	'style','edit', ...
	'units','normal', ...
	'string','fmin', ...
	'pos',btnpos, ...
        'back','w', ...
	'call',['fminm_t=str2num(get(hplttl(3),''string''));', ...
		' if isempty(fminm_t), disp(''Invalid Min. Freq.''),',...
  		'    set(hplttl(3),''string'',num2str(fminm)),',...
		' elseif fminm_t >= fmaxm, disp(''Invalid Min. Freq.''),',...
		'    set(hplttl(3),''string'',num2str(fminm)),',...
		' elseif fminm_t < 0, disp(''Invalid Min. Freq.''),',...
  		'    set(hplttl(3),''string'',num2str(fminm)),',...
		' else,',...
		'    fminm = fminm_t;',...
		' end,',...
		'set(haxes,''Xlim'',[fminm fmaxm]),',...
      'scaley,']);


%
% fmax button
%
btnnum=1;
ypos=top-(btnnum)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd/2 btnht];

hplttl(4)=uicontrol(hplttl(1), ...
	'style','text', ...
	'units','normal', ...
	'pos',btnpos, ...
    	'string','fmax');
btnpos = btnpos + [btnwd/2 0 btnwd/2 0];
hplttl(5)=uicontrol(hplttl(1), ...
     	'style','edit', ...
	'units','normal', ...
	'string','fmax',...
	'pos',btnpos, ...
        'back','w', ...
	'call',['fmaxm_t=str2num(get(hplttl(5),''string''));', ...
		' if isempty(fmaxm_t), disp(''Invalid Max. Freq.''),',...
  		'    set(hplttl(5),''string'',num2str(fmaxm)),',...
		' elseif fmaxm_t <= fminm, disp(''Invalid Max. Freq.''),',...
		'    set(hplttl(5),''string'',num2str(fmaxm)),',...
		' elseif fmaxm_t > fmax, disp(''Invalid Max. Freq.''),',...
  		'    set(hplttl(5),''string'',num2str(fmaxm)),',...
		' else,',...
		'    fmaxm = fmaxm_t;',...
		' end,',...
		'set(haxes,''Xlim'',[fminm fmaxm]),',...
		'scaley']);

%
% ymin button
%
btnnum=2;
ypos=top-(btnnum)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd/2 btnht];
hplttl(6)=uicontrol(hplttl(1), ...
	'style','text', ...
	'units','normal', ...
	'pos',btnpos, ...
    	'string','ymin');
btnpos = btnpos + [btnwd/2 0 btnwd/2 0];
hplttl(7)=uicontrol(hplttl(1), ...
     	'style','edit', ...
	'units','normal', ...
	'string','ymin', ...
	'pos',btnpos, ...
        'back','w', ...
	'call',['yminm_t=str2num(get(hplttl(7),''string''));', ...
		' if isempty(yminm_t), disp(''Invalid Lower Limit''),',...
  		'    set(hplttl(7),''string'',num2str(yminm)),',...
		' elseif yminm_t >= ymaxm, disp(''Invalid Lower Limit''),',...
		'    set(hplttl(7),''string'',num2str(yminm)),',...
		' else,',...
		'    yminm = yminm_t;',...
		' end,',...
		'set(haxes,''Ylim'',[yminm ymaxm])']);

%
% ymax button
%
btnnum=3;
ypos=top-(btnnum)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd/2 btnht];
hplttl(8)=uicontrol(hplttl(1), ...
	'style','text', ...
	'units','normal', ...
	'pos',btnpos, ...
    	'string','ymax');
btnpos = btnpos + [btnwd/2 0 btnwd/2 0];
hplttl(9)=uicontrol(hplttl(1), ...
     	'style','edit', ...
	'units','normal', ...
	'string','ymax', ...
	'pos',btnpos, ...
        'back','w', ...
	'call',['ymaxm_t=str2num(get(hplttl(9),''string''));', ...
		' if isempty(ymaxm_t), disp(''Invalid Upper Limit''),',...
  		'    set(hplttl(9),''string'',num2str(ymaxm)),',...
		' elseif ymaxm_t <= yminm, disp(''Invalid Upper Limit''),',...
		'    set(hplttl(9),''string'',num2str(ymaxm)),',...
		' else,',...
		'    ymaxm = ymaxm_t;',...
		' end,',...
		'set(haxes,''Ylim'',[yminm ymaxm])']);

%
%  Close Button
%
btnpos = [1-spacing-btnwd 0.01 btnwd btnht];
hplttl(10) = uicontrol(hplttl(1),...
	'style','push',...
	'units','normal',...
	'position',btnpos,...
	'string','Close',...
	'call','set(hplttl(1),''visible'',''off'')');


%
%  Line Style Editor Label and frame
left = 0.06;
btnnum=4;
btnpos = [0.05 0.06 btnwd*1.5+0.02 0.45];
uicontrol(hplttl(1),...
	'style','frame',...
        'back',[0.5 0.5 0.5], ...
	'position',btnpos);
ypos=top-(btnnum)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd*1.5 btnht];
uicontrol(hplttl(1),...
	'style','Text',...
	'units','normal',...
        'back',[0.5 0.5 0.5], ...
	'position',btnpos,...
	'string','Line Style Editor');

%
%  Line Selector popup
%
btnnum=5;
ypos=top-(btnnum)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd*1.5 btnht];
hplttl(11) = uicontrol(hplttl(1),...
	'style','popup',...
	'units','normal',...
	'position',btnpos,...
	'string','Primary Line | Secondary Line',...
	'value',1,...
	'callback',['iptline = get(hplttl(11),''value'');',...
			'lstemp = lnst(iptline,:);',...
			'if strcmp(lstemp,''- ''),',...
			'  iptlnst = 1;',...
			'elseif strcmp(lstemp,''--''),',...
			'  iptlnst = 2;',...
			'elseif strcmp(lstemp,''o ''),',...
			'  iptlnst = 3;',...
			'elseif strcmp(lstemp,''. ''),',...
			'  iptlnst = 4;',...
			'end,',...
			'set(hplttl(12),''value'',iptlnst),',...
			'lctemp = lnco(iptline);',...
			'if strcmp(lctemp,''y''),',...
			'  iptlnco = 1;',...
			'elseif strcmp(lctemp,''b''),',...
			'  iptlnco = 2;',...
			'elseif strcmp(lctemp,''r''),',...
			'  iptlnco = 3;',...
			'elseif strcmp(lctemp,''g''),',...
			'  iptlnco = 4;',...
			'end,',...
			'set(hplttl(13),''value'',iptlnco),']);

%
%  Line Style Popup
%
btnnum=6;
ypos=top-(btnnum)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd*1.5 btnht];
hplttl(12) = uicontrol(hplttl(1),...
	'style','popup',...
	'units','normal',...
	'position',btnpos,...
	'string','Solid|Dashed|Circles|Dots',...
	'value',1,...
	'callback',['  iptlnst = get(hplttl(12),''value'');',...
			' if iptlnst == 1, lnst(iptline,:) = ''- '';',...
			' elseif iptlnst == 2, lnst(iptline,:) = ''--'';',...
			' elseif iptlnst == 3, lnst(iptline,:) = ''o '';',...
			' elseif iptlnst == 4, lnst(iptline,:) = ''. ''; end,',...
			' set(hline(iptline,:),''linestyle'',lnst(iptline,:));']);

%
%  Line Color Popup
%
btnnum=7;
ypos=top-(btnnum)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd*1.5 btnht];
hplttl(13) = uicontrol(hplttl(1),...
	'style','popup',...
	'units','normal',...
	'position',btnpos,...
	'string','Yellow | Blue | Red | Green',...
	'value',1,...
	'callback',['iptlnco= get(hplttl(13),''value'');',...
			'if iptlnco == 1, lnco(iptline) = ''y'';',...
			' elseif iptlnco == 2, lnco(iptline) = ''b'';',... 
			' elseif iptlnco == 3, lnco(iptline) = ''r'';',...
			' elseif iptlnco == 4, lnco(iptline) = ''g'';',...
			'end,',...
			'set(hline(iptline,:),''color'',lnco(iptline));']);


%
%  y-axis plot categories
%
btnnum=0;
left = 0.65;
ypos=top-(btnnum)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd*1.2 btnht];
hplttl(14) = uicontrol(hplttl(1),...
	'style','popup',...
	'units','normal',...
	'position',btnpos,...
	'string','Mag |Phase |Real |Imag',...
	'value',1,...
	'callback',[' iyscale = get(hplttl(14),''value'');',...
			' iylog = get(hplttl(15),''value'');',...
			'if iyscale == 1, name=''Magnitude''; plotopt=''abs'';',...
			' elseif iyscale == 2, name=''Phase''; plotopt=''angle'';',...
			' elseif iyscale == 3, name=''Real''; plotopt=''real'';',...
			' elseif iyscale == 4, name=''Imaginary''; plotopt=''imag'';',...
			'if iyscale==1, iylog=1; else, iylog=0; end,',...
			'set(hplttl(15),''value'',iylog),',...
			'end,',...
			'assignG,',...
  			'datai=G((iresp-1)*nlines+1:nlines*iresp,iref);', ...
			'eval([''toplot='',plotopt,''(datai);'']);',...
                        'name = ''SISO'';', ...
			'modplot(haxes,freq,fminm,fmaxm,toplot,nn,iylog,nds,name,lnst,lnco,nfunc);', ...
                  'if ishandle(hlegax), dataleg(hlegax,nds,iresp,iref,filename,lnst,lnco); end,',...
			'defhline,',...
			'scaley']);

%
%  Y axis plot scaling
%
btnnum=1;
ypos=top-(btnnum)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd*1.2 btnht];
hplttl(15) = uicontrol(hplttl(1),...
	'style','check',...
	'units','normal',...
	'position',btnpos,...
	'string','Log Scale',...
	'value',1,...
	'callback',[' iylog = get(hplttl(15),''value'');',...
			' if iylog == 1, set(haxes,''yscale'',''log'');',...
			'    else, set(haxes,''yscale'',''lin'');',...
			'end,']);

%
% CMIF/SISO/MIF/APS button
%
btnnum=2;
ypos=top-(btnnum)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd*1.2 btnht];
hplttl(16)=uicontrol(hplttl(1), ...
      'style','popup', ...
      'unit','normalized', ...
      'position',btnpos, ...
      'string','CMIF|SISO|MIF|APS', ...
       'callback',[' which_plot = get(hplttl(16),''value'');',...
			'if which_plot == 1', ...
			'  set(hplttl([21:23]), ''visible'', ''off'');', ...
			'  set(hplttl([14 17:20]), ''visible'', ''off'');', ...
                        '  showall(hplttl(15));', ...
			'  assignG;',...
			'  toplot=cmif(G,nlines);', ...
			'  name = ''CMIF'';',...
			'  iyscale = 1; iylog=1;',...
			'  set(hplttl(14),''value'',iyscale),',...
			'  set(hplttl(15),''value'',iylog),',...
			'  modplot(haxes,freq,fminm,fmaxm,toplot,nn,iylog,nds,name,lnst,lnco,nfunc);', ...
                    '  if ishandle(hlegax), dataleg(hlegax,nds,-1,-1,filename,lnst,lnco); end,',...
			'  defhline,',...
			'  eval(get(hplttl(15),''call'')),',...
			'elseif which_plot == 2',...
			'  hideall(hplttl([21:23]));', ...
			'  showall(hplttl([14 15 17:20])),',...
			'  eval(get(hplttl(14),''call'')),',...
			'elseif which_plot == 3', ...
                        '  hideall(hplttl([14 15 17:20 23])),',...
			'  showall(hplttl([21:22]));', ...
			'  [MIF,APS,freq,delta_f] = genindi(nlines,size(respDOF,1),G,cf,sp,size(refDOF,1));', ...
			'  toplot = MIF;', ...
 			'  name = ''Mode Indicator Function'';', ...
                        '  mm = 0;', ...
			'  modplot(haxes,freq,fminm,fmaxm,toplot,nn,mm,nds,name,lnst,lnco,nfunc);', ...
			'  if ishandle(hlegax), dataleg(hlegax,nds,-1,-1,filename,lnst,lnco); end,',...
			'  defhline,',...
			'elseif which_plot == 4', ...
			'  hideall(hplttl([14 15 17:20 21 22 23])),',...
			'  [MIF,APS,freq,delta_f] = genindi(nlines,size(respDOF,1),G,cf,sp,size(refDOF,1));', ...
			'  toplot = APS;', ...
 			'  name = ''Average Power Spectra'';', ...
                        '  mm = 0;', ...
			'  modplot(haxes,freq,fminm,fmaxm,toplot,nn,mm,nds,name,lnst,lnco,nfunc);', ...
			'  if ishandle(hlegax), dataleg(hlegax,nds,-1,-1,filename,lnst,lnco); end,',...
			'  defhline,',...
			'end,',...
			'scaley']);

%
%  To choose which Response DOF to plot
%
btnnum=3;
ypos=top-(btnnum)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd*0.6 btnht];
hplttl(17)= uicontrol(hplttl(1), ...
      'style','text', ...
      'unit','normalized', ...
      'position',btnpos, ...
      'string','Resp');
btnpos=[left+btnwd*0.6 ypos-btnht btnwd*0.6 btnht];
hplttl(18)=uicontrol(hplttl(1), ...
      'style','edit', ...
      'unit','normalized', ...
      'position',btnpos, ...
      'string','1', ...
      'back','white', ...
       'callback',['iresp=str2num(get(hplttl(18),''string''));',...
			' if isempty(iresp), disp(''Resp # out of Range''),',...
  			'    iresp = 1; set(hplttl(18),''string'',num2str(iresp)),',...
			'elseif iresp > size(respDOF,1), disp('' Resp # out of Range''),',...
			'    iresp = size(respDOF,1); set(hplttl(18),''string'',num2str(iresp)),',...
			'elseif iresp < 1, disp(''Resp # out of Range''),',...
  			'    iresp = 1; set(hplttl(18),''string'',num2str(iresp)),',...
			'end,',...
			'if nds==2, nds =1; set(onHndlVector(12),''value'',nds),',...
  				'eval(get(onHndlVector(12),''call'')),',...
			'else, eval(get(hplttl(14),''call'')),end,']);


%
%  To choose which Reference DOF to plot
%
btnnum=4;
ypos=top-(btnnum)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd*0.6 btnht];
hplttl(19)=uicontrol(hplttl(1), ...
      'style','text', ...
      'unit','normalized', ...
      'position',btnpos, ...
      'string','Ref');
btnpos=[left+btnwd*0.6 ypos-btnht btnwd*0.6 btnht];
hplttl(20)=uicontrol(hplttl(1), ...
      'style','edit', ...
      'unit','normalized', ...
      'position',btnpos, ...
      'string','1', ...
      'back','white', ...
       'callback',['iref=str2num(get(hplttl(20),''string''));',...
			' if isempty(iref), , disp(''Ref # out of Range''),',...
  			'    iref = 1; set(hplttl(20),''string'',num2str(iref)),',...
			' elseif iref > size(refDOF,1), disp(''Ref # out of Range''),',...
			'    iref = size(refDOF,1); set(hplttl(20),''string'',num2str(iref)),',...
			' elseif iref < 1, disp(''Ref # out of Range''),',...
  			'    iref = 1; set(hplttl(20),''string'',num2str(iref)),',...
			'end,',...
			'if nds==2, nds = 1; set(onHndlVector(12),''value'',nds),',...
			'eval(get(onHndlVector(12),''call'')),',...
			'else, eval(get(hplttl(14),''call'')),end,']);

%%====================================
%  Min MIF button
%%====================================
btnnum=3;
ypos=top-(btnnum)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd*0.8 btnht];
hplttl(21)=uicontrol(hplttl(1), ...
      'style','text', ...
      'unit','normalized', ...
      'position',btnpos, ...
      'visible','off', ...      
      'string','Min MIF');
btnpos=[left+btnwd*0.8 ypos-btnht btnwd*0.4 btnht];
hplttl(22)=uicontrol(hplttl(1), ...
      'style','edit', ...
      'unit','normalized', ...
      'position',btnpos, ...
      'visible','off', ...      
      'string','?', ...
      'callback',[
      'showall(hplttl(23));']);

%%====================================
% Print Peaks to Command Window Button
%%====================================
btnnum=4;
ypos=top-(btnnum)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd*1.2 btnht];
hplttl(23)=uicontrol(hplttl(1), ...
 	'style','push', ...
	'unit','normalized', ...
	'position',btnpos, ...
	'Visible','off', ...      
	'string','Print Peaks', ...
	'callback',[ 
	       'min_MIF   = str2num(get(hplttl(22),''string''));', ...
               'low_freq  = str2num(get(hplttl(3),''string''));,',...
               'high_freq = str2num(get(hplttl(5),''string''));,',...
      	       'likelyv = likely(MIF,APS,min_MIF,nlines,delta_f,low_freq,high_freq);', ...
               'fprintf(1,''\nThe likely Natural Frequencies in Hertz for this system are:\n'');', ...
	       'fprintf(1,''Natural Frequency\t Normalized Power\n'');', ...
               'for i = 1:size(likelyv,1);fprintf(1,''    %7.3f \t\t    %7.3f \n'', likelyv(i,1), likelyv(i,2)/max(likelyv(:,2)));end;', ...
               'fprintf(1,''\n'');',]);
