-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasteroids.py
More file actions
184 lines (153 loc) · 7.21 KB
/
asteroids.py
File metadata and controls
184 lines (153 loc) · 7.21 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
import json, urllib.request
from datetime import timedelta, date, time
# Asteroids near Earth in next 7 days
def asteroids_seven_days():
today = date.today()
end_date = date.today() + timedelta(days=7)
url = "https://api.nasa.gov/neo/rest/v1/feed?start_date=" + str(today) + "&end_date=" + str(
end_date) + "&api_key=DEMO_KEY"
response = urllib.request.urlopen(url)
result = json.loads(response.read())
print("Start date: " + str(today))
print("End date: " + str(end_date))
print("In the next 7 days " + str(result["element_count"]) + " asteroids will be passing near Earth.\n")
asteroids = result["near_earth_objects"]
for asteroid in asteroids:
for field in asteroids[asteroid]:
try:
# name
print("Object name: " + field["name"])
# diameter min
print("Minimal diameter in km: " + str(
field["estimated_diameter"]["kilometers"]["estimated_diameter_min"]))
# diameter max
print("Maximum diameter in km: " + str(
field["estimated_diameter"]["kilometers"]["estimated_diameter_max"]))
# velocity
print("Velocity [km/h]: " + str(
field["close_approach_data"][0]["relative_velocity"]["kilometers_per_hour"]))
# approach time
print("Close Approach date and Time: " + field["close_approach_data"][0]["close_approach_date_full"])
# distance from Earth when passing
print("Object will pass Earth at distance of: " + str(
field["close_approach_data"][0]["miss_distance"]["kilometers"]) + " km")
if field["is_potentially_hazardous_asteroid"]:
print("This object may pose a threat to Earth!")
else:
print("This object should not be dangerous to our planet. We are safe.")
except Exception as e:
print(e)
finally:
print("....................................")
# Asteroids near Earth from today until given date
def asteroids_today_given_days():
today = date.today()
# start = input("Enter start date in format YYYY-MM-DD: ")
x = input("Enter number of days since today that you want to check: ")
end_date = date.today() + timedelta(days=int(x))
url2 = "https://api.nasa.gov/neo/rest/v1/feed?start_date=" + str(today) + "&end_date=" + str(
end_date) + "&api_key=DEMO_KEY"
response2 = urllib.request.urlopen(url2)
result2 = json.loads(response2.read())
print("Start date: " + str(today))
print("End date: " + str(end_date))
print("Between " + str(today) + " and " + str(end_date) + " " + str(
result2["element_count"]) + " asteroids will be passing near Earth.")
print("")
asteroids = result2["near_earth_objects"]
for asteroid in asteroids:
for field in asteroids[asteroid]:
try:
# name
print("Object name: " + field["name"])
# diameter min
print("Minimal diameter in km: " + str(
field["estimated_diameter"]["kilometers"]["estimated_diameter_min"]))
# diameter max
print("Maximum diameter in km: " + str(
field["estimated_diameter"]["kilometers"]["estimated_diameter_max"]))
# velocity
print("Velocity [km/h]: " + str(
field["close_approach_data"][0]["relative_velocity"]["kilometers_per_hour"]))
# approach time
print("Close Approach date and Time: " + field["close_approach_data"][0]["close_approach_date_full"])
# distance from Earth when passing
print("Object will pass Earth at distance of: " + str(
field["close_approach_data"][0]["miss_distance"]["kilometers"]) + " km")
if field["is_potentially_hazardous_asteroid"]:
print("This object may pose a threat to Earth!")
else:
print("This object should not be dangerous to our planet. We are safe.")
except Exception as e:
print(e)
finally:
print("....................................")
# Asteroids near Earth between given days
def asteroids_given_days():
start_str = input("Enter start date in format YYYY-MM-DD: ")
start = date.fromisoformat(start_str)
end_str = input("Enter end date in format YYYY-MM-DD: ")
end = date.fromisoformat(end_str)
url3 = "https://api.nasa.gov/neo/rest/v1/feed?start_date=" + str(start) + "&end_date=" + str(
end) + "&api_key=DEMO_KEY"
response3 = urllib.request.urlopen(url3)
result3 = json.loads(response3.read())
print(start)
print(end)
print("Between " + str(start) + " and " + str(end) + " " + str(
result3["element_count"]) + " asteroids will be passing near Earth.")
print("")
asteroids = result3["near_earth_objects"]
for asteroid in asteroids:
for field in asteroids[asteroid]:
try:
# name
print("Object name: " + field["name"])
# diameter min
print("Minimal diameter in km: " + str(
field["estimated_diameter"]["kilometers"]["estimated_diameter_min"]))
# diameter max
print("Maximum diameter in km: " + str(
field["estimated_diameter"]["kilometers"]["estimated_diameter_max"]))
# velocity
print("Velocity [km/h]: " + str(
field["close_approach_data"][0]["relative_velocity"]["kilometers_per_hour"]))
# approach time
print("Close Approach date and Time: " + field["close_approach_data"][0]["close_approach_date_full"])
# distance from Earth when passing
print("Object will pass Earth at distance of: " + str(
field["close_approach_data"][0]["miss_distance"]["kilometers"]) + " km")
if field["is_potentially_hazardous_asteroid"]:
print("This object may pose a threat to Earth!")
else:
print("This object should not be dangerous to our planet. We are safe.")
except Exception as e:
print(e)
finally:
print("....................................")
def which():
q = input(
"Enter 1 if you want to check objects passing Earth in the next 7 days. Enter 2 if you want to check specific number of days since today. Enter 3 if you want to specify start date and aend date. ")
if q == "1":
asteroids_seven_days()
restart()
elif q == "2":
asteroids_today_given_days()
restart()
elif q == "3":
asteroids_given_days()
restart()
else:
print("Invalid input. Try again.")
which()
def restart():
again = input("Do you want to check another date? Y/N ")
if again.lower() == "y":
which()
restart()
elif again.lower() == "n":
print("Bye!")
else:
print("Invalid input. Try again.")
restart()
which()