-
Notifications
You must be signed in to change notification settings - Fork 2
/
look_around_you.py
59 lines (45 loc) · 1.28 KB
/
look_around_you.py
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
# Write your find_closest function here
import numpy as np
def hms2dec(h, m, s):
return 15*(h + m/60 + s/3600)
def dms2dec(d, m, s):
if (d >= 0):
return d + m/60 + s/3600
else:
return d - m/60 - s/3600
def import_bss():
file = 'bss.dat'
lines = np.loadtxt(file, usecols=range(1, 7))
count=1
result = [ ]
for line in lines:
result.append((count, hms2dec(line[0], line[1], line[2]), dms2dec(line[3], line[4]
, line[5])))
count += 1
return result
def angular_dist(a1, d1, a2, d2):
a1 = np.radians(a1)
d1 = np.radians(d1)
a2 = np.radians(a2)
d2 = np.radians(d2)
p1 = np.sin(abs(d1-d2)/2)**2
p2 = np.cos(d1)*np.cos(d2)*np.sin(abs(a1-a2)/2)**2
p3 = 2*np.arcsin(np.sqrt(p1+p2))
return np.degrees(p3)
def find_closest(cat, ra, dec):
distmin = 1000
minindex = 0
for line in cat:
dist = angular_dist(ra, dec, line[1], line[2])
if dist < distmin:
minindex = line[0]
distmin = dist
return (minindex, distmin)
# You can use this to test your function.
# Any code inside this `if` statement will be ignored by the automarker.
if __name__ == '__main__':
cat = import_bss()
# First example from the question
print(find_closest(cat, 175.3, -32.5))
# Second example in the question
print(find_closest(cat, 32.2, 40.7))