Skip to content

Commit ab2fefe

Browse files
committed
validate Matlab function parameters
1 parent 77c36a7 commit ab2fefe

27 files changed

+331
-189
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.eggs/
12
.mypy_cache/
23
.pytest_cache/
34

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ endif()
5858

5959
find_package(Octave)
6060
if (OCTAVE_MAJOR_VERSION GREATER_EQUAL 4)
61-
add_test(NAME MatlabOctave COMMAND octave-cli -q Test.m
61+
add_test(NAME MatlabOctave COMMAND octave-cli -q Test_maptran.m
6262
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/tests)
6363
set_tests_properties(MatlabOctave PROPERTIES TIMEOUT 30)
6464
endif()

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ The three separate packages are independent, they don't rely on each other.
8282

8383
One can verify Matlab code functionality by running:
8484

85-
tests/Test.m
85+
tests/Test_maptran.m
8686

8787
## Usage
8888

matlab/aer2ecef.m

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@
1313
% outputs
1414
% -------
1515
% x,y,z: Earth Centered Earth Fixed (ECEF) coordinates of test point (meters)
16-
17-
if nargin < 7 || isempty(spheroid), spheroid = wgs84Ellipsoid(); end
18-
if nargin < 8, angleUnit = 'd'; end
16+
narginchk(6,8)
17+
if nargin < 7, spheroid = []; end
18+
if nargin < 8, angleUnit = []; end
1919

20-
%% Origin of the local system in geocentric coordinates.
21-
[x0, y0, z0] = geodetic2ecef(spheroid, lat0, lon0, alt0, angleUnit);
22-
%% Convert Local Spherical AER to ENU
23-
[e, n, u] = aer2enu(az, el, slantRange, angleUnit);
24-
%% Rotating ENU to ECEF
25-
[dx, dy, dz] = enu2uvw(e, n, u, lat0, lon0, angleUnit);
26-
%% Origin + offset from origin equals position in ECEF
27-
x = x0 + dx;
28-
y = y0 + dy;
29-
z = z0 + dz;
20+
%% Origin of the local system in geocentric coordinates.
21+
[x0, y0, z0] = geodetic2ecef(spheroid, lat0, lon0, alt0, angleUnit);
22+
%% Convert Local Spherical AER to ENU
23+
[e, n, u] = aer2enu(az, el, slantRange, angleUnit);
24+
%% Rotating ENU to ECEF
25+
[dx, dy, dz] = enu2uvw(e, n, u, lat0, lon0, angleUnit);
26+
%% Origin + offset from origin equals position in ECEF
27+
x = x0 + dx;
28+
y = y0 + dy;
29+
z = z0 + dz;
3030

3131
end
3232

matlab/aer2enu.m

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,25 @@
1111
% Outputs
1212
% -------
1313
% e,n,u: East, North, Up coordinates of test points (meters)
14+
narginchk(3,4)
1415

15-
if nargin==3 || isempty(angleUnit) || strcmpi(angleUnit(1),'d')
16-
az = deg2rad(az);
17-
el = deg2rad(el);
18-
end
16+
if nargin==3 || isempty(angleUnit), angleUnit='d'; end
17+
18+
validateattributes(az, {'numeric'}, {'real'})
19+
validateattributes(el, {'numeric'}, {'real','>=',-90,'<=',90})
20+
validateattributes(slantRange, {'numeric'}, {'real'})
21+
validateattributes(angleUnit,{'string','char'},{'scalar'})
22+
%% compute
23+
if strcmpi(angleUnit(1),'d')
24+
az = deg2rad(az);
25+
el = deg2rad(el);
26+
end
1927

2028
%% Calculation of AER2ENU
21-
u = slantRange .* sin(el);
22-
r = slantRange .* cos(el);
23-
e = r .* sin(az);
24-
n = r .* cos(az);
29+
u = slantRange .* sin(el);
30+
r = slantRange .* cos(el);
31+
e = r .* sin(az);
32+
n = r .* cos(az);
2533

2634
end
2735

matlab/aer2geodetic.m

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
% Outputs
1414
% -------
1515
% lat1,lon1,alt1: geodetic coordinates of test points (degrees,degrees,meters)
16+
narginchk(6,8)
1617

17-
if nargin<7, spheroid = []; end
18-
if nargin<8, angleUnit= []; end
18+
if nargin<7, spheroid = []; end
19+
if nargin<8, angleUnit= []; end
1920

20-
[x, y, z] = aer2ecef(az, el, slantRange, lat0, lon0, alt0, spheroid, angleUnit);
21-
22-
[lat1, lon1, alt1] = ecef2geodetic(spheroid, x, y, z, angleUnit);
21+
[x, y, z] = aer2ecef(az, el, slantRange, lat0, lon0, alt0, spheroid, angleUnit);
22+
23+
[lat1, lon1, alt1] = ecef2geodetic(spheroid, x, y, z, angleUnit);
2324

2425
end
2526

matlab/ecef2aer.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
% az, el, slantrange: look angles and distance to point under test (degrees, degrees, meters)
1414
% az: azimuth clockwise from local north
1515
% el: elevation angle above local horizon
16+
narginchk(6,8)
17+
if nargin < 7, spheroid = []; end
18+
if nargin < 8, angleUnit= []; end
1619

17-
if nargin < 7, spheroid = []; end
18-
if nargin < 8, angleUnit= []; end
19-
20-
[e, n, u] = ecef2enu(x, y, z, lat0, lon0, alt0, spheroid, angleUnit);
21-
[az,el,slantRange] = enu2aer(e, n, u, angleUnit);
20+
[e, n, u] = ecef2enu(x, y, z, lat0, lon0, alt0, spheroid, angleUnit);
21+
[az,el,slantRange] = enu2aer(e, n, u, angleUnit);
2222

2323
end
2424

matlab/ecef2enu.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
% -------
1313
% e,n,u: East, North, Up coordinates of test points (meters)
1414

15-
if nargin < 7, spheroid = []; end
16-
if nargin < 8, angleUnit = []; end
15+
narginchk(6,8)
1716

18-
[x0, y0, z0] = geodetic2ecef(spheroid, lat0, lon0, alt0, angleUnit);
19-
[e, n, u] = ecef2enuv(x - x0, y - y0, z - z0, lat0, lon0, angleUnit);
17+
if nargin < 7, spheroid = []; end
18+
if nargin < 8, angleUnit = []; end
19+
20+
[x0, y0, z0] = geodetic2ecef(spheroid, lat0, lon0, alt0, angleUnit);
21+
[e, n, u] = ecef2enuv(x - x0, y - y0, z - z0, lat0, lon0, angleUnit);
2022
end
2123

2224
% Copyright (c) 2014-2018 Michael Hirsch, Ph.D.

matlab/ecef2enuv.m

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function [e, n, Up] = ecef2enuv (u, v, w, lat0, lon0, angleUnit)
1+
function [e, n, Up] = ecef2enuv(u, v, w, lat0, lon0, angleUnit)
22
%ecef2enuv convert *vector projection* UVW to ENU
33
%
44
% Inputs
@@ -10,16 +10,27 @@
1010
% Outputs
1111
% -------
1212
% e,n,Up: East, North, Up vector
13-
%
14-
if nargin<6 || isempty(angleUnit) || strcmpi(angleUnit(1), 'd')
15-
lat0 = deg2rad(lat0);
16-
lon0 = deg2rad(lon0);
17-
end
13+
narginchk(5,6)
14+
if nargin<6 || isempty(angleUnit), angleUnit='d'; end
15+
16+
validateattributes(u, {'numeric'}, {'real'})
17+
validateattributes(v, {'numeric'}, {'real'})
18+
validateattributes(w, {'numeric'}, {'real'})
19+
validateattributes(lat0, {'numeric'}, {'real','>=',-90,'<=',90})
20+
validateattributes(lon0, {'numeric'}, {'real'})
21+
validateattributes(angleUnit,{'string','char'},{'scalar'})
22+
23+
%% compute
24+
25+
if strcmpi(angleUnit(1), 'd')
26+
lat0 = deg2rad(lat0);
27+
lon0 = deg2rad(lon0);
28+
end
1829

19-
t = cos(lon0) .* u + sin(lon0) .* v;
20-
e = -sin(lon0) .* u + cos(lon0) .* v;
21-
Up = cos(lat0) .* t + sin(lat0) .* w;
22-
n = -sin(lat0) .* t + cos(lat0) .* w;
30+
t = cos(lon0) .* u + sin(lon0) .* v;
31+
e = -sin(lon0) .* u + cos(lon0) .* v;
32+
Up = cos(lat0) .* t + sin(lat0) .* w;
33+
n = -sin(lat0) .* t + cos(lat0) .* w;
2334
end
2435

2536
% Copyright (c) 2014-2018 Michael Hirsch, Ph.D.

matlab/ecef2geodetic.m

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function [lat,lon,alt] = ecef2geodetic(spheroid, x, y, z, angleUnit)
1+
function [lat,lon,alt] = ecef2geodetic(spheroid, x, y, z, angleUnit)
22
%ecef2geodetic convert ECEF to geodetic coordinates
33
%
44
% Inputs
@@ -13,49 +13,72 @@
1313
%
1414
% also see: http://www.oc.nps.edu/oc2902w/coord/coordcvt.pdf
1515
% Fortran reference at bottom of: http://www.astro.uni.torun.pl/~kb/Papers/geod/Geod-BG.htm
16+
narginchk(3,5)
17+
if isempty(spheroid)
18+
spheroid = wgs84Ellipsoid();
19+
elseif isnumeric(spheroid) && nargin == 3
20+
z = y;
21+
y = x;
22+
x = spheroid;
23+
spheroid = wgs84Ellipsoid();
24+
elseif isnumeric(spheroid) && ischar(z) && nargin == 4
25+
angleUnit = z;
26+
z = y;
27+
y = x;
28+
x = spheroid;
29+
spheroid = wgs84Ellipsoid();
30+
end
1631

17-
if isempty(spheroid), spheroid = wgs84Ellipsoid(); end
32+
if nargin < 5 || isempty(angleUnit), angleUnit='d'; end
1833

19-
% Algorithm is based on
20-
% http://www.astro.uni.torun.pl/~kb/Papers/geod/Geod-BG.htm
21-
% This algorithm provides a converging solution to the latitude equation
22-
% in terms of the parametric or reduced latitude form (v)
23-
% This algorithm provides a uniform solution over all latitudes as it does
24-
% not involve division by cos(phi) or sin(phi)
25-
a = spheroid.SemimajorAxis;
26-
b = spheroid.SemiminorAxis;
27-
r = hypot(x, y);
28-
% Constant required for Latitude equation
29-
rho = atan2(b * z, a * r);
30-
% Constant required for latitude equation
31-
c = (a^2 - b^2) ./ hypot(a*r, b*z);
32-
count = 0;
34+
validateattributes(spheroid,{'struct'},{'nonempty'})
35+
validateattributes(x, {'numeric'}, {'real'})
36+
validateattributes(y, {'numeric'}, {'real'})
37+
validateattributes(z, {'numeric'}, {'real'})
38+
validateattributes(angleUnit,{'string','char'},{'scalar'})
39+
40+
%% compute
41+
42+
% Algorithm is based on
43+
% http://www.astro.uni.torun.pl/~kb/Papers/geod/Geod-BG.htm
44+
% This algorithm provides a converging solution to the latitude equation
45+
% in terms of the parametric or reduced latitude form (v)
46+
% This algorithm provides a uniform solution over all latitudes as it does
47+
% not involve division by cos(phi) or sin(phi)
48+
a = spheroid.SemimajorAxis;
49+
b = spheroid.SemiminorAxis;
50+
r = hypot(x, y);
51+
% Constant required for Latitude equation
52+
rho = atan2(b * z, a * r);
53+
% Constant required for latitude equation
54+
c = (a^2 - b^2) ./ hypot(a*r, b*z);
55+
count = 0;
3356
% Starter for the Newtons Iteration Method
34-
vnew = atan2(a * z, b * r);
57+
vnew = atan2(a * z, b * r);
3558
% Initializing the parametric latitude
36-
v = 0;
37-
while v ~= vnew && count < 5
38-
% disp([v,vnew])
39-
v = vnew;
40-
% Derivative of latitude equation
41-
w = 2 * (cos (v - rho) - c .* cos(2 * v));
42-
% Newtons Method for computing iterations
43-
vnew = v - ((2 * sin (v - rho) - c .* sin(2 * v)) ./ w);
44-
count = count+1;
45-
end %while
59+
v = 0;
60+
while all(v ~= vnew) && count < 5
61+
% disp([v,vnew])
62+
v = vnew;
63+
% Derivative of latitude equation
64+
w = 2 * (cos (v - rho) - c .* cos(2 * v));
65+
% Newtons Method for computing iterations
66+
vnew = v - ((2 * sin (v - rho) - c .* sin(2 * v)) ./ w);
67+
count = count+1;
68+
end %while
4669

4770
%% Computing latitude from the root of the latitude equation
48-
lat = atan2(a * tan (vnew), b);
49-
% Computing longitude
50-
lon = atan2(y, x);
51-
% Computing h from latitude obtained
52-
alt = ((r - a * cos (vnew)) .* cos (lat)) + ...
53-
((z - b * sin (vnew)) .* sin (lat));
54-
55-
if nargin < 5 || isempty(angleUnit) || strcmpi(angleUnit(1),'d')
56-
lat = rad2deg(lat);
57-
lon = rad2deg(lon);
58-
end
71+
lat = atan2(a * tan (vnew), b);
72+
% Computing longitude
73+
lon = atan2(y, x);
74+
% Computing h from latitude obtained
75+
alt = ((r - a * cos (vnew)) .* cos (lat)) + ...
76+
((z - b * sin (vnew)) .* sin (lat));
77+
78+
if strcmpi(angleUnit(1),'d')
79+
lat = rad2deg(lat);
80+
lon = rad2deg(lon);
81+
end
5982

6083
end % function
6184

0 commit comments

Comments
 (0)