-
Notifications
You must be signed in to change notification settings - Fork 0
/
solar.R
232 lines (170 loc) · 7.92 KB
/
solar.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# uncomment for first run only
#install.packages("reshape")
#install.packages("ggplot2")
#install.packages("lubridate")
library(reshape)
library(ggplot2)
library(lubridate)
#------------------------------------------------------------------------------
INPUT_DIR <- "database"
UNZIP_DIR <- "unzip"
OUTPUT_DIR <- "output"
#------------------------------------------------------------------------------
# pivots the table so that years are in columns
pivotByYear <- function(irradianceTable, valueColumnName) {
dataWithYearsInColumns <- cast(irradianceTable, HOUR_IN_YEAR + MONTH + HOUR_IN_DAY ~ YEAR, value=valueColumnName)
}
# average irradiance for each hour in year
averageByHourInYear <- function(irradiance, includeMonth = TRUE) {
if (includeMonth) {
avgsByHourInYear <- aggregate(
cbind(DIFFUSED, DIRECT, GLOBAL) ~ HOUR_IN_YEAR + MONTH + HOUR_IN_DAY,
data = irradiance, FUN = mean)
} else {
avgsByHourInYear <- aggregate(
cbind(DIFFUSED, DIRECT, GLOBAL) ~ HOUR_IN_YEAR + HOUR_IN_DAY,
data = irradiance, FUN = mean)
}
avgsByHourInYear <- avgsByHourInYear[ order(avgsByHourInYear$HOUR_IN_YEAR), ]
}
# average irradiance for each hour of the day in month
averageByHourOfDayAndMonth <- function(irradiance) {
avgsByHourOfDayAndMonth <- aggregate(
cbind(DIFFUSED,DIRECT) ~ MONTH + HOUR_IN_DAY, data = irradiance, FUN = mean)
avgsByHourOfDayAndMonth <- avgsByHourOfDayAndMonth[ order(avgsByHourOfDayAndMonth$MONTH, avgsByHourOfDayAndMonth$HOUR_IN_DAY), ]
}
# total irradiance for each month
totalByMonth <- function(irradiance) {
irradiance <- averageByHourOfDayAndMonth(irradiance)
sumByMonth <- aggregate(cbind(DIFFUSED,DIRECT) ~ MONTH, data = irradiance, FUN = sum)
sumByMonth <- sumByMonth[ order(sumByMonth$MONTH), ]
}
# total irradiance for each month in each year
totalByMonthAndYear <- function(irradiance) {
# sum by month and year
sumByMonthAndYear <- aggregate(cbind(DIFFUSED,DIRECT) ~ MONTH + YEAR, data = irradiance, FUN = sum)
# transform DIFFUSED and DIRECT columns into IRRADIANCE [type] and VALUE columns
sumByMonthAndYear <- melt(sumByMonthAndYear, measure.vars=c("DIFFUSED","DIRECT"), variable_name="IRRADIANCE")
}
totalByMonthInYears <- function(irradiance) {
sumByMonthAndYear <- totalByMonthAndYear(irradiance)
# pivot by year
sumByMonthInYears <- cast(sumByMonthAndYear, IRRADIANCE + MONTH ~ YEAR, value="VALUE")
# sort by month and irradiance type
sumByMonthInYears <- sumByMonthInYears[ order(sumByMonthInYears$MONTH, sumByMonthInYears$IRRADIANCE), ]
}
extractIrradianceData <- function(inData, skip29thFeb = TRUE) {
dateTimes <- as.POSIXlt(strptime(inData$MESS_DATUM_WOZ, format='%Y%m%d%H:%M')) # 2016063023:00
irradiance <- data.frame(
YEAR = 1900 + dateTimes$year,
MONTH = dateTimes$mon +1, # mon is 0-based
yday = dateTimes$yday, # yday is 0-based
mday = dateTimes$mday, # mday is 1-based
HOUR_IN_DAY = dateTimes$hour, # hour is 0-based
DIFFUSED = as.numeric(inData$DIFFUS_HIMMEL_KW_J),
GLOBAL = as.numeric(inData$GLOBAL_KW_J)
)
irradiance$DIRECT <- irradiance$GLOBAL - irradiance$DIFFUSED
if (skip29thFeb) {
# filter out 29th of Feb for leap years
irradiance <- subset(irradiance, !(MONTH == 2 & mday == 29))
# shift yday by 1 after removing 29th of Feb
years <- 1900 + unique(dateTimes$year)
leapYears <- years[leap_year(years)]
irradiance$yday <- irradiance$yday - (irradiance$YEAR %in% leapYears & irradiance$MONTH > 2)
}
irradiance$HOUR_IN_YEAR <- irradiance$yday*24 + irradiance$HOUR_IN_DAY+1 # yday is 0-based
result <- subset(irradiance, select=-c(mday, yday))
}
extractOtherData <- function(inData) {
dateTimes <- as.POSIXlt(strptime(inData$MESS_DATUM_WOZ, format='%Y%m%d%H:%M')) # 2016063023:00
retult <- data.frame(
STATION = unique(inData$STATIONS_ID),
FIRST_DATE = as.Date(strptime(head(inData$MESS_DATUM_WOZ,n=1), format='%Y%m%d%H:%M')),
LAST_DATE = as.Date(strptime(tail(inData$MESS_DATUM_WOZ,n=1), format='%Y%m%d%H:%M'))
)
}
#-----------------
readSingleFile <- function(fileName, colClasses = NA) {
inData <- read.csv(fileName, header=TRUE, dec=".", sep=";", strip.white=TRUE, na.strings=c("-999", "?"), comment.char="#", colClasses = colClasses)
}
unzipFiles <- function(sourceDirectory, destDirectory) {
files <- list.files(sourceDirectory, pattern=".*\\.zip$", full.names=TRUE, recursive=FALSE)
for (fileName in files) {
unzip(fileName, exdir = destDirectory)
}
}
#-----------------
calculateIrradiance <- function(workingDir = ".", dataFilePattern = "produkt_.*\\.txt$", unzipData = TRUE, plotData = TRUE) {
if (unzipData) {
unzipFiles(file.path(workingDir,INPUT_DIR), file.path(workingDir,UNZIP_DIR))
}
dataFiles <- list.files(path=file.path(workingDir,UNZIP_DIR), pattern=dataFilePattern, full.names=TRUE, recursive=FALSE)
for (dataFile in dataFiles) {
inData <- readSingleFile(dataFile)
irradiance <- extractIrradianceData(inData)
otherData <- extractOtherData(inData)
dataInterval <- sprintf("(%s to %s)", format(otherData$FIRST_DATE), format(otherData$LAST_DATE))
outputDir <- file.path(workingDir, OUTPUT_DIR, paste(otherData$STATION, dataInterval))
if (!dir.exists(outputDir)) {
dir.create(outputDir, recursive = TRUE)
}
# average irradiance for each hour of the day in month
avgsByHourOfDayAndMonth <- averageByHourOfDayAndMonth(irradiance)
write.csv(avgsByHourOfDayAndMonth, file.path(outputDir, "avgsByHourOfDayAndMonth.csv"))
if (plotData) {
p <- ggplot(avgsByHourOfDayAndMonth, aes(x=HOUR_IN_DAY)) +
geom_line(aes(y=DIFFUSED, colour="diffused")) +
geom_point(aes(y=DIFFUSED, colour="diffused")) +
geom_line(aes(y=DIRECT, colour="direct")) +
geom_point(aes(y=DIRECT, colour="direct")) +
facet_wrap(~MONTH) +
ggtitle(sprintf("Avg by hour of the day in each month %s", dataInterval)) +
xlab("Hour") + ylab("Irradiance")
png(file.path(outputDir, "avgsByHourOfDayAndMonth.png"), width=1200, height=800, res=120)
print(p)
dev.off()
}
# total irradiance for each month
sumByMonth <- totalByMonth(irradiance)
write.csv(sumByMonth, file.path(outputDir, "totalByMonth.csv"))
if (plotData) {
p <- ggplot(sumByMonth, aes(x=MONTH)) +
geom_line(aes(y=DIFFUSED, colour="diffused")) +
geom_point(aes(y=DIFFUSED, colour="diffused")) +
geom_line(aes(y=DIRECT, colour="direct")) +
geom_point(aes(y=DIRECT, colour="direct")) +
scale_x_discrete(limits=1:12) +
ggtitle(sprintf("Total irradiance per month %s", dataInterval)) +
xlab("Month") + ylab("Irradiance")
png(file.path(outputDir, "totalByMonth.png"), width=800, height=600, res=120)
print(p)
dev.off()
}
# average irradiance for each month in each year
sumByMonthInYears <- totalByMonthInYears(irradiance)
write.csv(sumByMonthInYears, file.path(outputDir, "totalByMonthInYears.csv"))
if (plotData) {
sumByMonthAndYear <- totalByMonthAndYear(irradiance)
# avarage in every 5 years
#sumByMonthAndYear$YEAR <- sumByMonthAndYear$YEAR %/% 5 * 5
#sumByMonthAndYear <- aggregate(value ~ IRRADIANCE + MONTH + YEAR, data = sumByMonthAndYear, FUN = mean)
sumByMonthAndYear <- subset(sumByMonthAndYear, IRRADIANCE=="DIFFUSED")
p <- ggplot(sumByMonthAndYear, aes(x=MONTH)) +
geom_line(aes(y=value, colour=YEAR, group=YEAR)) +
scale_x_discrete(limits=1:12) +
ggtitle(sprintf("Total diffused irradiance per month in each year %s", dataInterval)) +
xlab("Month") + ylab("Irradiance")
#print(p)
png(file.path(outputDir, "totalByMonthInYears-diffused.png"), width=800, height=600, res=120)
print(p)
dev.off()
}
# average irradiance for each hour in year
avgsByHourInYear <- averageByHourInYear(irradiance)
write.csv(avgsByHourInYear, file.path(outputDir, "avgsByHourOfYear-skip29thFeb.csv"))
irradiance <- extractIrradianceData(inData, skip29thFeb = FALSE)
avgsByHourInYear <- averageByHourInYear(irradiance, includeMonth=FALSE)
write.csv(avgsByHourInYear, file.path(outputDir, "avgsByHourOfYear-noSkip-noMonth.csv"))
}
}