%
%  Name:  RPwindow.m
%
%  Usage:  RPwindow.m
%
%  This m-file sets up the window to do
%  a rational polynomial fit to the FRF's 
%

%  Version SWD970619
%
%  Author(s): Phillip Cornwell, cornwell@lanl.gov
%	modified by Scott W. Doebling, doebling@lanl.gov
%
%  Institution: Los Alamos National Laboratory,
%               ESA-EA
%
%  Copyright (c) 1996-1997, The Regents of the University
%       of California. (See top level of code for
%       full copyright statement.)

clear Freqnew Dampnew phic_resp_new  phic_ref_new 

%
% Activate the appropriate handles
%

onHndlVector=handact(onHndlVector,hndlMatrix,3);


%
%  define initial parameters
%

   iresp=1;
   iref=1;

    nds = 1;

	nn=1;
	mm=1;

   fmin=cf-sp/2;
   fmax=cf+sp/2;
   fminm=fmin;
   fmaxm=fmax;
   f1=(fmaxm-fminm)*.25;
   f2=(fmaxm-fminm)*.75;
   flow = f1;
   fhigh = f2;
   df=sp/(nlines-1);
   numbins=(fmax-fmin)/df;
   i1=numbins*.25;
   i2=numbins*.75;
   freq=fminm:df:fmaxm;

   numbins=(fmaxm-fminm)/df;
   rpopt=1;
  
%
%  Define the axes for plottools
%

   haxes = onHndlVector(25);
   hlegax = [];
   
%
%  Make the Initial Plot
%

	set(hplttl(16),'value',1)
	eval(get(hplttl(16),'call'))

	lnst = ['- ';'- ';'- '];
      lnco = ['y';'r';'b'];
	set(hplttl(11),'string','Primary Data|Low Cursor|High Cursor')


%
%  Set the variables for Plottools
%

	flim = get(haxes(1),'xlim');
	ylim = get(haxes(1),'ylim');


	set(hplttl(3),'string',num2str(flim(1)))
	set(hplttl(5),'string',num2str(flim(2)))
	set(hplttl(7),'string',num2str(ylim(1)))
	set(hplttl(9),'string',num2str(ylim(2)))

	yminm = ylim(1);
     ymaxm = ylim(2);


defhline

%====================================
% Set up the cursors
%====================================

onHndlVector(26:27) = plotline(onHndlVector(26:27),...
			onHndlVector(25),f1,ymaxm,yminm,lnst(2,:),lnco(2,:));
onHndlVector(28:29) = plotline(onHndlVector(28:29),...
			onHndlVector(25),f2,ymaxm,yminm,lnst(3,:),lnco(3,:));


hline = [hline;onHndlVector(26);onHndlVector(28)];

%
%  Set up the number of bins selector
%

   nfitbin = floor(f2/df-f1/df+1);
   set(onHndlVector(34),'string',num2str(nfitbin))



%
%  Set various ui parameters correctly
%

%  Low Frequency Slider parameters

set(onHndlVector(6),'max',fmax);
set(onHndlVector(6),'min',fmin);
set(onHndlVector(6),'value',f1);
set(onHndlVector(6),'userdata',freq);
set(onHndlVector(6),'sliderstep',[df/(fmax - fmin) 0.1]);

%  Low Frequency text box parameters

set(onHndlVector(8),'string',f1);

%  High Frequency slider parameters

set(onHndlVector(9),'max',fmax);
set(onHndlVector(9),'min',fmin);
set(onHndlVector(9),'value',f2);
set(onHndlVector(9),'userdata',freq);
set(onHndlVector(9),'sliderstep',[df/(fmax - fmin) 0.1]);

%  High Frequency text box parameters

set(onHndlVector(11),'string',f2);


%  Set number of modes and extra terms

set(onHndlVector(15),'string','1');
set(onHndlVector(17),'string','4');

%
% the following was necessary for it to draw the sliders correctly
%

set(onHndlVector(6),'pos',[.13 .01 .2 0.04]);
set(onHndlVector(9),'pos',[.55 .01 .2 0.04]);


%  Define the bins to be fit

	rpbinfit



%
% Set initial parameters
%

if exist('Freq')~=1,
	Freq = [];
	Damp = [];
end

icountnew=[];Freqnew=[];Dampnew=[];

%
% buttons for the results window
%
figRPresults=figure( ...
    'Name','Rational Polynomial Results', ...
    'NumberTitle','off', ...
    'visible','off', ...
    'Resize','off', ...
    'Position',[180 88 380 420],...
    'Menubar','none');

%
%  Set up console
%
  
   uicontrol(figRPresults, ...
	'style','text', ...
	'units','normal', ...
	'visible','on', ...
	'string','Old Results', ...
	'BackgroundColor',[.5 .5 .5], ...
	'position',[0 .95 .68 .05]);

%
% set up the scroll windows
%
ifull = length(Freq);
icountfull = [1:ifull]';

textfull=RPtext(icountfull,Freq,Damp);
%hedit1=scrllwin('new','string',textfull,'position',[0 .5 .68 .45]);

hedit1 = uicontrol('style','listbox','position',[0 .5 .68 .45],'string',textfull);

   uicontrol(figRPresults, ...
	'style','text', ...
	'units','normal', ...
	'visible','on', ...
	'string','New Results', ...
	'BackgroundColor',[.5 .5 .5], ...
	'position',[0 .45 .68 .05]);

%hedit2=scrllwin('new','string',' ','position',[0 0 .68 .45]);

hedit2 = uicontrol('style','listbox','position',[0 0 .68 .45],'string',blanks(1));

%
% Add buttons
%
top=.95;
left=.7;
btnwd=.28;
btnht=.075;
spacing=.02;

%
% Add the new frequencies to the old
% 
btnnum=6;
ypos=top-(btnnum-1)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd btnht];
   uicontrol(figRPresults, ...
	'style','push', ...
	'units','normal', ...
	'visible','on', ...
	'string','Save New and Close', ...
	'position',btnpos, ...
	'callback', ['Freq=[Freq;Freqnew];', ...
		       'Damp=[Damp;Dampnew];', ...
			 'phic_resp = [phic_resp phic_resp_new];', ...
			 'phic_ref  = [phic_ref  phic_ref_new];', ...
			 '[Freq,ii]=sort(Freq);', ...
		    	 'Damp = Damp(ii);',...
		       'phic_resp = phic_resp(:,ii);', ...
		       'phic_ref  =phic_ref(:,ii);', ...
			 'icountnew=[]; Freqnew=[]; Dampnew=[];',...
			 'RPdisp,',...
			 'set(hmmenu(6),''enable'',''on''),',...
			'set(figRPresults,''visible'',''off'');', ...
		    'set(hsynthfig,''visible'',''off'');']);

%
% Delete one of the new frequencies
%
btnnum=1;
ypos=top-(btnnum-1)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd*2/3 btnht];
  uicontrol(figRPresults, ...
	'style','push', ...
	'units','normal', ...
	'visible','on', ...
	'string','Del. New #', ...
	'position',btnpos, ...
	'callback',['numdel=str2num(get(hRPdel,''string''));', ...
	'if numdel <= inew;', ...
	    'Freqnew(numdel)=[];', ...
	    'Dampnew(numdel)=[];', ...
	    'phic_resp_new(:,numdel)=[];', ...
	    'phic_ref_new(:,numdel)=[];', ...
	    'RPdisp,',...
	'else,', ...
	    'disp(''MODE NUMBER OUT OF RANGE''),',...
	'end;']);

btnpos=[left+btnwd*2/3 ypos-btnht btnwd/3 btnht];
hRPdel=uicontrol(figRPresults, ...
	'style','edit', ...
	'visible','on', ...
	'units','normal', ...
	'back','w', ...
	'position',btnpos);

%
%  Close (no save)
%
btnnum=7;
ypos=top-(btnnum-1)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd btnht];
   uicontrol(figRPresults, ...
	'style','push', ...
	'units','normal', ...
	'visible','on', ...
	'string','Close (No Save)', ...
	'position',btnpos, ...
	'callback',['set(figRPresults,''visible'',''off'');', ...
		    'set(hsynthfig,''visible'',''off'');']);

%
% Synthesis button
%
btnnum=2;
ypos=top-(btnnum-1)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd btnht];
   uicontrol(figRPresults, ...
	'style','push', ...
	'units','normal', ...
	'visible','on', ...
	'string','Synth. New Modes', ...
	'position',btnpos, ...
	'callback', 'RPsynth');

%
% mode shapes button
%
btnnum=4;
ypos=top-(btnnum-1)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd btnht];
   uicontrol(figRPresults, ...
	'style','push', ...
	'units','normal', ...
	'visible','on', ...
	'string','New Mode Shapes', ...
	'position',btnpos, ...
	'callback', ['Freqplot = Freqnew;',...
			'iopDOF=get(hiopbtn,''value'');',...
			'if iopDOF == 1, Phiplot = phic_resp_new; DOFvec = respDOF;',...
			'elseif iopDOF==2, Phiplot=phic_ref_new; DOFvec = refDOF;',...
			'end,',...
			'ipltcmplx = 2; viewmode,']);


%
%  Input/Output mode shape selector
%

btnnum=3;
ypos=top-(btnnum-1)*(btnht+spacing);
btnpos=[left ypos-btnht btnwd btnht];
hiopbtn=uicontrol(figRPresults, ...
      'style','popup', ...
      'unit','normalized', ...
      'position',btnpos, ...
      'Value',1,...
      'string','Resp DOF|Ref DOF');


%
%  Set up the synthesis window
%
hsynthfig=figure( ...
    'Name','Synthesis', ...
    'NumberTitle','off', ...
    'visible','off', ...
    'Resize','on', ...
    'Position',[580 88 440 420],...
    'menubar','none');

hsynthzoom=uicontrol(hsynthfig, ...
    'style','push',...
    'units','normal', ...
    'visible','on', ...
    'string','zoom on', ...
    'position',[.85 .94 .14 .05], ...
    'callback',[ ...
	'zoomit=get(hsynthzoom,''string'');', ...
	'if strcmp(zoomit,''zoom on''),', ...
	   'axes(haxis1);', ...
	   'maxfreq=max(wifit)/2/pi+1;', ...
	   'minfreq=min(wifit)/2/pi-1;', ...
	   'maxmag=max(abs(datai));', ...
	   'minmag=min(abs(datai));', ...
	   'axis([minfreq maxfreq minmag maxmag]);', ...
	   'axes(haxis2);', ...
	   'minang=min(angle(datai))/pi*180;', ...
	   'maxang=max(angle(datai))/pi*180;', ...
	   'axis([minfreq maxfreq minang maxang]);', ...
	   'set(hsynthzoom,''string'',''zoom off'');', ...
	'else,', ....
	   'axes(haxis1);', ...
	   'axis([fmin fmax minmag maxmag]);', ...
	   'axes(haxis2);', ...
	   'axis([fmin fmax minang maxang]);', ...
	   'set(hsynthzoom,''string'',''zoom on'');', ...
	'end;']);
	



