summaryrefslogtreecommitdiff
path: root/inst/dm2degrees.m
blob: 7d560d614a5ac1208f5cf4954ca07ce1bc0adca7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
## Copyright (C) 2014-2020 Carnë Draug <carandraug@octave.org>
##
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or (at
## your option) any later version.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {} dm2degrees (@var{dm})
## Convert degrees, minutes, and seconds components into decimal degrees.
##
## @var{dm} must be a 2 column matrix with one row per angle, each
## column correspoding to its degrees (an integer), and minutes (a less
## than 60 value, possibly fractional) components.
##
## The sign of the angle must be defined on its first non-zero component only,
## i.e., if an angle is negative, the minutes component must be positive
## unless its degrees component is zero.
##
## @seealso{degrees2dm, degree2dms, dms2degrees}
## @end deftypefn

## Author: Carnë Draug <carandraug@octave.org>

function deg = dm2degrees (dm)

  if (nargin != 1)
    print_usage ();
  elseif (! isnumeric (dm) || ndims (dm) != 2 || columns (dm) != 2)
    error ("dm2degrees: DM must be a numeric matrix with 2 columns");
  elseif (any (fix (dm(:,1)) != dm(:,1)))
    error ("dm2degrees: degrees component (first column) must be an integer");
  elseif (any (dm(:,2) >= 60))
    error ("dm2degrees: minutes component (second column) must be less than 60");
  endif

  ## join the degrees and minutes parts
  adm = abs (dm);
  deg = sum ([adm(:,1) adm(:,2)/60], 2);

  ## change the sign if any part is negative and check that negative
  ## sign is present on the first non-zero part only
  negs = dm < 0;
  if (any (dm(:,1) != 0 & negs(:,2)))
    error ("dm2degrees: minutes must be positive if degree is non-zero");
  endif
  deg(any (negs, 2)) *= -1;

endfunction

%!test
%! deg = [10 10.5 -10.5 -0.5 0.5]';
%! dm = [ 10    0
%!        10   30
%!       -10   30
%!         0  -30
%!         0   30];
%! for i = 1:rows (dm)
%!   assert (dm2degrees (dm(i,:)), deg(i));
%! endfor
%! assert (dm2degrees (dm), deg);
%! assert (dm2degrees (single (dm)), single (deg));

%!error <less than 60> dm2degrees ([5 60])
%!error <less than 60> dm2degrees ([5 61])
%!error <2 columns> dm2degrees ([5 50 3])
%!error <must be positive> dm2degrees ([5 -40])
%!error <must be positive> dm2degrees ([-5 -40])
%!error dm2degrees (rand (7, 2, 3))