<jupyter_start><jupyter_text>Exploratory Data AnalysisThe purpose of this notebook is to explore potential relationships and build an intuition about the distribution of curtailment events.  Naively, we are looking to find dimensions on which we might cluster or categorize different curtailment events, and to understand any seasonality or time dependence in the data.<jupyter_code>import pandas as pd
import numpy as np
import altair as alt

from src.conf import settings
# Enable Altair to Serve data from disk since we expect to have at least 8760 rows or more
alt.data_transformers.enable('json')

# Uncomment if using JupyterLab (see: https://github.com/altair-viz/altair/issues/1867)
# alt.data_transformers.enable("data_server")
df = pd.concat(
    [
        pd.read_parquet(fp_) for fp_ in settings.DATA_DIR.glob("processed/caiso/*.parquet")
    ]
).tz_convert(tz="US/Pacific")

# Single Year for debugging

# year = 2019
# df = pd.read_parquet(settings.DATA_DIR / f"processed/caiso/{year}.parquet").tz_convert(tz="US/Pacific")
column_map = zip(
    df.columns.tolist(), 
    df.columns.str.replace("\W+", "_").str.lower().tolist()
)

df = df.rename(columns=dict(column_map))<jupyter_output><empty_output><jupyter_text>**Data Dictionary**- load (MW): Total demand across the CAISO for a given interval.- solar (MW): Average interval Solar production- wind (MW): Average interval Wind production- net_load (MW): Load - solar - wind- renewables (MW): Average interval production from solar, wind, biomass, biogas, geothermal and small hydropower- nuclear (MW): Average nuclear production- large_hydro (MW): Average large hydro production- imports (MW): Imports coming into the ISO; note that exports are NOT deducted from imports.- generation (MW): Total generation across all generator types- thermal (MW): non-nuclear and non-geothermal thermal production- load_less_generation_imports_ (MW): data validation column to ensure supply ~ demand- wind_curtailment (MW): Curtailed wind in a given interval- solar_curtailment (MW): Curtailed solar in a given interval<jupyter_code>df.head()<jupyter_output><empty_output><jupyter_text>Since our interval timeseries are not continuous and complete (i.e. significant irregular gaps exist across years), we must convert this to MWh to get additivity.All columns are in MW at 5 minute intervals. MWh are calculated by taking$MWh = MW  5[min]\frac{1[hour]}{60[min]}$<jupyter_code>df_mwh = df * (5/60.)
df_mwh.head()<jupyter_output><empty_output><jupyter_text>We roll up our data to an hourly basis to remove effects from sub-hourly markets and highly localized or temporal weather events.<jupyter_code># Roll up to hourly 8760 by year
hourly_mwh = df_mwh.groupby(
    by=pd.Grouper(freq="H")
)[["solar_curtailment", "solar", "net_load", "load", "generation", "renewables", "wind_curtailment"]].sum()

# Calculate some other potentially interesting metrics

# How much of our load in a given hour was fulfilled by solar generation?
hourly_mwh["pct_solar"] = hourly_mwh["solar"]/hourly_mwh["generation"]

# How much curtailment is occurring for each unit of raw solar generation?
hourly_mwh["curtailment_intensity"] = (hourly_mwh["solar_curtailment"].fillna(0)/hourly_mwh["solar"]).fillna(0)

# Calculate Pct of Total Solar Potential
hourly_mwh["pct_solar_potential"] = (hourly_mwh["solar_curtailment"]/(hourly_mwh["solar"] + hourly_mwh["solar_curtailment"])).fillna(0)

# Calculate Pct of Total Renewable Potential
hourly_mwh["pct_renewable_potential"] = (hourly_mwh["solar_curtailment"]/(
    hourly_mwh["renewables"] + hourly_mwh["solar_curtailment"] + hourly_mwh["wind_curtailment"])
).fillna(0)<jupyter_output><empty_output><jupyter_text>For convenience, we define a subset of data with some non-zero amount of curtailment.<jupyter_code>curtailment_days = hourly_mwh.query("solar_curtailment > 0").reset_index()
curtailment_days = pd.concat(
    [_dat.sort_values("solar_curtailment", ascending=False).reset_index(drop=True).reset_index() for (grp, _dat) in curtailment_days.groupby(by=curtailment_days["timestamp"].dt.year)],
    ignore_index=True
)<jupyter_output><empty_output><jupyter_text>Due to correlation between nearby hours, we are also interested in a daily totals.<jupyter_code>daily_curtailment = curtailment_days.groupby(
    by=pd.Grouper(key="timestamp", freq="D")
)[["solar", "solar_curtailment", "net_load", "load", "generation", "renewables", "wind_curtailment"]].sum()
daily_curtailment["curtailment_intensity"] = daily_curtailment.eval("solar_curtailment/solar")
daily_curtailment.reset_index(inplace=True)<jupyter_output><empty_output><jupyter_text>Exploration: How much annual Curtailment is there?We are primarily interested in finding explanatory variables for curtailment.  We are interested specifically in time dependence at the hour-scale.  The goal of this study is to identify exogenous data sets which could explain a given curtailment day. **Chart Definitions**<jupyter_code># Interactive version to Drill Down Over Fixed Time periods
brush = alt.selection(type='interval', encodings=['x'])

base = alt.Chart(
    hourly_mwh.reset_index()
).mark_rect().encode(
    alt.Y("hours(timestamp):T", title="Hour of Day", sort="descending"), 
    alt.X("yearmonthdate(timestamp):T", title="Date", sort="ascending"),
    alt.Color(
        alt.repeat("row"), 
        type="quantitative", 
        sort="descending", 
        scale=alt.Scale(scheme="redgrey"),
        legend=alt.Legend(orient="top")
    )
).properties(
    width=1500,
    height=250
)

background = base.encode(
    color=alt.value('#ddd')
).add_selection(brush)

highlight = base.transform_filter(brush)

brush_chart = alt.layer(
    background,
    highlight
).repeat(
    row=["solar_curtailment", "solar", "curtailment_intensity"]
).resolve_scale(color="independent")
# A better version of the chart above with units and things but not interactive

basechart = alt.Chart(
    hourly_mwh.reset_index()
).mark_rect().encode(
    alt.Y("hours(timestamp):T", title="Hour of Day", sort="descending"), 
    alt.X("yearmonthdate(timestamp):T", title="Date", sort="ascending"),
).properties(
    width=1500,
    height=250
)

fixed_chart = alt.vconcat(
    basechart.encode(
        alt.Color(
            "solar_curtailment:Q", 
              scale=alt.Scale(type="pow", exponent=1/4, scheme="redgrey"), 
              sort="descending",
              title="Solar Curtailment (MWh)",
              legend=alt.Legend(orient="top"),
         )
    ),
    basechart.encode(
        alt.Color(
            "solar:Q", 
              scale=alt.Scale(type="pow", exponent=2, scheme="redgrey"),
              sort="descending",
              title="Solar Output (MW)",
              legend=alt.Legend(orient="top"),
        )
    ),
    basechart.encode(
        alt.Color(
            "curtailment_intensity:Q", 
            scale=alt.Scale(scheme="redgrey", type="pow", exponent=1/4, domain=[0,1]), 
            sort="descending", 
            title="MWh Cur. per MWh Load",
            legend=alt.Legend(orient="top"),
        )
    )
).resolve_scale(color="independent")
# Let's inspect our curtailed hours a bit more closely
load_chart = alt.Chart(curtailment_days).mark_line().encode(
    alt.X("index", title="rank"),
    alt.Y("solar_curtailment", title="Solar Curtailment (MW)"),
).facet("year(timestamp):N")
curtail_v_solar_production = alt.Chart(curtailment_days).mark_point().encode(
    alt.X("solar", title="Solar Production"),
    alt.Y("solar_curtailment", title="Solar Curtailment (MW)"),
    alt.Color("month(timestamp):N", scale=alt.Scale(scheme="tableau20")),
    alt.Facet("year(timestamp):O", columns=3)
)
base = alt.Chart().mark_point().encode(
    alt.X("monthdate(timestamp):T", title="Date (hour)"),
    alt.Y("curtailment_intensity", title="Curtailment Intensity (MW/MW)"),
    alt.Color("load:Q", scale=alt.Scale(scheme="redyellowblue"), sort="descending", title="Load (MW)"),
    # alt.Facet("year(timestamp):O", columns=3)
)

curtailment_intensity = alt.layer(base, data=curtailment_days.query("curtailment_intensity < 1")).facet("year(timestamp):O")
daily_curtailment_intensity = alt.layer(base, data=daily_curtailment).facet("year(timestamp):O")
daily_curtailment_vs_load = alt.Chart(daily_curtailment).mark_point().encode(
    alt.X("load", title="Load (MWh)"),
    alt.Y("solar_curtailment", title="Daily Solar Curtailment (MWh)"),
    alt.Color("month(timestamp):N", scale=alt.Scale(scheme="tableau20")),
    alt.Facet("year(timestamp):O"),
)<jupyter_output><empty_output><jupyter_text>Curtailment Characteristics<jupyter_code>alt.Chart(daily_curtailment).mark_bar().encode(
    alt.X("yearmonth(timestamp):T", title="Month"),
    alt.Y("mean(solar_curtailment)", title="Mean Curtailment (MWh)")
).interactive()
fixed_chart<jupyter_output><empty_output><jupyter_text>Shown above are- Hourly Solar curtailment in $x^{.25}$ scale to exaggerate visual differences in curtailment scale.- Hourly Solar Output (MW) in $x^{2}$ scale. - Hourly Curtailment "Intensity" w.r.t. Load (MW) in $x^{.25}$ scale. By visual inspection, two large curtailment "zones" or "clusters" appear to exist -- one centered around mid-March, and the other around mid-October.  Another prominent feature is the fast adoption of solar over time.<jupyter_code>daily_curtailment_vs_load<jupyter_output><empty_output><jupyter_text>By inspection, we can see a very rough pattern of high curtailment events during periods of "medium" load.  Intuitively, we might imagine these correspond to days with very high solar production, and very low demand (e.g. sunny in the desert, and temperate in populated centers.)<jupyter_code>load_chart<jupyter_output><empty_output><jupyter_text>Hourly curtailment by year in MW - Over time, the available curtailed capacity has increased drastically (by a factor of 3 from 2016 to 2019).<jupyter_code>curtail_v_solar_production<jupyter_output><empty_output><jupyter_text>One metric of interest may be the amount of solar curtailment per unit of solar production.  Each mark represents a single hour in a year.  By visual inspection, a vague envelope is visible on the right hand side.  The sum of curtailment and production might represent the total solar capacity in a given hour.  By inspection, we can also observe a larger portion of curtailment is occurring year on year.<jupyter_code>curtailment_intensity<jupyter_output><empty_output><jupyter_text>Hourly curtailment per unit of solar production.  A value of 1 represents an hour where 50% of solar capacity is curtailed.<jupyter_code>daily_curtailment_intensity<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12236247174076866, 'fraction_numerical': 0.006970610399397136, 'mean_word_length': 5.315883402736467, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 54, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '413389', 'license': 'MIT', 'n_tokens_mistral': 3465, 'n_tokens_neox': 3281, 'n_words': 870, 'path': 'notebooks/0-ttu-curtailments_eda.ipynb', 'repo_name': 'thomastu/CaReCur', 'size': '10616'}