How to migrate from the old sentinelsat (Copernicus Open Access Hub) to the new Copernicus Data Space Ecosystem?

660 views Asked by At

As you may know, the new Copernicus Data Space Ecosystem is now needs a registration. I mostly change my dates and coordinates on an Excel .csv file and run the script. So far it has always worked perfectly but now with this update its time to change the code. I was using the sentinelsat library.

If someone could please help me to just change this as simple as possible and keep using my system, I would really appreciate it!

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

I tried to change the link and of course the username and password but it does not work. Hope someone has a better solution.

I found this notebook to do the migration but I am struggling: https://github.com/eu-cdse/notebook-samples/blob/c0e0ade601973c5d4e4bf66a13c0b76ebb099805/sentinelhub/migration_from_scihub_guide.ipynb

1

There are 1 answers

0
Ioannis Nasios On

To search for available EO products you don't need an account but to download the product you will need a free registration account.

To search:

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'])

To download all returned products:

# 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)