|
| 1 | +import os |
| 2 | +import platform |
| 3 | +from ftplib import FTP |
| 4 | +import sys |
| 5 | + |
| 6 | + |
| 7 | +download_file_obj = None |
| 8 | +read_byte_count = None |
| 9 | +total_byte_count = None |
| 10 | + |
| 11 | + |
| 12 | +def get_padded_count(count, max_count): |
| 13 | + return str(count).zfill(len(str(max_count))) |
| 14 | + |
| 15 | + |
| 16 | +def file_byte_handler(data): |
| 17 | + global download_file_obj, read_byte_count, total_byte_count |
| 18 | + download_file_obj.write(data) |
| 19 | + read_byte_count = read_byte_count + len(data) |
| 20 | + progress_bar(read_byte_count, total_byte_count) |
| 21 | + |
| 22 | + |
| 23 | +def progress_bar(progress, total, prefix="", size=60, file=sys.stdout): |
| 24 | + if total != 0: |
| 25 | + x = int(size * progress / total) |
| 26 | + x_percent = int(100 * progress / total) |
| 27 | + file.write(f" {prefix} [{'='*x}{' '*(size-x)}] {x_percent} % \r") |
| 28 | + file.flush() |
| 29 | + |
| 30 | + |
| 31 | +def download_files(user_email, esa_files): |
| 32 | + global download_file_obj, read_byte_count, total_byte_count |
| 33 | + print("About to connect to ESA science server") |
| 34 | + with FTP("science-pds.cryosat.esa.int") as ftp: |
| 35 | + try: |
| 36 | + ftp.login("anonymous", user_email) |
| 37 | + print("Downloading {} files".format(len(esa_files))) |
| 38 | + |
| 39 | + for i, filename in enumerate(esa_files): |
| 40 | + padded_count = get_padded_count(i + 1, len(esa_files)) |
| 41 | + print("{}/{}. Downloading file {}".format(padded_count, len(esa_files), os.path.basename(filename))) |
| 42 | + |
| 43 | + with open(os.path.basename(filename), 'wb') as download_file: |
| 44 | + download_file_obj = download_file |
| 45 | + total_byte_count = ftp.size(filename) |
| 46 | + read_byte_count = 0 |
| 47 | + ftp.retrbinary('RETR ' + filename, file_byte_handler, 1024) |
| 48 | + print("\n") |
| 49 | + finally: |
| 50 | + print("Exiting FTP.") |
| 51 | + ftp.quit() |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == '__main__': |
| 55 | + |
| 56 | + esa_files = ['SIR_SAR_L2/2019/12/CS_LTA__SIR_SAR_2__20191227T110305_20191227T111751_E001.nc', 'SIR_SAR_L2/2020/03/CS_LTA__SIR_SAR_2__20200329T163208_20200329T164044_E001.nc', 'SIR_SAR_L2/2020/01/CS_LTA__SIR_SAR_2__20200114T203033_20200114T204440_E001.nc', 'SIR_SAR_L2/2019/11/CS_LTA__SIR_SAR_2__20191103T134759_20191103T135125_E001.nc', 'SIR_SAR_L2/2020/02/CS_LTA__SIR_SAR_2__20200204T191657_20200204T192558_E001.nc', 'SIR_SAR_L2/2019/12/CS_LTA__SIR_SAR_2__20191216T215645_20191216T220909_E001.nc', 'SIR_SAR_L2/2020/03/CS_LTA__SIR_SAR_2__20200315T065755_20200315T071241_E001.nc', 'SIR_SAR_L2/2019/10/CS_LTA__SIR_SAR_2__20191030T135252_20191030T135600_E001.nc', 'SIR_SAR_L2/2020/02/CS_LTA__SIR_SAR_2__20200219T081800_20200219T083303_E001.nc', 'SIR_SAR_L2/2020/01/CS_LTA__SIR_SAR_2__20200110T203717_20200110T204612_E001.nc', 'SIR_SAR_L2/2020/04/CS_LTA__SIR_SAR_2__20200409T053748_20200409T054151_E001.nc', 'SIR_SAR_L2/2020/04/CS_LTA__SIR_SAR_2__20200413T053254_20200413T053659_E001.nc', 'SIR_SAR_L2/2020/02/CS_LTA__SIR_SAR_2__20200208T191154_20200208T192117_E001.nc', 'SIR_SAR_L2/2020/03/CS_LTA__SIR_SAR_2__20200319T065300_20200319T070802_E001.nc', 'SIR_SAR_L2/2020/03/CS_LTA__SIR_SAR_2__20200304T175209_20200304T180102_E001.nc', 'SIR_SAR_L2/2019/11/CS_LTA__SIR_SAR_2__20191128T122800_20191128T123212_E001.nc', 'SIR_SAR_L2/2019/10/CS_LTA__SIR_SAR_2__20191009T150801_20191009T151142_E001.nc', 'SIR_SAR_L2/2019/11/CS_LTA__SIR_SAR_2__20191121T231659_20191121T232817_E001.nc', 'SIR_SAR_L2/2020/02/CS_LTA__SIR_SAR_2__20200215T082253_20200215T083741_E001.nc', 'SIR_SAR_L2/2020/01/CS_LTA__SIR_SAR_2__20200121T094259_20200121T095800_E001.nc', 'SIR_SAR_L2/2019/10/CS_LTA__SIR_SAR_2__20191005T151255_20191005T151621_E001.nc', 'SIR_SAR_L2/2020/04/CS_LTA__SIR_SAR_2__20200427T150701_20200427T151544_E001.nc', 'SIR_SAR_L2/2019/10/CS_LTA__SIR_SAR_2__20191024T004201_20191024T005059_E001.nc', 'SIR_SAR_L2/2020/03/CS_LTA__SIR_SAR_2__20200308T174708_20200308T175621_E001.nc', 'SIR_SAR_L2/2020/04/CS_LTA__SIR_SAR_2__20200402T162707_20200402T163602_E001.nc'] |
| 57 | + |
| 58 | + if int(platform.python_version_tuple()[0]) < 3: |
| 59 | + exit("Your Python version is {}. Please use version 3.0 or higher.".format(platform.python_version())) |
| 60 | + |
| 61 | + email = input("Please enter your e-mail: ") |
| 62 | + |
| 63 | + download_files(email, esa_files) |
0 commit comments