function plot4(field,varargin)
%plot4(field,varargin)
% plot all 4 beams of ADCP in r,g,b,k
% field is line data.amp
% varargin = name, value pairs
% 
% this function will plot beams 1,2,3,4 as r,g,b,k        default
% plots are either (1) horizontal :
%                      specify vertical bin range
%                      specify to average or not
%              or  (2) vertical :                         
%                      specify index range                
%                      specify to average or not          
%
% 'orient = 'v';          % orientation: 'v' or 'h'                  'v'
% 'index' = integer array  % default: all                            1:nbins
% 'avg = 1;            % run mstdgap first (1 or 0)                  1
% 'label = 0;          % click to label beams 1,2,3,4 in r,g,b,k     0
% 'r'   = 0;           %degrees to rotate text
% 'indep = vector;       % independent variable, default is           1:ntimes
%                          bins (if 'v') or index (if 'h')
% 'fn' = [];           % string for title (field name)              ' '
% 'newfig' = 0;        % make a new figure                           0


%JH 2003/04/24

cfg.orient = 'v';  % orientation: 'v' or 'h'
cfg.index = []; % default: all
cfg.avg = 1;    % run mstdgap first (1 or 0)
cfg.label = 0;  % click to label beams 1,2,3,4 in r,g,b,k 
cfg.indep = [];   % independent variable, default is 
%                          bins (if 'v') or index (if 'h')
cfg.fn = [];
cfg.newfig = 0; 
cfg.r = 0;      % degrees counterclockwise to rotate text

cfg = fillstruct(cfg, varargin, 0);

if (cfg.orient ~= 'v') & (cfg.orient ~= 'h')
   fprintf('must specify orientation as ''h'' or ''v''\n')
   help mfilename
   return
end

if (cfg.orient == 'v')
   vertical = 1;
else
   vertical = 0;
end


M=squeeze(field(:,1,:));

%%independent variable
if (isempty(cfg.indep))
   if vertical %plotting against vertical
      cfg.indep = [1: length(M(:,1))];
   else        % plotting against horizontal
      cfg.indep = [1: length(M(1,:))];
   end
end

if (isempty(cfg.index))  % subset of repeated variable
   if vertical % subset is in time
      cfg.index = [1: length(M(1,:))];
   else        % subset is in vertical
      cfg.index = [1: length(M(:,1))];
   end
end


if length(cfg.index) == 1
   cfg.index = [cfg.index cfg.index];
end


colors = ['rgbk'];


if cfg.newfig
   figure
end


for beamnum = 1:4
   if vertical
      if (cfg.avg)
         plot(mstdgap(squeeze(field(:,beamnum,cfg.index))'), ...
              cfg.indep, colors(beamnum))
         hold on
      else
         plot(squeeze(field(:,beamnum,cfg.index)), ...
              cfg.indep, colors(beamnum))
         hold on
      end
   else
      if (cfg.avg)
         plot( cfg.indep, mstdgap(squeeze(field(cfg.index, beamnum, :))), ...
             colors(beamnum))
         hold on
      else
         plot(cfg.indep, squeeze(field(cfg.index, beamnum, :)),...
              colors(beamnum))
         hold on
      end
   end
end




if isempty(cfg.fn)
   cfg.fn = ' ';
else
   cfg.fn = [cfg.fn ': '];
end
%
if (cfg.avg & (cfg.index(1) ~= cfg.index(end)))
   astr = 'averaged';
else
   astr = 'not averaged';
end
%

if vertical
   if (cfg.index(1) ~= cfg.index(end))
      bstr = sprintf('index %d to %d', cfg.index(1), cfg.index(end));
   else
      bstr = sprintf('index %d', cfg.index(1));
   end      
else
   if (cfg.index(1) ~= cfg.index(end))
      bstr = sprintf('bin %d to %d', cfg.index(1), cfg.index(end));
   else
      bstr = sprintf('bin %d', cfg.index(1));
   end
end

if (vertical)
   set(gca, 'ydir','reverse')
end

title(sprintf('%s %s, %s', cfg.fn, bstr, astr))
      
if (cfg.label)
   click_rgbk({'beam1','beam2','beam3','beam4'},...
              'c',[1 0 0;0 1 0; 0 0 1;0 0 0],...
              'r',cfg.r,'s',8)
end









