Hello, I am getting the following error when requesting data from the API:
Error: 500 {"error":{"status":500,"reason":"Internal Server Error","message":"Illegal request to creo://sh_idx_s2l2a_2023_01/Sentinel-2/MSI/L2A/2023/01/13/S2A_MSIL2A_20230113T183731_N0509_R027_T10SGB_20230113T215652.SAFE/GRANULE/L2A_T10SGB_A039492_20230113T183924/IMG_DATA/R60m/T10SGB_20230113T183731_B04_60m.json.gz. HTTP Status: '403'","code":"RENDERER_EXCEPTION"}}
This is the Python code, along with the necessary constants
"""
File to explore Copernicus datasets
"""
# import rasterio
import json
import requests
import pandas as pd
# Local imports
from constants import COLUMNS, EVALSCRIPT, PROCESSING_API, PROXIES, TOKEN_URL
from keys import COPERNICUS_CLIENT_ID, COPERNICUS_CLIENT_SECRET
from oauthlib.oauth2 import BackendApplicationClient
from rasterio.io import MemoryFile
from requests_oauthlib import OAuth2Session
# from datetime import datetime, timedelta
# Create a session
client = BackendApplicationClient(client_id=COPERNICUS_CLIENT_ID)
oauth = OAuth2Session(client=client)
california_bbox = {
"west": -124.409591,
"south": 32.534156,
"east": -114.131211,
"north": 42.009518,
}
# Set the date for processing
date = "2023-01-13"
time_range = {"from": f"{date}T00:00:00Z", "to": f"{date}T23:59:59Z"}
def flatten_list(original_list):
"""
Flattening a list of lists
"""
flat_list = []
for sublist in original_list:
flat_list.extend(sublist)
return flat_list
def get_access_token(oauth):
"""
Gets Sentinel Hub access token
"""
# Get token for the session
token = oauth.fetch_token(
token_url=TOKEN_URL,
client_secret=COPERNICUS_CLIENT_SECRET,
include_client_id=True,
proxies=PROXIES,
)
access_token = token["access_token"]
return access_token
def sentinelhub_compliance_hook(response):
"""
Outputs compliance hook correct
exception
"""
response.raise_for_status()
return response
def parse_raster(content):
"""
Parses the response's content with rasterio
"""
data_dict = {}
data_df = pd.DataFrame([])
with MemoryFile(processing_response.content) as memfile:
with memfile.open() as dataset:
data_array = dataset.read()
for matrix, column in zip(data_array, COLUMNS):
data_dict[column] = matrix.tolist()
data_df.loc[:, column] = flatten_list(matrix)
with open("copernicus-test.json", "w") as file:
json.dump(data_dict, file, indent=4)
data_df.dropna(how="all", inplace=True)
data_df.to_csv("copernicus-test.csv", index=False)
data_df.to_csv("copernicus-df.csv", index=False)
return data_df
if __name__ == "__main__":
# Authenticate, get token and build requests and JSON body
access_token = get_access_token(oauth)
resp = oauth.get(
"https://services.sentinel-hub.com/configuration/v1/wms/instances",
proxies=PROXIES,
)
oauth.register_compliance_hook("access_token_response", sentinelhub_compliance_hook)
print("Managed to get access token")
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
}
payload = {
"input": {
"bounds": {
"properties": {"crs": "http://www.opengis.net/def/crs/EPSG/0/4326"},
"bbox": [
california_bbox["west"],
california_bbox["south"],
california_bbox["east"],
california_bbox["north"],
],
},
"data": [
{"type": "sentinel-2-l2a", "dataFilter": {"timeRange": time_range}}
],
},
"output": {
# Adjust width/height as you need
"width": 1024,
"height": 1024,
"responses": [{"identifier": "default", "format": {"type": "image/tiff"}}],
},
"evalscript": EVALSCRIPT,
}
print("Sending processing request")
processing_response = requests.post(
PROCESSING_API, headers=headers, json=payload, proxies=PROXIES
)
# Print response content for debugging
if processing_response.status_code == 200:
# result = processing_response.json()
data_df = parse_raster(processing_response.content)
print(data_df)
else:
print("Error:", processing_response.status_code, processing_response.text)
The constants are:
PROCESSING_API = "https://sh.dataspace.copernicus.eu/api/v1/process"
TOKEN_URL = "https://identity.dataspace.copernicus.eu/auth/realms/CDSE/protocol/openid-connect/token"
PROXIES = {
"http": None,
"https": None,
}
EVALSCRIPT = """
//VERSION=3
function setup() {
return {
input: ["B01", "B02", "B03", "B04", "B08"],
output: {
bands: 6,
sampleType: "FLOAT32"
}
};
}
function evaluatePixel(sample) {
var NDWI = (sample.B03 - sample.B08) / (sample.B03 + sample.B08);
if (NDWI < 0) {
return [0, 0, 0, 0, 0, 0];
}
var Chl_a = 4.26 * Math.pow(sample.B03/sample.B01, 3.94);
var Cya = 115530.31 * Math.pow(sample.B03 * sample.B04 / sample.B02, 2.38);
var Turb = 8.93 * (sample.B03/sample.B01) - 6.39;
var CDOM = 537 * Math.exp(-2.93*sample.B03/sample.B04);
var DOC = 432 * Math.exp(-2.24*sample.B03/sample.B04);
var Color = 25366 * Math.exp(-4.53*sample.B03/sample.B04);
return [Chl_a, Cya, Turb, CDOM, DOC, Color];
}
"""