function IR=iduinpar(arg,TYpe,MSpa)
%IDUINPAR Performs non-parametric estimation for ident.
%       Arguments:
%	cra Gives correlation analysis using CRA. The options are read from XIDmen(12,1)
%	spa Gives spectral analysis using SPA/ETFE. The options are read from
%	    XIDmen(12,1) and from XIDplotw(2,2)
%       The input arguments TYpe and MSpa override the options

%	L. Ljung 4-4-94
%	Copyright (c) 1986-95 by the MathWorks, Inc.
%	$Revision: 1.8 $  $Date: 1995/05/10 21:36:59 $

global XIDplotw 


[eDat,eDat_info,eDat_n]=iduigetd('e');
TSamp=eval(eDat_info(1,:));
[ny,nu]=iduiiono('old',eDat_info,'dat');
usd=get(XIDplotw(16,1),'userdata');
if strcmp(arg,'cra')
   iduistat('Performing correlation analysis ...')
   if nu==0
      errordlg(str2mat('Correlation Analysis cannot be applied to',...'
                       'time series to estimate the impulse response.'));
   else 
      NA=eval(usd(1,:));
      OPt=get(XIDplotw(5,2),'UserData');
      TSpan=eval(deblank(OPt(2,:)));
      IR=[];
      for ky=1:ny
       for ku=1:nu
        [IRtemp,RR,COnf]=cra(eDat(:,[ky,ny+ku]),TSpan,NA,0);
        IR=[IR,[TSamp;COnf;IRtemp]];
       end
   end
   mod_info=str2mat('cra',eDat_info,...
       [' cra_d = cra(',eDat_n,',',num2str(TSpan),',',deblank(usd(1,:)),')']);
   iduiinsm(IR,mod_info,['cra_d'],1);%,eData_n]);
 end
elseif strcmp(arg,'spa')
   iduistat('Performing spectral analysis ...')
   if nargin==1
      OPt=usd([2,3],:);
      TYpe=eval((OPt(2,:)));
      MSpa=eval((OPt(1,:)));
   end
   OPt1=get(XIDplotw(2,2),'Userdata');
   W=eval(deblank(OPt1(4,:)));
   OPt2=get(XIDplotw(7,2),'Userdata');
   W2=eval(deblank(OPt2(4,:)));
   sep_est_spe=0;
   if ~(isempty(W)&isempty(W2))
       if length(W)~=length(W2);
          errordlg(str2mat('You can evaluate the frequency functions and',...
            'the disturbance spectra at different frequencies. However',...
            'the number of frequency points must be the same.'));
          iduistat('')
          return
       elseif norm(W-W2)>eps
          sep_est_spe=1;
       end
   end 
   if nu==0
      W=W2;sep_est_spe=0;
   end
   if isempty(W)
      LW=128;
   else 
      LW=2^nextpow2(W); 
   end
   if TYpe==2,nam='etf';else nam='spa';end
   if isempty(MSpa)
      nam=[nam,'_d'];MSpa1=30;
   else
      sord=num2str(MSpa);sord=sord(1:min(4,length(sord))); 
      nam=[nam,sord];MSpa1=MSpa;
   end,
   if TYpe==2
       g=[];
       for ky=1:ny
        if nu>0
           for ku=1:nu
             gg=etfe(eDat(:,[ky,ny+ku]),MSpa,LW,TSamp);
             g=[g,[[gg(1,1:3)+((ku-1)+1000*(ky-1))*ones(1,3)];gg(2:LW,:)]];
           end
        else
             gg=etfe(eDat(:,ky),MSpa,LW,TSamp);
             g=[g,[[gg(1,1:2)+1000*(ky-1)*ones(1,2)];gg(2:LW,:)]];
        end    
	phi=[];
       end
       STrcom=[' ',nam,' = etfe(',eDat_n,',',deblank(usd(2,:)),...
               ',',num2str(LW),',', num2str(TSamp),')'];
       meth='e';
    else
       if isempty(MSpa)&ny>1,MSpa=-1;end
       if ny>1
          textM='[';
          for ky=1:ny
             textM=[textM,int2str(MSpa),' '];
          end
          textM=[textM(1:length(textM)-1),']'];
       else
          textM=deblank(usd(2,:));
       end
       [g,phi]=spa(eDat,MSpa*ones(1,ny),W,[],TSamp,1);
       if sep_est_spe
          [dum,phi]=spa(eDat,MSpa*ones(1,ny),W2,[],TSamp,1);
       end
       STrcom=[' ',nam,' = spa(',eDat_n,',',textM,',',...
               deblank(OPt1(4,:)),',','[],',num2str(TSamp),')'];
       if sep_est_spe
         STrcom=str2mat(STrcom,...
           ['  (Disturbance spectrum estimated separately'],...
           ['  at frequencies ',deblank(OPt2(4,:)),')']);
       end
       meth='s'; 
    end
    mod_info=str2mat(['spa ',meth,' ',int2str(MSpa)],eDat_info,STrcom);
    if nu==0, model=g;else model=[g,phi];end
    iduiinsm(model,mod_info,nam,1);
end
