# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Single Scenario

import json  # needed to load in our scenario
import os  # to handle file paths across operating systems
from src.post_and_poll import get_api_results
API_KEY = 'DEMO_KEY'  # REPLACE WITH YOUR API KEY

# following is not necessary but silences warnings:
# InsecureRequestWarning: Unverified HTTPS request is being made to host 'developer.nrel.gov'. Adding certificate verification is strongly advised.
import urllib3
urllib3.disable_warnings()

# `get_api_results` POST's your JSON to the API `job` endpoint, which provides a `run_uuid` if the input is valid, and then polls the `results` endpoint using the `run_uuid` until the results come back with a status other than `Optimizing...`.
#
# `get_api_results` also saves the results to the `results_file`.
#
# A log file is also created in the current working directory.

post = json.load(
    open(
        os.path.join("inputs", "Scenario_POST1.json"), 
    "r")
)

results = get_api_results(
    post=post,
    API_KEY=API_KEY,
    api_url="https://developer.nrel.gov/api/reopt/v1",
    results_file=os.path.join("outputs", "my_results.json")
)

# (Note the above logging shows a local host URL, which was used to run this notebook without requiring an API_KEY)

# ### Here are some results keys examples:

results.keys()

results["outputs"]["Scenario"]["status"]

for k in results["outputs"]["Scenario"]["Site"].keys():
    print(k)

for k in results["outputs"]["Scenario"]["Site"]["PV"].keys():
    print(k)

# # Multi-scenario
# Multiple scenarios can be defined in a CSV file with rows for each scenrario and columns for the inputs. `all_api_inputs.csv` provides a template for all of the possible header values (input keys).
#
# Let's take a look at the example `scenarios.csv`:

# +
import pandas as pd  # only used to show the csv file

df = pd.read_csv(os.path.join("inputs", "scenarios.csv"))

df.head()
# -

# We have two Scenarios:
# 1. One with no PV nor Wind evaluated (by setting their `max_kw`s to zero)
# 2. One with PV and no Wind evaluated
#
# Both Scenarios have the same location, the same electricity tariff (set via the `urdb_label`), and use the same custom load profile - which is passed in via another csv file.

"""
Here are some convenience definitions for using the Multi-scenario capabilities
"""
##############################################################################################################
inputs_path = os.path.join(".", 'inputs')
outputs_path = os.path.join(".", 'outputs')
output_template = os.path.join(outputs_path, 'results_template.csv')
output_file = os.path.join(outputs_path, 'results_summary.csv')
##############################################################################################################

# Let's start by converting the `scenarios.csv` into a list of POST's that we can send to the API:

# +
from src.multi_site_inputs_parser import multi_site_csv_parser

path_to_inputs = os.path.join(inputs_path, 'scenarios.csv')

list_of_posts = multi_site_csv_parser(
    path_to_inputs, 
    api_url='https://developer.nrel.gov/api/reopt/v1', 
    API_KEY=API_KEY
)
# -

# Now we can collect all of the results using the `get_api_results` function from the Single Scenario example:

# +
responses = []

for post in list_of_posts:
    responses.append(
        get_api_results(
            post, 
            results_file=os.path.join(outputs_path, post['Scenario']['description'] + '.json'),
            api_url="https://developer.nrel.gov/api/reopt/v1", 
            API_KEY=API_KEY
        )
    )
# -

# Note that we used the `Scenario.description` to define the results file name. The `Scenario.description` was defined in the `scenarios.csv`.

# ### Summarizing multiple scenario results
#
# There are two options for making a summary of multiple scenarios' resutls:
# 1. Write to a csv using a template with column headers for desired summary keys (scalar values only)
# 2. Write all inputs, outputs, and dispatch to an Excel spreadsheet
#

# #### Option 1: Use a template CSV to collect certain results

# +
from src.parse_api_responses_to_csv import parse_responses_to_csv_with_template

parse_responses_to_csv_with_template(
    csv_template=output_template, 
    responses=responses, 
    output_csv=output_file, 
    input_csv=path_to_inputs,
    n_custom_columns=2
)
# -

# #### Option 2: Write all results out to an Excel file

# +
from src.parse_api_responses_to_excel import parse_api_responses_to_excel

parse_api_responses_to_excel(responses, spreadsheet='results_summary.xlsx')
# -



StarcoderdataJupyterScriptsDedupFiltered