function gtws=idparest(arg,onoff,enforce)
%IDPAREST  The basic callback function for parametric estimation.
%   Arguments:
%   open   Creates the dialog box for parametric estimation
%   mstype Creates the correct dialog box for model structure editor
%   mspop  Sets the correct pop-up menu for the various  supported
%              model structures. (This depends on the inputs and outputs
%          in the estimation data.)

%   L. Ljung 4-4-94
%   Copyright 1986-2000 The MathWorks, Inc.
%   $Revision: 1.9 $ $Date: 2000/08/24 12:53:23 $

set(0,'Showhiddenhandles','on');
Xsum = findobj(get(0,'children'),'flat','tag','sitb16');
XID = get(Xsum,'Userdata');
if nargin<2,onoff='on';end

gtws=[];
if strcmp(arg,'close')
   set(XID.parest(1),'Visible','off')
   try
      set(iduiwok(21),'vis','off');
   end
   try
      set(iduiwok(22),'vis','off');
   end
   set(0,'Showhiddenhandles','off');
   set(Xsum,'Userdata',XID);
   return
end
usd=get(XID.hw(3,1),'userdata');
nu=usd.nu;ny=usd.ny;

if strcmp(arg,'open')
   if nargin<2,onoff='on';end
   FigName=idlaytab('figname',20);
   if ~figflag(FigName,0)
      iduistat('Opening the Parametric Models dialog ...')
      if ~exist('XID.mse'), XID.mse=[0 0 0 0];end
      
      layout
      butw = mStdButtonWidth;
      FigW = iduilay2(3);
      f = figure('NumberTitle','off','Name',FigName,...
         'userdata',[nu ny],'tag','sitb20','Integerhandle','off',...
         'Color',get(0,'DefaultUIControlBackgroundColor'),'vis','off');
      XID.parest(1) = f;
      set(f,'Menubar','none','HandleVisibility','callback');
      s1='iduipoin(1);';s2='iduipoin(1);iduistat(''Compiling ...'');';
      s3='iduipoin(2);';
      % ******************
      % LEVEL 1
      pos = iduilay1(FigW,3); 
      uicontrol(f,'pos',pos(1,:),'style','frame');
      cb=[s2,'goto_ws=iduiarx(''estimate'');',...
            'if goto_ws,XIDarg=''arx'';idgtws;end,',s3];
      XID.parest(2)=uicontrol(f,'pos',pos(2,:),'string',...
         'Estimate','style','push','callback',cb);
      set(XID.parest(2),'UserData',str2mat('[]','[]','[]','[]'))
      uicontrol(f,'pos',pos(3,:),'string','Close',...
         'style','push','callback',[s1,'idparest(''close'');',s3]);
      uicontrol(f,'pos',pos(4,:),'string','Help','style',...
         'push','callback',...
         'iduihelp(''idparest.hlp'',''Help: Estimating Models'');');
      
      
      %  LEVEL 2
      lev2 = pos(1,2)+pos(1,4);
      pos = iduilay1(FigW,2,1,lev2,[],1.5);
      uicontrol(f,'pos',pos(1,:),'style','frame');
      XID.parest(5)=uicontrol(f,'pos',pos(2,:),...
         'style','push','string','Iteration control...','enable','off',...
         'callback',[s1,'iduiiter(''open'');',s3]);
      XID.parest(6)=uicontrol(f,'pos',pos(3,:),...
         'style','push','string','Order editor...',...
         'callback',[s1,'idparest(''mse'');',s3]);
      % LEVEL 3
      lev3 = pos(1,2)+pos(1,4);
      pos = iduilay1(FigW,6,3,lev3,[],[1 2.2]);
      uicontrol(f,'pos',pos(1,:),'style','frame');
      uicontrol(f,'pos',pos(6,:),'style','text',...
         'string','Covariance:','HorizontalAlignment','left');
      XID.parest(19) = uicontrol(f,'pos',pos(7,:),'style','pop',...
         'string','Estimate|None');
      
      uicontrol(f,'pos',pos(4,:),'style','text',...
         'string','Initial State:','HorizontalAlignment','left');
      XID.parest(17) = uicontrol(f,'pos',pos(5,:),'style','pop',...
         'string','Auto|Zero|Estimate|Backcast');
      uicontrol(f,'pos',pos(2,:),'style','text',...
         'string','Focus:','HorizontalAlignment','left');
      XID.parest(18) = uicontrol(f,'pos',pos(3,:),'style','pop',...
         'string','Prediction|Simulation|Filter|Stability','callback',[s1,'idparest(''filter'');',s3]);
      % LEVEL 4
      lev4 = pos(1,2)+pos(1,4);
      
      pos = iduilay1(FigW,10,5,lev4,[],[1 2.2]);
      uicontrol(f,'pos',pos(1,:),'style','frame');
      
      uicontrol(f,'pos',pos(8,:),...
         'string','Method:','style','text',...
         'HorizontalAlignment','left');
      posmeth1=pos(9,:)+[0 0 -1.1*mStdButtonWidth 0];
      posmeth1 = posmeth1 + [0 0 -mEdgeToFrame 0];
      posmeth2=pos(9,:)+[1.1 0 -1.1 0]*mStdButtonWidth;
      posmeth2 = posmeth2 + [mEdgeToFrame 0 -mEdgeToFrame 0];
      XID.arx(1,2)=uicontrol(f,'pos',posmeth1,'string','ARX',...
         'callback',[s1,'iduiarx(''arx'');idparest(''mstype'');',s3],...
         'style','radio');
      XID.arx(1,5)=uicontrol(f,'pos',posmeth2,'string','IV',...
         'callback',[s1,'iduiarx(''iv'');idparest(''mstype'');',s3],...
         'style','radio');
      XID.ss(1,2)=uicontrol(f,'pos',posmeth1,'string','PEM',...
         'callback',[s1,'iduiss(''pem'');idparest(''mstype'');',s3],...
         'style','radio','vis','off');
      XID.ss(1,5)=uicontrol(f,'pos',posmeth2,'string','N4SID',...
         'callback',[s1,'iduiss(''ssss'');idparest(''mstype'');',s3],...
         'style','radio','vis','off');
      XID.parest(11)=uicontrol(f,'pos',pos(9,:),...
         'style','text','string','Prediction error method',...
         'vis','off','HorizontalAlignment','left');
      set(XID.arx(1,2),'value',1);set(XID.ss(1,5),'value',1);
      
      % ******************************
      % LEVEL 4
      
      uicontrol(f,'pos',pos(6,:),'style','text',...
         'HorizontalAlignment','left','string','Equation:');
      XID.parest(10)=uicontrol(f,'pos',pos(7,:),'style','text',...
         'HorizontalAlignment','left','string','x');
      XID.parest(4)=uicontrol(f,'pos',pos(3,:),'style','popup',...
         'callback',[s1,'idparest(''mstype'',''off'');',s3],'value',1,...
         'string','x|x|x|x|x|x');
      XID.parest(3)=uicontrol(f,'pos',pos(5,:),...
         'style','edit','callback',[s1,'idparest(''ordedit'');',s3],...
         'HorizontalAlignment','left',...
         'backgroundcolor','white','tag','modst');
      XID.parest(9)=uicontrol(f,'pos',pos(2,:),...
         'HorizontalAlignment','left',...
         'string','Structure:','style','text');
      uicontrol(f,'pos',pos(10,:),...
         'HorizontalAlignment','left',...
         'string','Name:','style','text');
      XID.parest(16)=uicontrol(f,'pos',pos(4,:),...
         'HorizontalAlignment','left',...
         'string','Orders:','style','text');
      XID.parest(7)=uicontrol(f,'pos',pos(11,:),...
         'style','edit','string','arx441',...
         'HorizontalAlignment','left',...
         'backgroundcolor','white');
      
      FigH=pos(1,2)+pos(1,4)+mEdgeToFrame;
      FigWH=[FigW,FigH];
      sumbpos=get(XID.sumb(1),'pos');
      FigXY=max([sumbpos(1) sumbpos(2)-FigH-25],[0 0]);
      set(f,'pos',[FigXY FigWH]);
      set(get(f,'children'),'units','normal')
      if length(XID.layout)>19,
         if XID.layout(20,3)
            try
               set(f,'pos',XID.layout(20,1:4));
            end
         end,
      end
      set(f,'vis',onoff)
      set(Xsum,'UserData',XID);
      
      idparest('setpop');
      %idparest('mse');
      idparest('mstype','off');
      %idparest('ordedit');
   end % if figflag
   nn=get(XID.parest(1),'userdata');
   if ~all(nn==[nu ny])
      set(XID.parest(1),'userdata',[nu ny])
      [flag,hnr]=figflag(idlaytab('figname',21),1);
      if flag
         viss=get(hnr,'vis');set(hnr,'vis','off');
      else
         viss='off';
      end
      idparest('setpop');
      idparest('mstype','off');
      if nu==0
         set(XID.parest(18),'enable','off','value',1)
      else
         set(XID.parest(18),'enable','on')
      end
      
      if strcmp(viss,'on'),idparest('mse');XID=get(Xsum,'UserData');end
   end
   iduistat('Choose model Structure and Orders and then press the Estimate button.')
   % ************************
   
elseif strcmp(arg,'mstype')
   Mtype=get(XID.parest(4),'value');
   err=0;
   if ny>1
      if any(Mtype==[2 3 4]), err=1;text='multi-output systems.';end
   end
   if nu==0
      if any(Mtype==[3 4]),err=1;text='time-series data (no input).';end
   end
   if err,
      mse=0;oldMtype=1;
      ll1=iduiwok(21);if ishandle(ll1),if strcmp(get(ll1,'vis'),'on')
            mse=1;
         end,end
      if mse,
         oldMtype=get(XID.parest(15),'userdata');
      end
      
      errordlg(['This Model Structure is not supported for ' text]...
         ,'Error Dialog','modal');
      set(XID.parest(4),'value',oldMtype)
      set(0,'Showhiddenhandles','off');
      set(Xsum,'Userdata',XID); 
      return
   end
   if (Mtype==1&ny==1&get(XID.arx(1,2),'value'))|... %%%
         (Mtype==5&get(XID.ss(1,5),'value'))               %%%
      % These are the cases for order select      %%%
      set(XID.parest(5),'string','Order selection','enable','on',... %%%
         'callback','idparest(''ord_sel'');')          %%%
      onoffiter='off';
   else                                                %%%
      set(XID.parest(5),'string','Iteration control...',...  %%%
         'callback',['iduipoin(1);iduiiter(''open'');iduipoin(2);']) %%%
      onoffiter='on';itval=1;
      if Mtype==1,onoffiter='off';itval=0;end
      if Mtype==5&get(XID.ss(1,5),'value'),onoffiter='off';itval=0;end
      set(XID.parest(5),'enable',onoffiter)
   end
   try
      if strcmp(onoffiter,'off'),
         set(iduiwok(22),'vis','off');
      end
   end
   XID = get(Xsum,'UserData');
   if strcmp(onoff,'off')
      iduims('both',Mtype,nu,ny);
      XID = get(Xsum,'UserData');
      idparest('ordedit');
   end
   iduistat('Select orders and press the Estimate button.')
   
elseif strcmp(arg,'orders')
   if ~ishandle(iduiwok(21)),
      set(0,'Showhiddenhandles','off');
      set(Xsum,'Userdata',XID); 
      return
   end
   Mtype=get(XID.parest(4),'value');
   if Mtype==1,
      iduiarx('orders');
   elseif any(Mtype==[2 3 4]), 
      iduiio('orders');
   elseif Mtype==5, 
      iduiss('orders');
   end
   iduistat('Press Estimate to estimate model.');
elseif strcmp(arg,'ordedit')
   flag=0;
   set(XID.parest(3),'userdata',[]);
   Mtype=get(XID.parest(4),'value');
   if Mtype==1,
      flag=iduiarx('mname');
   elseif any(Mtype==[2 3 4]),
      flag=iduiio('mname');
   elseif Mtype==5,
      flag=iduiss('mname');
   elseif Mtype==6
      set(XID.parest(7),'string',[get(XID.parest(3),'string'),'c'])
   end
   if flag,
      set(0,'Showhiddenhandles','off');
      return
   end
   mse=0;
   [fl,fi]=figflag(idlaytab('figname',21),1);
   if fl,if strcmp(get(fi,'vis'),'on'),mse=1;end,end
   if mse
      if Mtype==1,iduiarx('ordedit')
      elseif any(Mtype==[2 3 4]), iduiio('ordedit')
      elseif Mtype==5, iduiss('ordedit');
      end
   end
   iduistat('Press Estimate to estimate model.')
   
elseif strcmp(arg,'setpop')
   str=idmspop(nu,ny);
   set(XID.parest(4),'string',str)
elseif strcmp(arg,'mse')
   iduiio('open');
   XID = get(Xsum,'UserData');
   idparest('ordedit');
elseif strcmp(arg,'estimate')
   Mtype=get(XID.parest(4),'value');
   if Mtype==1
      gtws=iduiarx('estimate');
   elseif any(Mtype==[2 3 4 6])
      gtws=iduiio('estimate');
   elseif Mtype==5
      iduiss('estimate');
   end
elseif strcmp(arg,'ord_sel')
   Mtype=get(XID.parest(4),'value');
   if Mtype==1&nu>0
      str='[1:10 1:10 1:10]';
   else
      str='1:10';
   end
   
   set(XID.parest(3),'string',str)
   set(XID.parest(7),'string','')
   if iduiwok(21)
      set(XID.io(1,4),'value',13);
      if Mtype==5
         nks = get(XID.ss(1,8),'string');
         nks = get(XID.ss(1,8),'string');
         pnr1 = find(nks == '[');
         pnr2 = find(nks == ']');
         if isempty(pnr1),pnr1 = 0;end
         if isempty(pnr2),pnr2 = length(nks)+1;end
         if ~isempty(nks)
            str = [str,' [',nks(1+pnr1:pnr2-1),']'];
         end
         set(XID.parest(3),'string',str)
      end
      if Mtype==1
         if ny>1|nu>4,nmu=min(1,nu);else nmu=nu;end
         set(XID.io(1:nmu,[1 3]),'value',13);
      end
   end
   iduistat('Press Estimate and select order in plot that will open.')
elseif strcmp(arg,'filter')
   value = get(XID.parest(18),'Value');
   set(XID.parest(18),'UserData',[]);
   if value == 3
      iduifilt('open',[],'model');
   end
   
end

set(0,'Showhiddenhandles','off');
%set(Xsum,'Userdata',XID);
