function gpanel(config);
% intermediary function called from 'panelmaster' to control the plotting
% of panels. Revised for increased versatility, including: 
% variable no. of pages 
% variable no. of panels/page
% variable types of panels
% non-integer no. of days/panel 
% (e.g. 1.5 days/panel; 3 panels/pg = 4.5 days/pg, starting dd 123.4)  

% It calls getmat -t <dbname> <year> <start_dd> <days_per_page> <bins>
% via 'agetmat' to extract data from the database for the time range specified.
% It then calls 'aplotit' to individually produce panels.

global PANELFIG

% assign variables
nbins = str2num(PANELFIG.nbins);
config.panelcolors = 1;
if PANELFIG.blkwht, config.panelcolors = 0; end;   % if 1, use color, otherwise gray scale (inverse btn)
reduce_res = PANELFIG.resolution;      % yes=1,no=0;50% resolution in time and depth
days_per_panel = str2num(PANELFIG.daysperpanel);
panels_per_page = str2num(PANELFIG.panelsppg);    
if PANELFIG.mixed == 0
   days_per_page = panels_per_page * days_per_panel;
else
   days_per_page = days_per_panel;
end
dbname = PANELFIG.dbname;
dbpath = PANELFIG.default_path;
year_base = num2str(PANELFIG.year_base);
cruiseid = PANELFIG.cruiseID;
pg_range = [0 100];
totaldays = str2num(PANELFIG.totdays); 
totalpages = str2num(PANELFIG.nopgs); 

config.minclipval.pg           = 30   ;config.maxclipval.pg            = 100; 
config.minclipval.amp          = 10   ;config.maxclipval.amp           = 230; 
config.minclipval.sw           = 30   ;config.maxclipval.sw            = 260; 
config.minclipval.e            = -70  ;config.maxclipval.e             = 70;
config.minclipval.w            = -70  ;config.maxclipval.w             = 70;
config.minclipval.umeas        =  -7  ;config.maxclipval.umeas         = 7;
config.minclipval.vmeas        =  -7  ;config.maxclipval.vmeas         = 7;
config.minclipval.uabs_sm      =  -1  ;config.maxclipval.uabs_sm       = 1;
config.minclipval.uabs_g       =  -1  ;config.maxclipval.uabs_g        = 1;
config.minclipval.vabs_sm      =  -1  ;config.maxclipval.vabs_sm       = 1;
config.minclipval.vabs_g       =  -1  ;config.maxclipval.vabs_g        = 1;
config.minclipval.fabs_sm      =  -1  ;config.maxclipval.fabs_sm       = 1;
config.minclipval.fabs_g       =  -1  ;config.maxclipval.fabs_g        = 1;
config.minclipval.pabs_sm      =  -1  ;config.maxclipval.pabs_sm       = 1;
config.minclipval.pabs_g       =  -1  ;config.maxclipval.pabs_g        = 1; 

curdir = pwd;
cd(PANELFIG.outpath)
[data, config] = agetmat2(config); % get all data, all dates

if PANELFIG.flagged % load mask info only once, if needed.
   load pf
end
cd(curdir)

all_dates = data.dday';
nbins = config.nbins;
%config.astitle = 1;  THIS NEEDS WORK

%%%%%%% BEGINNING OF OUTER LOOP
for page = 1:totalpages
   
   start_dd = str2num(PANELFIG.begdd) + ((page-1)*days_per_page);
   
   for f = 1: panels_per_page
      if PANELFIG.mixed == 0
         plot_dates(f) = start_dd + (f-1)*days_per_panel;
      else
         plot_dates(f) = start_dd;
      end
   end
   
   % set up the graphics
   hhfig = figure;
   
   set(gcf,'color',[1 1 1]);
   set(gcf,'units','pixels','position', [268    67   512   605]);
   set(gcf,'defaulttextfontsize',10) %12
   set(gcf,'defaultaxesfontsize',10) % 12
   if PANELFIG.bold
      set(gcf,'defaulttextfontweight','bold')
      set(gcf,'defaultaxesfontweight','bold')
   else
      set(gcf,'defaulttextfontweight','normal')
      set(gcf,'defaultaxesfontweight','normal')
   end
   
   % revise for varying panels/pg
   pn = PANELFIG.panelsppg;
   switch pn
   case '1'
      c_bot=[0.1];
      c_hgt=[0.8];
   case '2'
      c_bot=[ 0.55 0.1];    % panel vertial positions
      c_hgt= 0.35;
   case '3'
      c_bot=[ 0.7 0.4 0.1];    
      c_hgt= 0.20;
   case '4'
      c_bot=[ 0.75 0.55 0.35 0.15];   
      c_hgt= 0.15;
   case '5'
      c_bot=[ 0.78 0.61 0.44 .27 0.1];   
      c_hgt= 0.12;
   end
   c_left = 0.12; %0.1
   c_wid = 0.88; %.91; % 0.9; 0.8;
   
   %%%% INNER LOOP
   for panel_num=1:panels_per_page
      
      today=plot_dates(panel_num);
      ii=find((all_dates >= today) & (all_dates <= today+days_per_panel));
      
      if (reduce_res==1)
         ii=ii(1:2:length(ii)); jj=(1:2:nbins);
      else
         jj=1:nbins; 
      end
      
      curax = axes('position', [ c_left c_bot(panel_num) c_wid c_hgt ], 'Box', 'on', 'YDir', 'reverse');
      
      %%% NEW APPROACH = using 'aplotit' for data display
      config.fn = PANELFIG.order{panel_num};
      config.cur_ax = curax;
      subdata = getfield(data, config.fn);
      if PANELFIG.flagged,
         [m, n] = size(subdata);
         subdata(PFLAG(1:m,1:n) ~= 0) = NaN;   
      end
      subdata = subdata(jj,ii);
      paneldata = struct(config.fn, subdata);
      paneldata.dday = all_dates(ii);
      paneldata.bins = data.bins(jj);
      config.xlim = [today,  today+days_per_panel];
      
      curdir = pwd;
      cd(PANELFIG.outpath)
      cfg = aplotit2(paneldata, config);
      cd(curdir)    
      
      if (panel_num==1) 
         switch pn
         case '1'
            yhgt = 1.06;
            ybelow = -.075;
         case '2'
            yhgt = 1.11;
            ybelow = -.15;
         case '3'
            yhgt = 1.3;
            ybelow = -.25;
         case '4'
            yhgt = 1.4;
            ybelow = -.35;
         case '5'
            yhgt = 1.5;
            ybelow = -.40;
         end
         
         if ~PANELFIG.omit_title
            text(0.5, yhgt, PANELFIG.title,'fontsize',12, ...
               'horizontalalignment', 'center', ...
               'verticalalignment', 'bottom');
         end
      end
      
      if ~(isempty(ii))
         text(-0.08, 0.5, 'Bins', ...
            'horizontalalignment', 'center', ...
            'verticalalignment', 'baseline', ...
            'rotation', 90.0);
      end
      
      if str2num(pn) >= 4 & str2num(pn) ~= panel_num & PANELFIG.mixed == 1
         set(curax,'XTickLabelMode','manual');
         set(curax,'XTickLabel', []); 
      end
      
      if str2num(pn) >= 4 & panel_num ~= 1 & PANELFIG.mixed == 0
         th = get(curax,'title');
         set(th,'visible','off');
      end
      
      % shift aplotit's colorbar position (given colors in use)
      if ~PANELFIG.blkwht & ~isempty(cfg.cbar_ax) % (inverse btn)
         [p] = get(cfg.cbar_ax,'position');
         set(cfg.cbar_ax,'position',[0.97*p(1)  c_bot(panel_num)  p(3)  c_hgt ]);
      end
      
   end %%%% of INNER FOR loop for panels (page finished)
   
   xlabeltext = ['Decimal days, ', year_base];
   text(0.5, ybelow, xlabeltext, ...
      'horizontalalignment', 'center', ...
      'verticalalignment', 'middle');
   
   if PANELFIG.datestamp, date_it; end;
   
   if PANELFIG.printfile % print 
      [PANELFIG.printfilefn, sfix] = strtok(PANELFIG.printfilefn, '.');
      if isempty(sfix), sfix = '.ps'; end;
      pfname = fullfile(PANELFIG.outpath, [PANELFIG.printfilefn, sfix]);
      orient tall; 
      if PANELFIG.blkwht & strcmp(sfix, '.ps'), print( '-dps2', '-append', '-painters', pfname); end;
      if PANELFIG.blkwht & strcmp(sfix, '.eps'), print( '-deps2', '-painters',  pfname); end;
      if strcmp(sfix, '.ps') & ~PANELFIG.blkwht, print( '-dpsc2', '-append', '-painters',  pfname); end;
      if strcmp(sfix, '.eps') & ~PANELFIG.blkwht, print( '-depsc2', '-painters', pfname); end;           
   end % printing
   
end %%%% of OUTER FOR loop for pages

% delete('amp.mat', 'depth.mat', 'e.mat', 'other.mat')
% delete('pg.mat', 'u.mat', 'v.mat') %new location needed to give program time ?
% delete('w.mat', 'pf.mat', 'sw.mat') %new format to stop warnings = strange

%%%%%%%%% EOF

