-
Notifications
You must be signed in to change notification settings - Fork 1
/
eGRID.R
351 lines (285 loc) · 16 KB
/
eGRID.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#This code reads in eGRID data from the original Excel spreadsheets and then merges them all together into a single CSV file
#The benefit of this is that when using Google Refine and the rdf-extension, mappings only have to be done once.
library(XLConnect) #reads Excel files
library(plyr) #rbind.fill
library(stringr)
library(sqldf)
#don't use postgres with sqldf, just want to use sqlite. If this package is not removed, it will cause a problem.
if('package:RPostgreSQL' %in% search()){
detach('package:RPostgreSQL')
}
sqldf()
#don't use factors
options(stringsAsFactors = FALSE)
#time how long things take
#http://stackoverflow.com/questions/1716012/stopwatch-function-in-r
tic <- function(gcFirst = TRUE, type=c("elapsed", "user.self", "sys.self"))
{
type <- match.arg(type)
assign(".type", type, envir=baseenv())
if(gcFirst) gc(FALSE)
tic <- proc.time()[type]
assign(".tic", tic, envir=baseenv())
invisible(tic)
}
toc <- function()
{
type <- get(".type", envir=baseenv())
toc <- proc.time()[type]
tic <- get(".tic", envir=baseenv())
print(toc - tic)
invisible(toc)
}
tic()
#only keep text before the first period
cleanHeaders <- function(dataFrame){
#get rid of extra columns while we're here
dataFrame = removeExtraColumns(dataFrame)
colnames(dataFrame) <- str_extract(colnames(dataFrame), "[A-Za-z0-9]+")
return(dataFrame)
}
removeExtraColumns <- function(dataFrame){
#get rid of all columns that look like Col34, etc. These are likely to be empty - not sure why these are extracted
colsToRemove = grep("^Col[0-9]+", colnames(dataFrame))
if (length(colsToRemove) > 0) {
dataFrame = dataFrame[,-colsToRemove]
}
return(dataFrame)
}
if (file.exists('eGRID_All_Files_Year_1996_thru_2010_data.zip') == FALSE) {
download.file('http://www.epa.gov/cleanenergy/documents/egridzips/eGRID_All_Files_Year_1996_thru_2010_data.zip', 'eGRID_All_Files_Year_1996_thru_2010_data.zip')
}
unzip('eGRID_All_Files_Year_1996_thru_2010_data.zip')
#1996 through 2000 need to have their header names fixed
#Also each data frame has the corresponding year connected to it
eGRID_1996_plants <- cleanHeaders(readWorksheetFromFile('eGRID2002yr96_plant.xls',
sheet = 2, header = TRUE, startCol = 1, startRow = 4, endCol=0, endRow=0))
eGRID_1996_plants$year = 1996
xlcFreeMemory()
eGRID_1997_plants <- cleanHeaders(readWorksheetFromFile('eGRID2002yr97_plant.xls',
sheet = 2, header = TRUE, startCol = 1, startRow = 4, endCol=0, endRow=0))
eGRID_1997_plants$year = 1997
xlcFreeMemory()
eGRID_1998_plants <- cleanHeaders(readWorksheetFromFile('eGRID2002yr98_plant.xls',
sheet = 2, header = TRUE, startCol = 1, startRow = 4, endCol=0, endRow=0))
eGRID_1998_plants$year = 1998
xlcFreeMemory()
eGRID_1999_plants <- cleanHeaders(readWorksheetFromFile('eGRID2002yr99_plant.xls',
sheet = 2, header = TRUE, startCol = 1, startRow = 4, endCol=0, endRow=0))
eGRID_1999_plants$year = 1999
xlcFreeMemory()
eGRID_2000_plants <- cleanHeaders(readWorksheetFromFile('eGRID2002yr00_plant.xls',
sheet = 2, header = TRUE, startCol = 1, startRow = 4, endCol=0, endRow=0))
eGRID_2000_plants$year = 2000
xlcFreeMemory()
eGRID_2004_plants <- cleanHeaders(readWorksheetFromFile('eGRID2006V2_1_year04_plant.xls',
sheet = 4, header = TRUE, startCol = 1, startRow = 5, endCol=0, endRow=0))
eGRID_2004_plants$year = 2004
xlcFreeMemory()
eGRID_2005_plants <- cleanHeaders(readWorksheetFromFile('eGRID2007V1_1year05_plant.xls',
sheet = 4, header = TRUE, startCol = 1, startRow = 5, endCol=0, endRow=0))
eGRID_2005_plants$year = 2005
xlcFreeMemory()
eGRID_2007_plants <- cleanHeaders(readWorksheetFromFile('eGRID2010V1_1_year07_PLANT.xls',
sheet = 4, header = TRUE, startCol = 1, startRow = 5, endCol=0, endRow=0))
eGRID_2007_plants$year = 2007
xlcFreeMemory()
eGRID_2009_plants <- cleanHeaders(readWorksheetFromFile('eGRID2012V1_0_year09_DATA.xls',
sheet = 4, header = TRUE, startCol = 1, startRow = 5, endCol=0, endRow=0))
eGRID_2009_plants$year = 2009
xlcFreeMemory()
eGRID_2010_plants <- cleanHeaders(readWorksheetFromFile('eGRID_9th_edition_V1-0_year_2010_Data.xls',
sheet = 4, header = TRUE, startCol = 1, startRow = 5, endCol=0, endRow=0))
eGRID_2010_plants$year = 2010
xlcFreeMemory()
#bind everything together
#rind.fill is needed to fill in missing columns
eGRID_all_years_plants <- rbind.fill(eGRID_1996_plants,
eGRID_1997_plants,
eGRID_1998_plants,
eGRID_1999_plants,
eGRID_2000_plants,
eGRID_2004_plants,
eGRID_2005_plants,
eGRID_2007_plants,
eGRID_2009_plants,
eGRID_2010_plants)
#clean up after ourselves - these are not needed anymore
rm(list=c('eGRID_1996_plants',
'eGRID_1997_plants',
'eGRID_1998_plants',
'eGRID_1999_plants',
'eGRID_2000_plants',
'eGRID_2004_plants',
'eGRID_2005_plants',
'eGRID_2007_plants',
'eGRID_2009_plants',
'eGRID_2010_plants'))
#need to make sure that the year is in the first column - this keeps Google Refine from breaking
currentColumnNameOrder = colnames(eGRID_all_years_plants)
desiredColumnNameOrder = c('year', currentColumnNameOrder[-which(currentColumnNameOrder == 'year')])
eGRID_all_years_plants = eGRID_all_years_plants[,desiredColumnNameOrder]
#replace all "N/A" with NA
eGRID_all_years_plants[eGRID_all_years_plants=="N/A"] <- NA
#The names and coordinates change over time, want to only use the latest value for these
#Often, the names are first in all caps, then in later versions use title case
#For coordinates, it's common to see the point shift slightly.
#get the values for the latest year for each entry - ORISPL is the unique identifier
preferredValues = sqldf('select ORISPL, PNAME, MAX(year) AS LATEST_YEAR, LAT, LON from eGRID_all_years_plants group by ORISPL')
preferredValues$LATEST_YEAR = NULL
colnames(preferredValues) = c('ORISPL', 'preferredName', 'preferredLat', 'preferredLon')
eGRID_all_years_plants = merge(eGRID_all_years_plants, preferredValues, by='ORISPL')
#replace values with the latest
eGRID_all_years_plants$LAT = eGRID_all_years_plants$preferredLat
eGRID_all_years_plants$LON = eGRID_all_years_plants$preferredLon
eGRID_all_years_plants$PNAME = eGRID_all_years_plants$preferredName
#remove extra columns
eGRID_all_years_plants$preferredLat = NULL
eGRID_all_years_plants$preferredLon = NULL
eGRID_all_years_plants$preferredName = NULL
#write table, NA will be just ""
write.table(eGRID_all_years_plants, file="eGRID_all_years_plants.csv", sep="\t", row.names=FALSE, na="")
#1996 through 2000 need to have their header names fixed
#Also each data frame has the corresponding year connected to it
eGRID_1996_boilers <- cleanHeaders(readWorksheetFromFile('eGRID2002yr96_plant.xls',
sheet = 3, header = TRUE, startCol = 1, startRow = 4, endCol=0, endRow=0))
eGRID_1996_boilers$year = 1996
xlcFreeMemory()
eGRID_1997_boilers <- cleanHeaders(readWorksheetFromFile('eGRID2002yr97_plant.xls',
sheet = 3, header = TRUE, startCol = 1, startRow = 4, endCol=0, endRow=0))
eGRID_1997_boilers$year = 1997
xlcFreeMemory()
eGRID_1998_boilers <- cleanHeaders(readWorksheetFromFile('eGRID2002yr98_plant.xls',
sheet = 3, header = TRUE, startCol = 1, startRow = 4, endCol=0, endRow=0))
eGRID_1998_boilers$year = 1998
xlcFreeMemory()
eGRID_1999_boilers <- cleanHeaders(readWorksheetFromFile('eGRID2002yr99_plant.xls',
sheet = 3, header = TRUE, startCol = 1, startRow = 4, endCol=0, endRow=0))
eGRID_1999_boilers$year = 1999
xlcFreeMemory()
eGRID_2000_boilers <- cleanHeaders(readWorksheetFromFile('eGRID2002yr00_plant.xls',
sheet = 3, header = TRUE, startCol = 1, startRow = 4, endCol=0, endRow=0))
eGRID_2000_boilers$year = 2000
xlcFreeMemory()
eGRID_2004_boilers <- cleanHeaders(readWorksheetFromFile('eGRID2006V2_1_year04_plant.xls',
sheet = 2, header = TRUE, startCol = 1, startRow = 6, endCol=0, endRow=0))
eGRID_2004_boilers$year = 2004
xlcFreeMemory()
eGRID_2005_boilers <- cleanHeaders(readWorksheetFromFile('eGRID2007V1_1year05_plant.xls',
sheet = 2, header = TRUE, startCol = 1, startRow = 6, endCol=0, endRow=0))
eGRID_2005_boilers$year = 2005
xlcFreeMemory()
eGRID_2007_boilers <- cleanHeaders(readWorksheetFromFile('eGRID2010V1_1_year07_PLANT.xls',
sheet = 2, header = TRUE, startCol = 1, startRow = 6, endCol=0, endRow=0))
eGRID_2007_boilers$year = 2007
xlcFreeMemory()
eGRID_2009_boilers <- cleanHeaders(readWorksheetFromFile('eGRID2012V1_0_year09_DATA.xls',
sheet = 2, header = TRUE, startCol = 1, startRow = 6, endCol=0, endRow=0))
eGRID_2009_boilers$year = 2009
xlcFreeMemory()
eGRID_2010_boilers <- cleanHeaders(readWorksheetFromFile('eGRID_9th_edition_V1-0_year_2010_Data.xls',
sheet = 2, header = TRUE, startCol = 1, startRow = 6, endCol=0, endRow=0))
eGRID_2010_boilers$year = 2010
xlcFreeMemory()
eGRID_all_years_boilers <- rbind.fill(eGRID_1996_boilers,
eGRID_1997_boilers,
eGRID_1998_boilers,
eGRID_1999_boilers,
eGRID_2000_boilers,
eGRID_2004_boilers,
eGRID_2005_boilers,
eGRID_2007_boilers,
eGRID_2009_boilers,
eGRID_2010_boilers)
#clean up after ourselves - these are not needed anymore
rm(list=c('eGRID_1996_boilers',
'eGRID_1997_boilers',
'eGRID_1998_boilers',
'eGRID_1999_boilers',
'eGRID_2000_boilers',
'eGRID_2004_boilers',
'eGRID_2005_boilers',
'eGRID_2007_boilers',
'eGRID_2009_boilers',
'eGRID_2010_boilers'))
#replace all "N/A" with NA
eGRID_all_years_boilers[eGRID_all_years_boilers=="N/A"] <- NA
#need to make sure that the year is in the first column - this keeps Google Refine from breaking
currentColumnNameOrder = colnames(eGRID_all_years_boilers)
desiredColumnNameOrder = c('year', currentColumnNameOrder[-which(currentColumnNameOrder == 'year')])
eGRID_all_years_boilers = eGRID_all_years_boilers[,desiredColumnNameOrder]
#write table, NA will be just ""
write.table(eGRID_all_years_boilers, file="eGRID_all_years_boilers.csv", sep="\t", row.names=FALSE, na="")
#Now get all the generators
#1996 through 2000 need to have their header names fixed
#Also each data frame has the corresponding year connected to it
eGRID_1996_generators <- cleanHeaders(readWorksheetFromFile('eGRID2002yr96_plant.xls',
sheet = 4, header = TRUE, startCol = 1, startRow = 4, endCol=0, endRow=0))
eGRID_1996_generators$year = 1996
xlcFreeMemory()
eGRID_1997_generators <- cleanHeaders(readWorksheetFromFile('eGRID2002yr97_plant.xls',
sheet = 4, header = TRUE, startCol = 1, startRow = 4, endCol=0, endRow=0))
eGRID_1997_generators$year = 1997
xlcFreeMemory()
eGRID_1998_generators <- cleanHeaders(readWorksheetFromFile('eGRID2002yr98_plant.xls',
sheet = 4, header = TRUE, startCol = 1, startRow = 4, endCol=0, endRow=0))
eGRID_1998_generators$year = 1998
xlcFreeMemory()
eGRID_1999_generators <- cleanHeaders(readWorksheetFromFile('eGRID2002yr99_plant.xls',
sheet = 4, header = TRUE, startCol = 1, startRow = 4, endCol=0, endRow=0))
eGRID_1999_generators$year = 1999
xlcFreeMemory()
eGRID_2000_generators <- cleanHeaders(readWorksheetFromFile('eGRID2002yr00_plant.xls',
sheet = 4, header = TRUE, startCol = 1, startRow = 4, endCol=0, endRow=0))
eGRID_2000_generators$year = 2000
xlcFreeMemory()
eGRID_2004_generators <- cleanHeaders(readWorksheetFromFile('eGRID2006V2_1_year04_plant.xls',
sheet = 3, header = TRUE, startCol = 1, startRow = 6, endCol=0, endRow=0))
eGRID_2004_generators$year = 2004
xlcFreeMemory()
eGRID_2005_generators <- cleanHeaders(readWorksheetFromFile('eGRID2007V1_1year05_plant.xls',
sheet = 3, header = TRUE, startCol = 1, startRow = 6, endCol=0, endRow=0))
eGRID_2005_generators$year = 2005
xlcFreeMemory()
eGRID_2007_generators <- cleanHeaders(readWorksheetFromFile('eGRID2010V1_1_year07_PLANT.xls',
sheet = 3, header = TRUE, startCol = 1, startRow = 6, endCol=0, endRow=0))
eGRID_2007_generators$year = 2007
xlcFreeMemory()
eGRID_2009_generators <- cleanHeaders(readWorksheetFromFile('eGRID2012V1_0_year09_DATA.xls',
sheet = 3, header = TRUE, startCol = 1, startRow = 6, endCol=0, endRow=0))
eGRID_2009_generators$year = 2009
xlcFreeMemory()
eGRID_2010_generators <- cleanHeaders(readWorksheetFromFile('eGRID_9th_edition_V1-0_year_2010_Data.xls',
sheet = 3, header = TRUE, startCol = 1, startRow = 6, endCol=0, endRow=0))
eGRID_2010_generators$year = 2010
xlcFreeMemory()
eGRID_all_years_generators <- rbind.fill(eGRID_1996_generators,
eGRID_1997_generators,
eGRID_1998_generators,
eGRID_1999_generators,
eGRID_2000_generators,
eGRID_2004_generators,
eGRID_2005_generators,
eGRID_2007_generators,
eGRID_2009_generators,
eGRID_2010_generators)
#clean up after ourselves - these are not needed anymore
rm(list=c('eGRID_1996_generators',
'eGRID_1997_generators',
'eGRID_1998_generators',
'eGRID_1999_generators',
'eGRID_2000_generators',
'eGRID_2004_generators',
'eGRID_2005_generators',
'eGRID_2007_generators',
'eGRID_2009_generators',
'eGRID_2010_generators'))
#replace all "N/A" with NA
eGRID_all_years_generators[eGRID_all_years_generators=="N/A"] <- NA
#need to make sure that the year is in the first column - this keeps Google Refine from breaking
currentColumnNameOrder = colnames(eGRID_all_years_generators)
desiredColumnNameOrder = c('year', currentColumnNameOrder[-which(currentColumnNameOrder == 'year')])
eGRID_all_years_generators = eGRID_all_years_generators[,desiredColumnNameOrder]
#write table, NA will be just ""
write.table(eGRID_all_years_generators, file="eGRID_all_years_generators.csv", sep="\t", row.names=FALSE, na="")
toc()