Skip to content

Instantly share code, notes, and snippets.

@davidbrochart
Last active April 25, 2019 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidbrochart/fad080059ba622226d800fd630f2b844 to your computer and use it in GitHub Desktop.
Save davidbrochart/fad080059ba622226d800fd630f2b844 to your computer and use it in GitHub Desktop.
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