forked from olejandro/times-ts-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.R
126 lines (110 loc) · 4.25 KB
/
main.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Copyright 2019 Energy Modelling Lab ApS
# Copyright 2020 Olexandr Balyk
# Copyright 2021 University College Cork
#
# This file is part of TIMES-TS-Tool.
#
# TIMES-TS-Tool is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# TIMES-TS-Tool is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with TIMES-TS-Tool. If not, see <https://www.gnu.org/licenses/>.
library(dplyr)
library(tidyr)
library(openxlsx)
source("write.R")
source("fetch.R")
source("categorise.R")
source("specify.R")
source("transform.R")
# File locations ----
mopath <- "../../"
subres <- paste(mopath, "SubRES_TMPL/", sep="")
supxls <- paste(mopath, "SuppXLS/", sep="")
subannual <- paste(checkModelPath(supxls), "Scen_SYS_SubAnnual_Data.xlsx", sep="")
syssettings <- paste(mopath, "SysSettings.xlsx", sep="")
# User choices ----
year <- 2018
# Retrieve all the timeseries
ts_data <- fetch_timeseries()
# Categorise all the hours in a year
ts_cats <- categorise_ts(year,syssettings=syssettings)
# Map hours to DayNite, Weekly, and Season time slices
ts_map <- as.data.frame(map_ts(ts_cats))
# Add categories to the timeseries
ts_data <- cbind(ts_cats,ts_data)
### Main dictionary ----
rules <- create_main_dict(mopath)
### Transform and write ----
# Create a workbook
wb <- createWorkbook()
# Add a sheet with timeslice duration
write_TFM(ts_yrfr_data(ts_map[,"DayNite"],unique(rules$Region)), wb, sheet="TimeSlices",
type="INS", fresh_sheet = TRUE)
# Populate the workbook with timeslice data
for (aTarget_Sheet in unique(rules$Target_Sheet))
{
if (!is.na(aTarget_Sheet))
{
print(paste("Processing", aTarget_Sheet, sep = " "))
for (aTS_Level in unique(rules[rules$Target_Sheet==aTarget_Sheet,"TS_Level"]))
{
if (!is.na(aTS_Level))
{
for (aTransformation in unique(rules[which(rules$Target_Sheet==aTarget_Sheet &
rules$TS_Level==aTS_Level),"Transformation"]))
{
if (!is.na(aTransformation))
{
for (aSerie in unique(rules[which(rules$Target_Sheet==aTarget_Sheet &
rules$TS_Level==aTS_Level &
rules$Transformation==aTransformation &
!is.na(rules$Serie)),"Serie"]))
{
current_data <- select(rules[which(rules$Target_Sheet==aTarget_Sheet &
rules$TS_Level==aTS_Level &
rules$Transformation==aTransformation &
rules$Serie==aSerie),],
-Target_Sheet,-TS_Level,-Transformation,-Serie)
transformed_data <- transform_data(current_data,aTransformation,ts_data[aSerie],ts_map[aTS_Level])
if (!is.null(get0("aDataSet")))
{
aDataSet <- rbind(aDataSet,transformed_data)
}
else
{
aDataSet <- transformed_data
}
}
}
}
}
}
# Check if aDataSet is populated
if (!is.null(get0("aDataSet")))
{
# Do rounding
aDataSet$TS_Value <- round(aDataSet$TS_Value,5)
# Move Regions to columns for unique rows (duplicates are dicarded)
pDataSet <- pivot_wider(unique(aDataSet),names_from=Region,values_from=TS_Value)
# Write data to a sheet
write_TFM(pDataSet,wb,aTarget_Sheet,fresh_sheet=TRUE)
print(paste("Created", aTarget_Sheet, sep = " "))
# Empty aDataSet
aDataSet <- NULL
}
}
}
# Save the workbook
if (saveWorkbook(wb, subannual, overwrite = TRUE, returnValue = TRUE)){
print(paste("Successfully created:",subannual))
} else {
print(paste("Failed creating:",subannual))
}