function [enu, xyze] = beam_enu(vel, varargin);
% function [enu, xyze] = beam_enu(vel, varargin);
%           (vel, H, P, R, 'orientation', 'up', 'angle', 30)
%      or   (vel, HPR, 'orientation', 'up', 'angle', 30)
%      key/value pairs are optional.
%      defaults are downward orientation and 30-degree angles.
%

% 99/10/22 modified for orientation and angle inputs.

looking_up = 0;
beam_angle_deg = 30;


% Find, use, and remove either or both of the optional
% keyword/argument pairs.
for ii = (length(varargin)-1):-1:1
   if strcmp(varargin{ii}, 'orientation')
      if strcmp(varargin{ii+1}, 'up')
         looking_up = 1;
      elseif ~strcmp(varargin{ii+1}, 'down')
         help('beam_enu')
         error('unrecognized orientation value')
      end
      varargin(ii:ii+1) = [];
   elseif strcmp(varargin{ii}, 'angle')
      beam_angle_deg = varargin{ii+1};
      varargin(ii:ii+1) = [];
   end
end

% Remaining arguments should be either the hpr array from
% adp.m, or separate arrays of heading, pitch, and roll.
nva = length(varargin);
if nva == 1,
   hpr = varargin{1};
   Pitch = hpr(:,2);
   Roll = hpr(:,3);
   Heading = hpr(:,1);
elseif nva == 3,
   Heading = varargin{1};
   Pitch = varargin{2};
   Roll = varargin{3};
else
   help('beam_enu')
   error('Cannot parse input arguments.')
end

if looking_up,
   orient_string = 'up';
else
   orient_string = 'down';
end

xyze = beam_xyz(vel, 'angle', beam_angle_deg, 'orientation', orient_string);
enu = xyz_enu(xyze(:, 1:3, :), Heading, Pitch, Roll);

