NCEPLIBS-w3emc  2.11.0
w3fb10.f
Go to the documentation of this file.
1 C> @file
2 C> @brief Lat/long pair to compass bearing, gcd.
3 C> @author Peter Chase @date 1988-08-29
4 
5 C> Given a pair of points (1) and (2) given by latitude and
6 C> longitude, w3fb10() computes the bearing and great circle distance
7 C> from point (1) to point (2) assuming a spherical Earth. The
8 C> north and south poles are special cases. If latitude of point
9 C> (1) is within 1e-10 degrees of the north pole, bearing is the
10 C> negative longitude of point (2) by convention. If latitude of
11 C> point (1) is within 1e-10 degrees of the south pole, bearing is
12 C> the longitude of point (2) by convention. If point (2) is within
13 C> 1e-6 radians of the antipode of point (1), the bearing will be
14 C> set to zero. If point (1) and point (2) are within 1e-10 radians
15 C> of each other, both bearing and distance will be set to zero.
16 C>
17 C> Program history log:
18 C> - Peter Chase 1988-08-29
19 C> - Peter Chase 1988-09-23 Fix dumb south pole error.
20 C> - Peter Chase 1988-10-05 Fix bearing ambiguity.
21 C> - Ralph Jones 1990-04-12 Convert to cft77 fortran.
22 C>
23 C> @param[in] DLAT1 REAL Latitude of point (1) in degrees north.
24 C> @param[in] DLON1 REAL Longitude of point (1) in degrees east.
25 C> @param[in] DLAT2 REAL Latitude of point (2) in degrees north.
26 C> @param[in] DLON2 REAL Longitude of point (2) in degrees east.
27 C> @param[out] BEARD REAL Bearing of point (2) from point (1) in
28 C> compass degrees with north = 0.0, values from
29 C> -180.0 to +180.0 degrees.
30 C> @param[out] GCDKM REAL Great circle distance from point (1) to
31 C> point (2) in kilometers.
32 C>
33 C> @note According to the nmc handbook, the Earth's radius is
34 C> 6371.2 kilometers. This is what we use, even though the value
35 C> recommended by the smithsonian meteorological handbook is
36 C> 6371.221 km. (I wouldn't want you to think that I didn't know
37 C> what the correct value was.)
38 C>
39 C> @note Method: The poles are special cases, and handled separately.
40 C> otherwise, from spherical trigonometry, the law of cosines is used
41 C> to calculate the third side of the spherical triangle having
42 C> sides from the pole to points (1) and (2) (the colatitudes).
43 C> then the law of sines is used to calculate the angle at point
44 C> (1). A test is applied to see whether the arcsine result may be
45 C> be used as such, giving an acute angle as the bearing, or whether
46 C> the arcsine result should be subtracted from pi, giving an obtuse
47 C> angle as the bearing. This test is derived by constructing a
48 C> right spherical triangle using the pole, point (2), and the
49 C> meridian through point(1). The latitude of the right-angled
50 C> vertex then provides a test--if latitude (1) is greater than this
51 C> latitude, the bearing angle must be obtuse, otherwise acute.
52 C> If the two points are within 1e-6 radians of each other
53 C> a flat Earth is assumed, and the four-quadrant arctangent
54 C> function is used to find the bearing. The y-displacement is
55 C> the difference in latitude and the x-displacement is the
56 C> difference in longitude times cosine latitude, both in radians.
57 C> distance is then the diagonal.
58 C>
59 C> @note Fundamental trigonometric identities are used freely, such
60 C> as that cos(x) = sin(pi/2 - x), etc. See almost any mathematical
61 C> handbook, such as the c.r.c. standard math tables under 'relations
62 C> in any spherical triangle', or the national bureau of standards
63 C> 'handbook of mathematical functions' under section 4.3.149,
64 C> formulas for solution of spherical triangles.
65 C>
66 C> @note Double precision is used internally because of the wide
67 C> range of geographic values that may be used.
68 C>
69 C> @author Peter Chase @date 1988-08-29
70  SUBROUTINE w3fb10(DLAT1, DLON1, DLAT2, DLON2, BEARD, GCDKM)
71 C
72 C *** IMPLICIT TYPE DEFAULTS.....
73 C
74  IMPLICIT REAL (A-H,O-Z)
75 C
76 C *** CONSTANTS......
77 C
78  REAL PI
79  REAL HALFPI
80  REAL DR
81  REAL RD
82  REAL TDEG, TRAD, TPOD, TFLT
83  REAL EARTHR
84  REAL WHOLCD, HALFCD, QUARCD
85 C
86 C *** VARIABLES......
87 C
88  REAL RLAT1, RLAT2, COSLA1, COSLA2, SINLA1, SINLA2
89  REAL DLOND, RLOND, COSLO, SINLO, SANGG, ABEAR
90  REAL YDISP, XDISP, DDLAT1, DDLAT2, DBANG
91  REAL DLAT1, DLAT2, DLON1, DLON2, BEARD, GCDKM
92 C
93 C *** CONVERT LATITUDES AND LONGITUDE DIFFERENCE TO RADIANS.
94 C
95  DATA pi /3.141592653589793238462643/
96  DATA halfpi/1.570796326794896619231322/
97  DATA dr /0.017453292519943295769237/
98  DATA rd /57.295779513082320876798155/
99  DATA tdeg /1e-10/, trad/1e-10/, tpod/1e-6/, tflt/1e-6/
100  DATA earthr/6371.2/
101  DATA wholcd/360.0/, halfcd/180.0/, quarcd/90.0/
102 
103  ddlat1 = dlat1
104  ddlat2 = dlat2
105  rlat1 = dr * ddlat1
106  rlat2 = dr * ddlat2
107  dlond = dlon2 - dlon1
108  IF (dlond .GT. halfcd) dlond = dlond - wholcd
109  IF (dlond .LT. -halfcd) dlond = dlond + wholcd
110  rlond = dr * dlond
111 C
112 C *** FIRST WE ATTACK THE CASES WHERE POINT 1 IS VERY CLOSE TO THE
113 C *** NORTH OR SOUTH POLES.
114 C *** HERE WE USE CONVENTIONAL VALUE FOR BEARING.. - LONG (2) AT THE
115 C *** NORTH POLE, AND + LONG (2) AT THE SOUTH POLE.
116 C
117  IF (abs(ddlat1-quarcd) .LT. tdeg) THEN
118  IF (abs(ddlat2-quarcd) .LT. tdeg) THEN
119  dbang = 0.0
120  sangg = 0.0
121  ELSE IF (abs(ddlat2+quarcd) .LT. tdeg) THEN
122  dbang = 0.0
123  sangg = pi
124  ELSE
125  dbang = -dlon2
126  sangg = halfpi - rlat2
127  ENDIF
128  ELSE IF (abs(ddlat1+quarcd) .LT. tdeg) THEN
129  IF (abs(ddlat2-quarcd) .LT. tdeg) THEN
130  dbang = 0.0
131  sangg = pi
132  ELSE IF (abs(ddlat2+quarcd) .LT. tdeg) THEN
133  dbang = 0.0
134  sangg = 0.0
135  ELSE
136  dbang = +dlon2
137  sangg = halfpi + rlat2
138  ENDIF
139 C
140 C *** NEXT WE ATTACK THE CASES WHERE POINT 2 IS VERY CLOSE TO THE
141 C *** NORTH OR SOUTH POLES.
142 C *** HERE BEARING IS SIMPLY 0 OR 180 DEGREES.
143 C
144  ELSE IF (abs(ddlat2-quarcd) .LT. tdeg) THEN
145  dbang = 0.0
146  sangg = halfpi - rlat1
147  ELSE IF (abs(ddlat2+quarcd) .LT. tdeg) THEN
148  dbang = halfcd
149  sangg = halfpi + rlat1
150 C
151 C *** THE CASE REMAINS THAT NEITHER POINT IS AT EITHER POLE.
152 C *** FIND COSINE AND SINE OF LATITUDES AND LONGITUDE DIFFERENCE
153 C *** SINCE THEY ARE USED IN MORE THAN ONE FORMULA.
154 C
155  ELSE
156  cosla1 = cos(rlat1)
157  sinla1 = sin(rlat1)
158  cosla2 = cos(rlat2)
159  sinla2 = sin(rlat2)
160  coslo = cos(rlond)
161  sinlo = sin(rlond)
162 C
163 C *** FOLLOWING IS FORMULA FOR GREAT CIRCLE SUBTENDED ANGLE BETWEEN
164 C *** POINTS IN RADIAN MEASURE.
165 C
166  sangg = acos(sinla1*sinla2 + cosla1*cosla2*coslo)
167 C
168 C *** IF THE GREAT CIRCLE SUBTENDED ANGLE IS VERY SMALL, FORCE BOTH
169 C *** BEARING AND DISTANCE TO BE ZERO.
170 C
171  IF (abs(sangg) .LT. trad) THEN
172  dbang = 0.0
173  sangg = 0.0
174 C
175 C *** IF THE GREAT CIRCLE SUBTENDED ANGLE IS JUST SMALL, ASSUME A
176 C *** FLAT EARTH AND CALCULATE Y- AND X-DISPLACEMENTS. THEN FIND
177 C *** BEARING USING THE ARCTANGENT FUNCTION AND DISTANCE USING THE
178 C *** SQUARE ROOT.
179 C
180  ELSE IF (abs(sangg) .LT. tflt) THEN
181  ydisp = rlat2-rlat1
182  xdisp = rlond*cosla2
183  abear = atan2(xdisp, ydisp)
184  dbang = rd*abear
185  sangg = sqrt(ydisp**2 + xdisp**2)
186 C
187 C *** IF THE ANGLE IS RATHER CLOSE TO PI RADIANS, FORCE BEARING TO
188 C *** BE ZERO AND DISTANCE TO BE PI.
189 C *** THE TEST FOR 'CLOSE TO PI' IS MORE RELAXED THAN THE TEST FOR
190 C *** 'CLOSE TO ZERO' TO ALLOW FOR GREATER RELATIVE ERROR.
191 C
192  ELSE IF (abs(sangg-pi) .LT. tpod) THEN
193  dbang = 0.0
194  sangg = pi
195 C
196 C *** OTHERWISE COMPUTE THE PRINCIPAL VALUE OF THE BEARING ANGLE
197 C *** USING THE LAW OF SINES. THE DIVISION BY THE SINE FORCES US TO
198 C *** LIMIT THE DOMAIN OF THE ARCSINE TO (-1,1).
199 C
200  ELSE
201  abear = asin(amax1(-1.0,amin1(+1.0,cosla2*sinlo/
202  & sin(sangg))))
203 C
204 C *** IF THE LONGITUDE DIFFERENCE IS LESS THAN PI/2 IT IS NECESSARY
205 C *** TO CHECK WHETHER THE BEARING ANGLE IS ACUTE OR OBTUSE BY
206 C *** COMPARING LATITUDE (1) WITH THE LATITUDE OF THE GREAT CIRCLE
207 C *** THROUGH POINT (2) NORMAL TO MERIDIAN OF LONGITUDE (1). IF
208 C *** LATITUDE (1) IS GREATER, BEARING IS OBTUSE AND THE ACTUAL
209 C *** BEARING ANGLE IS THE SUPPLEMENT OF THE ANGLE CALCULATED ABOVE.
210 C
211  IF (0.0 .LE. cosla1*sinla2 .AND. cosla1*sinla2 .LE.
212  & cosla2*sinla1*coslo .OR. cosla1*sinla2 .LE. 0.0 .AND.
213  & cosla2*sinla1*coslo .GE. cosla1*sinla2) abear =
214  & sign(pi,abear) - abear
215  dbang = rd * abear
216  ENDIF
217  ENDIF
218 C
219 C *** THIS FINISHES THE CASE WHERE POINTS ARE NOT AT THE POLES.
220 C *** NOW CONVERT BEARING TO DEGREES IN RANGE -180 TO +180 AND FIND
221 C *** GREAT CIRCLE DISTANCE IN KILOMETERS.
222 C
223  IF (dbang .LE. -halfcd) dbang = dbang + wholcd
224  IF (dbang .GT. halfcd) dbang = dbang - wholcd
225  gcdkm = earthr * sangg
226  beard = dbang
227  RETURN
228  END
subroutine w3fb10(DLAT1, DLON1, DLAT2, DLON2, BEARD, GCDKM)
Given a pair of points (1) and (2) given by latitude and longitude, w3fb10() computes the bearing and...
Definition: w3fb10.f:71