NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/seawater/sw_dist.m

Revision 495 (checked in by cbc, 11 years ago)

Initial import of Stark code.

Line 
1
2 function [dist,phaseangle] = sw_dist(lat,lon,units)
3
4 % SW_DIST    Distance between two lat,lon coordinates
5 %===================================================================
6 % SW_DIST  $Id: sw_dist.m,v 1.1 2003/12/12 04:23:22 pen078 Exp $
7 %          Copyright (C) CSIRO, Phil Morgan & Steve Rintoul 1992.
8 %
9 % USAGE:  [dist,phaseangle] = sw_dist(lat,lon {,units} )
10 %
11 % DESCRIPTION:
12 %   Calculate distance between two positions on glode using the "Plane
13 %   Sailing" method.  Also uses simple geometry to calculate the bearing of
14 %   the path between position pairs.
15 %
16 % INPUT:
17 %    lat      = decimal degrees (+ve N, -ve S) [- 90.. +90]
18 %    lon      = decimal degrees (+ve E, -ve W) [-180..+180]
19 %    units    = optional string specifing units of distance
20 %               'nm'  = nautical miles (default)
21 %               'km'  = kilometres
22 %
23 % OUTPUT:
24 %    dist        = distance between positions in units
25 %    phaseangle  = angle of line between stations with x axis (East).
26 %                  Range of values are -180..+180. (E=0, N=90, S=-90)
27 %
28 % AUTHOR:   Phil Morgan and Steve Rintoul 92-02-10
29 %
30 % DISCLAIMER:
31 %   This software is provided "as is" without warranty of any kind.
32 %   See the file sw_copy.m for conditions of use and licence.
33 %
34 % REFERENCE:
35 %    The PLANE SAILING method as descriibed in "CELESTIAL NAVIGATION" 1989 by
36 %    Dr. P. Gormley. The Australian Antartic Division.
37 %==================================================================
38
39 % Modifications
40 % 99-06-25. Lindsay Pender, Function name change from distance to sw_dist.
41 % 99-06-25. Lindsay Pender, Fixed transpose of row vectors.
42
43 % CALLER:   general purpose
44 % CALLEE:   none
45
46 %----------------------
47 % CHECK INPUT ARGUMENTS
48 %----------------------
49 if nargin > 3
50   error('sw_dist.m: No more than 3 arguments allowed')
51 elseif nargin==3
52   if ~isstr(units)
53       error('sw_dist.m: units argument must be string')
54   end %if
55 elseif nargin==2
56   units = 'nm';  % default units
57 else
58   error('sw_dist.m: wrong number of arguments')
59 end%if
60
61 [mlat,nlat] = size(lat);
62 if mlat~=1 & nlat~=1
63    error('sw_dist.m: lat, lon must be vectors.  No matrices allowed')
64 end%if
65
66 if length(lat)~=length(lon)
67    error('sw_dist.m: lat and lon must have same number of elements')
68 end%if
69
70 %-----------------
71 % DEFINE CONSTANTS
72 %-----------------
73 DEG2RAD = (2*pi/360);
74 RAD2DEG = 1/DEG2RAD;
75 DEG2MIN = 60;
76 DEG2NM  = 60;
77 NM2KM   = 1.8520;    % Defined in Pond & Pickard p303.
78
79 % BEGIN
80 npositions = length(lat);
81 ind=1:npositions-1;     % index to first of position pairs
82
83 dlon = diff(lon);
84 if any(abs(dlon)>180)
85    flag = find(abs(dlon)>180);
86    for ii=1:length(flag)
87      dlon(flag(ii))= -sign(dlon(flag(ii))) * (360 - abs(dlon(flag(ii))) );
88    end %for
89 end %if
90 latrad = abs(lat*DEG2RAD);
91 dep    = cos( (latrad(ind+1)+latrad(ind))./2 ) .* dlon;
92 dlat   = diff(lat);
93 dist   = DEG2NM*sqrt(dlat.^2 + dep.^2);  % in n.miles
94
95 if strcmp(units,'km')   % defaults to n.miles
96     dist = dist * NM2KM;
97 end %if
98
99 % CALCUALTE ANGLE TO X AXIS
100 phaseangle  = angle(dep+dlat*sqrt(-1))*RAD2DEG;
101 return
102 %--------------------------------------------------------------------
103
104
Note: See TracBrowser for help on using the browser.