提问人:msalpiq95 提问时间:10/20/2023 最后编辑:Brian Tompsett - 汤莱恩msalpiq95 更新时间:11/9/2023 访问量:228
如何从旧的 sentinelsat(哥白尼开放获取中心)迁移到新的哥白尼数据空间生态系统?
How to migrate from the old sentinelsat (Copernicus Open Access Hub) to the new Copernicus Data Space Ecosystem?
问:
如您所知,新的哥白尼数据空间生态系统现在需要注册。我主要在 Excel .csv 文件上更改日期和坐标并运行脚本。到目前为止,它一直运行良好,但现在有了此更新,是时候更改代码了。我正在使用 sentinelsat 库。
如果有人能帮助我尽可能简单地改变这一点并继续使用我的系统,我将不胜感激!
def s5papi(latitude, longitude, startdate, enddate, xy):
username = 's5pguest'
password = 's5pguest'
url = 'https://s5phub.copernicus.eu/dhus/'
lat = str(latitude[xy])
lon = longitude[xy]
startdate1 = str(startdate[xy])
enddate1 = str(enddate[xy])
point = "intersects("+str(lat)+", "+str(lon)+")"
#path_geojson = Path('footprint/Turkmenistan.geojson')
api = SentinelAPI(username, password, url)
# footprint = geojson_to_wkt(read_geojson(path_geojson))
tqdm.write('Fetching products...\n')
products = api.query(date=(startdate1, enddate1),
footprint=point,
platformname='Sentinel-5 Precursor',
producttype='L2__CH4___')
if len(products) == 0:
tqdm.write('No products found\n')
return
else:
tqdm.write(str(len(products))+'Products found\n'+'Fetching Done\n')
tqdm.write('Total product size :'+str(api.get_products_size(products))+' GB\n')
tqdm.write('Downloading products...\n')
api.download_all(products, directory_path=Path('data'))
tqdm.write('Download finished.\n')
products_df = api.to_dataframe(products)
while xy > 0: #when xy turn to 1, stops adding the header to the data.csv
header = False
break
else:
header = True
products_df.to_csv(Path('data.csv'), mode="a", header = header) for each downloaded product
return products_df
我试图更改链接,当然还有用户名和密码,但它不起作用。希望有人有更好的解决方案。
我找到了这个笔记本来进行迁移,但我正在挣扎:https://github.com/eu-cdse/notebook-samples/blob/c0e0ade601973c5d4e4bf66a13c0b76ebb099805/sentinelhub/migration_from_scihub_guide.ipynb
答:
1赞
Ioannis Nasios
11/9/2023
#1
要搜索可用的EO产品,您不需要帐户,但要下载产品,您需要一个免费的注册帐户。
要搜索:
import os, requests
from datetime import datetime, timedelta
import pandas as pd
# set the timerange
N_DAYS_AGO = 5
today = datetime.now().date()
n_days_ago = today - timedelta(days=N_DAYS_AGO)
start_date = n_days_ago
end_date = today
data_collection = "SENTINEL-2"
# set your area of interest
aoi = "POLYGON((long0 lat0,long1 lat1,......,long0 lat0))'"
# make the request
json = requests.get(f"https://catalogue.dataspace.copernicus.eu/odata/v1/Products?$filter=Collection/Name eq '{data_collection}' and OData.CSC.Intersects(area=geography'SRID=4326;{aoi}) and ContentDate/Start gt {start_date}T00:00:00.000Z and ContentDate/Start lt {end_date}T00:00:00.000Z&$top=1000").json()
df=pd.DataFrame.from_dict(json['value'])
要下载所有退回的产品:
# you will probably need to install package creds
from creds import *
def get_keycloak(username: str, password: str) -> str:
data = {
"client_id": "cdse-public",
"username": username,
"password": password,
"grant_type": "password",
}
try:
r = requests.post("https://identity.dataspace.copernicus.eu/auth/realms/CDSE/protocol/openid-connect/token",
data=data)
r.raise_for_status()
except Exception as e:
raise Exception(
f"Keycloak token creation failed. Response from the server was: {r.json()}")
return r.json()["access_token"]
# Import credentials
keycloak_token = get_keycloak('Your username', 'Your password')
session = requests.Session()
session.headers.update({'Authorization': f'Bearer {keycloak_token}'})
download_dir ='directory to download all products'
for i in range(len(df)):
pr = df.Id.values[i]
prName = df.Name.values[i][:-5]
url = f"https://catalogue.dataspace.copernicus.eu/odata/v1/Products({pr})/$value"
response = session.get(url, allow_redirects=False)
while response.status_code in (301, 302, 303, 307):
url = response.headers['Location']
response = session.get(url, allow_redirects=False)
file = session.get(url, verify=False, allow_redirects=True)
with open(f"{download_dir}{prName}.zip", 'wb') as p:
p.write(file.content)
评论