function stnundpl(stn, udw, type, label, diff, kind, b1, bn)
%
% Usage:  stnundpl(stn,udw,type,label,diff,kind,b1,bn)
%
% Function stnudwpl.m plots the mean and standard deviation of
% on station and underway data of variables such as U, V, W,
% ERROR_VEL, AMPLITUDE and SPECTRAL_WIDTH.
%
% The script file STN_UDW.M can be used to set the MAT file names and
% call this funciton.  The input parameters are:
%
%    stn -- on station data array 'pstat'
%    udw -- underway data array 'pstat'
%    type -- character string of data type name
%    label -- data label input from PROFSTAT.CNT
%    diff  -- difference
%    kind -- 0 means plot U or V or W or ERROR_VEL which is in cm/s,
%         -- 1 means plot AMP or SPECTRAL_WIDTH which has no unit,
%         -- 2 means PERCENT_GOOD, which runs from 0 to 100.
%    b1 and bn -- optional. If they are given, data will be plotted
%                 from bin number b1 to bn. Otherwise, data will be
%                 plotted from first bin to the last.
%
% To set the depth axis, declare a global variable MAX_DEPTH and set
% it to the maximum (positive) depth desired; minimum will be zero.
% To return to auto depth scaling, set MAX_DEPTH = [];
%----------------------------------------------------------------------
global j_npts j_depth j_mean j_std outfile sub_title nplot MAX_DEPTH

[m_stn n_stn] = size(stn);
[m_udw n_udw] = size(udw);
if (nargin == 8)            % plot bins from b1 to bn by default
  m_stn = bn;
  m_udw = bn;
else
  b1 = 1;
end
plotwhat = (length(stn) ~= 0) + (length(udw) ~= 0);

meanlab = ['mean ',type];
stdlab = ['std_dev ',type];
if (kind == 0)
  meanlab = [meanlab,' (cm/s)'];
  stdlab = [stdlab,' (cm/s)'];
  if(length(stn) ~= 0)
    stn(:,j_mean:j_std) = stn(:,j_mean:j_std)*100;
  end
  if (length(udw) ~= 0)
    udw(:,j_mean:j_std) = udw(:,j_mean:j_std)*100;
  end
end


if (diff == 0)
   TITLE = label;
else
   TITLE = [label,' diff: ',num2str(diff)];
end

if rem(nplot,4) == 0
  figure
  orient tall
end

subplot(2,2,1+rem(nplot,4)),
nplot = nplot+1;
if plotwhat == 1
  if(length(stn) ~= 0)
     plot(stn(b1:m_stn,j_mean),stn(b1:m_stn,j_depth),'-')
  else
     plot(udw(b1:m_udw,j_mean),udw(b1:m_udw,j_depth),'--')
  end
elseif plotwhat == 2
  plot(stn(b1:m_stn,j_mean),stn(b1:m_stn,j_depth),'-', ...
    udw(b1:m_udw,j_mean),udw(b1:m_udw,j_depth),'--')
end

set(gca, 'YDir', 'reverse')
if (kind == 1)
  set(gca, 'XLim', [0, 250])
elseif (kind == 2)
  set(gca, 'XLim', [0, 100])
end
if ~isempty(MAX_DEPTH)
   set(gca, 'YLim', [0, MAX_DEPTH]);
end

text(1.20,1.10,TITLE,'units', 'normalized', 'HorizontalAlignment', 'center'),
text(1.20,1.05,sub_title,'units', 'normalized', 'HorizontalAlignment', 'center'),
ylabel('Depth (m)'),
xlabel(meanlab),
if rem(nplot,4) == 1
 text(-0.25, -0.35, pctime, 'units', 'normalized', 'rotation', 90.0);
end
grid

subplot(2,2,1+rem(nplot,4))
nplot = nplot+1;
if plotwhat == 1
   if(length(stn) ~= 0)
      plot(stn(b1:m_stn,j_std), stn(b1:m_stn,j_depth),'-')
   else
      plot(udw(b1:m_udw,j_std), udw(b1:m_udw,j_depth),'--')
   end
elseif plotwhat == 2
   plot(stn(b1:m_stn,j_std), stn(b1:m_stn,j_depth),'-', ...
        udw(b1:m_udw,j_std), udw(b1:m_udw,j_depth),'--')
end

set(gca, 'YDir', 'reverse')
if (kind == 1)
  set(gca, 'XLim', [0, 70])
elseif (kind == 2)
  set(gca, 'XLim', [0, 50])
end
if ~isempty(MAX_DEPTH)
   set(gca, 'YLim', [0, MAX_DEPTH]);
end

ylabel('Depth (m)'),
xlabel(stdlab),
grid

if rem(nplot,4) == 0
  eval(['print -append ' outfile]) 
end


