-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeo_math.py
More file actions
236 lines (164 loc) · 4.62 KB
/
geo_math.py
File metadata and controls
236 lines (164 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#
# Messy but works
# Created by Taylor B. Sage
# pulled from: https://www.oc.nps.edu/oc2902w/coord/llhxyz.htm Javascript and converted into python
#
#
import numpy as np
def datum(a, b, f):
f = 1-b/a
eccsq = 1 - (b*b)/(a*a)
ecc = np.sqrt(eccsq)
return {
"a" : a,
"b" : b,
"f" : f,
"ecc" : ecc,
"eccsq" : eccsq
}
#WGS84 Datum
def wgs84Datum():
wgs84a = 6378.137
wgs84f = 1.0/298.257223563
wgs84b = wgs84a * ( 1.0 - wgs84f )
return datum(wgs84a,wgs84b,wgs84f)
wgs84 = wgs84Datum()
def radcur(lat):
dtr = np.pi/180.0
a = wgs84['a']
b = wgs84['b']
eccsq = wgs84['eccsq']
ecc = wgs84['ecc']
clat = np.cos(dtr*lat)
slat = np.sin(dtr*lat)
dsq = 1.0 - eccsq * slat * slat
d = np.sqrt(dsq)
rn = a/d
rm = rn * (1.0 - eccsq ) / dsq
rho = rn * clat
z = (1.0 - eccsq ) * rn * slat
rsq = rho*rho + z*z
r = np.sqrt( rsq )
return np.array([r, rn, rm])
#convert from Lat lon to cartesian
def latlonelv_xyz(flat, flon, altkmi):
dtr = np.pi/180.0
altkm = altkmi
clat = np.cos(dtr*flat)
slat = np.sin(dtr*flat)
clon = np.cos(dtr*flon)
slon = np.sin(dtr*flon)
rrnrm = radcur (flat)
rn = rrnrm[1]
re = rrnrm[0]
ecc = wgs84['ecc']
esq = ecc*ecc
x = (rn + altkm) * clat * clon
y = (rn + altkm) * clat * slon
z = ( (1-esq)*rn + altkm ) * slat
return np.array([x, y, z])
def rearth (lati):
rrnrm = radcur ( lati )
r = rrnrm[0]
return r
def gd2gc (flatgd, altkm ):
dtr = np.pi/180.0
rtd = 1/dtr
ecc = wgs84['ecc']
esq = ecc*ecc
altnow = altkm
rrnrm = radcur (flatgd)
rn = rrnrm[1]
ratio = 1 - esq*rn/(rn+altnow)
tlat = pi.tan(dtr*flatgd) / ratio
flatgc = rtd * pi.arctan(tlat)
return flatgc
def gc2gd (flatgc, altkm):
dtr = np.pi/180.0
rtd = 1/dtr
ecc = wgs84['ecc']
esq = ecc*ecc
altnow = altkm
rrnrm = radcur (flatgc)
rn = rrnrm[1]
ratio = 1 - esq*rn/(rn+altnow)
tlat = np.tan(dtr*flatgc) / ratio
flatgd = rtd * np.arctan(tlat)
rrnrm = radcur ( flatgd )
rn = rrnrm[1]
ratio = 1 - esq*rn/(rn+altnow)
tlat = np.tan(dtr*flatgc)/ratio
flatgd = rtd * np.arctan(tlat)
return flatgd
#convert from cartesian to lat lon and elv
def xyz_latlonelv ( xvec ):
# degrees to radians conversion value
dtr = np.pi/180.0
esq = wgs84["ecc"]*wgs84["ecc"]
x = xvec[0]
y = xvec[1]
z = xvec[2]
# rp = np.sqrt(np.dot(xvec, xvec.T))
rp = np.sqrt(xvec[0] * xvec[0] + xvec[1] * xvec[1] + xvec[2] * xvec[2] )
flatgc = np.arcsin ( xvec[2] / rp )/dtr
testval = np.abs(xvec[:1]).sum()
if testval < 1.0e-10:
flon = 0.0
else:
flon = np.arctan2 ( y,x )/dtr
# if flon < 0.0:
# flon = flon + 360.0
p = np.linalg.norm(np.array([x,y]))
p = np.sqrt(x*x + y*y)
if p < 1.0e-10:
flat = 90.0
if z < 0.0:
flat = -90.0
altkm = rp - rearth(flat)
return np.array([flat,flon,altkm])
rnow = rearth(flatgc)
altkm = rp - rnow
flat = gc2gd (flatgc,altkm)
rrnrm = radcur(flat)
rn = rrnrm[1]
for kount in range(5):
slat = np.sin(dtr*flat)
tangd = ( z + rn*esq*slat ) / p
flatn = np.arctan(tangd)/dtr
dlat = flatn - flat
flat = flatn
clat = np.cos( dtr*flat )
rrnrm = radcur(flat)
rn = rrnrm[1]
altkm = (p/clat) - rn
if np.abs(dlat) < 1.0e-12:
break
return np.array([flat,flon,altkm])
# the elevation of a target (B) above the horizon given in degrees as viewed from pont A
def tgt_elv(a,b):
b_norm = (b - a) / np.linalg.norm(b - a)
a_norm = a/np.linalg.norm(a)
return 90 - np.rad2deg(np.arccos(np.dot(b_norm,a_norm)))
def test_elv():
# GPS BIIR-11
# Position:
# Altitude: 20038 km
# Lat: 3.1°S
# Long: 39.2°W
# From Herndon:
# Mag: Unknown
# Dist: 23327 km
# Alt: 22°
# Az: 131°
#
#sat position
target = (-3.1, -39.2,20038)
#test location position
test_loc = (38.919380,-77.448290,0)
target_xyz = latlonelv_xyz(*target)
test_loc_xyz = latlonelv_xyz(*test_loc)
print(tgt_elv(test_loc_xyz,target_xyz))
if __name__ == "__main__":
test_elv()
x = ''
x = input()