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
| """ @Time : 2023/3/31 15:35 @Auth : RS迷途小书童 @File :Batch download of Sentinel data.py @IDE :PyCharm @Purpose :批量下载哨兵数据 """ from sentinelsat import SentinelAPI, read_geojson, geojson_to_wkt
from subprocess import call
from datetime import date import time import xlwt import xlrd2
from tqdm import tqdm def Search_data(login, key, path_geojson, start_date, end_date, name, product_type, cloud, filepath): """ :param login: 欧空局账号,字符串类型 :param key: 欧空局密码,字符串类型 :param path_geojson: 兴趣区路径及文件名 :param start_date: 开始时间,字符串 :param end_date: 结束时间,字符串 :param name: 卫星名称 :param product_type: 卫星类型 :param cloud: 云量筛选,格式:(0,15) :param filepath: Url保存路径及文件名 :return: 返回存有下载链接的excel路径 """ api = SentinelAPI(login, key, 'https://scihub.copernicus.eu/dhus') footprint = geojson_to_wkt(read_geojson(path_geojson)) products = api.query(footprint, date=(start_date, end_date), platformname=name, producttype=product_type, cloudcoverpercentage=cloud) row = 0 workbook_write = xlwt.Workbook(encoding='utf-8') worksheet_write = workbook_write.add_sheet('Url_image') for product in products: info_product = api.get_product_odata(product) worksheet_write.write(row, 0, info_product['url']) worksheet_write.write(row, 1, info_product['title']) print(info_product['title']) row += 1 workbook_write.save(filepath) return filepath, api def Download_image(filepath, Path_Download, Path_IDM, api): workbook_read = xlrd2.open_workbook(filepath) sheet1 = workbook_read.sheet_by_name('Url_image') link_list = sheet1.col_values(0) print('所有链接下载完成,现在开始下载对应数据') num = 0 while link_list: print('---------------------------------------------------') num += 1 print('\n') print('第' + str(num) + '次循环' + '\n') id = link_list[0].split('\'')[1] link = link_list[0] info_product = api.get_product_odata(id) print('查询当前列表里的第一个数据的状态') if info_product['Online']: print(info_product['title'] + '为:online产品') print('加入IDM的下载列表中: ') print('\n') call([Path_IDM, '/d', link, '/p', Path_Download, '/n', '/a']) link_list.remove(link) call([Path_IDM, '/s']) else: print(info_product['title'] + '为:offline产品') print('\n') print('激活offline产品') code_id = link_list[0].split('\'')[1] api.trigger_offline_retrieval(code_id) print('检查任务列表里是否存在online产品: .........') if len(link_list) > 1: link_list_1 = [] for i in range(1, len(link_list)): id2 = link_list[i].split('\'')[1] link_1 = link_list[i] info_product2 = api.get_product_odata(id2) if info_product2['Online']: print(info_product2['title'] + '为Online产品') print('加入IDM的下载列表中: ') print('--------------------------------------------') call([Path_IDM, '/d', link_1, '/p', Path_Download, '/n', '/a']) link_list_1.append(link_1) else: continue if len(link_list_1) > 0: call([Path_IDM, '/s']) for link_2 in link_list_1: link_list.remove(link_2) print('本轮次检查结束,开始等到40分钟') link_list.remove(link) link_list.append(link) for i in tqdm(range(int(1200)), ncols=100): time.sleep(2) if __name__ == "__main__": """说明文档:https://sentinelsat.readthedocs.io/en/latest/api_overview.html, https://scihub.copernicus.eu/userguide/AdvancedSearch""" login = '**********' key = '********' path_geojson = "G:/map.geojson" start_date = "20230101" end_date = "20230301" name = 'Sentinel-2' product_type = 'S2MSI2A' cloud = (0, 15) filepath = 'G:/url.xls' filepath, api = Search_data(login, key, path_geojson, start_date, end_date, name, product_type, cloud, filepath) Download_Path = 'G:/try_download/' Path_IDM = "D:/IDM/IDMan.exe" Download_image(filepath, Download_Path, Path_IDM, api)
|