# Copyright (c) 2014-2018 Michael Hirsch, Ph.D.
# Copyright (c) 2013, Felipe Geremia Nievinski
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
#    in the documentation and/or other materials provided with the distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

## -*- texinfo -*-
## @deftypefn {Function File} {@var{e}, @var{n}, @var{u} =} aer2enu (@var{az}, @var{el}, @var{slantrange}, @var{angleUnit})
## converts azimuth, elevation, range to ENU coordinates
##
## Inputs:
##
## az, el, slantrange: look angles and distance to point under test (degrees, degrees, meters)
##
## az: azimuth clockwise from local north
##
## el: elevation angle above local horizon
##
## angleUnit: string for angular units. Default 'd': degrees
##
## Outputs:
##
## e,n,u:  East, North, Up coordinates of test points (meters)
## Example:
## @example
## [e, n, u] = aer2enu (33, 70, 1e3)
## e =  186.28
## n =  286.84
## u =  939.69 
## @end example 
## With radians
## @example
## [e, n, u] = aer2enu (pi/4, pi/3,1e3, "radians")
## e =  353.55
## n =  353.55
## u =  866.03
## @end example
## @end deftypefn
function [e, n, u] = aer2enu (az, el, slantrange, angleUnit = "degrees")
  
  if nargin < 3
    print_usage();
  endif  

  if (!isnumeric (az) || !isreal (az) || !isnumeric (el) || !isreal (el) ...
    ||!isnumeric (slantrange) || !isreal (slantrange))
    error ("aer2enu.m : numeric values expected");
  endif


  if nargin == 3 
    az = deg2rad (az);
    el = deg2rad (el);
  endif    

# Calculation of AER2ENU
   u = slantrange .* sin (el);
   r = slantrange .* cos (el);
   e = r .* sin (az);
   n = r .* cos (az);

endfunction

%!test
%! [e, n, u] = aer2enu (33,70,1e3);
%! assert ([e, n, u], [186.277521, 286.84222, 939.69262], 10e-6)
%! [e, n, u] = aer2enu (0.57595865,1.221730476,1e3, "rad");
%!  assert ([e, n, u], [186.277521, 286.84222, 939.69262], 10e-6)

%!error <numeric> aer2enu("s", 25, 1e3)
%!error <numeric> aer2enu(3i, 25, 1e3)
%!error <numeric> aer2enu(33, "s", 1e3)
%!error <numeric> aer2enu(33, 3i, 1e3)
%!error <numeric> aer2enu(33, 25, "s")
%!error <numeric> aer2enu(33, 25, 3i)



