Skip to content

Instantly share code, notes, and snippets.

@krishnaglodha
Last active April 3, 2024 17:34
Show Gist options
  • Save krishnaglodha/b6ed3a510b3ff345f4666a23362c49bc to your computer and use it in GitHub Desktop.
Save krishnaglodha/b6ed3a510b3ff345f4666a23362c49bc to your computer and use it in GitHub Desktop.
New Sentinel Data downloader using python
from datetime import date, timedelta
import requests
import pandas as pd
import geopandas as gpd
from shapely.geometry import shape
copernicus_user = os.getenv("copernicus_user") # copernicus User
copernicus_password = os.getenv("copernicus_password") # copernicus Password
ft = "POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))" # WKT Representation of BBOX
data_collection = "SENTINEL-2" # Sentinel satellite
today = date.today()
today_string = today.strftime("%Y-%m-%d")
yesterday = today - timedelta(days=1)
yesterday_string = yesterday.strftime("%Y-%m-%d")
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. Reponse from the server was: {r.json()}"
)
return r.json()["access_token"]
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;{ft}') and ContentDate/Start gt {yesterday_string}T00:00:00.000Z and ContentDate/Start lt {today_string}T00:00:00.000Z&$count=True&$top=1000"
).json()
p = pd.DataFrame.from_dict(json_["value"]) # Fetch available dataset
if p.shape[0] > 0 :
p["geometry"] = p["GeoFootprint"].apply(shape)
productDF = gpd.GeoDataFrame(p).set_geometry("geometry") # Convert PD to GPD
productDF = productDF[~productDF["Name"].str.contains("L1C")] # Remove L1C dataset
print(f" total L2A tiles found {len(productDF)}")
productDF["identifier"] = productDF["Name"].str.split(".").str[0]
allfeat = len(productDF)
if allfeat == 0:
print("No tiles found for today")
else:
## download all tiles from server
for index,feat in enumerate(productDF.iterfeatures()):
try:
session = requests.Session()
keycloak_token = get_keycloak(copernicus_user,copernicus_password)
session.headers.update({"Authorization": f"Bearer {keycloak_token}"})
url = f"https://catalogue.dataspace.copernicus.eu/odata/v1/Products({feat['properties']['Id']})/$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)
print(feat["properties"]["Id"])
file = session.get(url, verify=False, allow_redirects=True)
with open(
f"{feat['properties']['identifier']}.zip", #location to save zip from copernicus
"wb",
) as p:
print(feat["properties"]["Name"])
p.write(file.content)
except:
print("problem with server")
else :
print('no data found')
requests
pandas
geopandas
shapely
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment