OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
gctp_utility.c
Go to the documentation of this file.
1 /******************************************************************************
2 Name: gctp_utility.c
3 
4 Purpose: Provides a number of simple utility routines.
5 
6 ******************************************************************************/
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <math.h>
10 #include <limits.h>
11 #include "oli_local.h"
12 #include "oli_cproj.h"
13 
14 /******************************************************************************
15 Name: gctp_print_transformation_info
16 
17 Purpose: Prints out information about the two projections involved in the
18  transformation.
19 
20 Returns:
21  nothing
22 
23 ******************************************************************************/
25 (
26  const GCTP_TRANSFORMATION *trans
27 )
28 {
29  if (trans->forward.print_info)
30  {
31  GCTP_PRINT_INFO("Forward projection:");
32  trans->forward.print_info(&trans->forward);
33  }
34  if (trans->inverse.print_info)
35  {
36  GCTP_PRINT_INFO("Inverse projection:");
37  trans->inverse.print_info(&trans->inverse);
38  }
39 }
40 
41 /******************************************************************************
42 Name: gctp_get_input_proj
43 
44 Purpose: Provides access to the input projection parameter information.
45 
46 Returns:
47  Pointer to the input projection parameters.
48 
49 ******************************************************************************/
50 const GCTP_PROJECTION *gctp_get_input_proj
51 (
52  const GCTP_TRANSFORMATION *trans
53 )
54 {
55  return &trans->inverse.proj;
56 }
57 
58 /******************************************************************************
59 Name: gctp_get_input_proj
60 
61 Purpose: Provides access to the output projection parameter information.
62 
63 Returns:
64  Pointer to the output projection parameters.
65 
66 ******************************************************************************/
67 const GCTP_PROJECTION *gctp_get_output_proj
68 (
69  const GCTP_TRANSFORMATION *trans
70 )
71 {
72  return &trans->forward.proj;
73 }
74 
75 /******************************************************************************
76 Name: gctp_calc_utm_zone
77 
78 Purpose: Calculate the UTM zone for a given longitude (in degrees).
79 
80 Returns:
81  The UTM zone (1 - 60)
82 
83 ******************************************************************************/
85 (
86  double lon /* I: longitude (in degrees) */
87 )
88 {
89  return (int)(((lon + 180.0) / 6.0) + 1.0);
90 }
91 
92 /******************************************************************************
93 Name: gctp_get_sign
94 
95 Purpose: Get the sign of a number
96 
97 Returns:
98  -1 if number is < 0
99  1 otherwise
100 
101 ******************************************************************************/
102 int gctp_get_sign
103 (
104  double x
105 )
106 {
107  if (x < 0.0)
108  return -1;
109  else
110  return 1;
111 }
112 
113 /******************************************************************************
114 Name: gctp_calc_e0|e1|e2|e3
115 
116 Purpose: Functions to compute the constants e0, e1, e2, and e3 which are used
117  in a series for calculating the distance along a meridian. The input x
118  represents the eccentricity squared.
119 
120 Returns:
121  Calculated value
122 
123 ******************************************************************************/
124 double gctp_calc_e0
125 (
126  double x
127 )
128 {
129  return 1.0-0.25*x*(1.0+x/16.0*(3.0+1.25*x));
130 }
131 double gctp_calc_e1
132 (
133  double x
134 )
135 {
136  return 0.375*x*(1.0+0.25*x*(1.0+0.46875*x));
137 }
138 double gctp_calc_e2
139 (
140  double x
141 )
142 {
143  return 0.05859375*x*x*(1.0+0.75*x);
144 }
145 double gctp_calc_e3
146 (
147  double x
148 )
149 {
150  return x*x*x*(35.0/3072.0);
151 }
152 
153 /******************************************************************************
154 Name: gctp_calc_e4
155 
156 Purpose: Function to compute the constant e4 from the input of the eccentricity
157  of the spheroid, x. This constant is used in the Polar Stereographic
158  projection.
159 
160 Returns:
161  Calculated value
162 
163 ******************************************************************************/
164 double gctp_calc_e4
165 (
166  double x
167 )
168 {
169  double con;
170  double com;
171  con = 1.0 + x;
172  com = 1.0 - x;
173  return (sqrt((pow(con,con))*(pow(com,com))));
174 }
175 
176 /******************************************************************************
177 Name: gctp_calc_dist_from_equator
178 
179 Purpose: Function computes the value of M which is the distance along a
180  meridian from the Equator to latitude phi.
181 
182 Returns:
183  Calculated value
184 
185 ******************************************************************************/
187 (
188  double e0,
189  double e1,
190  double e2,
191  double e3,
192  double phi
193 )
194 {
195  return e0*phi-e1*sin(2.0*phi)+e2*sin(4.0*phi)-e3*sin(6.0*phi);
196 }
197 
198 /******************************************************************************
199 Name: gctp_calc_phi2
200 
201 Purpose: Function to compute the latitude angle, phi2, for the inverse of the
202  Lambert Conformal Conic and Polar Stereographic projections.
203 
204 Returns:
205  GCTP_SUCCESS or GCTP_ERROR
206 
207 ******************************************************************************/
208 int gctp_calc_phi2
209 (
210  double eccent, /* I: Spheroid eccentricity */
211  double ts, /* I: Constant value t */
212  double *phi2 /* O: calculated value of phi2 */
213 )
214 {
215  double eccnth;
216  double phi;
217  double con;
218  double dphi;
219  double sinpi;
220  long i;
221 
222  eccnth = .5 * eccent;
223  phi = HALF_PI - 2 * atan(ts);
224  for (i = 0; i <= 15; i++)
225  {
226  sinpi = sin(phi);
227  con = eccent * sinpi;
228  dphi = HALF_PI - 2 * atan(ts *(pow(((1.0 - con)/(1.0 + con)),eccnth)))
229  - phi;
230  phi += dphi;
231  if (fabs(dphi) <= .0000000001)
232  {
233  *phi2 = phi;
234  return GCTP_SUCCESS;
235  }
236  }
237 
238  GCTP_PRINT_ERROR("Failed to converge to a solution for phi2");
239  return GCTP_ERROR;
240 }
241 
242 /******************************************************************************
243 Name: gctp_calc_small_radius
244 
245 Purpose: Function to compute the constant small m which is the radius of a
246  parallel of latitude, phi, divided by the semimajor axis.
247 
248 Returns:
249  calculated value
250 
251 ******************************************************************************/
253 (
254  double eccent,
255  double sinphi,
256  double cosphi
257 )
258 {
259  double con;
260 
261  con = eccent * sinphi;
262  return (cosphi / (sqrt (1.0 - con * con)));
263 }
264 
265 /******************************************************************************
266 Name: gctp_calc_small_t
267 
268 Purpose: Function to compute the constant small t for use in the forward
269  computations in the Lambert Conformal Conic and the Polar Stereographic
270  projections.
271 
272 Returns:
273  calculated value
274 
275 ******************************************************************************/
276 double gctp_calc_small_t
277 (
278  double eccent, /* Eccentricity of the spheroid */
279  double phi, /* Latitude phi */
280  double sinphi /* Sine of the latitude */
281 )
282 {
283  double con;
284  double com;
285 
286  con = eccent * sinphi;
287  com = .5 * eccent;
288  con = pow(((1.0 - con) / (1.0 + con)),com);
289  return tan(.5 * (HALF_PI - phi))/con;
290 }
double gctp_calc_e2(double x)
Definition: gctp_utility.c:139
#define GCTP_ERROR
Definition: gctp.h:81
int gctp_calc_utm_zone(double lon)
Definition: gctp_utility.c:85
#define GCTP_PRINT_ERROR(format,...)
Definition: oli_local.h:81
int gctp_get_sign(double x)
Definition: gctp_utility.c:103
void gctp_print_transformation_info(const GCTP_TRANSFORMATION *trans)
Definition: gctp_utility.c:25
const GCTP_PROJECTION * gctp_get_input_proj(const GCTP_TRANSFORMATION *trans)
Definition: gctp_utility.c:51
const GCTP_PROJECTION * gctp_get_output_proj(const GCTP_TRANSFORMATION *trans)
Definition: gctp_utility.c:68
int gctp_calc_phi2(double eccent, double ts, double *phi2)
Definition: gctp_utility.c:209
#define GCTP_PRINT_INFO(format,...)
Definition: oli_local.h:75
#define HALF_PI
Definition: proj_define.h:84
double gctp_calc_e0(double x)
Definition: gctp_utility.c:125
double gctp_calc_small_t(double eccent, double phi, double sinphi)
Definition: gctp_utility.c:277
#define GCTP_SUCCESS
Definition: gctp.h:82
double gctp_calc_e3(double x)
Definition: gctp_utility.c:146
double gctp_calc_e1(double x)
Definition: gctp_utility.c:132
double gctp_calc_small_radius(double eccent, double sinphi, double cosphi)
Definition: gctp_utility.c:253
#define fabs(a)
Definition: misc.h:93
float * lon
int i
Definition: decode_rs.h:71
double gctp_calc_dist_from_equator(double e0, double e1, double e2, double e3, double phi)
Definition: gctp_utility.c:187
double gctp_calc_e4(double x)
Definition: gctp_utility.c:165