使用Python and shell 批量下载哨兵一号(sentinel-1)的精密轨道数据(precise orbit data)

说明:本文只为记录脚本使用过程

提示:本文python需要安装request库,需要用到爬虫

由于,欧空局网站更新导致之前的下载精密轨道的网站已经失效,导致数据不能批量下载,目前大多数选择从ASF网站(https://search.asf.alaska.edu)下载。因此,在这为大家提供从欧空局(https://dataspace.copernicus.eu/browser)批量下载精密轨道教程与脚本。
脚本下载链接

一、使用爬虫获取下载链接

代码如下:

# 导入对应的库
import requests
import json
import argparse
import os

# 从脚本获取对应参数 -s 表示开始时间 -e 表示结束时间
parser = argparse.ArgumentParser(description="this is get asf download orbit link script")
parser.add_argument("-s", "--start_time", help="start time ,example:2022-10-31", default='')
parser.add_argument("-e", "--end_time", help="end time ,example:2022-10-31", default='')
args = parser.parse_args()

# 检查目录下是否存在对应文件,其中Downlink_POE_name.txt
# 保存了满足对应时间的所有下载链接,该文件第一列为请求的url,第二列为文件名
files_to_check = ["Id_name.txt", "POE_name.txt", "Downlink_POE_name.txt"]
current_directory = os.getcwd()
for file_name in files_to_check:
    file_path = os.path.join(current_directory, file_name)
    if os.path.isfile(file_path):
        os.remove(file_path)
        
# 打印开始时间与结束时间
print("start time:", args.start_time)
print("end time:", args.end_time)

# 创建请求头
headers = {
        'Accept':'application/json, text/plain, */*',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Host': 'catalogue.dataspace.copernicus.eu',
        'Origin': 'https://dataspace.copernicus.eu',
        'Referer': 'https://dataspace.copernicus.eu/',
        'Sec-Ch-Ua': '"Not/A)Brand"v="99", "Google Chrome";v="115", "Chromium";v="115"',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36'
}

# 发起request请求获取下载链接与文件名
url = 'https://catalogue.dataspace.copernicus.eu/odata/v1/Products?&$filter=(startswith(Name,%27S1%27)%20and%20contains(Name,%27AUX_POEORB%27)%20and%20Online%20eq%20true)%20and%20ContentDate/Start%20ge%20{}T00:00:00.000Z%20and%20ContentDate/Start%20lt%20{}T23:59:59.999Z&$orderby=ContentDate/Start%20desc&$expand=Attributes&$count=True&$top=1000&$expand=Assets&$skip=0'.format(args.start_time, args.end_time)
print("request URL:", url)
res = requests.get(url=url, headers=headers).content
dirt = json.loads(res.decode("utf-8"))
list1 = dirt["value"]
name_list = [d['Name'] for d in list1]
ID_list = [d['Id'] for d in list1]

# 写入文件名
with open('./POE_name.txt', 'w') as fp1:
        for i in name_list:
                name_str = i + "\n"
                fp1.writelines(name_str)
# 写入数据ID
with open('./Id_name.txt', 'w') as fp2:
        for i in ID_list:
                ID_str = i + "\n"
                fp2.writelines(ID_str)

# 写入下载链接
with open('./Downlink_POE_name.txt', 'w') as fp3:
        for i, n in zip(ID_list,name_list):
                Detail_URL = "https://zipper.dataspace.copernicus.eu/odata/v1/Products({})/$value".format(i) + " " + n + '\n'
                fp3.writelines(Detail_URL)
                print(Detail_URL)

该脚本将在当前目录下产生三个文件分别为"Id_name.txt", “POE_name.txt”, “Downlink_POE_name.txt”,其中"Downlink_POE_name.txt"是我们所需要的文件
运行示例(download_poe_orbit.py为我的脚本名称,可以自行命名):

node1@tim~#: ./download_poe_orbit.py -s 2021-02-03 -e 2022-03-02

2.Shell脚本下载数据

这里我们需要提前注册好欧空局的帐号(默认大家已经注册好了)。我们根据欧空局官方提供的下载教程,首先我们获取TOKEN令牌(代码如下),并将其写入到"ACCESS_TOKEN.txt"文件中。此处如果没有令牌会导致使用wget下载失败。

代码如下(示例):

#!/bin/bash

# 获取TOKEN并写入到"ACCESS_TOKEN.txt"文件中 其中$1为脚本的第一个参数(用户名) $2为脚本的第二个参数(密码)
file_to_check="ACCESS_TOKEN.txt"
if [ -e "$file_to_check" ]; then
    echo " $file_to_check already exist"
else
    curl --location --request POST 'https://identity.dataspace.copernicus.eu/auth/realms/CDSE/protocol/openid-connect/token' \
  --header 'Content-Type: application/x-www-form-urlencoded' \
  --data-urlencode 'grant_type=password' \
  --data-urlencode 'username'="$1" \
  --data-urlencode 'password'="$2" \
  --data-urlencode 'client_id=cdse-public' > ASSCEE_TOKEN.txt

fi
first_split=$(cat ASSCEE_TOKEN.txt | cut -d',' -f1)
ACCESS_TOKEN=$(echo "$first_split" | cut -d':' -f2 | tr -d '"')

# while循环下载对应的链接
while IFS=' ' read -r line1 line2; do
    echo $line1
    echo $line2
    wget  --header "Authorization: Bearer $ACCESS_TOKEN" $line1 -O $line2

done < Downlink_POE_name.txt

运行示例wget_orbit.sh为我的脚本名称,可以自行命名):

node1@tim~#: ./wget_orbit.sh your_username your_passwd 

总结

这里有需要的可以将shell脚本使用python的系统命令运行该脚本。
第一次写脚本有代码冗余,可以自行修改。

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值