Due to the lapse in federal government funding, NASA is not updating this website. We sincerely regret this inconvenience.
NASA Logo
Ocean Color Science Software

ocssw V2022
ll2vec.c
Go to the documentation of this file.
1 #include <math.h>
2 #include "l1.h"
3 
4 static float raddeg = 180. / PI;
5 
6 int ll2vec(float *ll, float *vec)
7 /*******************************************************************
8 
9  ll2vec
10 
11  purpose: convert lat, lon (or az, ele) into vectors in x, y, z
12 
13  Returns type: int - 0 if all is OK
14 
15  Parameters: (in calling order)
16  Type Name I/O Description
17  ---- ---- --- -----------
18  float * ll I latitude longitude pair
19  float * vec O x, y, z vector with x
20  along the 0 lat, lon,
21  y at 0 lat, 90 lon and z
22  at 90 lat
23  (assumed to exist - array passed in)
24 
25  Modification history:
26  Programmer Date Description of change
27  ---------- ---- ---------------------
28  W. Robinson 10 Sep 2010 Original development
29 
30  *******************************************************************/ {
31  int32_t err;
32  float latr, lonr;
33 
34  if ((*ll > 90.) || (*ll < -90.)) {
35  *vec = -999.;
36  *(vec + 1) = -999.;
37  *(vec + 2) = -999.;
38  err = -1;
39  } else {
40  latr = *ll / raddeg;
41  lonr = *(ll + 1) / raddeg;
42  /*
43  * get the components
44  */
45  *vec = cos(lonr) * cos(latr);
46  *(vec + 1) = sin(lonr) * cos(latr);
47  *(vec + 2) = sin(latr);
48  err = 0;
49  }
50  return err;
51 }
52 
53 int vec2ll(float *vec, float *ll)
54 /*******************************************************************
55 
56  vec2ll
57 
58  purpose: convert a vectors in x, y, z into lat, lon (or az, ele)
59 
60  Returns type: int - 0 if all is OK
61 
62  Parameters: (in calling order)
63  Type Name I/O Description
64  ---- ---- --- -----------
65  float * vec I x, y, z vector with x
66  along the 0 lat, lon,
67  y at 0 lat, 90 lon and z
68  at 90 lat
69  float * ll O latitude longitude pair
70  (assumed to exist - array passed in)
71 
72  Modification history:
73  Programmer Date Description of change
74  ---------- ---- ---------------------
75  W. Robinson 10 Sep 2010 Original development
76 
77  *******************************************************************/ {
78  int32_t err;
79  float lonr, latr, vls, vlen;
80 
81  vls = pow(*vec, 2.) + pow(*(vec + 1), 2.) + pow(*(vec + 2), 2.);
82  if (vls <= 0) {
83  *ll = -999.;
84  *(ll + 1) = -999.;
85  err = -1;
86  } else {
87  /*
88  * get the vector lengths for normalization and derive lat, lon
89  */
90  vlen = sqrt(vls);
91  latr = asin(*(vec + 2) / vlen);
92  lonr = atan2(*(vec + 1) / vlen, *vec / vlen);
93 
94  *ll = latr * raddeg;
95  *(ll + 1) = lonr * raddeg;
96  err = 0;
97  }
98  return err;
99 }
int ll2vec(float *ll, float *vec)
Definition: ll2vec.c:6
#define PI
Definition: l3_get_org.c:6
int vec2ll(float *vec, float *ll)
Definition: ll2vec.c:53