import json import requests import numpy as np import pandas as pd # Predico instance URL. Switch BASE_URL to the environment you want to use: # - Playground: https://playground.predico.inesctec.pt # - Production: https://predico.inesctec.pt BASE_URL = "https://predico.inesctec.pt" API_URL = f"{BASE_URL}/api/v1" # Authenticate via `/token` endpoint access_token = "your_access_token_here" headers = { 'Authorization': f'Bearer {access_token}', 'Accept': 'application/json' } # Get the session id from `/market/session/` endpoint open_market_session_id = "your_open_market_session_id" # Challenge resource identifier (name): resource_name = "wind_farm_1" # Request the challenges for the open market session: response = requests.get( url=f"{API_URL}/market/challenge", params={'market_session': int(open_market_session_id), 'resource_name': resource_name}, headers=headers, timeout=30 ) # Check if the request was successful if response.status_code == 200: challenges = response.json() else: print("Failed to retrieve challenges.") print(f"Status code: {response.status_code}") exit() # Select the first challenge of the list of challenges previous retrieved # Note that as we're filtering by resource_name, there should be only a # single challenge per session selected_challenge = challenges["data"][0] # Unpack selected challenge information resource_id = selected_challenge["resource"] # challenge resource UUID challenge_id = selected_challenge["id"] # challenge UUID start_datetime = selected_challenge["start_datetime"] # first forecast leadtime end_datetime = selected_challenge["end_datetime"] # last forecast leadtime print(f""" Challenge info: ID: {challenge_id} Resource ID: {resource_id} First forecast leadtime): {start_datetime} Last forecast leadtime): {end_datetime} """) # Create a random 24h submission (random values): # Generate datetime values for the challenge period: datetime_range = pd.date_range(start=start_datetime, end=end_datetime, freq='15min') datetime_range = [x.strftime("%Y-%m-%dT%H:%M:%SZ") for x in datetime_range] # Generate random Q50 (median) values, then derive Q10/Q90 around it so that # Q10 <= Q50 <= Q90 holds at every timestamp (as required by the API). q50 = np.random.uniform(low=0.1, high=0.9, size=len(datetime_range)) quantile_values = { "q10": [round(float(x), 3) for x in np.clip(q50 - 0.1, 0.0, 1.0)], "q50": [round(float(x), 3) for x in q50], "q90": [round(float(x), 3) for x in np.clip(q50 + 0.1, 0.0, 1.0)], } # Prepare submissions for the three quantiles Q10, Q50, Q90: submission_list = [] for qt in ["q50", "q10", "q90"]: qt_forec = pd.DataFrame({ 'datetime': datetime_range, 'value': quantile_values[qt], }) submission_list.append({ "variable": qt, "forecasts": qt_forec.to_dict(orient="records") }) # Your submissions: print("Submission List:") for i, submission in enumerate(submission_list): print("-"*79) print(f"Submission #{i+1}") print(json.dumps(submission, indent=3)) # Submit the forecasts: for submission in submission_list: response = requests.post(url=f"{API_URL}/market/challenge/submission/{challenge_id}", json=submission, headers=headers, timeout=30) # Check if the request was successful if response.status_code == 201: print(f"Forecast submission successful for {submission['variable']} quantile.") else: print(f"Failed to submit forecast for {submission['variable']} quantile.") print(f"Status code: {response.status_code}")