-
Notifications
You must be signed in to change notification settings - Fork 29
/
dataFetch.py
executable file
·294 lines (257 loc) · 11 KB
/
dataFetch.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# dataFetch.py
#
# -------------------------------------------------
# Copyright 2015-2024 Dominic Ford
#
# This file is part of StarCharter.
#
# StarCharter is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# StarCharter is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with StarCharter. If not, see <https://www.gnu.org/licenses/>.
# -------------------------------------------------
"""
Automatically download all the required data files from the internet.
"""
import argparse
import os
import sys
import logging
from typing import Dict, List, Union
def fetch_file(web_address: str, destination: str, force_refresh: bool = False) -> bool:
"""
Download a file that we need, using wget.
:param web_address:
The URL that we should use to fetch the file
:param destination:
The path we should download the file to
:param force_refresh:
Boolean flag indicating whether we should download a new copy if the file already exists.
:return:
Boolean flag indicating whether the file was downloaded. Raises IOError if the download fails.
"""
logging.info("Fetching <{}>".format(destination))
# Check if the file already exists
if os.path.exists(destination):
if not force_refresh:
logging.info("File already exists. Not downloading fresh copy.")
return False
else:
logging.info("File already exists, but downloading fresh copy.")
os.unlink(destination)
# Check whether source URL is gzipped
supplied_source_is_gzipped: bool = web_address.endswith(".gz")
target_is_gzipped: bool = destination.endswith(".gz")
# Try downloading file in both gzipped and uncompressed format, as CDS archive sometimes changes compression
for source_is_gzipped in [supplied_source_is_gzipped, not supplied_source_is_gzipped]:
# Make sure source URL has the right suffix
url: str = web_address
if source_is_gzipped and not supplied_source_is_gzipped:
url = web_address + ".gz"
elif supplied_source_is_gzipped and not source_is_gzipped:
url = web_address.strip(".gz")
# Make sure file destination has the right suffix
destination_download: str = destination
if source_is_gzipped and not target_is_gzipped:
destination_download = destination + ".gz"
elif target_is_gzipped and not source_is_gzipped:
destination_download = destination.strip(".gz")
# Fetch the file with wget
logging.info("Downloading <{}> to <{}>".format(url, destination_download))
try:
# It would be great to use Python's urllib here. But handling connection retries, catching 404 errors,
# preserving file timestamps, etc., all has to be done manually. Oh, and if you want a progress bar...
status: int = os.system("wget '{}' -O '{}'".format(url, destination_download))
if status != 0:
raise IOError("wget returned a non-zero status")
except IOError:
logging.info("wget returned a non-zero status")
logging.info("Download failed; attempting alternative URLs")
continue
# Check that the file now exists
if not (os.path.exists(destination_download) and os.path.getsize(destination_download) > 0):
logging.info("Download failed; attempting alternative URLs")
continue
# Check that file is compressed or uncompressed, as required
if source_is_gzipped and not target_is_gzipped:
logging.info("Uncompressing to <{}>".format(destination))
os.system("gunzip {}".format(destination_download))
elif target_is_gzipped and not source_is_gzipped:
logging.info("Compressing to <{}>".format(destination))
os.system("gzip {}".format(destination_download))
# Check that the file now exists
if not os.path.exists(destination):
raise IOError("Failed to create target file. Is gzip/gunzip installed?")
# Success!
return True
# We didn't manage to download the file...
raise IOError("Could not download file <{}>".format(web_address))
def fetch_required_files(refresh: bool) -> None:
"""
Fetch all the files we require.
:param refresh:
Switch indicating whether to fetch fresh copies of any files we've already downloaded.
:return:
None
"""
# List of the files we require
required_files: List[Dict[str, Union[str, bool]]] = [
{
'url': 'https://ftp.lowell.edu/pub/elgb/astorb.dat.gz',
'destination': 'data/astorb.dat',
'force_refresh': True
},
{
'url': 'https://www.minorplanetcenter.net/iau/MPCORB/CometEls.txt',
'destination': 'data/Soft00Cmt.txt',
'force_refresh': True
},
{
'url': 'https://ssd.jpl.nasa.gov/ftp/eph/planets/ascii/de430/header.430_572',
'destination': 'data/header.430',
'force_refresh': refresh
},
# Definitions of constellation boundaries
# {
# 'url': 'https://cdsarc.u-strasbg.fr/ftp/VI/49/bound_20.dat.gz',
# 'destination': 'data/constellations/downloads/boundaries.dat',
# 'force_refresh': refresh
# },
# {
# 'url': 'https://cdsarc.u-strasbg.fr/ftp/VI/49/ReadMe',
# 'destination': 'data/constellations/downloads/ReadMe',
# 'force_refresh': refresh
# },
# Yale Bright Star Catalog (copy for use in making constellation stick figures)
{
'url': 'https://cdsarc.u-strasbg.fr/ftp/V/50/catalog.gz',
'destination': 'data/constellations/downloads/ybsc.gz',
'force_refresh': refresh
},
# Hipparcos Catalog (copy for use in making constellation stick figures)
{
'url': 'https://cdsarc.u-strasbg.fr/ftp/I/239/hip_main.dat',
'destination': 'data/constellations/downloads/hip_main.dat.gz',
'force_refresh': refresh
},
# Axel Mellinger's Milky Way Panorama 2.0 (licensed for personal use only)
{
'url': 'https://galaxy.phy.cmich.edu/~axel/mwpan2/mwpan2_Merc_2000x1200.jpg',
'destination': 'data/milkyWay/mwpan2_Merc_2000x1200.jpg',
'force_refresh': refresh
},
# HD-DM-GC-HR-HIP-Bayer-Flamsteed Cross Index
{
'url': 'https://cdsarc.u-strasbg.fr/ftp/IV/27A/ReadMe',
'destination': 'data/stars/bayerAndFlamsteed/ReadMe',
'force_refresh': refresh
},
{
'url': 'https://cdsarc.u-strasbg.fr/ftp/IV/27A/catalog.dat',
'destination': 'data/stars/bayerAndFlamsteed/catalog.dat',
'force_refresh': refresh
},
# Yale Bright Star Catalog
{
'url': 'https://cdsarc.u-strasbg.fr/ftp/V/50/ReadMe',
'destination': 'data/stars/brightStars/ReadMe',
'force_refresh': refresh
},
{
'url': 'https://cdsarc.u-strasbg.fr/ftp/V/50/catalog.gz',
'destination': 'data/stars/brightStars/catalog.gz',
'force_refresh': refresh
},
# Gaia DR1
{
'url': 'https://cdsarc.u-strasbg.fr/ftp/I/337/ReadMe',
'destination': 'data/stars/gaiaDR1/ReadMe',
'force_refresh': refresh
},
{
'url': 'https://cdsarc.u-strasbg.fr/ftp/I/337/tgas.dat.gz',
'destination': 'data/stars/gaiaDR1/tgas.dat.gz',
'force_refresh': refresh
},
# Tycho 1
{
'url': 'https://cdsarc.u-strasbg.fr/ftp/I/239/ReadMe',
'destination': 'data/stars/tycho1/ReadMe',
'force_refresh': refresh
},
{
'url': 'https://cdsarc.u-strasbg.fr/ftp/I/239/tyc_main.dat',
'destination': 'data/stars/tycho1/tyc_main.dat',
'force_refresh': refresh
},
{
'url': 'https://cdsarc.u-strasbg.fr/ftp/I/239/hip_main.dat',
'destination': 'data/stars/tycho1/hip_main.dat.gz',
'force_refresh': refresh
},
# Hipparcos catalogue new reduction
{
'url': 'https://cdsarc.u-strasbg.fr/ftp/I/311/ReadMe',
'destination': 'data/stars/hipparcosNewReduction/ReadMe',
'force_refresh': refresh
},
{
'url': 'https://cdsarc.u-strasbg.fr/ftp/I/311/hip2.dat.gz',
'destination': 'data/stars/hipparcosNewReduction/hip2.dat.gz',
'force_refresh': refresh
},
# Tycho 2
{
'url': 'https://cdsarc.u-strasbg.fr/ftp/I/259/ReadMe',
'destination': 'data/stars/tycho2/ReadMe',
'force_refresh': refresh
}
]
# Fetch the various files which make up the Tycho 2 catalogue
file_number: int
for file_number in range(20):
required_files.append({
'url': 'https://cdsarc.u-strasbg.fr/ftp/I/259/tyc2.dat.{:02d}.gz'.format(file_number),
'destination': 'data/stars/tycho2/tyc2.dat.{:02d}.gz'.format(file_number),
'force_refresh': refresh
})
# Fetch the JPL DE430 ephemeris
for year in range(1550, 2551, 100):
required_files.append({
'url': 'https://ssd.jpl.nasa.gov/ftp/eph/planets/ascii/de430/ascp{:04d}.430'.format(year),
'destination': 'data/ascp{:04d}.430'.format(year),
'force_refresh': refresh
})
# Fetch all the files
for required_file in required_files:
fetch_file(web_address=required_file['url'],
destination=required_file['destination'],
force_refresh=required_file['force_refresh']
)
if __name__ == "__main__":
# Read command-line arguments
parser = argparse.ArgumentParser(description=__doc__)
# Add command-line options
parser.add_argument('--refresh', dest='refresh', action='store_true', help='Download a fresh copy of all files.')
parser.add_argument('--no-refresh', dest='refresh', action='store_false', help='Do not re-download existing files.')
parser.set_defaults(refresh=False)
args = parser.parse_args()
# Set up logging
logging.basicConfig(level=logging.INFO,
stream=sys.stdout,
format='[%(asctime)s] %(levelname)s:%(filename)s:%(message)s',
datefmt='%d/%m/%Y %H:%M:%S')
logger = logging.getLogger(__name__)
logger.info(__doc__.strip())
# Fetch all the data
fetch_required_files(refresh=args.refresh)