%   plotting axis limits: (normalized units within figure)
%  
comp=computer;
plotParams;
%xi = 0.4; lxi = 0.55; yi = 0.1;lyi = 0.79;
xi=plotpos(1);lxi=plotpos(3);yi=plotpos(2);lyi=plotpos(4);
sfac = 1.5;
action = get(gcbo,'Tag');
Ifig = gcf;
ii = TSfigInd(Ifig);

nch = TSd{ii}.nch;
npts = TSd{ii}.npts;

i0 = TSw{ii}.i0;
di = TSw{ii}.di;
i1 = TSw{ii}.i1;
t0 = TSd{ii}.t0*(1-TSw{ii}.t0Eq0)/TSw{ii}.tFac;
dt = TSd{ii}.dt/TSw{ii}.tFac;
nPw = TSw{ii}.nPw;
yFac = TSw{ii}.yFac;

switch action
   case 'chnumpt'
%  change number of points plotted in window
      if (length(str2num(get(gco,'String'))) == 0) 
         set(gco,'String',num2str(nPw));
      else
         nPw = str2num(get(gco,'String'));
         nPw = min(nPw,npts);
         set(gco,'String',num2str(nPw));
         TSw{ii}.nPw = nPw;
         TSw{ii}.page = 1;
         i0 = 1;
         TSw{ii}.i0 = i0;
         TSw{ii}.npage = ceil(npts/nPw);
         i1 = min(i0+nPw-1,npts);
         TSw{ii}.i0 = i0;
         TSw{ii}.i1 = i1;
         xl = dt*[i0 i1]+t0;
         for ic = nch:-1:1
           if TSw{ii}.chPlt(ic)
              set(TSw{ii}.ACh(ic),'xlim',xl);
              rePltCh
           end
         end
         ss = sprintf('page#\n %d/%d',TSw{ii}.page,TSw{ii}.npage); 
         set(TSw{ii}.PAGE,'String',ss); 
         TSw{ii}.i0 = i0;
         TSw{ii}.i1 = i1;
         ch1 = find(TSw{ii}.chPlt,1);
         xl = get(TSw{ii}.ACh(ch1),'xlim');
         xl = xl*TSw{ii}.tFac+TSw{ii}.t0Eq0*TSd{ii}.t0;
         axes(TSw{ii}.hMask);
         T12 = TSw{ii}.T12;
         plot(T12',ones(size(T12')),'LineWidth',6,'Color','k');
         set(gca,'xlim',xl,'Xtick',[],'Ytick',[]);
         UpdateCohPlot = 1;
         CohCB
      end

   case 'Centered'
%  Center or uncenter range
      TSw{ii}.centered=1-TSw{ii}.centered;centered=TSw{ii}.centered;
      if centered==1,set(gcbo,'checked','on');else set(gcbo,'checked','off');end
      for ic = 1:nch
         TSw{ii}.YLch(ic,1) = (1-TSw{ii}.centered) * mean(TSd{ii}.range(ic,1:2));
         if(TSd{ii}.SysTF.PhysU)
            TSw{ii}.YLch(ic,1) = TSw{ii}.YLch(ic,1)*TSd{ii}.range(ic,3);
         end
         yl = mkshort(TSw{ii}.YLch(ic,1)+[-1,1]*TSw{ii}.YLch(ic,2));
         if TSw{ii}.chPlt(ic)
            set(TSw{ii}.mCH(ic),'String',num2str(yl(1)));
            set(TSw{ii}.MCH(ic),'String',num2str(yl(2)));
         end
         if TSw{ii}.chPlt(ic)
            set(TSw{ii}.ACh(ic),'ylim',yl);
            rePltCh
         end
      end

   case 'replot'
%  replot page
      rePltPage

   case 'reset'
%  reset plotting ranges to default
      for ic = 1:nch
         TSw{ii}.YLch(ic,1) = (1-TSw{ii}.centered) * mean(TSd{ii}.range(ic,1:2));
         TSw{ii}.YLch(ic,2) = (TSd{ii}.range(ic,2)-TSd{ii}.range(ic,1))/2;
         if(TSd{ii}.SysTF.PhysU)
            TSw{ii}.YLch(ic,2) = (TSw{ii}.YLch(ic,2))*TSd{ii}.range(ic,3);
         end
         if TSw{ii}.chPlt(ic)
            yl = mkshort(TSw{ii}.YLch(ic,1)+[-1,1]*TSw{ii}.YLch(ic,2));
            set(TSw{ii}.mCH(ic),'String',num2str(yl(1)));
            set(TSw{ii}.MCH(ic),'String',num2str(yl(2)));
            set(TSw{ii}.ACh(ic),'ylim',yl);
         end
      end

   case 'zoomOut'
%  Zoom out for all channels simultaneously 
%  Increase range by factor sfac
      for ic = 1:nch
         TSw{ii}.YLch(ic,2) = mkshort(TSw{ii}.YLch(ic,2)*sfac);
         if TSw{ii}.chPlt(ic)
            yl = TSw{ii}.YLch(ic,1)+[-1,1]*TSw{ii}.YLch(ic,2);
            set(TSw{ii}.mCH(ic),'String',num2str(yl(1)));
            set(TSw{ii}.MCH(ic),'String',num2str(yl(2)));
            set(TSw{ii}.ACh(ic),'ylim',yl);
         end
      end

   case 'zoomIn'
%  Zoom out for all channels simultaneously 
%  Decrease plotting range by factor sfac
      for ic = 1:nch
         TSw{ii}.YLch(ic,2) = mkshort(TSw{ii}.YLch(ic,2)/sfac);
         if TSw{ii}.chPlt(ic)
            yl = TSw{ii}.YLch(ic,1)+[-1,1]*TSw{ii}.YLch(ic,2);
            set(TSw{ii}.mCH(ic),'String',num2str(yl(1)));
            set(TSw{ii}.MCH(ic),'String',num2str(yl(2)));
            set(TSw{ii}.ACh(ic),'ylim',yl);
         end
      end

   case 'zoomOutCh'
%  Zoom out for one channel 
%  Increase plotting range by factor sfac
      ic=get(gco,'UserData');
      TSw{ii}.YLch(ic,2) = TSw{ii}.YLch(ic,2)*sfac;
      tmp= TSw{ii}.YLch(ic,1)+[-1,1]*TSw{ii}.YLch(ic,2);
      yl = mkshort(tmp);
      set(TSw{ii}.mCH(ic),'String',num2str(yl(1)));
      set(TSw{ii}.MCH(ic),'String',num2str(yl(2)));
      set(TSw{ii}.ACh(ic),'ylim',yl);

   case 'zoomInCh'
%  Zoom out for one channel
%  Decreas plotting range by factor sfac
      ic=get(gco,'UserData');
      TSw{ii}.YLch(ic,2) = TSw{ii}.YLch(ic,2)/sfac;
      tmp=TSw{ii}.YLch(ic,1)+[-1,1]*TSw{ii}.YLch(ic,2);
      yl = mkshort(tmp);
      set(TSw{ii}.mCH(ic),'String',num2str(yl(1)));
      set(TSw{ii}.MCH(ic),'String',num2str(yl(2)));
      set(TSw{ii}.ACh(ic),'ylim',yl);

   case 'chonoff'
%  Activate/deactivate a data channel for plotting
      ic=get(gco,'UserData');
      TSw{ii}.chPlt(ic)=1-TSw{ii}.chPlt(ic);
      set(gco,'ForeGroundColor',fgc(1+TSw{ii}.chPlt(ic),:));
      if TSw{ii}.chPlt(ic)
         yl = mkshort(TSw{ii}.YLch(ic,1)+[-1,1]*TSw{ii}.YLch(ic,2));
         set(TSw{ii}.mCH(ic),'String',num2str(yl(1)));
         set(TSw{ii}.MCH(ic),'String',num2str(yl(2)));
      end
      rePltPage

   case 'set_mCH'
%  change plotting range lower limit for one channel by editing mCH string
      h_ob = gco;
      ic=get(h_ob,'UserData');
      yl = mkshort(TSw{ii}.YLch(ic,1)+[-1,1]*TSw{ii}.YLch(ic,2));  
      if (length(str2num(get(h_ob,'String'))) == 0)
         set(h_ob,'String',num2str(yl(1)));
      else
         newMin = str2num(get(h_ob,'String'));
         newMin =mkshort(newMin); %round(newMin*10)/10;
         newMin = min(newMin,-1);
         if TSw{ii}.centered
            yl = mkshort([-abs(newMin), abs(newMin)]);
            TSw{ii}.YLch(ic,2) = abs(newMin);
            set(TSw{ii}.MCH(ic),'String',yl(2));
            set(TSw{ii}.mCH(ic),'String',yl(1));
         else
            yl(1) = newMin;
            TSw{ii}.YLch(ic,1) = mean(yl);
            TSw{ii}.YLch(ic,2) = (yl(2)-yl(1))/2;
            set(TSw{ii}.MCH(ic),'String',yl(2));
            set(TSw{ii}.mCH(ic),'String',yl(1));
         end
         if TSw{ii}.chPlt(ic) 
            set(TSw{ii}.ACh(ic),'ylim',yl);
         end
      end

   case 'set_MCH'
%  change plotting range lower limit for one channel by editing mCH string
      h_ob = gco;
      ic=get(h_ob,'UserData');
      yl = mkshort(TSw{ii}.YLch(ic,1)+[-1,1]*TSw{ii}.YLch(ic,2));  
      if (length(str2num(get(h_ob,'String'))) == 0)
         set(h_ob,'String',num2str(yl(2)));
      else
         newMax = str2num(get(h_ob,'String'));
         newMax = mkshort(newMax);%round(newMax*10)/10;
         newMax=max(newMax,1);
         if TSw{ii}.centered
            yl = mkshort([-abs(newMax), abs(newMax)]);
            TSw{ii}.YLch(ic,2) = abs(newMax);
            set(TSw{ii}.MCH(ic),'String',yl(2));
            set(TSw{ii}.mCH(ic),'String',yl(1));
         else
            yl(2) = newMax;
            TSw{ii}.YLch(ic,1) = mean(yl);
            TSw{ii}.YLch(ic,2) = (yl(2)-yl(1))/2;
            set(TSw{ii}.MCH(ic),'String',yl(2));
            set(TSw{ii}.mCH(ic),'String',yl(1));
         end
         if TSw{ii}.chPlt(ic) 
            set(TSw{ii}.ACh(ic),'ylim',yl);
         end
      end

   case 'PrevPage'
%  plot previous page  
      TSw{ii}.page=TSw{ii}.page-1;
      if (TSw{ii}.page <= 0)
         TSw{ii}.page = TSw{ii}.page + 1;
         beep_vec('splat');
      end
      i0 = nPw*(TSw{ii}.page-1) + 1;
      i1 = i0+nPw-1;
      if(i1 > npts)
            i1 = npts;
            i0 = i1-nPw+1;
      end
      t = [i0:di:i1]*dt+t0;
      TSw{ii} = plotPage(t,yFac*TSdata{ii}(:,[i0:di:i1]),TSw{ii},TSd{ii},0);
      ss = sprintf('page#\n %d/%d',TSw{ii}.page,TSw{ii}.npage);
      set(TSw{ii}.PAGE,'String',ss);
      TSw{ii}.i0 = i0;
      TSw{ii}.i1 = i1;
      UpdateCohPlot = 1;
      CohCB

   case 'NextPage'
%  plot next page
      TSw{ii}.page =    TSw{ii}.page + 1; 
      if (TSw{ii}.page > TSw{ii}.npage)
         TSw{ii}.page = TSw{ii}.page - 1;
         beep_vec('splat');
      end
       i0 = nPw*(TSw{ii}.page-1) + 1;
       i1 = i0+nPw-1;
       if(i1 > npts)
            i1 = npts;
            i0 = i1-nPw+1;
       end
       t = [i0:di:i1]*dt+t0;
       TSw{ii} = plotPage(t,yFac*TSdata{ii}(:,[i0:di:i1]),TSw{ii},TSd{ii},0);
       ss = sprintf('page#\n %d/%d',TSw{ii}.page,TSw{ii}.npage);
       set(TSw{ii}.PAGE,'String',ss);
       TSw{ii}.i0 = i0;
       TSw{ii}.i1 = i1;
       UpdateCohPlot = 1;
       CohCB;
%

   case 'gotoPage'
%  go to page # typed in page indicator
   if (length(str2num(get(gco,'String'))) == 0)
      set(gco,'String',num2str(TSw{ii}.page));
   else
      newpage = str2num(get(gco,'String'));
      if (newpage > TSw{ii}.npage) | (newpage < 1)
         beep_vec('laughter');
         set(gco,'String',num2str(TSw{ii}.page));
      else
         TSw{ii}.page = newpage;
         i0 = nPw*(TSw{ii}.page-1) + 1;
         i1 = i0+nPw-1;
         if(i1 > npts)
            i1 = npts;
            i0 = i1-nPw+1;
         end
         t = [i0:di:i1]*dt+t0;
         TSw{ii} = plotPage(t,yFac*TSdata{ii}(:,[i0:di:i1]),TSw{ii},TSd{ii},0);
         ss = sprintf('page#\n %d/%d',TSw{ii}.page,TSw{ii}.npage);
         set(TSw{ii}.PAGE,'String',ss);
         TSw{ii}.i0 = i0;
         TSw{ii}.i1 = i1;
         UpdateCohPlot = 1;
         CohCB
     end 
   end

   case 'Quit'
%  quit this window
      delete(Ifig)

   case 't0'
      chk = strcmp(get(gcbo,'checked'),'on');
      if chk
          set(gcbo,'checked','off')
          TSw{ii}.t0Eq0 = 0;
      else
          set(gcbo,'checked','on')
          TSw{ii}.t0Eq0 = 1;
      end         
      rePltPage

   case 'tUnits'
      h = gcbo;
      set(findobj('Tag','tUnits'),'checked','off');
      set(h,'checked','on');
      TSw{ii}.tUnits = get(h,'Label');
      TSw{ii}.tFac = get(h,'UserData');
      rePltPage

   case 'yUnits'
      h = gcbo;
      set(findobj('Tag','yUnits'),'checked','off');
      set(h,'checked','on');
      TSw{ii}.yFac = 1./get(h,'UserData');
      for ic = 1:TSd{ii}.nch
         YLch(ic,1) = (1-TSw{ii}.centered) * mean(TSd{ii}.range(ic,1:2));
         YLch(ic,2) = (TSd{ii}.range(ic,2)-TSd{ii}.range(ic,1))/2;
         if(TSd{ii}.SysTF.PhysU)
            YLch(ic,2) = YLch(ic,2)*TSd{ii}.range(ic,3);
         end
      end   
      TSw{ii}.YLch = TSw{ii}.yFac*YLch;
      for ic = 1:nch
         if TSw{ii}.chPlt(ic)
            yl = mkshort(TSw{ii}.YLch(ic,1)+[-1,1]*TSw{ii}.YLch(ic,2));
            set(TSw{ii}.mCH(ic),'String',num2str(yl(1)));
            set(TSw{ii}.MCH(ic),'String',num2str(yl(2)));
         end
      end
      rePltPage

   case 'Uniform ylim'
     hTSw = gcf;
%     xy = get(gcf,'CurrentPoint');
     xy = [200,200];
     hUy = figure('Position',[xy(1) xy(2) 210,83],'Menubar','None',...
                  'NumberTitle','off');
     uicontrol('Parent',hUy, ...
        'Units','normalized', ...
        'FontUnits','normalized', ...
        'BackgroundColor',[0.7 0.7 0.7], ...
        'FontSize',0.3787, ...
        'Position',[0.0428 0.5783 0.9285 0.3734], ...
        'Style','frame', ...
        'Tag','Frame1');
     uicontrol('Parent',hUy, ...
        'Units','normalized', ...
        'FontUnits','normalized', ...
        'BackgroundColor',[0.7 0.7 0.7], ...
        'FontSize',0.5681, ...
        'FontWeight','demi', ...
        'Position',[0.0714 0.6144 0.8619 0.2650], ...
        'String','Set Uniform Y-Limits', ...
        'Style','text', ...
        'Tag','StaticText1');
     uicontrol('Parent',hUy, ...
        'Units','normalized', ...
        'FontUnits','normalized', ...
        'BackgroundColor',[0.7 0.7 0.7], ...
        'FontSize',0.1404, ...
        'Position',[0.04285 0.0843 0.9142 0.4698], ...
        'Style','frame', ...
        'Tag','Frame2');
     uicontrol('Parent',hUy, ...
        'Units','normalized', ...
        'FontUnits','normalized', ...
        'BackgroundColor',[1 1 1], ...
        'Callback','setUnifYlim', ...
        'FontSize',0.4464, ...
        'Position',[0.4428 0.1566 0.4476 0.3373], ...
        'Style','edit', ...
        'Tag','Edit Max');
     uicontrol('Parent',hUy, ...
        'Units','normalized', ...
        'FontUnits','normalized', ...
        'BackgroundColor',[0.7 0.7 0.7], ...
        'FontSize',0.625, ...
        'FontWeight','demi', ...
        'Position',[0.1190 0.1687 0.3190 0.2289],...
        'String','Max', ...
        'Style','text', ...
        'Tag','Max text');

   case 'Mark'
      figure(gcf);
      set(gcf,'Pointer','FullCrossHair')
      posFig = get(gcf,'Position');
      k = waitforbuttonpress;
      p1 = get(gcf,'CurrentPoint');
      np1(1) = p1(1)/posFig(3);
      np1(2) = p1(2)/posFig(4);
      pyi = yi*posFig(4);
      plyi = lyi*posFig(4);
      if(np1(1) > xi-.05 & np1(1) < lxi+xi & np1(2)> yi & np1(2) < lyi+yi)
         if(np1(1) < xi)
             np1(1) = xi;
         end
         initialRect = [p1(1) pyi 0 plyi];
         fixedPoint = [p1(1) pyi];
         set(gcf,'Pointer','Arrow');
         finalRect = rbbox(initialRect,fixedPoint);
         t1 = (np1(1)-xi)/lxi;
         p2 = finalRect(3)/posFig(3);
         t2 = p2/lxi+t1;
         t2 = min(t2,1);
         t1 =  fix(t1*(i1-i0+1))*TSd{ii}.dt + i0*TSd{ii}.dt + TSd{ii}.t0;
         t2 =  fix(t2*(i1-i0+1))*TSd{ii}.dt + i0*TSd{ii}.dt + TSd{ii}.t0;
         T12 = addT(t1,t2,TSw{ii}.T12);
         [nSeg,dum] = size(T12);
         ich = find(TSw{ii}.chPlt,1);
         xl = get(TSw{ii}.ACh(ich),'xlim');
         xl = xl*TSw{ii}.tFac+TSw{ii}.t0Eq0*TSd{ii}.t0;
         axes(TSw{ii}.hMask);
         TSw{ii}.T12 = T12;
         plot(T12',ones(size(T12')),'LineWidth',6,'Color','k');
         set(gca,'xlim',xl,'Xtick',[],'Ytick',[]);
      else
         set(gcf,'Pointer','Arrow');
      end

   case 'Unmark'
      figure(gcf);
      set(gcf,'Pointer','FullCrossHair');
      posFig = get(gcf,'Position');
      k = waitforbuttonpress;
      p1 = get(gcf,'CurrentPoint');
      np1(1) = p1(1)/posFig(3);
      np1(2) = p1(2)/posFig(4);
      pyi = yi*posFig(4);
      plyi = lyi*posFig(4);
      if(np1(1) > xi-.05 & np1(1) < lxi+xi & np1(2)> yi & np1(2) < lyi+yi)
         if(np1(1) < xi)
             np1(1) = xi;
         end
         initialRect = [p1(1) pyi 0 plyi];
         fixedPoint = [p1(1) pyi];
         set(gcf,'Pointer','Arrow');
         finalRect = rbbox(initialRect,fixedPoint);
         t1 = (np1(1)-xi)/lxi;
         p2 = finalRect(3)/posFig(3);
         t2 = p2/lxi+t1;
         t2 = min(t2,1);
         t1 =  fix(t1*(i1-i0+1))*TSd{ii}.dt + i0*TSd{ii}.dt + TSd{ii}.t0;
         t2 =  fix(t2*(i1-i0+1))*TSd{ii}.dt + i0*TSd{ii}.dt + TSd{ii}.t0;
         T12 = subT(t1,t2,TSw{ii}.T12);
         [nSeg,dum] = size(T12);
         ich = find(TSw{ii}.chPlt,1);
         xl = get(TSw{ii}.ACh(ich),'xlim');
         xl = xl*TSw{ii}.tFac+TSw{ii}.t0Eq0*TSd{ii}.t0;
         axes(TSw{ii}.hMask);
         TSw{ii}.T12 = T12;
         if(isempty(T12))
            delete(findobj('Parent',TSw{ii}.hMask,'Type','line'));
         else
            plot(T12',ones(size(T12')),'LineWidth',6,'Color','k');
         end
         set(gca,'xlim',xl,'Xtick',[],'Ytick',[]);
      else
         set(gcf,'Pointer','Arrow');
      end

   case 'Zoom plot'
      figure(gcf);
      set(gcf,'Pointer','FullCrossHair');
      posFig = get(gcf,'Position');
      k = waitforbuttonpress;
      p1 = get(gcf,'CurrentPoint');
      np1(1) = p1(1)/posFig(3);
      np1(2) = p1(2)/posFig(4);
      pyi = yi*posFig(4);
      plyi = lyi*posFig(4);
      if(np1(1) > xi-.05 & np1(1) < lxi+xi & np1(2)> yi & np1(2) < lyi+yi)
         if(np1(1) < xi)
             np1(1) = xi;
         end
         initialRect = [p1(1) pyi 0 plyi];
         fixedPoint = [p1(1) pyi];
         set(gcf,'Pointer','Arrow');
         finalRect = rbbox(initialRect,fixedPoint);
         t1 = (np1(1)-xi)/lxi;
         p2 = finalRect(3)/posFig(3);
         t2 = p2/lxi+t1;
         t2 = min(t2,1);
         temp = i1-i0+1
         i1 =  fix(t2*temp) + i0;
         i0 =  fix(t1*temp) + i0;
         t = [i0:i1]*dt+t0;
         plotPage(t,yFac*TSdata{ii}(:,[i0:i1]),TSw{ii},TSd{ii},0);
         ich = find(TSw{ii}.chPlt,1);
         xl = get(TSw{ii}.ACh(ich),'xlim');
         xl = xl*TSw{ii}.tFac+TSw{ii}.t0Eq0*TSd{ii}.t0;
         axes(TSw{ii}.hMask);
         T12 = TSw{ii}.T12;
         plot(T12',ones(size(T12')),'LineWidth',6,'Color','k');
         set(gca,'xlim',xl,'Xtick',[],'Ytick',[]);
      else
         set(gcf,'Pointer','Arrow');
      end

    case 'Load bad rec'
        BadRecDir = [TSd{ii}.DataDir 'BR/'];
        if exist(BadRecDir,'dir')==0; mkdir(BadRecDir); end
        filterSpec = [ BadRecDir '*.bad'];
        if strcmp(comp(1:4),'PCWI'),
            filterSpec=strrep(filterSpec,'/','\');
        end
        [fileName,pathName] = uigetfile(filterSpec,'Bad record file to load');
        pathName=strrep(pathName,'\','/');
        if(pathName ~= 0)
            cfile = [pathName fileName ];
            [T12,dtBR] = rdBadRec(cfile);
            T12 = (T12.*dtBR); % T12 should be expressed in seconds
				    %  need to use sample rate used when
                                    %  BR file was written ... allow for dbn
        end
        ich = find(TSw{ii}.chPlt,1);
        xl = get(TSw{ii}.ACh(ich),'xlim');
        xl = xl*TSw{ii}.tFac+TSw{ii}.t0Eq0*TSd{ii}.t0;
        axes(TSw{ii}.hMask);
        TSw{ii}.T12 = T12;
        plot(T12',ones(size(T12')),'LineWidth',6,'Color','k');
        set(gca,'xlim',xl,'Xtick',[],'Ytick',[]);

   case 'Save bad rec'
      BadRecDir = [TSd{ii}.DataDir 'BR/'];
      if exist(BadRecDir,'dir')==0; mkdir(BadRecDir); end
      initFile = [ BadRecDir TSd{ii}.arrayID '.bad'];
      [fileName,pathName] = uiputfile(initFile,'Output marked segments');
      pathName=strrep(pathName,'\','/');
      if(pathName ~= 0)
        cfile = [pathName fileName ];
        %   bad record intervals are written in sample numbers ...
        t12 = round(TSw{ii}.T12/TSd{ii}.dt);
        wrBadRec(cfile,t12,TSd{ii}.dt,TSd{ii}.dataZero);
      end

   case 'Clear marks'
      TSw{ii}.T12 = [];
      delete(findobj('Parent',TSw{ii}.hMask,'Type','line'));

   case 'Reprocess'
      BadRecDir = [TSd{ii}.DataDir 'BR/'];
      if exist(BadRecDir,'dir')==0; mkdir(BadRecDir); end
      initFile = [ BadRecDir '*.bad'];
      redoMTR = 1;
      redoMMT = 1;
      useCM = 1;
      defSuffix = 'a';
      reProc = struct('MTR',redoMTR,'MMT',redoMMT,...
		'UseCM',useCM,'suffix',defSuffix,...
                'File',initFile,'Fig',0,'iFig',ii);
      reProc.Fig = reProcWin(reProc);

   case 'McohPlot'
     ii = TSfigInd(gcf);
     CohInd = zeros(TSd{ii}.nch,3);
     if length(TSc) >= ii
        if ~isempty(TSc{ii})
           h = findobj;
           if(~isempty(find(TSc{ii}.hFig==h)))
              delete(TSc{ii}.hFig)
              TSc{ii} = [];
           end
        end
     end
     NcohBand = 25;
     UpdateCohPlot = 0;
     [hFig,hAx] = CohMenu(TSd{ii}.ch_id',TSd{ii}.sta',ii,NcohBand);
     TScor = struct('CohInd',CohInd,'hFig',hFig,'hAx',hAx,'NcB',NcohBand);
     TSc{ii} = TScor;

   case 'SpecPlot'
     ii = TSfigInd(gcf);
     if length(TSp) >= ii
        if ~isempty(TSp{ii})
           h = findobj;
           if(~isempty(find(TSp{ii}.hFig==h)))
              delete(TSp{ii}.hFig)
              TSp{ii} = [];
           end
        end
     end
     Nwin = 8;
     UpdateSpecPlot = 0;
     [hFig,hAx] = PspecMenu(TSd{ii}.ch_id',TSd{ii}.sta',ii,Nwin);
     TSspec = struct('hFig',hFig,'hAx',hAx,'Nwin',Nwin);
     TSp{ii} = TSspec;

   case 'Save Y Limits'
      suffix = 'ylm';
      DataDir = [TSd{ii}.DataDir];
      if exist(DataDir,'dir')==0; mkdir(DataDir); end
      yLimSave = TSw{ii}.YLch;
      FilterSpec = [ DataDir '*.' suffix];
      [FileName,PathName] = uiputfile(FilterSpec,'Select Y limit file');
      cfile = [PathName FileName];
      eval(['save ' cfile ' yLimSave']); 

   case 'Load Y Limits'
      DataDir = [TSd{ii}.DataDir];
      FilterSpec = [ DataDir '*.ylm'];
      [FileName,PathName] = uigetfile(FilterSpec,'Select Y limit file');
      eval(['load -MAT ' PathName FileName]);
      TSw{ii}.YLch = yLimSave;
      for ic = 1:nch
         if TSw{ii}.chPlt(ic)
            yl = TSw{ii}.YLch(ic,1)+[-1,1]*TSw{ii}.YLch(ic,2);
            set(TSw{ii}.mCH(ic),'String',num2str(yl(1)));
            set(TSw{ii}.MCH(ic),'String',num2str(yl(2)));
            set(TSw{ii}.ACh(ic),'ylim',yl);
         end
      end
      rePltPage
end
dt=TSd{ii}.dt;
TSw{ii}.i0 = i0;
TSw{ii}.i1 = i1;
