openclimatefix / quartz-frontend

Front End repo for the Nowcasting project.
https://openclimatefix.org/projects/nowcasting/
MIT License
100 stars 17 forks source link

delta - pull data for an interesting month with high solar production #297

Open rachel-labri-tipton opened 1 year ago

rachel-labri-tipton commented 1 year ago

Detailed Description

For the delta view prototype, it would be good to run the delta view on data with lots of sunshine (ie. July in the UK).

peterdudfield commented 1 year ago

Would you prefer raw SQL commands, or python code to do the data fetching? Perhaps the python way would be better to learn, it looked something like

# import packages
from nowcasting_datamodel.connection import DatabaseConnection
from nowcasting_datamodel.read.read_gsp import get_gsp_yield
from datetime import datetime

# make connection
url='need-to-set)
connection = DatabaseConnection(url=db_url, echo=True)

with connection.get_session() as session:
    # read database
    gsp_yields = get_gsp_yield(session=session, 
                                                    gsp_ids=[0,1,2,3]
                                                    start_datetime_utc=datetime(2022,7,1)
                                                    gsp_ids=datetime(2022,7,2))

# change to json and save
rachel-labri-tipton commented 1 year ago

personally, I'd prefer working with python to do the fetching. but let's see what @braddf feels like.

peterdudfield commented 1 year ago

prep for meeting

# import packages
from nowcasting_datamodel.connection import DatabaseConnection
from nowcasting_datamodel.read.read_gsp import get_gsp_yield
from nowcasting_datamodel.read.read import get_latest_forecast_for_gsps
from nowcasting_datamodel.models.gsp import GSPYield
from nowcasting_datamodel.models.forecast import Forecast
from datetime import datetime

# make connection
url = "need-to-set"
connection = DatabaseConnection(url=url, echo=True)

# get gsp yields
with connection.get_session() as session:
    # read database
    gsp_yields = get_gsp_yield(
        session=session,
        gsp_ids=range(0, 1),  # could be 0 to 318
        start_datetime_utc=datetime(2022, 7, 1),
        end_datetime_utc=datetime(2022, 7, 2),
    )

    # list of pydantic objects
    gsp_yields = [GSPYield.from_orm(gsp_yield) for gsp_yield in gsp_yields]

# get latest foreasts
with connection.get_session() as session:
    forecasts = get_latest_forecast_for_gsps(
        session=session,
        gsp_ids=range(0, 1),
        historic=True,
        start_target_time=datetime(2022, 7, 1),
        end_target_time=datetime(2022, 7, 2),
    )

    forecasts = [Forecast.from_orm_latest(forecast) for forecast in forecasts]

# plot
import plotly.graph_objects as go

x_pv_live = [gsp_yield.datetime_utc for gsp_yield in gsp_yields]
y_pv_live = [gsp_yield.solar_generation_kw / 1000 for gsp_yield in gsp_yields]

x_forecast = [forecast_value.target_time for forecast_value in forecasts[0].forecast_values]
y_forecast = [
    forecast_value.expected_power_generation_megawatts
    for forecast_value in forecasts[0].forecast_values
]

fig = go.Figure(
    data=[
        go.Scatter(x=x_pv_live, y=y_pv_live, name="pvlive"),
        go.Scatter(x=x_forecast, y=y_forecast, name="forecast"),
    ]
)
fig.show(renderer="browser")