-
-
Save davidbrochart/fad080059ba622226d800fd630f2b844 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime, timedelta | |
import subprocess | |
import shutil | |
def download_files(datetime_0, datetime_nb): | |
''' | |
Download files from FTP server. | |
Arguments: | |
- datetime_0: date from which to download. | |
- datetime_nb: number of dates (~files) to download. | |
Returns: | |
- filenames: list of file names to be downloaded. | |
- datetimes: list of dates corresponding to the downloaded files. | |
''' | |
datetimes = [datetime_0 + timedelta(hours=3*i) for i in range(datetime_nb)] | |
urls, filenames = [], [] | |
for dt in datetimes: | |
year = dt.year | |
month = str(dt.month).zfill(2) | |
day = str(dt.day).zfill(2) | |
hour = str(dt.hour).zfill(2) | |
# file name changed suddenly one day | |
if dt < datetime(2012, 11, 7, 6): | |
filename = f'3B42RT.{year}{month}{day}{hour}.7R2.bin.gz' | |
else: | |
filename = f'3B42RT.{year}{month}{day}{hour}.7.bin.gz' | |
urls.append(f'ftp://trmmopen.gsfc.nasa.gov/pub/merged/3B42RT/{year}/' | |
f'{month}/{filename}') | |
filenames.append(filename) | |
with open('trmm_file_list.txt', 'w') as f: | |
f.write('\n'.join(urls)) | |
shutil.rmtree('trmm_data', ignore_errors=True) | |
subprocess.check_call(f'aria2c -x 4 -i trmm_file_list.txt -d trmm_data ' | |
'--continue=true'.split(), | |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | |
return filenames, datetimes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment