---
title: "Generate a standard load profile"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Generate a standard load profile}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse  = TRUE,
  comment   = "#>",
  dpi       = 300,
  fig.asp   = 0.618,
  fig.width = 6,
  fig.height = 3,
  fig.align = "center",
  out.width = "90%"
)
```

```{=html}
<style>
  .content h3 {
    margin-top: -30px !important;
  }
  
  details {
    margin-bottom: 40px;
  }
</style>
```

``` {r, message=FALSE, include=FALSE}
library(standardlastprofile)
library(ggplot2)
```

Standard load profiles are crucial for electricity providers, grid operators, and
the energy industry as a whole. They support planning and optimising electricity generation and distribution. Additionally, they serve as the foundation for billing and balancing electricity quantities in the energy
market. For smaller consumers, the financial expense of continuous consumption measurement is often unreasonable. Energy supply companies can therefore use a standard load profile as the basis for creating a consumption forecast.

The aim of this vignette is to show how the algorithm of the `slp_generate()`
function works.[^bdew-1] The data in the `slp` dataset forms the basis for all subsequent steps.

  [^bdew-1]: More information on the algorithm can be found [here](https://www.bdew.de/media/documents/2000131_Anwendung-repraesentativen_Lastprofile-Step-by-step.pdf)

``` {r, message=FALSE}
head(slp)
```

There are 96 x 1/4 hour measurements of electrical power for each unique combination of `profile_id`, `period` and `day`, which we refer to as the "standard load profile". The value for "00:00" indicates the average power consumed between 00:00 and 00:15. The `slp` dataset contains 26,784 observations and covers two generations of profiles published by the German Association of Energy and Water Industries (BDEW Bundesverband der Energie- und Wasserwirtschaft e.V.):

* **1999 profiles** (`H0`, `G0`–`G6`, `L0`–`L2`): based on an analysis of 1,209 load profiles of low-voltage electricity consumers. Each profile uses three seasonal `period` values: `summer`, `winter`, and `transition`.[^bdew-2]
* **2025 profiles** (`H25`, `G25`, `L25`, `P25`, `S25`): an updated set reflecting changes in electricity consumption patterns. Instead of seasons, `period` carries a lowercase month name (`january` … `december`).

  [^bdew-2]: More information on the data and methodology can be found [here](https://www.bdew.de/media/documents/1999_Repraesentative-VDEW-Lastprofile.pdf).

```{r small_multiples_vignette, echo = FALSE, fig.asp = 2.64, fig.retina=2}
#| fig.alt = "Small multiple line chart of 11 standard load profiles
#|  published by the German Association of Energy and Water Industries (BDEW
#|  Bundesverband der Energie- und Wasserwirtschaft e.V.). The lines compare
#|  the consumption for three different periods over a year, and
#|  also compare the consumption between different days of a week."

standardlastprofile:::slp_plot_1999()
```

Those measurements are normalised to an annual consumption of 1,000 kWh. So, if we convert all the quarter-hourly power measurements to energy and sum them for a year, the result is (approximately) 1,000 kWh/year.

```{r H0_data, message=FALSE}
library(standardlastprofile)
H0_2026 <- slp_generate(
  profile_id = "H0",
  start_date = "2026-01-01",
  end_date = "2026-12-31"
  )
```

```{r normalization-1, message=FALSE}
sum(H0_2026$watts)
```

'Hold on - didn't you just say 1,000?!', you might be thinking. Yes, you are correct; we must [convert power units into energy units](https://en.wikipedia.org/wiki/Watt#Distinction_between_watts_and_watt-hours). The values returned are 1/4-hour measurements in watts. To convert the values to watt-hours, we must, therefore, divide them by 4. Since one watt-hour is equal to 1/1000 kilowatt-hour, we also divide by 1,000:

```{r normalization-2, message=FALSE}
sum(H0_2026$watts / 4 / 1000)
```

## Units and normalisation

The two generations of profiles come from different source files and use different units:

- The **1999 profiles** (`H0`, `G0`–`G6`, `L0`–`L2`) were published as an Excel file in which every value is already expressed as **average electric power in watts (W)**, normalised so that the annual sum of all 15-minute intervals equals 1,000 kWh.[^data-2]

- The **2025 profiles** (`H25`, `G25`, `L25`, `P25`, `S25`) were published as a separate Excel file in which every value is expressed as **energy consumed in the 15-minute interval in kilowatt-hours (kWh)**, but normalised to an annual consumption of **1,000,000 kWh**.[^data-3]

> To give users a single, consistent interface we convert all values to watts normalised to 1,000 kWh/a. This conversion is applied once, at data-build time, in `data-raw/DATASET.R`. As a result, `slp_generate()` always returns watts regardless of which profile is requested. We can verify that the normalisation holds for a 2025 profile just as it does for a 1999 profile:

[^data-2]: See the source Excel file distributed with the step-by-step guide: <https://www.bdew.de/media/documents/2000131_Anwendung-repraesentativen_Lastprofile-Step-by-step.pdf>

[^data-3]: See the BDEW 2025 publication: <https://www.bdew.de/energie/standardlastprofile-strom/>

```{r units-verify, message=FALSE}
P25_2026 <- slp_generate("P25", "2026-01-01", "2026-12-31")
sum(P25_2026$watts / 4 / 1000)
```

### Converting the output to kWh

The values returned by `slp_generate()` represent **average electric power** during each 15-minute interval. To obtain the **energy consumed** during that interval in kWh you can wrap `slp_generate()` once:

```{r units-wrapper, message=FALSE}
slp_generate_kwh <- \(...) {
  out <- slp_generate(...)
  out$kwh <- out$watts / 4 / 1000
  out
}

H0_kwh <- slp_generate_kwh("H0", "2026-01-01", "2026-12-31")
sum(H0_kwh$kwh)
```

## Algorithm step by step

When you call `slp_generate()`, you generate (surprise!) a standard load profile. These are the steps that are then performed:

1. Generate a date sequence from `start_date` to `end_date`.
2. Map each day to combination of `day` and `period` (1999 profiles: seasonal period; 2025 profiles: calendar month).
3. Use result from 2nd step to extract values from `slp`.[^data-1]

[^data-1]: That is actually a lie. There is an internal data object from which the data is extracted for efficiency.

4. Apply polynomial function to values of profile identifiers `H0`, `H25`, `P25`, and `S25`.
5. Return data.

### Generate a date sequence

In the initial step, a date sequence is created from `start_date` to `end_date` based on the user input. Here's a simple example:

```{r date_seq-1, message=FALSE, echo=TRUE}
start <- as.Date("2023-12-22")
end <- as.Date("2023-12-27")

(date_seq <- seq.Date(start, end, by = "day"))
```

### Map each day to a period and a weekday

The measured load profiles analysed in the study showed that electricity
consumption across all groups fluctuates both over the period of a year and
over the days within a week. For the **1999 profiles**, the `period` definition is:

* `summer`: May 15 to September 14
* `winter`: November 1 to March 20
* `transition`: March 21 to May 14, and September 15 to October 31

> For the **2025 profiles**, each calendar month is treated as its own period
> (`january` … `december`) rather than grouping months into seasons.

The 1999 study also found no significant difference in consumption on
weekdays from Monday to Friday for any group. For this reason, the days Monday
to Friday are grouped together as `workday`. December 24th and 31st are
considered Saturdays too if they are not Sundays. Public holidays are regarded as Sundays.

**Note**: The function `slp_generate()` supports by default nationwide
public holidays for Germany. Those were retrieved from the
[nager.Date API](https://github.com/nager/Nager.Date):

* New Year's (Jan 1)
* Good Friday
* Easter Monday
* Labour Day (May 1)
* Ascension Day
* Whit Monday
* German Unity Day (Oct 3)
* Christmas Day (Dec 25)
* Boxing Day (Dec 26)

> State-level holidays are **not** included by default, as these vary by state
> and can change over time. Use the optional `holidays` argument to pass your
> own vector of dates and take full control over which dates are treated as
> public holidays; the built-in data are then ignored entirely. See the
> [README](https://flrd.github.io/standardlastprofile/index.html#public-holidays) for an
> example of how to fetch state-level holidays from the
> [nager.Date API](https://date.nager.at) and pass them to `slp_generate()`.

The result of this second step is a mapping from each date to a so-called
characteristic profile day, i.e. a combination of weekday and period:

```{r characteristic_days, message=FALSE}
wkday_period <- standardlastprofile:::get_wkday_period(date_seq)
data.frame(input = date_seq, output = wkday_period)
```

### Assign consumption values to each day

The third step is to assign the measurements we know from the `slp` dataset to
each characteristic profile day. This is the job of the `slp_generate()` function:

``` {r G5_data_vignette, echo=TRUE}
G5 <- slp_generate(
  profile_id = "G5",
  start_date = "2023-12-22",
  end_date = "2023-12-27"
  )
```

This function returns a data frame with 4 columns:

```{r}
head(G5)
```

The data analysis revealed that load fluctuations for both commercial and agricultural customers remain moderate throughout the year. Specifically, for customers and customer groups labelled as `G0` to `G6` and `L0` to `L2`, the standard load profile can be accurately derived from the nine characteristic profile day combinations (3 day types × 3 seasonal periods) available in the dataset `slp`.

Below is the code snippet from the [README](https://github.com/flrd/standardlastprofile#generate-a-load-profile),
which can be used to reproduce the plot for the G5 profile, showcasing the
algorithm's outcome:

``` {r G5_plot_vignette, echo=TRUE, eval=TRUE, message=FALSE, fig.retina=2, fig.asp=0.5}
#| fig.alt = "Line plot of the BDEW standard load profile 'G5' (Bakery
#|  with a bakehouse) from December 22nd to December 27th 2023; values
#|  are normalized to an annual consumption of 1,000 kWh."
library(ggplot2)
ggplot(G5, aes(start_time, watts)) +
  geom_line(color = "#0CC792") +
  scale_x_datetime(
    date_breaks = "1 day",
    date_labels = "%b %d") +
  scale_y_continuous(NULL, labels = \(x) paste(x, "W")) +
  labs(
    title = "'G5': bakery with bakehouse",
    subtitle = "1/4h measurements, based on consumption of 1,000 kWh/a",
    caption = "data: www.bdew.de",
    x = NULL) +
  theme_minimal() +
  theme(
    panel.grid.minor.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.grid = element_line(
      linetype = "12",
      lineend = "round",
      colour = "#FAF6F4"
      )
  ) +
  NULL
```

As you can see, the values in 2023 for December 24 (a Sunday) and
December 25 and 26 (both public holidays) are identical.

### Special case: H0, H25, P25, S25

In contrast to most commercial and agricultural businesses, which have a
relatively even and constant electricity consumption throughout the year,
household electricity consumption decreases from winter to summer and
vice versa (at least in Germany). Because of the distinctive annual load
profile characteristics of household customers, we contend that these customers
cannot be adequately described through a static representation using characteristic
days alone. Consequently, the values in the `slp` dataset for `H0`, `H25`, `P25`, and `S25` serve as
base values to be scaled by a dynamization factor.

This is taken into account when you call `slp_generate()`. The study suggested
the application of a 4th order polynomial function to the values of these
profiles.

$$
w_d = w_s \times(-3.92\mathrm{e}{-10} \times d^4 + 3.20\mathrm{e}{-7} \times  d^3 - 7.02\mathrm{e}{-5} \times d^2 + 2.10\mathrm{e}{-3} \times d + 1.24)
$$
Where:

* $w_d$ is the resulting 'dynamic' value
* $w_s$ is the 'static' value
* $d$ is the day of the year as integer, starting at 1 on January 1st

The following plot shows how the electrical power develops over the year for
profile `H0`; for a clearer picture, the values are aggregated
at daily level:

``` {r H0_2026_daily, message=FALSE, echo=FALSE, fig.retina=2, fig.asp=0.5}
# aggregate by day of year as decimal number (1 - 365)
H0_2026_daily <- by(H0_2026, INDICES = format(H0_2026$start_time, "%j"), FUN = \(x) {
  data.frame(
    start_time = x[["start_time"]][1],
    watts = mean(x[["watts"]])
  )
})
H0_2026_daily <- do.call(rbind, args = H0_2026_daily)
```


``` {r H0_2026_plot, message=FALSE, echo=FALSE, fig.retina=2, fig.asp=0.5}
#| fig.alt = "Line plot of standard load profile 'H0' (households)
#|  aggregated by day from January 1st to December 31st, 2026. The plot shows that
#|  households have a continuously decreasing load from winter
#|  to summer and vice versa."
ggplot(H0_2026_daily, aes(start_time, watts)) +
  geom_line(color = "#0CC792") +
  scale_y_continuous(NULL, labels = \(x) paste(x, "W")) +
  labs(title = "Dynamic Load Profile 'H0': Households",
       subtitle = "Electrical power per day",
       caption = "data: www.bdew.de",
       x = NULL) +
  theme_minimal() +
  theme(
    panel.grid.minor.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.grid = element_line(
      linetype = "12",
      lineend = "round",
      colour = "#FAF6F4"
      )
    ) +
  NULL
```

This dynamization step produces a representative, dynamic load profile. Finally, the following chart compares the dynamic values with their
static counterparts.[^bdew-3]

  [^bdew-3]: Refer to page 9 in [Anwendung der Repräsentativen VDEW-Lastprofile step-by-step](https://www.bdew.de/media/documents/2000131_Anwendung-repraesentativen_Lastprofile-Step-by-step.pdf).

```{r H0_dynamic, echo=FALSE, message=FALSE, fig.asp=0.8, fig.retina=2}
#| fig.alt = "A plot of standard load profile 'H0' (households)
#|  that shows a comparision between the static values, and their
#|  dynamic counterparts."

# why these days? refer to page 9 in:
# https://www.bdew.de/media/documents/2000131_Anwendung-repraesentativen_Lastprofile-Step-by-step.pdf
lst <- Map(paste,
  list("1997-01", "1996-07", "1997-04"),
  list(17:19, 19:21, 18:20),
  sep = "-")

periods <- c("winter", "summer", "transition")
names(lst) <- periods

days <- c("workday", "saturday", "sunday")
lst <- lapply(lst, setNames, days)

# list to be populated
out <- vector("list", length(periods))
names(out) <- periods

# generates slp/s given params in lst
for(i in periods) {
  out[[i]] <- slp_generate("H0", lst[[i]][[1]], lst[[i]][[3]])
}

# add day column (locale-independent: %u gives 1=Mon…7=Sun)
out <- lapply(out, \(x) {
  wday <- as.integer(format(as.Date(x$start_time), "%u"))
  tmp <- ifelse(wday <= 5, "workday", ifelse(wday == 6, "saturday", "sunday"))
  cbind(x, data.frame(day = tmp))
})

# adds period column
H0 <- lapply(names(out), \(x) {
  cbind(out[[x]], data.frame(period = x))
})

H0 <- do.call(rbind, H0)

# remove date from start_time to make it "%H:%M"
H0$timestamp <- format(H0$start_time, "%H:%M")

# reorder, remove columns we do not need
H0 <- H0[, names(slp)]

# create variable for faceting
H0$type <- "dynamic"

# rbind with subset of H0 from slp
H0_slp <- subset(slp, subset = profile_id == "H0")
H0_slp$type <- "static"

H0_plot <- rbind(H0, H0_slp)

# reorder facets
H0_plot$day <- factor(H0_plot$day, levels = days)

# labeller
label_names <- c(
  "saturday" = "Saturday",
  "sunday" = "Sunday",
  "workday" = "Workday"
)

label_fun <- \(x) label_names[[x]]

# plot
library(ggplot2)
ggplot(H0_plot,
       aes(x = as.POSIXct(paste(Sys.Date(), timestamp)),
           y = watts,
           color = period)) +
  geom_line() +
  facet_grid(day ~ type,
             labeller = labeller(day = as_labeller(label_names))) +
  scale_x_datetime(NULL, date_breaks = "6 hours", date_labels = "%k:%M") +
  scale_y_continuous(NULL, labels = \(x) paste(x, "W")) +
  scale_color_manual(name = NULL,
                     values = c(
                       "winter" = "#961BFA",
                       "summer" = "#FA9529",
                       "transition" = "#0CC792"
                     )) +
  labs(title = "Dynamic vs. Static Values of Standard Load Profile 'H0'",
       subtitle = "96 x 1/4h measurements, based on consumption of 1,000 kWh/a",
       caption = "data: www.bdew.de") +
  theme_minimal() +
  theme(legend.position = "top") +
  theme(strip.text.y.right = element_text(angle = 0)) +
  theme(
    panel.grid.minor.x = element_blank(),
    panel.grid.minor.y = element_blank(),
      panel.grid = element_line(
      linetype = "12",
      lineend = "round",
      colour = "#FAF6F4"
      )
  ) +
  NULL
```
