# add libraries here
library("tidyverse")
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.4     ✓ dplyr   1.0.7
## ✓ tidyr   1.1.3     ✓ stringr 1.4.0
## ✓ readr   2.0.1     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library("lubridate")
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library("timetk")
library(data.table)
## 
## Attaching package: 'data.table'
## The following object is masked from 'package:timetk':
## 
##     :=
## The following objects are masked from 'package:lubridate':
## 
##     hour, isoweek, mday, minute, month, quarter, second, wday, week,
##     yday, year
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
## The following object is masked from 'package:purrr':
## 
##     transpose
# Just made an R script to just show reverse power flow for a zone sub
# code copied form CooranZoneSubAnalysis.rmd

# **** A quick excursion added - so that reverse flows for 2021-2022 data can be visualised....
# Copy the next code chunk & process for 2021-2022
# Then do as previous 2020-2021 to match data we have for that period....
# So just copy / paste code from next chunck back to here & change filename

# read Cooran 2021-22 ZS load csv
# create the object zs_load by reading from the csv file
filename = "ZoneSubData/Cooran_EGX_20212022.csv"
zs_load <- read.csv(filename)
zs_load <- zs_load %>% 
  mutate(datetime = 
           as.POSIXct(paste(zs_load$Date, zs_load$Time), format="%d-%b-%y %I:%M:%S %p")) %>% 
  select(datetime,MW)
# 
summary(zs_load$MW)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   -2.07    1.49    2.09    2.10    3.16    6.39
# can get GWh by sum MW - div by 2000 - 2 x 30min slots/hour, MW -> GW
cat(sprintf("GWh for period = %.3f\n", sum(zs_load$MW)/2000))
## GWh for period = 18.448
# count of all zero & negative MW loads
cat(sprintf("Number 30-min periods when load < 0 %.0f\n",sum(zs_load$MW<0) ))
## Number 30-min periods when load < 0 1861
# try "summarize_by_time"

# create a new object representing reverse flow time 30-minute intervals
reverse_flow <- zs_load[zs_load$MW < 0, ]
# plot it just for fun
# plot_time_series(reverse_flow,datetime,MW)

# try "summarize_by_time" for reverse loads
# shows daily MWh exported
reverse_flow_days <- summarize_by_time(reverse_flow,
                                       .date_var = datetime,
                                       .by       = "day",
                                       MWh = sum(MW)/2)
cat(sprintf("Number of days with reverse flow = %.0f\n", nrow(reverse_flow_days)))
## Number of days with reverse flow = 264
# and note that summary shows number of days
# could also try mutate_by_time to create object for more analysis
# plot it just for fun
plot_time_series(reverse_flow_days,
                 datetime,MWh,
                 .title = "Cooran ZS - reverse flows",
                 .x_lab = "Date",
                 .y_lab = "Reverse Flow (MWh)")