function [hTSw,TSw] = plotTSwin(TSd,plotStart)
%   initialize plotting window
%   Usage:  [hTSw,TSw] = plotTSwin(TSd,plotStart)
    hTSw = figure('Interruptible','on','Position',plotStart.rect,...
                  'menubar','none','numbertitle','off');
    plotParams % Colors: bgc,bgc2,fgc Frame: ixf,iyf,xframe,yframe
               % TS plots: plots
    dtsp=(1-xframe)*0.1;
    zbarpos=[ plotpos(1) 0.97 plotpos(3) 0.02]; % was [0.4 0.97 0.55 0.02]
% Axis Scaling on upper menu %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    hTaxis = uimenu('Parent',hTSw,'Label','Axis Scaling/Centering');
	uimenu(hTaxis,'Label','Relative to Start     ',...
		'checked','off','Tag','t0','Callback','plotTScbk',...
		'UserData',0);
        hTunits = uimenu(hTaxis,'Label','Time Units',...
		'Tag','Time units');
              uimenu(hTunits,'Label','Seconds',...
                'checked','off','Tag','tUnits','callback','plotTScbk',...
		'UserData',1);
              uimenu(hTunits,'Label','Minutes',...
                'checked','off','Tag','tUnits','callback','plotTScbk',...
		'UserData',60);
              uimenu(hTunits,'Label','Hours',...
                'checked','off','Tag','tUnits','callback','plotTScbk',...
		'UserData',3600);
              uimenu(hTunits,'Label','Days',...
                'checked','off','Tag','tUnits','callback','plotTScbk',...
		'UserData',86400);
	hYunits = uimenu(hTaxis,'Label','Y axes factor',...
		'Tag','Y units');
              uimenu(hYunits,'Label','.001',...
                'checked','off','Tag','yUnits','callback','plotTScbk',...
		'UserData',.001);
              uimenu(hYunits,'Label','1',...
                'checked','off','Tag','yUnits','callback','plotTScbk',...
		'UserData',1);
              uimenu(hYunits,'Label','10',...
                'checked','off','Tag','yUnits','callback','plotTScbk',...
		'UserData',10);
              uimenu(hYunits,'Label','1000',...
                'checked','off','Tag','yUnits','callback','plotTScbk',...
		'UserData',1000);   
          uimenu(hTaxis,'Label','Set unif. y limit',...
               'Tag','Uniform ylim','Callback','plotTScbk');
          uimenu(hTaxis,'label','Centered       ','Tag','Centered',...
                                'checked','on','CallBack','plotTScbk');
          uimenu(hTaxis,'label','Save Y limits  ','Tag','Save Y Limits',...
                                'checked','off','CallBack','plotTScbk');
          uimenu(hTaxis,'label','Load Y limits  ','Tag','Load Y Limits',...
                                'checked','off','CallBack','plotTScbk');
% end of Axis Scaling/Centering %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Mark Data Segments Menu (now on UPPER menu)
    hMDS= uimenu(hTSw,'Label','Mark Data Segments');
          uimenu(hMDS,'label','Mark data      ','Tag','Mark','Callback','plotTScbk');
          uimenu(hMDS,'label','Unmark data    ','Tag','Unmark','Callback','plotTScbk');
          uimenu(hMDS,'label','Zoom plot      ','Tag','Zoom plot','Callback','plotTScbk');
          uimenu(hMDS,'label','Clear marks    ','Tag','Clear marks','Callback','plotTScbk');
          uimenu(hMDS,'label','Load bad record','Tag','Load bad rec','Callback','plotTScbk');
          uimenu(hMDS,'label','Save bad record','Tag','Save bad rec','Callback','plotTScbk');
         % uimenu(hMDS,'label','Reprocess data ','Tag','Reprocess','Callback','plotTScbk');
    hMask = axes('Position',zbarpos,'Ytick',[],'Xtick',[]); % keep for now
                                                            % get rid of later
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Coherence on Upper Menu
    hTaxis = uimenu('Parent',hTSw,'Label','Spectra');
	uimenu(hTaxis,'Label','Plot Coherence',...
		'checked','off','Tag','McohPlot','Callback','plotTScbk',...
		'UserData',0);
	uimenu(hTaxis,'Label','Plot Spectrum',...
		'checked','off','Tag','SpecPlot','Callback','plotTScbk',...
		'UserData',0);
% end of Coherence %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    MCH = zeros(TSd.nch,1);
    mCH = zeros(TSd.nch,1);
    i0 = plotStart.i0;
    di = plotStart.di;
    tUnits = plotStart.tUnits;
    tFac = plotStart.tFac;
    eval(['set(findobj(' '''Label''' ',tUnits),' '''checked''' ',' '''on''' ')'])
    t0Eq0 = plotStart.t0Eq0;
    if(t0Eq0)
      set(findobj('Tag','t0'),'checked','on')
    end
    T12 = plotStart.T12;
    yFac = plotStart.yFac;
    eval(['set(findobj(' '''Label''' ',num2str(yFac)),' '''checked''' ',' '''on''' ')'])
    nPw = plotStart.nPw;
    chPlt = ones(TSd.nch,1);
    chPltOld = ones(TSd.nch,1);
    centered = plotStart.centered;
    if centered==0, set(findobj('Tag','Centered'),'checked','off');end
    page = plotStart.page;
    npage = ceil(TSd.npts/nPw);
    nch = TSd.nch;
% YL is range NOW on right side of plots
    for ic = 1:nch
       YLch(ic,1) = (1-centered) * mean(TSd.range(ic,1:2));
       YLch(ic,2) = (TSd.range(ic,2)-TSd.range(ic,1))/2;
       if(TSd.SysTF.PhysU)
          YLch(ic,2) = YLch(ic,2)*TSd.range(ic,3);
       end
    end    
    YL = [YLch(:,2)-YLch(:,1) YLch(:,2)+YLch(:,1)];
    YL = mkshort(YL);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    ACh = zeros(nch,1);
    PCh = zeros(nch,1); 
    SChM = zeros(nch,1); 
    SChP = zeros(nch,1); 
%%%%%%%%%%%%%%%%%%% double frame for control panel %%%%%%%%%%%%%%%%%%%%%%%%%%%
    uicontrol(hTSw,'Style','frame','Units','normalized',...
              'Position',[ixf iyf xframe yframe],...
              'BackgroundColor',bgc);
    dwin=min(0.02*xframe,0.02*yframe);
    uicontrol(hTSw,'Style','frame','Units','normalized',...
             'Position',[ixf+dwin iyf+dwin xframe-2*dwin yframe-2*dwin],...
             'BackgroundColor',bgc);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% UPPER BOTTONS, 3 fields %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    ubn=3;
    ubdwin=max(0.5*dwin,0.002);
    ubwidth = (xframe-2*dwin-(ubn+1)*ubdwin)/ubn;
    ubheight= max(0.05,0.05*yframe);
    ubxpos=ixf+dwin+ubdwin;
    ubypos=iyf+yframe-2*dwin-ubdwin-ubheight;
    din=0.002;
% #Pts frame
    uicontrol(hTSw,'Style','frame','Units','normalized',...
             'Position',[ubxpos ubypos 2*ubwidth+ubdwin ubheight],...
             'BackgroundColor',bgc);
% #Pts text
    uicontrol(hTSw,'Style','text','String','# Pts',...
             'Units','normalized','Position',...
             [ubxpos+din ubypos+din ubwidth ubheight-2*din],...
             'HorizontalAlignment','Center',...
             'FontUnits','normalized',...
             'FontSize',.5,...
             'FontWeight','demi',...
             'BackgroundColor',bgc2,...
             'ForegroundColor',fgc(2,:));
% #Pts edit
    ubxpos=ubxpos+ubwidth+ubdwin;
    NPtWin = uicontrol(hTSw,'Style','edit',...
             'String',num2str(nPw),...
             'Tag','chnumpt',...
             'Units','normalized',...
             'Position',[ubxpos+din ubypos+din ubwidth-2*din ubheight-2*din],...
             'FontUnits','normalized',...
             'FontSize',.5,...
             'BackgroundColor',bgc2,...
             'ForegroundColor',fgc(2,:),...
             'Callback','plotTScbk');
% Replot
    ubxpos=ubxpos+ubwidth+ubdwin;
    uicontrol(hTSw,'Style','Pushbutton',...
              'Units','normalized',...
              'Position',[ubxpos ubypos ubwidth ubheight],...
              'Tag','replot',...
              'String','Replot',...
              'FontUnits','normalized',...
              'FontSize',.5,...
              'FontWeight','demi',...
              'Callback','plotTScbk',...
              'BackgroundColor',bgc2,...
              'ForegroundColor',fgc(2,:));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% "-" Reset "+" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    ubxpos=ixf+dwin+ubdwin;
    ubypos=iyf+yframe-2*dwin-2*ubdwin-2*ubheight;
% -
    uicontrol(hTSw,'Style','Pushbutton','String','-',...
             'Units','normalized',...
             'Position',[ubxpos ubypos ubwidth ubheight],...
	     'Tag','zoomOut',...
             'FontUnits','normalized',...
             'FontSize',.6,...
             'FontWeight','bold',...
             'BackgroundColor',bgc,...
             'ForegroundColor',fgc(2,:),...
             'Callback','plotTScbk');
% Reset
    ubxpos=ubxpos+ubwidth+ubdwin;
    uicontrol(hTSw,'Style','Pushbutton','String','Reset',...
             'Units','normalized',...
             'Position',[ubxpos ubypos ubwidth ubheight],...
             'Tag','reset',...
             'FontUnits','normalized',...
             'FontSize',.5,...
             'FontWeight','demi',...
             'BackgroundColor',bgc,...
             'ForegroundColor',fgc(2,:),...
             'Callback','plotTScbk');
% +
    ubxpos=ubxpos+ubwidth+ubdwin;
    uicontrol(hTSw,'Style','Pushbutton','String','+',...
             'Units','normalized',...
             'Position',[ubxpos ubypos ubwidth ubheight],...
	     'Tag','zoomIn',...
             'FontUnits','normalized',...
             'FontSize',.6,...
             'FontWeight','bold',...
             'BackgroundColor',bgc,...
             'ForegroundColor',fgc(2,:),...
             'Callback','plotTScbk');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%% LOWER buttons in control panel %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%% now frame adjustable %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    lbn=3; % number of lower buttons
    lbdwin=max(0.5*dwin,0.002);
    lbwidth = (xframe-2*dwin-(lbn+1)*lbdwin)/lbn;
    lbheight= max(0.05,0.05*yframe);
    lbxpos=ixf+dwin+lbdwin;
% Prev
    uicontrol(hTSw,'Style','Pushbutton',...
              'Units','normalized',...
              'Position',[lbxpos iyf+2*dwin lbwidth lbheight],...
              'String','Prev',...
              'Tag','PrevPage',...
              'FontUnits','normalized',...
              'FontSize',.4167,...
              'FontWeight','demi',...
              'Callback','plotTScbk',...
              'BackgroundColor',bgc2,...
              'ForegroundColor',fgc(2,:));
% Next
    lbxpos=lbxpos+lbwidth+lbdwin;
    uicontrol(hTSw,'Style','Pushbutton', ...
              'Units','normalized',...
              'Position',[lbxpos iyf+2*dwin lbwidth lbheight],...
              'String','Next',...
              'Tag','NextPage',...
              'FontUnits','normalized',...
              'FontSize',.4167,...
              'FontWeight','demi',...
              'Callback','plotTScbk',...
              'BackgroundColor',bgc2,...
              'ForegroundColor',fgc(2,:));
% Quit
    lbxpos=lbxpos+lbwidth+lbdwin;
    uicontrol('Style','Pushbutton',...
              'Units','normalized',...
              'Position',[lbxpos iyf+2*dwin lbwidth lbheight],...
              'String','Quit',...
              'Tag','Quit',...
              'FontUnits','normalized',...
              'FontSize',.4167,...
              'FontWeight','demi',...
              'Callback','plotTScbk',...
              'BackgroundColor',bgc2,...
              'ForegroundColor',fgc(2,:));
% Goto
    lbxpos=ixf+dwin+lbdwin;lbypos=iyf+lbheight+dwin+2*lbdwin;
    uicontrol(gcf,'Style','text',...
             'String',['Go';'to'],...
             'Units','normalized',...
             'Position',[lbxpos lbypos lbwidth lbheight],...
             'FontUnits','normalized',...
             'FontSize',.4167,...
             'FontWeight','demi',...
             'BackgroundColor',bgc,...
             'ForegroundColor',fgc(2,:));
    lbxpos=lbxpos+lbwidth+lbdwin;
    h_gotopage =uicontrol(gcf,'Style','edit',...
             'String',num2str(page),...
             'Units','normalized',...
             'Position',[lbxpos lbypos lbwidth lbheight],...
             'Tag','gotoPage',... 
             'BackgroundColor',bgc2,...
             'ForegroundColor',fgc(2,:),...
             'FontUnits','normalized',...
             'FontSize',.4167,...
             'FontWeight','demi',...
             'Callback','plotTScbk');
    lbxpos=lbxpos+lbwidth+lbdwin;
    ss = sprintf('page#\n %d/%d',page,npage);
    PAGE = uicontrol(gcf,'Style','text',...
           'String',ss,...
           'Units','normalized',...
           'Position',[lbxpos lbypos lbwidth lbheight],...
           'FontWeight','demi',...
           'BackgroundColor',bgc,...
           'ForegroundColor',fgc(2,:));
%%%%%%%%%%%%%%%%%%%%% Channel BUTTONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    [ncol,nrow,staN]=NcolNrow(TSd.sta,TSd.ch_id);
% space for the buttons in control panel
    cby=yframe-2*(ubheight+ubdwin)-2*(lbheight+lbdwin);
    cbx=xframe-4*dwin;
    cbh=(cby-(max(nrow+1)+1)*ubdwin)/max(nrow+1);
    cbw=cbx/ncol;
    cbh=min(cbh,0.05);
    cbw=min(cbw,lbwidth); % Lana, Aug 2005, was min(cbw,0.05);
% Channel buttons are centered
    xpos=ixf+2*dwin+(cbx-cbw*ncol-ubdwin*(ncol-1))/2;
    ypos=iyf+yframe-2*(lbheight+lbdwin)-(cby-cbh*max(nrow)-ubdwin*(max(nrow)-1))/2;
%    
    for icol = 1:ncol
      ypos=iyf+yframe-2*(lbheight+lbdwin)-(cby-cbh*max(nrow)-ubdwin*(max(nrow)-1))/2;
      uicontrol(hTSw,'Style','Text',...
             'String',staN(icol,:),...
             'Units','normalized','Position',[xpos ypos cbw cbh],...
             'BackgroundColor',bgc,...
             'ForegroundColor',fgc(2,:),...
             'FontUnits','normalized',...
             'FontSize',.4,...
             'FontWeight','demi');
     chid=[];ic=[];
     for k=1:nch
      if TSd.sta(k,:)==staN(icol,:),chid=[chid;TSd.ch_id(k,:)];ic=[ic,k];end 
     end   
     for irow=1:nrow(icol)
       ypos=ypos-cbh-ubdwin;
       uicontrol(hTSw,'Style','PushButton',...
             'String',chid(irow,:),...
             'Units','normalized','Position',[xpos ypos cbw cbh],...
             'Tag','chonoff','ToolTipString','Channel plot ON/OFF',...
             'UserData',ic(irow),...
             'BackgroundColor',bgc,...
             'ForegroundColor',fgc(2,:),...
             'FontUnits','normalized',...
             'FontSize',.4,...
             'FontWeight','demi',...
             'CallBack','plotTScbk');
     end
     xpos=xpos+ubdwin+cbw;
    end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   plotting window structure:
%   Structure TSw     (Time series plot description)
%    ACh(nch)  axis handles for each channel
%    PCh(nch)  line handles  for each channel
%    SChM(nch),SChP(nch)  handles  for channel zoom controls
%    YLch(nch,2)   [center,range]    defines ylim for each channel
%    mCH, MCH are handles for max/min windows
%    nPw    # of points to be plotted in one frame
%    i0        data array index for first point plotted in window
%    di        data index skip factor  (1 = no skip)
%    i1        data array index for last point plotted in window
%    chPlt, chPltOld     0/1 arrays indicating which channels
%                are active (i.e., to be plotted)
%    centered  indicator for centered TS
%    PAGE      handle to page number indicator
%    page      page number
%    npage     number of pages
%  NOTE: many of the fields are blank until the first call to plotPage
     TSw = struct('ACh',ACh,'PCh',PCh,'YLch',YLch,'mCH',mCH,'MCH',MCH,...
           'nPw',nPw,'i0',i0,'i1',nPw,'di',di,'chPlt',chPlt,...
           'chPltOld',chPltOld,'centered',centered,'PAGE',PAGE,...
           'page',page,'npage',npage,'SChP',SChP,'SChM',SChM,...
           't0Eq0',t0Eq0,'tUnits',tUnits,'tFac',tFac,'yFac',yFac,...
           'hMask',hMask,'T12',T12);
