|
2 | 2 | GRSS GitHub repository and the NAIF FTP server""" |
3 | 3 | import os |
4 | 4 | import time |
| 5 | +import requests |
| 6 | + |
| 7 | +def check_and_download(url, local_path, note, force=False): |
| 8 | + if os.path.exists(local_path) and not force: |
| 9 | + return |
| 10 | + try: |
| 11 | + response = requests.head(url, timeout=30, allow_redirects=True) |
| 12 | + if response.status_code == 200: |
| 13 | + print(f'Downloading {note}...') |
| 14 | + response = requests.get(url, timeout=30) |
| 15 | + with open(local_path, 'wb') as f: |
| 16 | + f.write(response.content) |
| 17 | + else: |
| 18 | + msg = f'File {url} ({note}) does not exist on server. Code: {response.status_code}' |
| 19 | + raise FileNotFoundError(msg) |
| 20 | + except requests.RequestException as e: |
| 21 | + raise ConnectionError(f'Error checking/downloading {note}') from e |
5 | 22 |
|
6 | 23 | # get the path to the directory containing this script |
7 | 24 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
|
11 | 28 |
|
12 | 29 | # get the spice kernels if they are not already present |
13 | 30 | # de430 planets + de431 big16 |
14 | | -mb430_exists = os.path.exists(f'{script_dir}/de430.bsp') |
15 | | -sb431_exists = os.path.exists(f'{script_dir}/sb431-n16s.bsp') |
16 | | -if (not mb430_exists or not sb431_exists): |
17 | | - print('Downloading DE430/431 planet and big16 asteroid kernels...') |
18 | | - os.system((f'curl --connect-timeout 30 --show-error -o {script_dir}/de430.bsp ' |
19 | | - f'{NAIF_SITE}/spk/planets/de430.bsp')) |
20 | | - os.system((f'curl --connect-timeout 30 --show-error -o {script_dir}/sb431-n16s.bsp ' |
21 | | - f'{SSD_SITE}/xfr/sb431-n16s.bsp')) |
22 | | -# de440 planets + de441 big16 |
23 | | -mb440_exists = os.path.exists(f'{script_dir}/de440.bsp') |
24 | | -sb441s_exists = os.path.exists(f'{script_dir}/sb441-n16s.bsp') |
25 | | -mb441_exists = os.path.exists(f'{script_dir}/de441.bsp') |
26 | | -sb441_exists = os.path.exists(f'{script_dir}/sb441-n16.bsp') |
27 | | -if (not mb440_exists or not mb441_exists or not sb441s_exists or not sb441_exists): |
28 | | - print('Downloading DE440/441 planet and big16 asteroid kernels...') |
29 | | - os.system((f'curl --connect-timeout 30 --show-error -o {script_dir}/de440.bsp ' |
30 | | - f'{SSD_SITE}/eph/planets/bsp/de440.bsp')) |
31 | | - os.system((f'curl --connect-timeout 30 --show-error -o {script_dir}/sb441-n16s.bsp ' |
32 | | - f'{SSD_SITE}/xfr/sb441-n16s.bsp')) |
33 | | - os.system((f'curl --connect-timeout 30 --show-error -o {script_dir}/de441.bsp ' |
34 | | - f'{SSD_SITE}/eph/planets/bsp/de441.bsp')) |
35 | | - os.system((f'curl --connect-timeout 30 --show-error -o {script_dir}/sb441-n16.bsp ' |
36 | | - f'{SSD_SITE}/eph/small_bodies/asteroids_de441/sb441-n16.bsp')) |
| 31 | +check_and_download(f'{NAIF_SITE}/spk/planets/de430.bsp', |
| 32 | + f'{script_dir}/de430.bsp', 'DE430 planet kernel') |
| 33 | +check_and_download(f'{SSD_SITE}/xfr/sb431-n16s.bsp', |
| 34 | + f'{script_dir}/sb431-n16s.bsp', 'DE431 big16 asteroid kernel') |
| 35 | +# de440/441 planets + de441 big16 |
| 36 | +check_and_download(f'{SSD_SITE}/eph/planets/bsp/de440.bsp', |
| 37 | + f'{script_dir}/de440.bsp', 'DE440 planet kernel') |
| 38 | +check_and_download(f'{SSD_SITE}/xfr/sb441-n16s.bsp', |
| 39 | + f'{script_dir}/sb441-n16s.bsp', 'short-term DE441 big16 asteroid kernel') |
| 40 | +check_and_download(f'{SSD_SITE}/eph/planets/bsp/de441.bsp', |
| 41 | + f'{script_dir}/de441.bsp', 'DE441 planet kernel') |
| 42 | +check_and_download(f'{SSD_SITE}/eph/small_bodies/asteroids_de441/sb441-n16.bsp', |
| 43 | + f'{script_dir}/sb441-n16.bsp', 'long-term DE441 big16 asteroid kernel') |
37 | 44 |
|
38 | | -# get the earth orientation binary spice kernels and their comments if they are not already present |
39 | 45 | # latest earth pck |
40 | 46 | latest_earth_pck_exists = os.path.exists(f'{script_dir}/earth_latest.bpc') |
41 | 47 | latest_earth_pck_old = (latest_earth_pck_exists and |
42 | 48 | time.time() - os.path.getmtime(f'{script_dir}/earth_latest.bpc') |
43 | 49 | > 86400) |
44 | 50 | if not latest_earth_pck_exists or latest_earth_pck_old: |
45 | | - print('Downloading latest Earth binary PCK...') |
46 | | - os.system((f'curl --connect-timeout 30 --show-error -o {script_dir}/earth_latest.bpc ' |
47 | | - f'{NAIF_SITE}/pck/earth_latest_high_prec.bpc')) |
| 51 | + check_and_download(f'{NAIF_SITE}/pck/earth_latest_high_prec.bpc', |
| 52 | + f'{script_dir}/earth_latest.bpc', 'latest Earth binary PCK', force=True) |
48 | 53 | # historical earth pck |
49 | | -historical_earth_pck_exists = os.path.exists(f'{script_dir}/earth_historic.bpc') |
50 | | -if not historical_earth_pck_exists: |
51 | | - print('Downloading historic Earth binary PCK...') |
52 | | - os.system((f'curl --connect-timeout 30 --show-error -o {script_dir}/earth_historic.bpc ' |
53 | | - f'{NAIF_SITE}/pck/earth_620120_240827.bpc')) |
| 54 | +check_and_download(f'{NAIF_SITE}/pck/earth_620120_250826.bpc', |
| 55 | + f'{script_dir}/earth_historic.bpc', 'historic Earth binary PCK') |
54 | 56 | # predicted earth pck |
55 | | -predicted_earth_pck_exists = os.path.exists(f'{script_dir}/earth_predict.bpc') |
56 | | -if not predicted_earth_pck_exists: |
57 | | - print('Downloading predicted Earth binary PCK...') |
58 | | - os.system((f'curl --connect-timeout 30 --show-error -o {script_dir}/earth_predict.bpc ' |
59 | | - f'{NAIF_SITE}/pck/earth_200101_990827_predict.bpc')) |
| 57 | +check_and_download(f'{NAIF_SITE}/pck/earth_2025_250826_2125_predict.bpc', |
| 58 | + f'{script_dir}/earth_predict.bpc', 'predicted Earth binary PCK') |
60 | 59 | # moon pck |
61 | | -moon_pa_de440_exists = os.path.exists(f'{script_dir}/moon_pa_de440.bpc') |
62 | | -if not moon_pa_de440_exists: |
63 | | - print('Downloading Moon PCK for DE440...') |
64 | | - os.system((f'curl --connect-timeout 30 --show-error -o {script_dir}/moon_pa_de440.bpc ' |
65 | | - f'{NAIF_SITE}/pck/moon_pa_de440_200625.bpc')) |
| 60 | +check_and_download(f'{NAIF_SITE}/pck/moon_pa_de440_200625.bpc', |
| 61 | + f'{script_dir}/moon_pa_de440.bpc', 'Moon PCK for DE440') |
66 | 62 | # generic frame kernels |
67 | | -generic_frame_kernel_exists = os.path.exists(f'{script_dir}/pck00011.tpc') |
68 | | -if not generic_frame_kernel_exists: |
69 | | - print('Downloading generic frame kernel...') |
70 | | - os.system((f'curl --connect-timeout 30 --show-error -o {script_dir}/pck00011.tpc ' |
71 | | - f'{NAIF_SITE}/pck/pck00011.tpc')) |
| 63 | +check_and_download(f'{NAIF_SITE}/pck/pck00011.tpc', |
| 64 | + f'{script_dir}/pck00011.tpc', 'generic frame kernel') |
0 commit comments