function gplotp(arg1);

if nargin ~= 1,
  help gplotp
  error('must set single input argument structure, cfg')
end


%here are the defaults
cfg.nbins = 60;         % maximum number of bins
cfg.icolor = 1;         % if 1, use color, otherwise gray scale
cfg.reduce_res = 0;     % yes=1,no=0;50% resolution in time and depth
cfg.d_step = 1;
cfg.t_step = 1;
cfg.days_per_panel = 1; % number of days to plot/panel; default is 2
                             % there are 4 panels per page
cfg.printflag = 1;      % if 1, print to file.
cfg.breakpoints = [0.2 0.3 0.8 0.9]; % suitable for pg
cfg.variable = 'pg';
cfg.pg_min = 0;         % pg mask
cfg.use_flags = 0;      % set to 1 to mask with all flags

% Transfer input parameters, overriding defaults.
fn = fieldnames(arg1);
for ii = 1:length(fn)
   fname = fn{ii};
   cfg = setfield(cfg, fname, getfield(arg1, fname));
end
if cfg.reduce_res,
   cfg.d_step = 2;
   cfg.t_step = 2;
end

titles.pg = 'Percent Good';
titles.sw = 'Spectral Width';
titles.amp = 'AGC';
titles.uabs = 'U absolute';
titles.vabs = 'V absolute';
titles.u    = 'U relative';
titles.v    = 'V relative';
titles.w    = 'W';
titles.e    = 'Error velocity';
% ...add more later; or change to use cfg.title instead


cruiseid = cfg.comment;  %using the name "comment" in gplogadcp

nbins = cfg.nbins;  % may be adjusted by the program
range = [cfg.MIN cfg.MAX];


% not a free parameter at present.
panels_per_page = 4;     % set as an integer >=1 (for number of panels/page)
days_per_page = panels_per_page * cfg.days_per_panel;

% extract data with getmat; clean up workspace

time_specs = [' ',num2str(cfg.year_base),' ',num2str(cfg.start_dd),...
           ' ',num2str(days_per_page)];

gm_opt = '';
if strcmp(cfg.variable, 'sw'), gm_opt = '-s'; end


cmd = ['!getmat -t ', gm_opt, ' ', cfg.dbpath, cfg.dbname, time_specs];
cmd = [cmd, ' ', int2str(nbins)]
eval(cmd)

load other
all_date=DAYS;


disp(sprintf('Bins requested: %d, available: %d.\n', nbins, NBINS))
nbins=min(nbins, NBINS);  %nbins was set earlier, NBINS is from the data
bins=[1:nbins]';


var_array = load_file(cfg.variable, bins);
if cfg.pg_min,
   load pg
   var_array(PGOOD(bins,:) < cfg.pg_min) = NaN;
end
if cfg.use_flags,
   load pf
   var_array(PFLAG(bins,:) ~= 0) = NaN;
end


clear DAYS IBLKPRF LAT LGB LON NBINS NPRFS TIME PGOOD PFLAG
% delete amp.mat depth.mat e.mat other.mat
% delete  pg.mat u.mat v.mat w.mat pf.mat


%now plot it ==================================================
plot_dates = [cfg.start_dd:cfg.days_per_panel:all_date(end-5)];
   % (Don't start a panel if there is nothing to plot.)

% set up the graphics
clf

% Ticks on colorbar every 10 or 20 units:
%if diff(range) <= 100, tl = 10; else tl = 20; end
l10r = log10(diff(range)); l10int = floor(l10r); l10m = mod(l10r, 1);
if l10m > log10(5), ttt = 10;
elseif l10m > log10(2), ttt = 5;
else ttt = 2;
end
tl = ttt * 10^(l10int-1)
colorticks = (tl*ceil(range(1)/tl)):tl:(tl*floor(range(2)/tl));


position_contour = [.2 .73 .65 .163 ];   % size same as ref.layer plots
c_left = position_contour(1);
c_bot = [position_contour(2) .53 .29 .09];    % panels' vertical positions
c_wid = position_contour(3);
c_hgt = position_contour(4);
xtick_str = ['XTickLabel'];
ytick_str = ['YTickLabel'];


ncmap=35;

if (cfg.icolor == 1)
   h = color_map(ncmap, cfg.breakpoints);
else
   hgray =  table1(...
           [[0 .01 .15 .28 .3 .31 .78 .82 1]' ...
           [0 .01 .35 .4 .52 .55 .6 .7 1]'],...
           (1:ncmap)'/ncmap);
   h = [hgray hgray hgray];
end


% set up arrays for colorbar
x = 0:1;
y = range(1) + (0:0.01:1.01)*diff(range);% :(range(2)+1);    % +1 because pcolor does not
               %   use last row, column
z = [y.' y.'];
z = z(:,ones(1,length(x)));

psfile = sprintf('%s%03d.ps', cfg.variable, round(plot_dates(1)));
                                                           % start day

for panel_num = 1:length(plot_dates)


   today = plot_dates(panel_num);
   ii = find((all_date >= today) & (all_date <= today+cfg.days_per_panel));

   ii = ii(1:cfg.t_step:length(ii));
   jj=(1:cfg.d_step:nbins);

   % make image for this panel  ------------------------------
   position_contour = [ c_left c_bot(panel_num) c_wid c_hgt ];
   axes('position', position_contour, 'Box', 'on', 'YDir', 'reverse');
   axis([today  today+cfg.days_per_panel 0 (nbins+1)])
   hold on
   colormap(h)
   if (isempty(ii))
      fprintf('no data in time range %7.4f to %7.4f\n',...
        today,today+cfg.days_per_panel)
   else
      pcolor(all_date(ii), bins(jj), var_array(jj, ii));
      shading flat
   end
   caxis(range)

   %changed to make xticklabels follow xtick being set (automatically)
   if (cfg.days_per_panel>=1)
      xticks = today:0.2:today+cfg.days_per_panel;
      set(gca,'xtick',xticks);
   end
   %now do the vertical
   yticks = 0:10:nbins;
   set(gca,'ytick',yticks);
   set(gca, 'TickDir', 'out');
   ylabel(['bin number'])

   % add title  and color bar to top of first plot
   if (panel_num==1)
      title([cruiseid,': ', getfield(titles, cfg.variable)]);

      % now colorbar
      h1 = axes('position',[.87 .730 .02 .163], ...
         'Box','on','YLim',range );
      hold on
      colormap(h)
      pcolor(x,y,z)
      shading flat
      caxis(range)
      set(h1, 'XTick', [], 'YTick', colorticks);
      set(h1, 'TickDir', 'out', 'TickLength', [0.02 0.02])
      set(h1, 'YAxisLocation', 'right');
      nct = length(colorticks);
      ct = get(h1, 'YTickLabel');
      ct(2:(end-1), :) = ' ';
      colorticks
      if mod(nct, 2)
         if colorticks((nct+1)/2) == 0
            ct((nct+1)/2, 1) = '0';
         end
      end
      set(h1, 'YTickLabel', ct);
   end

   % add x-axis label to bottom panel, and print to file
   if (panel_num == length(plot_dates))
      xlabel([num2str(cfg.year_base),' decimal days']);

      orient('tall')
      date_it('UH ADCP');
      if (cfg.printflag == 0)
         disp('not saving figure to disk: do that manually if desired')
      else
         disp(['printing to ', psfile])
         if cfg.icolor == 1,
            pdev = '-dpsc';
         else
            pdev = '-dps';
         end
         print(pdev, psfile);
      end
   end
end


%----------------------------------------------------------------------
function h = color_map(ncmap, breakpoints)
% note: breakpoints must be row vector of 4 numbers between 0 and 1
   allindex= [1, round(ncmap * breakpoints), ncmap]';
   r=table1([allindex [0 0 0 1 1 .6]'],[1:ncmap]);
   g=table1([allindex [0 0 1 1 0 0]'],[1:ncmap]);
   b=table1([allindex [.6 1 1 0 0 0]'],[1:ncmap]);
   h = [r g b];


%-----------------------------------------------------------------------
function x = load_file(var, bins)

fname.pg = 'pg';      vname.pg = 'PGOOD';
fname.amp = 'amp';    vname.amp = 'AMP';
fname.sw = 'sw';      vname.sw = 'SPECTRAL_WIDTH';
fname.u = 'u';        vname.u = 'U';
fname.uabs = 'u';     vname.uabs = 'U';
fname.v = 'v';        vname.v = 'V';
fname.vabs = 'v';     vname.vabs = 'V';
fname.w = 'w';        vname.w = 'W';
fname.e = 'e';        vname.e = 'E';
% We can add more later...
load(getfield(fname, var));
x = []; % might help the parser by initializing x
eval(['x = ', getfield(vname, var), ';']);
x = x(bins,:);

nbins = length(bins);

if strcmp(var, 'uabs')
   x = x + U_SHIP(ones(1, nbins), :);
elseif strcmp(var, 'vabs')
   x = x + V_SHIP(ones(1, nbins), :);
end

