function iduispa(arg)
%IDUISPA Handles the spectral analysis dialog.

%   L. Ljung 9-27-94
%   Copyright 1986-2000 The MathWorks, Inc.
%   $Revision: 1.8 $  $Date: 2000/06/15 11:46:31 $


Xsum = findobj(get(0,'children'),'flat','tag','sitb16');
XID = get(Xsum,'Userdata');
XIDplotw = XID.plotw;
XIDlayout = XID.layout;

if strcmp(arg,'open')
   figname=idlaytab('figname',33);
   if ~figflag(figname,0)
      iduistat('Opening the spectral analysis dialog box ...')
      layout
      FigW = iduilay2(3);
      
      f=figure('vis','off',...
         'NumberTitle','off','Name',figname,'HandleVisibility','callback',...
         'Color',get(0,'DefaultUIControlBackgroundColor'),...
         'tag','sitb33','Menubar','none');
      
      % LEVEL1
      
      pos = iduilay1(FigW,3);
      uicontrol(f,'pos',pos(1,:),'style','frame');
      uicontrol(f,'Pos',pos(2,:),'style','push',...
         'string','Estimate','callback',...
         'iduipoin(1);iduinpar(''spa'');iduipoin(2);');
      uicontrol(f,'Pos',pos(3,:),'style','push',...
         'callback','set(gcf,''visible'',''off'')','string','Close');
      uicontrol(f,'Pos',pos(4,:),'style','push','string','Help',...
         'callback','iduihelp(''spa.hlp'',''Help: Spectral Analysis'');');
      
      % LEVEL 2
      lev2 = pos(1,2)+pos(1,4);
      boxno=1;
      pos = iduilay1(FigW,4,4,lev2,[],3);
      uicontrol(f,'pos',pos(1,:),'style','frame');
      h1=uicontrol(f,'pos',pos(5,:),'style','edit','Horizontalalignment',...
         'left',...
         'backgroundcolor','white','callback','iduispa(''resol'');');
      uicontrol(f,'pos',pos(4,:),'style','text','string',...
         'Frequency resolution parameter (M) (Empty = Default)','Horizontalalignment',...
         'left');
      h2=uicontrol(f,'pos',pos(3,:),'style','popup',...
         'string','Blackman-Tukey (spa)|Smoothed Fourier Transform (etfe)',...
         'backgroundcolor',get(0,'defaultUIcontrolbackgroundcolor'),...
         'callback','iduispa(''method'');');
      uicontrol(f,'pos',pos(2,:),'style','text','string',...
         'Choice of method','HorizontalAlignment','left');
      usd=get(XIDplotw(16,1),'userdata');
      mspa=deblank(usd(2,:));
      if isempty(eval(mspa))
         mspa = 'Default';
      end
      set(h1,'string',mspa);
      set(h2,'value',eval(usd(3,:)));
      poslev=pos(1,2)+pos(1,4)+mEdgeToFrame;
      ScreenPos = get(0,'ScreenSize');
      FigWH=[FigW poslev];
      FigPos=[(ScreenPos(3:4)-FigWH)/2 FigWH];
      set(f,'pos',FigPos);
      set(get(f,'children'),'unit','norm')
      if length(XIDlayout)>32,if XIDlayout(33,3)
            eval('set(f,''pos'',XIDlayout(33,1:4))','')
         end,end
      set(f,'vis','on')
      iduistat('')
   end
   
elseif strcmp(arg,'resol')
   usd=get(XIDplotw(16,1),'userdata');
   mspa=get(iduigco,'string');
   flag=0;
   if isempty(mspa),
      mspa='Default';
   elseif strcmp(lower(mspa(1)),'d')
      mspa ='Default';
   else
      try
         if isempty(eval(mspa)),
            mspa='Default';
         end
      catch 
         flag = 1;
      end
   end
   
   if ~strcmp(mspa,'Default')
      imspa=0;
      eval('imspa=eval(mspa);','flag=1;')
      if imspa<=0|floor(imspa)~=imspa,
         flag=1;
      end
   end
   if flag,
      strM = usd(2,:);
      try
         if isempty(eval(strM))
            strM = '';
         end
      catch
      end
      
      set(iduigco,'string',strM);
      errordlg('The resolution parameter must be a positive integer.','Error Dialog','modal');
      return
   end
   if strcmp(lower(mspa(1)),'d')
      mspa = '[]';
   end
   usd=str2mat(usd(1,:),mspa,usd(3,:));
   set(XIDplotw(16,1),'userdata',usd);
   
elseif strcmp(arg,'method')
   usd=get(XIDplotw(16,1),'userdata');
   usd=str2mat(usd(1,:),usd(2,:),int2str(get(iduigco,'value')));
   set(XIDplotw(16,1),'userdata',usd);
end
