    function [TSo] = plotPage(t,data,TSw,TSd,newplt)
%   plotPage  plots a page of data in current window;
%   
%   Usage: [TSo] = plotPage(t,data,TSw,TSd,newplt)
%
%    TSw is data structure containing needed parameters, handles, ctc.
%    data is data to plot , t is time  for x axis, TDw is structure containing
%    data array properties, newplt = 1 to initialinze, 0 thereafter

    plotParams
    E_clr = [.9 0 0 ];
    H_clr = [0 .9 0 ];
    YL = [TSw.YLch(:,1)-TSw.YLch(:,2) TSw.YLch(:,2)+TSw.YLch(:,1)];
    YL=mkshort(YL);
    nchplt = sum(TSw.chPlt);
%    lxi = 0.55;xi  = 0.4;yi = 0.1;
    xi=plotpos(1);lxi=plotpos(3);
    yi=plotpos(2);
    dy=0.005;
    lyi = (plotpos(4)-dy*nchplt)/nchplt;        
    xl = [min(t),max(t)];
    fontFac = sum(TSw.chPlt)/10;
%
    if newplt
%      create new plotting axes
       for ic = TSd.nch:-1:1
         if TSw.chPlt(ic)
            TSw.ACh(ic) = axes('Position',[xi yi lxi lyi]);
%           scale buttons for individual channels
            SChPosM = [xi+lxi,yi,.03,lyi/2];psc1=[xi+lxi+0.033,yi,.065,lyi/2];
            SChPosP = [xi+lxi,yi+lyi/2,.03,lyi/2];psc2=[xi+lxi+0.033,yi+lyi/2,.065,lyi/2];
            yi = yi + lyi + dy;
            TSw.SChM(ic) = uicontrol(gcf,'Style','Pushbutton',...
             'String',' -  ',...
             'FontWeight','bold',...
             'Units','normalized',...
             'Position',SChPosM,...
             'FontUnits','normalized',...
             'FontSize',.6*fontFac,...
             'UserData',ic,...
             'Tag','zoomOutCh',...
             'BackgroundColor',bgc,...
             'ForegroundColor',fgc(2,:),...
             'Callback','plotTScbk');
%     set min for plotting channel ic
            TSw.mCH(ic) = uicontrol(gcf,'Style','edit',...
             'String',num2str(YL(ic,1)),...
             'Units','normalized',...
             'Position',psc1,...
             'Tag','set_mCH',...
             'BackgroundColor',bgc2,...
             'FontSize',8,...
             'FontWeight','normal',...
             'ForegroundColor',fgc(2,:),...
             'UserData',ic,...
             'Callback','plotTScbk');

            TSw.SChP(ic) = uicontrol(gcf,'Style','Pushbutton',...
             'String',' +  ',...
             'FontWeight','bold',...
             'Units','normalized',...
             'FontUnits','normalized',...
             'FontSize',.6*fontFac,...
             'Position',SChPosP,...
             'UserData',ic,...
             'Tag','zoomInCh',...
             'BackgroundColor',bgc,...
             'ForegroundColor',fgc(2,:),...
             'Callback','plotTScbk');
%     set max for plotting channel ic
            TSw.MCH(ic) = uicontrol(gcf,'Style','edit',...
             'String',num2str(YL(ic,2)),...
             'Units','normalized',...
             'Position',psc2,...
             'Tag','set_MCH',...
             'UserData',ic,...
             'BackgroundColor',bgc2,...
             'FontSize',8,...
             'FontWeight','normal',...
             'ForegroundColor',fgc(2,:),...
             'Callback','plotTScbk');     
         end
       end
    elseif max(TSw.chPlt ~= TSw.chPltOld)
       for ic = TSd.nch:-1:1
         if TSw.chPltOld(ic)
            delete(TSw.ACh(ic));
            delete(TSw.SChM(ic));
            delete(TSw.SChP(ic));
            set(TSw.mCH(ic),'visible','off');
            set(TSw.MCH(ic),'visible','off');
         end  
         if TSw.chPlt(ic)
            TSw.ACh(ic) = axes('Position',[xi yi lxi lyi]);
%           scale buttons for individual channels 
            SChPosM = [xi+lxi,yi,.03,lyi/2];psc1=[xi+lxi+0.033,yi,.065,lyi/2];
            SChPosP = [xi+lxi,yi+lyi/2,.03,lyi/2];psc2=[xi+lxi+0.033,yi+lyi/2,.065,lyi/2];
            yi = yi + lyi + dy;
            TSw.SChM(ic) = ...    
             uicontrol(gcf,'Style','Pushbutton','String',' -  ',...
             'Units','normalized',... 
             'FontWeight','bold',...
             'FontUnits','normalized',...
             'FontSize',.6*fontFac,...
             'Position',SChPosM,... 
             'UserData',ic,... 
             'Tag','zoomOutCh',... 
             'BackgroundColor',bgc,... 
             'ForegroundColor',fgc(2,:),... 
             'Callback','plotTScbk');

%     set min for plotting channel ic
            TSw.mCH(ic) = uicontrol(gcf,'Style','edit',...
             'String',num2str(YL(ic,1)),...
             'Units','normalized',...
             'Position',psc1,...
             'Tag','set_mCH',...
             'BackgroundColor',bgc2,...
             'FontSize',8,...
             'FontWeight','normal',...
             'ForegroundColor',fgc(2,:),...
             'UserData',ic,'visible','on',...
             'Callback','plotTScbk');

            TSw.SChP(ic) = ...
             uicontrol(gcf,'Style','Pushbutton','String',' +  ',...
             'Units','normalized',...
             'FontWeight','bold',...
             'FontUnits','normalized',...
             'FontSize',.6*fontFac,...
             'Position',SChPosP,...
             'UserData',ic,...
             'Tag','zoomInCh',...
             'BackgroundColor',bgc,...
             'ForegroundColor',fgc(2,:),...
             'Callback','plotTScbk');

%     set max for plotting channel ic
            TSw.MCH(ic) = uicontrol(gcf,'Style','edit',...
             'String',num2str(YL(ic,2)),...
             'Units','normalized',...
             'Position',psc2,...
             'Tag','set_MCH',...
             'UserData',ic,'visible','on',...
             'BackgroundColor',bgc2,...
             'FontSize',8,...
             'FontWeight','normal',...
             'ForegroundColor',fgc(2,:),...
             'Callback','plotTScbk');

         end
       end 
    end

    nchMax = max(find(TSw.chPlt));
    if newplt | max( TSw.chPlt ~= TSw.chPltOld )
       for ic = TSd.nch:-1:1
          if TSw.chPlt(ic)
             axes(TSw.ACh(ic));
             dbar = 0.;
             if TSw.centered
                inan=find(isnan(data(ic,:))==0);
                if isempty(inan)==0
                   dbar = mean(data(ic,inan)); 
                end
             end
             TSw.PCh(ic) = plot(t,data(ic,:)-dbar);
             if upper(TSd.ch_id(ic,1)) == 'E'
                clr = E_clr;
             else
                clr = H_clr;
             end
             set(TSw.ACh(ic),'fontweight','demi',...
			'FontUnits','normalized',...
			'FontSize',.3*fontFac,'ytick',[]);
             set(TSw.PCh(ic),'Color',clr);
             yl = TSw.YLch(ic,1)+[-1,1]*TSw.YLch(ic,2);
           txt = text(.1,.3,TSd.sta(ic,:),'units','normalized',...
			'Fontweight','demi', ...
			'FontUnits','normalized',...
			'Parent',TSw.ACh(ic),...
			'FontSize',.3*fontFac);
     set (txt,'Interpreter','none')
             set(TSw.ACh(ic),'xlim',xl,'ylim',yl);
             set(TSw.ACh(ic),'ylabel',text('String',TSd.ch_id(ic,:)));
             ylH = get(TSw.ACh(ic),'ylabel');
             set(ylH,'String',TSd.ch_id(ic,:),...
			'Fontweight','demi', ...
			'FontUnits','normalized',...
			'FontSize',.25*fontFac);
             if (ic ~= nchMax)
                set(gca,'XtickLabelMode','manual','XtickLabel',' ');
             else
                set(get(TSw.ACh(ic),'xlabel'),'String',TSw.tUnits,...
                       'FontWeight','demi','FontUnits','normalized',...
                       'FontSize',.25*fontFac);
             end
          end
       end
    else
       for ic = TSd.nch:-1:1
          if TSw.chPlt(ic)
             dbar = 0.;
             if TSw.centered
                inan=find(isnan(data(ic,:))==0);
                if isempty(inan)==0
                   dbar = mean(data(ic,inan)); 
                end
             end
             if TSw.PCh(ic)~=0,
                set(TSw.PCh(ic),'Xdata',t,'Ydata',data(ic,:)-dbar);
             else
                axes(TSw.ACh(ic));
                TSw.PCh(ic) = plot(t,data(ic,:)-dbar);
             end
             set(TSw.ACh(ic),'fontweight','demi',...
                       'FontUnits','normalized',...
                        'FontSize',.3*fontFac);
             yl = TSw.YLch(ic,1)+[-1,1]*TSw.YLch(ic,2); 
           txt = text(.1,.3,TSd.sta(ic,:),'units','normalized',...
			'Fontweight','demi', ...
			'FontUnits','normalized',...
			'Parent',TSw.ACh(ic),...
			'FontSize',.3*fontFac);
     set (txt,'Interpreter','none')
             set(TSw.ACh(ic),'ylim',yl);
             ylH = get(TSw.ACh(ic),'ylabel');
             set(ylH,'String',TSd.ch_id(ic,:),...
			'Fontweight','demi', ...
			'FontUnits','normalized',...
			'FontSize',.30*fontFac);
             set(TSw.ACh(ic),'xlim',xl);
             if upper(TSd.ch_id(ic,1)) == 'E'
                clr = E_clr;
             else
                clr = H_clr;
             end
             set(TSw.PCh(ic),'Color',clr);
             if (ic == nchMax)
                set(get(TSw.ACh(ic),'xlabel'),'String',TSw.tUnits);
             end
          end
       end
     end 
     ich = min(find(TSw.chPlt));
     xl = get(TSw.ACh(ich),'xlim');
     xl = xl*TSw.tFac+TSw.t0Eq0*TSd.t0;
     axes(TSw.hMask);
     plot(TSw.T12',ones(size(TSw.T12')),'LineWidth',6,'Color','k');
%     set (txt,'Interpreter','none')
     set(gca,'xlim',xl,'Xtick',[],'Ytick',[])

     TSw.chPltOld = TSw.chPlt;
     TSo = TSw;
     return
