forked from wmpg/WesternMeteorPyLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdateOrbitFiles.py
79 lines (49 loc) · 1.89 KB
/
UpdateOrbitFiles.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
""" Updates asteroid and comet orbit files from MPC and JPL website. """
from __future__ import print_function, division, absolute_import
import os
import sys
import ssl
import socket
import shutil
# Fix certificates error
ssl._create_default_https_context = ssl._create_unverified_context
if sys.version_info.major < 3:
import urllib as urllibrary
else:
import urllib.request as urllibrary
def updateOrbitFiles():
""" Updates asteroid and comet orbit files from MPC and JPL website. """
# Set a 15 second connection timeout so the compile is not held by a hanging download
socket.setdefaulttimeout(15)
# Comets
comets_url = "https://ssd.jpl.nasa.gov/dat/ELEMENTS.COMET"
# Amors
amors_url = "http://cgi.minorplanetcenter.net/cgi-bin/textversion.cgi?f=lists/Amors.html"
# Apollos
apollos_url = "http://cgi.minorplanetcenter.net/cgi-bin/textversion.cgi?f=lists/Apollos.html"
# Atens
atens_url = "http://cgi.minorplanetcenter.net/cgi-bin/textversion.cgi?f=lists/Atens.html"
dir_path = os.path.split(os.path.abspath(__file__))[0]
dir_path = os.path.join(dir_path, 'wmpl', 'share')
file_names = ['ELEMENTS.COMET', 'Amors.txt', 'Apollos.txt', 'Atens.txt']
url_list = [comets_url, amors_url, apollos_url, atens_url]
# Temporary file path
temp_path = os.path.join(dir_path, "temp.part")
# Download all orbit files
for fname, url in zip(file_names, url_list):
print('Downloading {:s}...'.format(fname))
tries = 0
while tries < 5:
# Download the file to a temporary location (as not to overwrite the existing file)
try:
# Download the file to a temporary location
urllibrary.urlretrieve(url, temp_path)
# Move the downloaded file to a final location
shutil.move(temp_path, os.path.join(dir_path, fname))
break
except Exception as e:
print("Download failed with:" + repr(e))
tries += 1
print(' ... done!')
if __name__ == "__main__":
updateOrbitFiles()