# Copyright (c) 2018 Kelvin Say # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. library(lubridate) library(readr) preprocess_Read_Scenario <- function(i.file_name_load = "pattern_double_peak.csv", i.file_path_load = file.path(getwd(), "_Scenarios/ConsumptionPatterns_SunWiz"), i.file_name_insolation = "pvwatts_perth_hourly.csv", i.file_path_insolation = file.path(getwd(), "_Scenarios/Insolation_PVWatts"), i.annual_consumption_kWh = 5600, i.sample_period_sec = 900) { load_profile <- preprocess_Generate_Load_Pattern(file.path(i.file_path_load, i.file_name_load), i.annual_consumption_kWh/365, i.sample_period_sec); fwrite(load_profile, file.path(getwd(), "_Inputs", "HouseholdLoad.csv")) insolation <- preprocess_Generate_Isolation_Pattern(file.path(i.file_path_insolation, i.file_name_insolation), i.sample_period_sec) fwrite(insolation, file.path(getwd(), "_Inputs", "UnitInsolation.csv")) inputs <- list("Load Filename" = i.file_name_load, "Load Filepath" = i.file_path_load, "Insolation Filename" = i.file_name_insolation, "Insolation Filepath" = i.file_path_insolation, "Annual Consumption (kWh)" = i.annual_consumption_kWh, "Sample Period (s)" = i.sample_period_sec) saveRDS(inputs, file.path(getwd(), "_Inputs", "load_insolation_info.rds")) sink(file.path(getwd(), "_Inputs", "load_insolation_info.txt")) print(inputs) sink() } # x <- preprocess_Generate_Load_Pattern(file.path(getwd(), "Inputs/Demand", "pattern_day_focus.csv"), 5600/365, 900) preprocess_Generate_Load_Pattern <- function(i.pattern_file, i.daily_consumption_kWh, i.sample_period_sec) { raw_pattern <- fread(i.pattern_file, data.table = FALSE) raw_pattern$StartDatetime <- as.POSIXct(strptime(raw_pattern$StartDatetime, "%Y-%m-%dT%H:%M:%SZ")) raw_pattern$FinishDatetime <- as.POSIXct(strptime(raw_pattern$FinishDatetime, "%Y-%m-%dT%H:%M:%SZ")) time_start <- raw_pattern$StartDatetime[1] time_end <- raw_pattern$FinishDatetime[length(raw_pattern$FinishDatetime)] time_period <- as.numeric(difftime(raw_pattern$FinishDatetime[1], raw_pattern$StartDatetime[1], units = "secs")) num_samples <- floor(as.numeric(difftime(time_end, time_start, units = "secs")) / i.sample_period_sec) time_series <- time_start + seconds(i.sample_period_sec * (0:(num_samples-1))) raw_pattern$NetPower <- as.integer(raw_pattern$Daily1kWh_LoadWh * 3600 / time_period * i.daily_consumption_kWh) # Massage the last same through to midnight using the first value of the day (needed to get an even transition) num_raw_pattern <- length(raw_pattern$StartDatetime) + 1 raw_pattern[num_raw_pattern,] <- raw_pattern[1,] raw_pattern$StartDatetime[num_raw_pattern] <- raw_pattern$StartDatetime[num_raw_pattern] + days(1) raw_pattern$FinishDatetime[num_raw_pattern] <- raw_pattern$FinishDatetime[num_raw_pattern] + days(1) resample_func <- approxfun(raw_pattern$StartDatetime, raw_pattern$NetPower, method = "linear", f = 0, rule = 2) daily_load_pattern <- data.frame(Time = time_series, NetPower = as.integer(resample_func(time_series))) num_pattern_elements <- length(daily_load_pattern$NetPower) for (i in 1:365) { if (i == 1) { num_samples <- floor(as.numeric(difftime(ymd_hm("201801010000"), ymd_hm("201701010000"), units = "secs")) / i.sample_period_sec) full_time_series <- ymd_hm("201701010000") + seconds(i.sample_period_sec * (0:(num_samples-1))) year_load_pattern <- data.table(Time = full_time_series, NetPower = integer(length(full_time_series))) } index_start <- (i-1) * num_pattern_elements + 1 index_end <- i * num_pattern_elements year_load_pattern$NetPower[index_start:index_end] <- daily_load_pattern$NetPower } return(year_load_pattern) } preprocess_Generate_Isolation_Pattern <- function(i.insolation_file, i.sample_period_sec) { raw_year_insolation <- fread(i.insolation_file, data.table = FALSE) raw_time_series <- ymd_h(paste0("2017", sprintf("%02i", raw_year_insolation$Month), sprintf("%02i", raw_year_insolation$Day), sprintf("%02i", raw_year_insolation$Hour))) raw_insolation_power <- raw_year_insolation$`AC System Output (W)` # Massage the last same through to midnight using the first value of the day (needed to get an even transition) raw_time_series[length(raw_time_series)+1] <- raw_time_series[1] + years(1) raw_insolation_power[length(raw_insolation_power)+1] <- raw_insolation_power[1] time_start <- raw_time_series[1] time_end <- raw_time_series[length(raw_time_series)] num_samples <- floor(as.numeric(difftime(time_end, time_start, units = "secs")) / i.sample_period_sec) time_series <- time_start + seconds(i.sample_period_sec * (0:(num_samples-1))) resample_func <- approxfun(raw_time_series, raw_insolation_power, method = "linear", f = 0, rule = 2) year_insolation <- data.frame(Time = time_series, Power = resample_func(time_series)) return(year_insolation) }