Basic stats for Zone Substations - all csv files in /ZoneSubData folder

Processing 2021-2022 data

Comment out mapping code for now…..

Processing the csv data files……..

base_folder <- getwd()
data_folder <- paste(base_folder,"/ZoneSubData/",sep = "")
file_pattern = ".csv"
# threshold normally = 0, but allow to change to simulate effect of extra solar
# e.g. threshold of 0.1 MW, would simulate effect of extra solar
# to do more accurately would require solar generation curve for period 
threshold = 0.0

# Specify empty vectors in data.frame to store results
ZS_analysis <- data.frame(zone_sub = character(),
                         total_GWh = numeric(),
                         min_load = numeric(),
                         max_load = numeric(),
                         mean_load = numeric(),
                         median_load = numeric(),
                         neg_slots = numeric(),
                         neg_days = numeric(),
                         total_neg_GWh = numeric(),
                         total_pos_GWh = numeric()
                         )

# create list of files matching the pattern
file_list <- as.list(list.files(path=data_folder,pattern = file_pattern)) 

setwd(data_folder)

for (filename in file_list) {
#  print(sprintf("Processing %s", filename))

  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)
  
  reverse_flow <- zs_load[zs_load$MW < threshold, ]
  positive_flow <- zs_load[zs_load$MW >= threshold,]
  # 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)
  daily_MW_spread <- summarize_by_time(zs_load,
                                       .date_var = datetime,
                                       .by       = "day",
                                       MWh = sum(MW)/2,
                                       Min_MW = min(MW),
                                       Max_MW = max(MW),
                                       Spread_MW = max(MW)-min(MW))
  
  # Accumulate each Zone Sub basic analysis
    ZS_analysis <- ZS_analysis %>% add_row(
      zone_sub = filename,
      total_GWh = sum(zs_load$MW)/2000,
      min_load = min(zs_load$MW),
      max_load = max(zs_load$MW),
      mean_load = mean(zs_load$MW),
      median_load = median(zs_load$MW),
      neg_slots = sum(zs_load$MW<threshold),
      neg_days = nrow(reverse_flow_days),
      total_neg_GWh = sum(reverse_flow$MW/2000),
      total_pos_GWh = sum(positive_flow$MW/2000)
  )
}
run_time <- sprintf("%s",Sys.time())

setwd(base_folder)

write.csv(ZS_analysis,sprintf("ZS_analysis_summary:%s.csv",run_time), row.names = FALSE)

Summary data for Zone Substations

Note full historical data for all Noosa LGA Zone Substations

ie Black Mountain, Cooran, Cooroy, Noosaville, Tewantin & Peregian

- note Peregian Zone Sub - serves approx 20% Noosa LGA

All other Zone Substations in Energex area - just latest, ie Jul 2020 - Jun 2021

Use the search box to find your Zone Substation…….

datatable(ZS_analysis,
          rownames = FALSE,
          width = '100%',
#          class = 'stripe',
#          container = sketch,
          # caption = htmltools::tags$caption(
          #   style = 'caption-side: bottom; text-align: center;',
          #   'Table 2: ', htmltools::em('This is a simple caption for the table.')),
          options = list(
  #                        scrollY = '500px',
  #                       scrollX = TRUE,
  #                       pageLength = 20,
  #                       lengthMenu = c(10, 20, 50),
  #                       dom = 'fltip',     # show table, info & paging, fliter
  #                       autoWidth = TRUE,
                         columnDefs = list(list(width = '20px', targets = "_all"))
                           )
) %>% formatRound(2:6, 2)  %>% formatRound(9:10, 2)

Now show map of Zone Substations

maybe try with Leaflet instead of tmap to see if can get some better control of initial viewport….

# define some global variables for folders
base_folder <- getwd()

# will need to fix this hard coding.......
maps_folder = "/Users/geremida/Dropbox/ZENE/R/EQld_NetworkMaps/Energex_maps/"

myZoneSub <- st_read(dsn = paste0(maps_folder,"EnegexZoneSub.shp"))
## Reading layer `EnegexZoneSub' from data source 
##   `/Users/geremida/Dropbox/ZENE/R/EQld_NetworkMaps/Energex_maps/EnegexZoneSub.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 324 features and 8 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 151.9472 ymin: -28.1766 xmax: 153.5302 ymax: -25.94345
## Geodetic CRS:  WGS 84
myLGA <- readRDS(file=paste0(maps_folder,"Queensland_LGA_map.rds"))

# tmap_mode(mode = "view")
# 
# tm_shape(myLGA) + tm_polygons(col = "MAP_COLORS", alpha = 0.2,
#                               border.col = "darkmagenta",
#                               id='LGA') +
# tm_shape(myZoneSub)+tm_dots(size=0.1,id='SITE_ALIAS')
# 
# tm_view(set.view = c(153, -27, 10)) # longitude 7, latitude 51, zoom 4 - approx Cabooluture

Map Zone Subs using Leaflet

# Set value for the minZoom and maxZoom settings.
#leaflet(options = leafletOptions(minZoom = 0, maxZoom = 18))




# m <- leaflet() %>%
#   addTiles() %>%  # Add default OpenStreetMap map tiles
#   addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
# 
# setView(m,153,-27,7)   # centre map near Caboolture

leaflet(data = myZoneSub) %>% addTiles() %>%
  addMarkers(~LON, ~LAT, popup = ~as.character(SITE_ALIAS), label = ~as.character(SITE_ALIAS),
             clusterOptions = markerClusterOptions()
             )
# icons <- awesomeIcons(
#   icon = 'ios-close',
#   iconColor = 'black',
#   library = 'ion',
#   markerColor = 'red'
# )
# 
# # https://icons.getbootstrap.com/assets/icons/asterisk.svg
# 
# asterixIcon <- makeIcon(
#   iconUrl = "https://leafletjs.com/examples/custom-icons/leaf-green.png"
#   # ,
#   # iconWidth = 30, iconHeight = 30
#   )
# 
# leaflet() %>% addTiles() %>%
#       addPolygons(data = myLGA,
#                 color = "#444444", weight = 1, smoothFactor = 0.5,
#     opacity = 1.0,
#     fillOpacity = 0.5,
#     label = ~as.character(LGA)
#     ) %>%
#   addAwesomeMarkers(data = myZoneSub,
#     ~LON, ~LAT, popup = ~as.character(SITE_ALIAS), label = ~as.character(SITE_ALIAS),
#              clusterOptions = markerClusterOptions(),
#              icon = asterixIcon
#   )
# 
# 
# leaflet() %>% addTiles() %>%
#     addPolygons(data = myLGA,
#                 color = "#444444", weight = 1, smoothFactor = 0.5,
#     opacity = 1.0,
#     fillOpacity = 0.5,
#     label = ~as.character(LGA)
#     )



  # addMarkers(~LON, ~LAT, popup = ~as.character(SITE_ALIAS), label = ~as.character(SITE_ALIAS),
  #            clusterOptions = markerClusterOptions()  )

Map Zone Substation locations to Local Government Area

and display

# myZoneSubLGA <- myZoneSub %>% mutate(
#   intersection = as.integer(st_intersects(myZoneSub,myLGA)),
#     LGA_loc = if_else(is.na(intersection), '', myLGA$LGA[intersection])
# )
# # for code example used see
# # https://gis.stackexchange.com/questions/282750/identify-polygon-containing-point-with-r-sf-package/343477#343477
# 
# datatable(myZoneSubLGA[,c(3,11)],
#           rownames = FALSE,
#           width = '100%')

Next task - fuzzy match Zone Sub names from Zone Sub map data to Zone Sub Load Data - csv file

Here’s some sample code to try https://github.com/Adamishere/Fuzzymatching/blob/master/Fuzzy%20String%20Match%20FunctionV1.R

Once this is done, could have a pop-up from the map to display Zone Sub summary data Or link to some place to show Zone Sub data This last bit may have to be a Shiny App :-) Or could possibly do it on a subset, say in a smal LGA like Noosa