1 /*
2  *  - - - - - - - - - - - - -
3  *   g a l _ t 2 l a t l o n
4  *  - - - - - - - - - - - - -
5  *
6  *  This routine is part of the General Astrodynamics Library
7  *
8  *  Description:
9  *
10  *     This routine converts a position vector in the ITRF reference
11  *     frame to geodetic latitude and longitude.
12  *
13  *  Status:
14  *
15  *     support routine.
16  *
17  *  Given:
18  *
19  *     itrf               d[3]      ITRF position vector ( m )
20  *     re                  d        Earth Equatorial Radius ( m )
21  *     invf                d        Inverse flattening factor
22  *
23  *  Returned:
24  *
25  *     *lat                d        Latitude ( radians )
26  *     *lon                d        Longitude ( radians )
27  *     *height             d        Height above the reference spheroid ( m )
28  *
29  *  Called:
30  *
31  *     gal_anpm                     Normalize angle into the range -pi <= a < +pi
32  *
33  *  Notes:
34  *
35  *  1) The height refers to a height above the reference spheroid and differs from
36  *     the height above mean sea level (i.e. above ground) by the "undulation of the geoid"
37  *     at that point.
38  *
39  *  References:
40  *
41  *     Fundamentals of Astrodynamics and Applications
42  *     Vallado, David A. Second Edition 2004
43  *     Pages 177-178
44  *
45  *     Explantory Supplement to the Astronomical Supplement
46  *     Seidelmann P. Kenneth 1992
47  *     Pages 202-207
48  *
49  *     The Astronomical Almanac 1997
50  *     Pages K11-K12
51  *
52  *  This revision:
53  *
54  *     2008 May 4
55  *
56  *  Copyright (C) 2008 Paul C. L. Willmott. See notes at end.
57  *
58  *-----------------------------------------------------------------------
59  */
60 
61 #include <math.h>
62 #include "gal_t2latlon.h"
63 #include "gal_const.h"
64 #include "gal_anpm.h"
65 
66 void
gal_t2latlon(double itrf[3],double re,double invf,double * lat,double * lon,double * height)67 gal_t2latlon
68  (
69    double itrf[3],
70    double re,
71    double invf,
72    double *lat,
73    double *lon,
74    double *height
75  )
76 
77 {
78 
79   double f, e2, r, latold, sinlat, c ;
80 
81   const double TOL = 1e-16 ;
82 
83 /*
84  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
85  */
86 
87 /*
88  * Calculate Longitude
89  */
90 
91   *lon = gal_anpm ( atan2 ( itrf[1], itrf[0] ) ) ;
92 
93 /*
94  * Calculate Latitude
95  */
96 
97   r = sqrt ( itrf[0] * itrf[0] + itrf[1] * itrf[1] ) ;
98   f = 1.0 / invf ;
99   e2 = f * ( 2.0 - f ) ;
100 
101   *lat = atan2 ( itrf[2], r ) ;
102   do {
103     latold = *lat ;
104     sinlat = sin ( *lat ) ;
105     c = re / sqrt ( 1.0 - e2 * sinlat * sinlat ) ;
106     *lat = atan2 ( itrf[2] + c * e2 * sinlat, r ) ;
107   } while ( fabs ( *lat - latold ) >= TOL ) ;
108 
109 /*
110  * Calculate Height ASL
111  */
112 
113   *height = ( r / cos ( *lat ) - c ) ;
114 
115 /*
116  * Finished.
117  */
118 
119 }
120 
121 /*
122  *  gal - General Astrodynamics Library
123  *  Copyright (C) 2008 Paul C. L. Willmott
124  *
125  *  This program is free software; you can redistribute it and/or modify
126  *  it under the terms of the GNU General Public License as published by
127  *  the Free Software Foundation; either version 2 of the License, or
128  *  (at your option) any later version.
129  *
130  *  This program is distributed in the hope that it will be useful,
131  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
132  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
133  *  GNU General Public License for more details.
134  *
135  *  You should have received a copy of the GNU General Public License along
136  *  with this program; if not, write to the Free Software Foundation, Inc.,
137  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
138  *
139  *  Contact:
140  *
141  *  Paul Willmott
142  *  vp9mu@amsat.org
143  */
144 
145