-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskynet.py
More file actions
273 lines (156 loc) · 6.42 KB
/
Copy pathskynet.py
File metadata and controls
273 lines (156 loc) · 6.42 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from AST1100SolarSystem import AST1100SolarSystem
import scipy.interpolate as inter
from numpy.linalg import norm
import time as tid
import seaborn
class skynet:
def __init__(self):
with open("positionsHomePlanet.npy", "rb") as npy:
self.planetPositions = np.load(npy)
with open("himmelkule.npy") as him:
self.himmelkule = np.load(him)
self.time_planet_simulated = 20
self.steps = self.planetPositions[0,0,:].size
self.time = np.linspace(0,self.time_planet_simulated,self.steps)
self.planetPosFunction = inter.interp1d(self.time, self.planetPositions)
self.seed = 75041
self.system = AST1100SolarSystem(self.seed)
self.numberOfPlanets = self.system.numberOfPlanets
def find_distance_planets(self,sat_pos,time):
distances = np.zeros(self.numberOfPlanets + 1)
r = self.planetPosFunction(time) - sat_pos[:,np.newaxis]
distances[:-1] = norm(r,axis = 0)
distances[-1] = norm(sat_pos)
r_with_sun = np.zeros([2,self.numberOfPlanets + 1])
r_with_sun[:,:-1] = r
r_with_sun[:,-1] = sat_pos
return distances
def find_position(self, guess, weight,distances, time):
x0 = guess[0]
y0 = guess[1]
h = 1e-8
regression_steps = 1e5
for i in range(int(regression_steps)):
dist= self.find_distance_planets(guess,time)
#dx = (4./dist.size)*sum((r[0] - real_r[0]))
#dy = (4./dist.size)*sum((r[1] - real_r[1]))
dx = (8./dist.size)*np.sum(dist[:-1]*(distances[:-1]-dist[:-1])*(self.planetPosFunction(time)[0,:]-guess[0]))
dy = (8./dist.size)*np.sum(dist[:-1]*(distances[:-1]-dist[:-1])*(self.planetPosFunction(time)[1,:]-guess[1]))
guess[0] -= weight*dx
guess[1] -= weight*dy
return guess
def test_dist(self,number_of_tests):
epsilon = 1e-5
correct = 0
wrong = 0
for i in range(number_of_tests):
time = np.random.uniform(0,18)
point = np.array([np.random.uniform(-50,50),np.random.uniform(-50,50)])
guess = np.array([np.random.uniform(-50,50),np.random.uniform(-50,50)])
dist = self.find_distance_planets(point,time)
guess = self.find_position(guess,0.00001,dist,time)
if np.max(abs(guess-point)) < epsilon:
correct += 1
else:
wrong += 1
print "Correct: ", correct
print "Wrong: ",wrong
def make_image(self,phi_midpoint, name = ""):
folder = "pic/"
phi_midpoint = phi_midpoint*np.pi/180
theta_midpoint = np.pi/2.
ypix = 480
xpix = 640
field_of_view = 1.22173#70.
max_xy = (2*np.sin(field_of_view/2.))/(1+np.cos(field_of_view/2.))
pic = np.zeros((ypix,xpix,3), dtype=np.uint8)
x = np.linspace(-max_xy,max_xy,xpix)
y = np.linspace(-max_xy,max_xy,ypix)
for i in xrange(xpix):
for j in xrange(ypix):
rho = norm(np.array([x[i],y[j]]))
c = 2*np.arctan2(rho,2.)
theta = (np.pi/2.) - np.arcsin(np.cos(c)*np.cos(theta_midpoint) + y[j]*np.sin(c)*np.sin(theta_midpoint)/rho)
phi = phi_midpoint + np.arctan2(x[i]*np.sin(c),(rho*np.sin(theta_midpoint)*np.cos(c) - y[j]*np.cos(theta_midpoint)*np.sin(c)))
temp = self.himmelkule[self.system.ang2pix(theta,phi)]
rbg = np.array([temp[2],temp[3],temp[4]])
pic[j,i,:] = rbg
if name != "":
img = Image.fromarray(pic)
img.save(folder + name + ".png")
return
return pic
def make_sky(self):
phi = 0
ypix = 480
xpix = 640
degrees = 360
sky = np.zeros((degrees,ypix,xpix,3), dtype=np.uint8)
for i in range(degrees):
#name = "degree" + str(i)
sky[i,:,:,:] = self.make_image(i)
print i
np.save("sky.npy",sky)
def find_angle(self,pic):
with open("sky.npy", "rb") as infile:
sky = np.load(infile)
degrees = 360
least_error = 10000
least_error_deg = 0
for i in range(degrees):
pic_from_file = sky[i,:,:,:]
error = norm(pic_from_file-pic)
if (error < least_error):
least_error = error
least_error_deg = i
return least_error_deg
def test_find_angle(self,number_of_tests = 1):
correct = 0
wrong = 0
for i in range(number_of_tests):
test_deg = int(np.random.uniform(0,359))
pic = self.make_image(test_deg)
calculated_deg = self.find_angle(pic)
if (calculated_deg == test_deg):
correct += 1
else:
wrong += 1
print "Correct: ",correct
print "Wrong: ",wrong
def find_velocity(self,lambda1,lambda2):
c = 63239.7263
h_alpha = 656.3
star1_phi = 77.518724
star1_shift = -0.017002884383
star1_v = (star1_shift/h_alpha)*c
star2_phi = 325.121916
star2_shift = 0.017144686369
star2_v = (star2_shift/h_alpha)*c
v1 = (star1_v - (lambda1/h_alpha)*c)
v2 = (star2_v - (lambda2/h_alpha)*c)
div_factor = 1.0/np.sin(star2_phi-star1_phi)
vx = div_factor*(np.sin(star2_phi)*v1 - np.sin(star1_phi)*v2)
vy = div_factor*(-np.cos(star2_phi)*v1 + np.cos(star1_phi)*v2)
return vx,vy
def test_find_velocity(self):
phi1 = 77.518724
phi2 = 325.121916
vs_1 = -1.63836317948
vs_2 = 1.65202692896
eps = 1e-6
vtx,vty = self.find_velocity(-0.017002884383, 0.017144686369)
vx,vy = self.find_velocity(0.0, 0.0)
v1 = np.cos(phi1)*vx + np.sin(phi1)*vy
v2 = np.cos(phi2)*vx + np.sin(phi2)*vy
success = (abs(vtx - 0.0) < eps and abs(vtx - 0.0)<eps) and (abs(v1 - vs_1) < eps and abs(v2 - vs_2) <eps)
assert success, "There is something wrong with the velocity finder"
skynet = skynet()
#dist = skynet.find_distance_planets(np.array([1.2,100.8]),2)
#print skynet.find_position(np.array([-1.0001,1.0001]), 0.000001,dist,2)
#skynet.test_dist(10)
#skynet.test_find_angle(5)
skynet.test_find_velocity()
#skynet.get_image(43.5)