如何从旧的哨兵卫星(哥白尼开放访问中心)迁移到新的哥白尼数据空间生态系统?

问题描述 投票:0回答:1

如您所知,新的哥白尼数据空间生态系统现在需要注册。我主要更改 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

python sentinel sentinelsat cds.copernicus
1个回答
0
投票

要搜索可用的 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)
© www.soinside.com 2019 - 2024. All rights reserved.