forked from Ecohen4/Energy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaster.R
More file actions
1434 lines (1110 loc) · 66.5 KB
/
Copy pathMaster.R
File metadata and controls
1434 lines (1110 loc) · 66.5 KB
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# CGS.R
###### MONTHLY SCHEDULE-DRAWAL FROM CGS BY SOURCE (PP)
setwd("/Users/elliotcohen/Dropbox/data/Electricity/CEA/Rcommands")
library(xlsx)
library(plyr)
library(reshape2)
library(ggplot2)
library(scales)
# Define Multiplot Function
multiplot <- function(..., plotlist=NULL, cols) {
require(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# Make the panel
plotCols = cols # Number of columns of plots
plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))
vplayout <- function(x, y)
viewport(layout.pos.row = x, layout.pos.col = y)
# Make each plot, in the correct location
for (i in 1:numPlots) {
curRow = ceiling(i/plotCols)
curCol = (i-1) %% plotCols + 1
print(plots[[i]], vp = vplayout(curRow, curCol ))
}
}
##
## Import data...
#Delhi Monthly Schedule Drawal from CGS at Ex-Bus in LU, April 2011 - Feb 2013 (Note: Any NULLs converted to 0 in excel)
# ENERGY UNITS: LU (10^5 KWh)
CGS=read.xlsx(file="/Users/elliotcohen/Dropbox/data/Electricity/CEA/Delhi_schedule_drawal_from_CGS_at_Ex-Bus.xlsx",sheetIndex=1,as.data.frame=TRUE,header=TRUE)
#convert any NA's to zeros
CGS[,][is.na(CGS[,])]<-0
#create POSIXct time series
# Day is arbitrary b/c data is monthly
CGS$POSIXct<-as.POSIXct(paste(CGS$year,CGS$month_id,'01',sep='-'),format='%Y-%m-%d',tz='IST')
#add Date (day is arbitrary b/c data is monthly)
CGS$Date<-as.Date(CGS$POSIXct,"%Y-%m-%d")
# combine multiple generating units at the same location (stn_code coerced to match in Excel)
CGS<-ddply(CGS,.(POSIXct,Date,year,month_id,stn_code,seb_code), summarize,t_energy_month=sum(t_energy_month))
#plot CGS allocatsion to Delhi
# CGSplot<-ggplot(CGS,aes(x=POSIXct,y=t_energy_month, colour=stn_code)) + geom_line()
# CGSplot + scale_y_continuous(name='Monthly CGS Allocations to Delhi (LU)') + scale_x_datetime(breaks=date_breaks("3 months"))
# Good, but too busy....
# Now try facet_wrap
# USEFUL FOR VISUAL INPSECTION OF PLANT-LEVEL DATA...
# CGSplot + facet_wrap(~stn_code, scale="free")
# Better, but CGS plot is still too busy... let's filter out PPs below a certain threshold of annual allocation to Delhi
# Select one year's data: April 2011 through March 2012.
AprilToDec2011<-subset(CGS,CGS$year %in% 2011 & CGS$month_id %in% c(4:12))
JanToMarch2012<-subset(CGS,CGS$year %in% 2012 & CGS$month_id %in% c(1:3))
yr<-rbind(AprilToDec2011,JanToMarch2012)
range(yr$POSIXct) # Check to see if we have the right months
# combine multiple generating units at the same location (stn_code coerced to match in Excel)
# already completed above.
yr<-ddply(yr,.(month_id,year,stn_code,seb_code,POSIXct,Date), summarize,t_energy_month=sum(t_energy_month))
levels(as.factor(yr$POSIXct)) # double check to see if we have the right months --> YES.
#sum monthly energy allocations from CGS to Delhi over the year (April 1 2011 - March 31 2012)
zz<-ddply(yr, .(stn_code),summarize, sum=sum(t_energy_month))
zz #cumulative energy allocations to Delhi from CGS in 2011-12.
# keep CGS with non-zero contributions to Delhi (>0 LU/yr)
keep<-subset(zz,sum>0)
keep.names<-keep$stn_code
dim(keep)[1] #26 CGS with non-zero energy allocations to Delhi in 2011-2012
# NOTE: this is after combining multiple units at the same location:
# ANT = ANTG + ANTL + ANTR
# AUR = AURG + AURL + AURR
# DDR = DDRG + DDRL + DDRR
keep.data<-yr[yr$stn_code%in%keep.names,]
keep.data<-droplevels(keep.data) #drops unused factor levels
drop<-subset(zz,sum==0)
drop.names<-drop$stn_code
dim(drop)[1]
drop.data<-yr[yr$stn_code%in%drop.names,]
drop.data<-droplevels(drop.data)
# Repeat above procedure to double-check ##
intersect <- function(x, y) y[match(x, y, nomatch = 0)]
index<-intersect(yr$stn_code,keep.names)
keep.data2<-subset(yr,yr$stn_code%in%index)
keep.data2<-droplevels(keep.data2)
identical(keep.data,keep.data2) # TRUE
# Check if we captured all the data
identical(dim(keep.data)[1]+dim(drop.data)[1], dim(yr)[1])
# Now plot again but only for CGS with non-zero annual allocation to Delhi
# CGSplot2<-ggplot(keep.data,aes(x=Date,y=t_energy_month, colour=stn_code)) + geom_line()
#
# #Fixed Y range
# CGSplot2 + facet_wrap(~stn_code, scale="fixed") + scale_x_date(labels = date_format("%b-%Y")) + labs(title="Stationwise CGS Allocations to Delhi, April 2011 - March 2012") + scale_y_continuous(name="10^5 KWh (LU)")
#
# #Free Y range
# CGSplot2 + facet_wrap(~stn_code, scale="free_y") + scale_x_date(labels = date_format("%b")) + labs(title="Stationwise CGS Allocations to Delhi, April 2011 - March 2012") + scale_y_continuous(name="10^5 KWh (LU)")
#########
# Repeat for top-twelve CGS with largest contributions to Delhi
zz<-zz[ order(zz[,2],decreasing=TRUE),]
Top12<-zz[1:12,]
Top<-Top12
Top.names<-Top$stn_code
dim(Top)[1] # 12 CGS with highest cumulative energy allocations to Delhi in 2011-2012
Top.data<-yr[yr$stn_code%in%Top.names,]
Top.data<-droplevels(Top.data) #drops unused factor levels
# # Now plot again but only for 12 CGS with the highest energy annual allocation to Delhi
# CGSplot3<-ggplot(Top.data,aes(x=Date,y=t_energy_month, colour=stn_code)) + geom_line()
#
# #Fixed Y range
# CGSplot3 + facet_wrap(~stn_code, scale="fixed") + scale_x_date(labels = date_format("%b-%Y")) + labs(title="Top-12 CGS Allocations to Delhi, April 2011 - March 2012") + scale_y_continuous(name="10^5 KWh (LU)")
#
# #Free Y range
# CGSplot3 + facet_wrap(~stn_code, scale="free_y") + scale_x_date(labels = date_format("%b")) + labs(title="Top-12 CGS Allocations to Delhi, April 2011 - March 2012") + scale_y_continuous(name="10^5 KWh (LU)")
#########
# Now import additional meta-data for CGS...
CGSmeta<-read.xlsx(file="/Users/elliotcohen/Dropbox/Data/Electricity/CEA/Data/CGS-Allocations-NR.xlsx",sheetIndex=2,colIndex=c(1:25),rowIndex=c(1:33),as.data.frame=TRUE,header=TRUE)
#convert any NA's to zeros for columns that are numeric
CGSmeta[,12:25][is.na(CGSmeta[,12:25])]<-0
#compare stn_code btw CGSmeta and keep.data
levels(CGSmeta$stn_code)
levels(keep.data$stn_code)
keep.meta<-CGSmeta[CGSmeta$stn_code%in%keep.names,]
keep.meta<-droplevels(keep.meta) #drops unused factor levels
#compare stn_code btw keep.meta and keep.data
levels(keep.meta$stn_code)
levels(keep.data$stn_code)
identical(levels(keep.meta$stn_code),levels(keep.data$stn_code))
# Now the two data.frames can be merged!
# Merge monthly data and meta data
merge<-merge(keep.data,keep.meta,by="stn_code") # UREKA!
# order dataframe in chronological order
merge<-merge[ order(merge[,2],decreasing=FALSE),]
# drop unused attributes (columns)
merge<-merge[,-c(19:28)]
merge<-merge[,c(1,8,17,14,15,16,19,11,18,21,2,3,5,6,7)]
# Add Monsoon/non-Monsoon attribute
n=dim(merge)[1]
for (i in 1:n){
if(merge$month_id[i]==7) {merge$Monsoon[i]<-"Monsoon"} else
if(merge$month_id[i]==8) {merge$Monsoon[i]<-"Monsoon"} else
if(merge$month_id[i]==9) {merge$Monsoon[i]<-"Monsoon"} else
{merge$Monsoon[i]<-"Dry"} }
# Add seasonal attribute
n=dim(merge)[1]
for (i in 1:n){
if(merge$month_id[i]==6) {merge$Season[i]<-"Summer"} else
if(merge$month_id[i]==7) {merge$Season[i]<-"Summer"} else
if(merge$month_id[i]==8) {merge$Season[i]<-"Summer"} else
if(merge$month_id[i]==9) {merge$Season[i]<-"Fall"} else
if(merge$month_id[i]==10) {merge$Season[i]<-"Fall"} else
if(merge$month_id[i]==11) {merge$Season[i]<-"Fall"} else
if(merge$month_id[i]==12) {merge$Season[i]<-"Winter"} else
if(merge$month_id[i]==1) {merge$Season[i]<-"Winter"} else
if(merge$month_id[i]==2) {merge$Season[i]<-"Winter"} else
{merge$Season[i]<-"Spring"} }
merge$Season<-factor(merge$Season, levels=c("Spring","Summer","Fall","Winter"))
for (i in 1:n){
if(merge$month_id[i]==6) {merge$SeasNum[i]<-2} else
if(merge$month_id[i]==7) {merge$SeasNum[i]<-2} else
if(merge$month_id[i]==8) {merge$SeasNum[i]<-2} else
if(merge$month_id[i]==9) {merge$SeasNum[i]<-3} else
if(merge$month_id[i]==10) {merge$SeasNum[i]<-3} else
if(merge$month_id[i]==11) {merge$SeasNum[i]<-3} else
if(merge$month_id[i]==12) {merge$SeasNum[i]<-4} else
if(merge$month_id[i]==1) {merge$SeasNum[i]<-4} else
if(merge$month_id[i]==2) {merge$SeasNum[i]<-4} else
{merge$SeasNum[i]<-1} }
merge$SeasNum<-as.numeric(merge$SeasNum)
############# Add WWIF and WCIF attributes ###############
# Get WWIC and WCIF from "Life cycle water use for electricity generation - a review and harmonization of literature estimates" (NREL 2012)
WIF<-read.xlsx(file="/Users/elliotcohen/Dropbox/Data/Electricity/USA/NREL/WWIF_WCIF_for_Elec_Gen-NREL_2012.xlsx",sheetName="Compiled", colIndex=c(1:11), as.data.frame=TRUE,header=TRUE)
merge1<-merge
merge2<-merge
# Operation-phase Water Withdrwal Intensities given in gal/MWh for the following technology types:
# coal-PC with cooling tower
# NGCC with cooling tower
# Hydroelectric power station (generic - all types), cooling=NA
# Nuclear (generic - all types) with cooling tower
merge1$metric<-as.factor("Withdrawals")
min<-merge1
mean<-merge1
max<-merge1
# Add min WWIF attribute
min$stat<-as.factor("min")
n=dim(min)[1]
for (i in 1:n){
if(min$Fuel[i]=="Coal") {min$WI.gal[i]<-WIF[19,9]} else
if(min$Fuel[i]=="Gas") {min$WI.gal[i]<-WIF[46,9]} else
if(min$Fuel[i]=="Hydro") {min$WI.gal[i]<-WIF[68,9]} else {min$WI.gal[i]<-WIF[64,9] }}
# Add mean WWIF attribute
mean$stat<-as.factor("mean")
for (i in 1:n){
if(mean$Fuel[i]=="Coal") {mean$WI.gal[i]<-WIF[19,8]} else
if(mean$Fuel[i]=="Gas") {mean$WI.gal[i]<-WIF[46,8]} else
if(mean$Fuel[i]=="Hydro") {mean$WI.gal[i]<-WIF[68,8]} else {mean$WI.gal[i]<-WIF[64,8] }}
# Add max WWIF attribute
max$stat<-as.factor("max")
for (i in 1:n){
if(max$Fuel[i]=="Coal") {max$WI.gal[i]<-WIF[19,10]} else
if(max$Fuel[i]=="Gas") {max$WI.gal[i]<-WIF[46,10]} else
if(max$Fuel[i]=="Hydro") {max$WI.gal[i]<-WIF[68,10]} else {max$WI.gal[i]<-WIF[64,10] }}
merge1<-rbind(min,mean,max)
## NOW REPEAT FOR CONSUMPTION
# Operation-phase Water Consumption Intensities given in gal/MWh for the following technology types:
# coal-PC with cooling tower
# NGCC with cooling tower
# Hydroelectric (generic - all types), cooling=NA
# Nuclear (generic - all types) with cooling tower
merge2$metric<-as.factor("Consumption")
min<-merge2
mean<-merge2
max<-merge2
# Add min WCIF attribute
min$stat<-as.factor("min")
n=dim(min)[1]
for (i in 1:n){
if(min$Fuel[i]=="Coal") {min$WI.gal[i]<-WIF[4,9]} else
if(min$Fuel[i]=="Gas") {min$WI.gal[i]<-WIF[34,9]} else
if(min$Fuel[i]=="Hydro") {min$WI.gal[i]<-WIF[67,9]} else {min$WI.gal[i]<-WIF[58,9] }}
# Add mean WCIF attribute
mean$stat<-as.factor("mean")
for (i in 1:n){
if(mean$Fuel[i]=="Coal") {mean$WI.gal[i]<-WIF[4,8]} else
if(mean$Fuel[i]=="Gas") {mean$WI.gal[i]<-WIF[34,8]} else
if(mean$Fuel[i]=="Hydro") {mean$WI.gal[i]<-WIF[67,8]} else {mean$WI.gal[i]<-WIF[58,8] }}
# Add max WCIF attribute
max$stat<-as.factor("max")
for (i in 1:n){
if(max$Fuel[i]=="Coal") {max$WI.gal[i]<-WIF[4,10]} else
if(max$Fuel[i]=="Gas") {max$WI.gal[i]<-WIF[34,10]} else
if(max$Fuel[i]=="Hydro") {max$WI.gal[i]<-WIF[67,10]} else {max$WI.gal[i]<-WIF[58,10] }}
merge2<-rbind(min,mean,max)
merge3<-rbind(merge1,merge2)
merge<-merge3
merge$WI.liter<-merge$WI.gal*3.78
#merge$stat<-factor(merge$stat, levels=c("mean","min","max"))
# DON'T NEED ANYMORE...
# # Add min WWIF attribute
# n=dim(merge)[1]
# for (i in 1:n){
# if(merge$Fuel[i]=="Coal") {merge$WWImin[i]<-WIF[19,9]} else
# if(merge$Fuel[i]=="Gas") {merge$WWIFmin[i]<-WIF[46,9]} else
# if(merge$Fuel[i]=="Hydro") {merge$WWIFmin[i]<-WIF[68,9]} else {merge$WWIFmin[i]<-WIF[64,9] }}
# # Add mean WWIF attribute
# for (i in 1:n){
# if(merge$Fuel[i]=="Coal") {merge$WWIFmean[i]<-WIF[19,8]} else
# if(merge$Fuel[i]=="Gas") {merge$WWIFmean[i]<-WIF[46,8]} else
# if(merge$Fuel[i]=="Hydro") {merge$WWIFmean[i]<-WIF[68,8]} else {merge$WWIFmean[i]<-WIF[64,8] }}
# # Add max WWIF attribute
# for (i in 1:n){
# if(merge$Fuel[i]=="Coal") {merge$WWIFmax[i]<-WIF[19,10]} else
# if(merge$Fuel[i]=="Gas") {merge$WWIFmax[i]<-WIF[46,10]} else
# if(merge$Fuel[i]=="Hydro") {merge$WWIFmax[i]<-WIF[68,10]} else {merge$WWIFmax[i]<-WIF[64,10] }}
#
#
# # Repeat for consumption (WCIF)
# # Add min WCIF attribute
# n=dim(merge)[1]
# for (i in 1:n){
# if(merge$Fuel[i]=="Coal") {merge$WCIFmin[i]<-WIF[4,9]} else
# if(merge$Fuel[i]=="Gas") {merge$WCIFmin[i]<-WIF[34,9]} else
# if(merge$Fuel[i]=="Hydro") {merge$WCIFmin[i]<-WIF[67,9]} else {merge$WCIFmin[i]<-WIF[58,9] }}
# # Add mean WCIF attribute
# for (i in 1:n){
# if(merge$Fuel[i]=="Coal") {merge$WCIFmean[i]<-WIF[4,8]} else
# if(merge$Fuel[i]=="Gas") {merge$WCIFmean[i]<-WIF[34,8]} else
# if(merge$Fuel[i]=="Hydro") {merge$WCIFmean[i]<-WIF[67,8]} else {merge$WCIFmean[i]<-WIF[58,8] }}
# # Add max WCIF attribute
# for (i in 1:n){
# if(merge$Fuel[i]=="Coal") {merge$WCIFmax[i]<-WIF[4,10]} else
# if(merge$Fuel[i]=="Gas") {merge$WCIFmax[i]<-WIF[34,10]} else
# if(merge$Fuel[i]=="Hydro") {merge$WCIFmax[i]<-WIF[67,10]} else {merge$WCIFmax[i]<-WIF[58,10] }}
######### Done Adding WWI Attributes #############
# Compute the WF for each CGS
# (LU)*(10^2 MWH/LU)*(L/MWh)*(ML/10^6 L) Million Liters (ML), per month
merge$WF<-merge$t_energy_month*(10^2)*merge$WI.liter/(10^6) #units: ML
# First try to show both withdrawals and consumption on one axis...
title<-"Trans-Boundary Water Footprint\nof Energy Supplied From Grid to Delhi, Disaggregated by Source"
B<-ggplot(merge,aes(x=Date,y=WF,colour=metric,linetype=stat)) + geom_line()
B + facet_wrap(~stn_code, scale="free_y", nrow=4) + scale_x_date(labels = date_format("%b")) + labs(title=title) + scale_y_continuous(name="Million Liters Freshwater per Month")
Withdrawals<-subset(merge,metric=="Withdrawals")
Withdrawals<-subset(Withdrawals, Fuel!="Hydro")
W<-ggplot(Withdrawals,aes(x=Date,y=WF,colour=Fuel,linetype=stat)) + geom_line()
# Fixed
TBoundWWF_fixed<- W + facet_wrap(~stn_code, scale="fixed") + scale_x_date(labels = date_format("%b")) + labs(title="Trans-Boundary Water Withdrawal Footprint\nof Energy Supplied From Grid to Delhi, Disaggregated by Source") + scale_y_continuous(name="Million Liters Freshwater per Month")
TBoundWWF_fixed
# Free
TBoundWWF_free<- W + facet_wrap(~stn_code, scale="free_y") + scale_x_date(labels = date_format("%b")) + labs(title="Trans-Boundary Water Withdrawal Footprint\nof Energy Supplied From Grid to Delhi, Disaggregated by Source") + scale_y_continuous(name="Million Liters Freshwater per Month")
TBoundWWF_free
# Repeat for Consumption
Consumption<-subset(merge,metric=="Consumption")
Consumption<-subset(Consumption, Fuel!="Hydro")
C<-ggplot(Consumption,aes(x=Date,y=WF,colour=Fuel,linetype=stat)) + geom_line()
# Fixed
TBoundWCF_fixed<- C + facet_wrap(~stn_code, scale="fixed") + scale_x_date(labels = date_format("%b")) + labs(title="Trans-Boundary Water Consumption Footprint\nof Energy Supplied From Grid to Delhi, Disaggregated by Source") + scale_y_continuous(name="Million Liters Freshwater per Month")
TBoundWCF_fixed
# Free
TBoundWCF_free<- C + facet_wrap(~stn_code, scale="free_y") + scale_x_date(labels = date_format("%b")) + labs(title="Trans-Boundary Water Consumption Footprint\nof Energy Supplied From Grid to Delhi, Disaggregated by Source") + scale_y_continuous(name="Million Liters Freshwater per Month")
TBoundWCF_free
# Plot withdrawals and consumption together on one axis for Stationwise CGS allocations to Delhi and convert energy units from LU (10^5 KWh) to MU (10^6 KWh)
Stationwise<-ggplot(merge,aes(x=Date,y=t_energy_month/10, colour=Fuel)) + geom_line()
# Free
title1="Stationwise CGS Allocations to Delhi, April 2011 - March 2012"
title2="Energy Supplied to Delhi from Grid, Disaggregated by Source"
title3<-"Energy Supplied to Delhi from Grid: Trans-boundary production disaggregated by source\n(Showing 12 largest CGS suppliers, 2011-12)"
title4<-"Trans-boundary Electricity Supply to Dehi Disaggregated by Source:\n12 power plants with highest cumulative energy allocations to Delhi in 2011-12"
title5<-"Trans-Boundary power plants with highest annual energy allocations to Delhi"
title6<-"Water Withdrawal and Consumption Requirements\nof the 12-Largest Trans-boundary Power Plants Serving Delhi"
title8<-"Trans-Boundary Water Footprint of Energy Supplied From Grid to Delhi\nSummation of All Sources"
title9<-"Trans-boundary electricity supply to Delhi\n(Top-12 CGS allocations, 2011-12)"
title10<-"Water withdrawal and consumption\nassociated with trans-boundary electricity supply to Delhi"
ylab1<-"GWh per month"
ylab2<-"Billion liters water per month"
xlab1<-"Month"
CGSp<-Stationwise + facet_wrap(~stn_code, scale="free") + scale_x_date(labels = date_format("%b"),name=xlab1) + labs(title=title2) + scale_y_continuous(name=ylab1)
CGSp
# Fixed_y
Stationwise + facet_wrap(~stn_code, scale="fixed") + scale_x_date(labels = date_format("%b"), name=xlab1) + labs(title=title2) + scale_y_continuous(name=ylab1)
# Repeat for top-12 CGS with highest annual energy allocations to Delhi
Top
Top.data<-merge[merge$stn_code%in%Top.names,]
Top.data<-droplevels(Top.data) #drops unused factor levels
#Fixed Y range
Top12e<-ggplot(Top.data,aes(x=Date,y=t_energy_month/10, colour=Fuel, group=Fuel)) + geom_line()
Top12ep_fixed<-Top12e + facet_wrap(~stn_code, scale="fixed") + scale_x_date(labels = date_format("%b"), name="Month") + labs(title=title9) + scale_y_continuous(name="GWh per month")
Top12ep_fixed
#Free Y
Top12ep_free<-Top12e + facet_wrap(~stn_code, scale="free_y") + scale_x_date(labels = date_format("%b"), name="Month") + labs(title=title3) + scale_y_continuous(name="GWh per month")
Top12ep_free
# Now plot WWF and WCF and convert from ML to BL
# WF of 12 CGS with highest cumulative energy allocations to Delhi in 2011-2012
Top12.mean<-subset(Top.data, stat=="mean")
Top12WF<-ggplot(Top12.mean,aes(x=Date,y=WF/10^3,colour=Fuel,group=metric, linetype=metric)) + geom_line()
Top12WF + facet_wrap(~stn_code) + scale_x_date(labels = date_format("%b"),name="Month") + labs(title=title6) + scale_y_continuous(name=ylab2)
Top12WFp<-Top12WF + facet_wrap(~stn_code) + scale_x_date(labels = date_format("%b"),name="Month") + labs(title=title10) + scale_y_continuous(name=ylab2)
Top12WFp
# Compute the total WF of all CGS allocations to Delhi
TotalTBWF<-ddply(merge, .(year,month_id,POSIXct,Date,metric,stat),summarize,WF=sum(WF))
TotalTBWF$Scale<-"Transboundary"
TotalTBWFp<-ggplot(TotalTBWF, aes(x=Date,y=WF/10^3,linetype=stat)) + geom_line() + facet_wrap(~metric) + scale_x_date(labels = date_format("%b"),name="Month") + scale_y_continuous(name=ylab2) + labs(title=title8)
TotalTBWFp
# Fuelwise energy supply to Delhi from CGS
Top12WF + facet_wrap(~Fuel)
fuelwiseElec<-ddply(merge, .(year,month_id,POSIXct,Date,metric,stat,Fuel),summarize,energy=sum(t_energy_month))
p6<-ggplot(fuelwiseElec, aes(x=Date,y=energy/10^3,colour=Fuel)) + geom_line()
p6 + scale_y_continuous(name="Energy Supplied (GWh)", breaks=c(0,5,10,15), limits=c(0,15), expand=c(0,0)) + labs(title="Fuelwise Monthly CGS Allocations to Delhi") + scale_x_date(labels = date_format("%b"),breaks="2 months" ,name=xlab1)
fuelwiseWF<-ddply(merge, .(year,month_id,POSIXct,Date,metric,stat,Fuel),summarize,WF=sum(WF))
p7<-ggplot(fuelwiseWF, aes(x=Date,y=WF/10^3, colour=Fuel,linetype=stat)) + geom_line()
p7 + facet_wrap(~metric) + labs(title="Water Footprint of Delhi's Electricity Supply") + scale_y_continuous(name="Billions of Liters of Freshwater per Month (BL)")
#######################
# Location-wise energy supply to Delhi from CGS
# Map CGS with lat-long coords and size relative to t_energy_month
# First, need to convert Lat-Long coords to decimal
xx <- lapply(strsplit(as.character(merge$X),' '), as.numeric)
merge$Longitude<-lapply(xx, function(x) x[1]+((x[1]>0)-0.5)*(x[2]+x[3]/60)/30)
yy<- lapply(strsplit(as.character(merge$Y),' '), as.numeric)
merge$Latitude<-lapply(yy, function(x) x[1]+((x[1]>0)-0.5)*(x[2]+x[3]/60)/30)
merge$Longitude<-as.numeric(merge$Longitude)
merge$Latitude<-as.numeric(merge$Latitude)
# Keep attribues necessary for locationwise plot
merge3<-subset(merge,select=c(State,year,month_id,POSIXct,Date,Monsoon,stn_code,Fuel,Longitude,Latitude,t_energy_month))
#convert any NA's to zeros
merge3[,9:10][is.na(merge3[,9:10])]<-0
# annual locationwise energy supply to Delhi (monthly to annual aggreagation)
AnnLocwise<-ddply(merge3,.(State,stn_code,Longitude,Latitude,Fuel),summarize,value=sum(t_energy_month))
title10<-"Spatially-delineated trans-boundary electricity supply to Delhi"
title11<-"Spatially-Delineated Trans-Boundary Water Footprint of Delhi's Electricity Supply"
AnnPlot<-ggplot(AnnLocwise, aes(x=Longitude,y=Latitude,colour=Fuel,size=value))
AnnPlot + geom_point() + scale_area() + labs(title=title10) + ylim(22,35) + xlim(75,85)
# Repeat for WF
AnnLocwiseWF<-ddply(merge,.(stn_code,State,Longitude,Latitude,Fuel,metric,stat),summarize,value=sum(WF))
mean<-subset(AnnLocwiseWF, stat=="mean")
mean<-droplevels(mean)
AnnTBWF<-ggplot(mean,aes(x=Longitude,y=Latitude, colour=metric, size=value))
AnnTBWF + geom_point(alpha=0.5) + scale_area(range=c(1,10),breaks=c(0,500,2000,5000,10000)) + facet_wrap(~Fuel) + labs(title=title11) + ylim(22,35) + xlim(75,85)
AnnTBWF2<-ggplot(mean,aes(x=Longitude,y=Latitude, colour=Fuel, size=value))
AnnTBWF2 + geom_point(alpha=0.5) + scale_area(range=c(1,10),breaks=c(0,500,2000,5000,10000)) + facet_wrap(~metric) + labs(title=title11) + ylim(22,35) + xlim(75,85)
# Monsoon vs Non-Monsoon locationwise energy supply to Delhi [need to look at mean rather than sum of months because unequal number of months in Monsoon and Non-Monsoon -- 3 for Monsoon, 9 for Non-Monsoon]
SeasLocwise<-ddply(merge3, .(State,stn_code,Longitude, Latitude,Fuel,Monsoon),summarize,value=mean(t_energy_month))
title12<-"Monsoon vs non-Monsoon locationwise energy supply from CGS to Delhi\n(dot size proportional to seasonal monthly mean energy supply in GWh)"
SeasPlot<-ggplot(SeasLocwise,aes(x=Longitude,y=Latitude,colour=Fuel,size=value))
SeasPlot + geom_point() + scale_area() + facet_wrap(~Monsoon) + labs(title=title12) + ylim(23,34) + xlim(75,84)
# Repeat for WF
MonsoonLocwiseWF<-ddply(merge,.(stn_code,State,Longitude,Latitude,Fuel,metric,stat,Monsoon),summarize,value=mean(WF))
mean<-subset(MonsoonLocwiseWF, stat=="mean")
title13<-"Spatially- and Temporally-Delineated Trans-Boundary Water Footprint of Delhi's Electricity Supply\n(Dot Size Proportional to Monthly Mean Water Withdrawals/Consumption in ML During Monsoon/Dry Season)"
SeasPlot<-ggplot(mean,aes(x=Longitude,y=Latitude,colour=Fuel,size=value))
SeasPlot + geom_point(alpha=0.5) + scale_area(range=c(1,10)) + facet_wrap(~metric + Monsoon) + labs(title="Comparison of Monthly Mean Water Withdrawals and Consumption During Monsoon vs Dry Seasons (in ML)") + ylim(23,34) + xlim(75,84)
# season-wise location-wise energy supply to Delhi (monthly to seasonal aggregation)
SeasLocwise<-ddply(merge, .(State,stn_code,Longitude,Latitude,Fuel,Season,SeasNum),summarize,value=sum(t_energy_month))
SeasLocwise<-SeasLocwise[ order(SeasLocwise[,8],decreasing=FALSE),]
title14<-"Season-wise location-wise energy supply from CGS to Delhi\n(dot size proportional to amount of energy supplied in GWh)"
SeasPlot<-ggplot(SeasLocwise,aes(x=Longitude,y=Latitude,colour=Fuel,size=value))
SeasPlot + geom_point() + scale_area(range=c(1,10),breaks=c(0,500,2000,5000,10000),labels=c("0-500","500-2000","2000-5000","5000-1000","10000+")) + facet_wrap(~Season) + labs(title=title14) + ylim(23,34) + xlim(75,84)
SeasPlot + geom_point() + scale_area(range=c(1,10),breaks=waiver()) + facet_wrap(~Season) + labs(title=title14) + ylim(23,34) + xlim(75,84)
# Repeat for WF
SeasonLocwiseWF<-ddply(merge,.(stn_code,State,Longitude,Latitude,Fuel,metric,stat,Season),summarize,value=sum(WF))
mean<-subset(SeasonLocwiseWF, stat=="mean")
title15<-"Spatially- and Temporally-Delineated Trans-Boundary Water Footprint of Delhi's Electricity Supply\n(Dot Size Proportional to Seasonal Water Withdrawals/Consumption in ML)"
SeasPlot<-ggplot(mean,aes(x=Longitude,y=Latitude,colour=Fuel,size=value))
SeasPlot + geom_point(alpha=0.5) + scale_area(range=c(1,10)) + facet_wrap(~metric + Season, nrow=2) + labs(title="Seasonal Water Withdrawals and Consumption in ML") + ylim(23,34) + xlim(75,84)
# Monthly locationwise energy supply to Delhi
title16<-"Monthly locationwise energy supply to Delhi"
MonPlot<-ggplot(merge3,aes(x=Longitude,y=Latitude,colour=Fuel,size=t_energy_month))
MonPlot + geom_point() + scale_area() + facet_wrap(~month_id) + labs(title=title16) + ylim(22,35) + xlim(75,85)
# Monthly statewise energy supply to Delhi from CGS
StatewiseMonthwise<-ddply(merge3, .(State,year,month_id,Date,Monsoon),summarize,value=sum(t_energy_month))
StatewisePlot<-ggplot(StatewiseMonthwise, aes(x=month_id, y=value, group=State, colour=State))
StatewisePlot + geom_line() + scale_x_discrete()
MonPlot<-ggplot(merge3,aes(x=Longitude,y=Latitude,colour=Fuel,size=t_energy_month))
MonPlot + geom_point() + scale_area() + facet_wrap(~month_id) + labs(title="Monthly statewise energy supply to Delhi") + ylim(22,35) + xlim(75,85)
#####
# Monthwise statewise own generation by NR States
StateGen=read.xlsx(file="/Users/elliotcohen/Dropbox/data/Electricity/CEA/Data/Statewise_fuelwise_monthly_generation.xlsx",sheetIndex=1,as.data.frame=TRUE,header=TRUE)
#convert any NA's to zeros
#StateGen[,][is.na(StateGen[,])]<-0
StateGen<-melt(StateGen, id.var=c("State","Fuel"))
StateGen$time<-c(rep("Apr-2011",27),rep("May-2011",27),rep("Jun-2011",27),rep("Jul-2011",27),rep("Aug-2011",27),rep("Sep-2011",27), rep("Oct-2011",27),rep("Nov-2011",27),rep("Dec-2011",27),rep("Jan-2012",27),rep("Feb-2012",27),rep("Mar-2012",27))
# add Date (day is arbitrary b/c data is monthly)
my <- strsplit(StateGen$time,'-')
#split the date string into year, month, day
StateGen$year<-laply(my, '[[', 2) #assign the list of years to an array called StateGen$year
StateGen$month<-laply(my, '[[', 1)
StateGen$day<-rep(1,length(my)) #arbitrary
# create POSIXct time series
# Day is arbitrary b/c data is monthly
StateGen$POSIXct<-as.POSIXct(paste(StateGen$year,StateGen$month,'01',sep='-'),format='%Y-%b-%d',tz='IST')
StateGen$Date<-as.Date(StateGen$POSIXct,"%Y-%m-%d")
StateGenPlot<-ggplot(StateGen, aes(x=Date,y=value,group=State,colour=State, linetype=State))
StateGenPlot + geom_line() + facet_wrap(~Fuel) + labs(title="Fuelwise monthly energy generation by NR States") + scale_y_continuous(name="Monthly Generation (GWh)")
StateGenPlot2<-ggplot(StateGen, aes(x=Date,y=value,group=Fuel,colour=Fuel))
StateGenPlot2 + geom_line() + facet_wrap(~State) + labs(title="Statewise monthly energy generation for NR States")
######## Stationwise monthly PLF ##########
# Monthwise statewise own generation by NR States
PLF=read.xlsx(file="/Users/elliotcohen/Dropbox/data/Electricity/CEA/WorkingDocs/Monthly PLF of NR Thermal CGS.xlsx",sheetIndex=1,as.data.frame=TRUE,header=TRUE)
#convert any NA's to zeros
#PLF[,][is.na(PLF[,])]<-0
PLF<-melt(PLF, id.var="Station")
PLF$time<-c(rep("Apr-2011",11),rep("May-2011",11),rep("Jun-2011",11),rep("Jul-2011",11),rep("Aug-2011",11),rep("Sep-2011",11), rep("Oct-2011",11),rep("Nov-2011",11),rep("Dec-2011",11),rep("Jan-2012",11),rep("Feb-2012",11),rep("Mar-2012",11))
# add Date (day is arbitrary b/c data is monthly)
my <- strsplit(PLF$time,'-')
#split the date string into year, month, day
PLF$year<-laply(my, '[[', 2) #assign the list of years to an array called PLF$year
PLF$month<-laply(my, '[[', 1)
PLF$day<-rep(1,length(my)) #arbitrary
# create POSIXct time series
# Day is arbitrary b/c data is monthly
PLF$POSIXct<-as.POSIXct(paste(PLF$year,PLF$month,'01',sep='-'),format='%Y-%b-%d',tz='IST')
PLF$Date<-as.Date(PLF$POSIXct,"%Y-%m-%d")
PLFPlot<-ggplot(PLF, aes(x=Date,y=value,colour=Station, linetype=Station))
PLFPlot + geom_line() + labs(title="Stationwise monthly Plant Load Factor of Central Generating Stations") + scale_y_continuous(name="PLF (%)")
PLFPlot2<-ggplot(PLF, aes(x=Date,y=value))
PLFPlot2 + geom_line() + facet_wrap(~Station) + labs(title="Stationwise Monthly Plant Load Factor of Central Sector Thermal Power Stations") + scale_y_continuous(name="PLF (%)")
# Now sum over all the CGS to find the system-wide PLF in each month. Recall (1-PLF)=Reserve Capacity
# NEED TO WIEGHT THE CGS BY CAPACITY.
# PLF2<-subset(PLF,select=c(Station,Date,value))
# MonSumPLF<-ddply(PLF2, .(Date), summarize, MonSum=sum(value))
# p<-ggplot(MonSumPLF, aes(x=Date,y=MonSum))
# p + geom_line()
###########################################
#map the spatially-delineated WWFES using maps
library(maps)
library(akima)
library(fields)
world(xlim=c(75,85),ylim=c(20,34), shift=FALSE)
## Now let's look at total supply from CSGS (sum of all CGS) and convert into MU (GWh)
PSP<-ddply( CGS, .(year,month_id), summarize, CGS=sum(t_energy_month)/10)
PSP$POSIXct<-as.POSIXct(paste(PSP$year,PSP$month_id,'01',sep='-'),format='%Y-%m-%d',tz='IST')
tss<-ggplot(PSP,aes(x=POSIXct,y=CGS))
tss + geom_line() + scale_y_continuous(limits=c(0,round(max(PSP$CGS)+500, digits=-3)),name='Energy (MU)') + scale_x_datetime(name='time') + scale_color_discrete(name='month') + labs(title="Total Energy Supply from CGS to Delhi")
#############################################
############################################
###########################################
# Delhi_OwnGen.R
setwd("/Users/elliotcohen/Dropbox/data/Electricity/CEA/Rcommands")
library(xlsx)
library(plyr)
library(reshape2)
library(ggplot2)
library(scales)
OwnGen=read.xlsx(file="/Users/elliotcohen/Dropbox/Data/Electricity/SLDC/Delhi_Own_Gen_by_PP_Monthly_2011-2012.xlsx",sheetIndex=1,as.data.frame=TRUE,header=TRUE)
#convert any NA's to zeros
OwnGen[,][is.na(OwnGen[,])]<-0
#create POSIXct time series
# Day is arbitrary b/c data is monthly
OwnGen$POSIXct<-as.POSIXct(paste(OwnGen$year,OwnGen$month_id,'01',sep='-'),format='%Y-%m-%d',tz='IST')
#add Date (day is arbitrary b/c data is monthly)
OwnGen$Date<-as.Date(OwnGen$POSIXct,"%Y-%m-%d")
# Add Monsoon/non-Monsoon attribute
n=dim(OwnGen)[1]
for (i in 1:n){
if(OwnGen$month_id[i]==7) {OwnGen$Monsoon[i]<-"Monsoon"} else
if(OwnGen$month_id[i]==8) {OwnGen$Monsoon[i]<-"Monsoon"} else
if(OwnGen$month_id[i]==9) {OwnGen$Monsoon[i]<-"Monsoon"} else
{OwnGen$Monsoon[i]<-"Dry"} }
# Add seasonal attribute
n=dim(OwnGen)[1]
for (i in 1:n){
if(OwnGen$month_id[i]==6) {OwnGen$Season[i]<-"Summer"} else
if(OwnGen$month_id[i]==7) {OwnGen$Season[i]<-"Summer"} else
if(OwnGen$month_id[i]==8) {OwnGen$Season[i]<-"Summer"} else
if(OwnGen$month_id[i]==9) {OwnGen$Season[i]<-"Fall"} else
if(OwnGen$month_id[i]==10) {OwnGen$Season[i]<-"Fall"} else
if(OwnGen$month_id[i]==11) {OwnGen$Season[i]<-"Fall"} else
if(OwnGen$month_id[i]==12) {OwnGen$Season[i]<-"Winter"} else
if(OwnGen$month_id[i]==1) {OwnGen$Season[i]<-"Winter"} else
if(OwnGen$month_id[i]==2) {OwnGen$Season[i]<-"Winter"} else
{OwnGen$Season[i]<-"Spring"} }
OwnGen$Season<-factor(OwnGen$Season, levels=c("Spring","Summer","Fall","Winter"))
for (i in 1:n){
if(OwnGen$month_id[i]==6) {OwnGen$SeasNum[i]<-2} else
if(OwnGen$month_id[i]==7) {OwnGen$SeasNum[i]<-2} else
if(OwnGen$month_id[i]==8) {OwnGen$SeasNum[i]<-2} else
if(OwnGen$month_id[i]==9) {OwnGen$SeasNum[i]<-3} else
if(OwnGen$month_id[i]==10) {OwnGen$SeasNum[i]<-3} else
if(OwnGen$month_id[i]==11) {OwnGen$SeasNum[i]<-3} else
if(OwnGen$month_id[i]==12) {OwnGen$SeasNum[i]<-4} else
if(OwnGen$month_id[i]==1) {OwnGen$SeasNum[i]<-4} else
if(OwnGen$month_id[i]==2) {OwnGen$SeasNum[i]<-4} else
{OwnGen$SeasNum[i]<-1} }
OwnGen$SeasNum<-as.numeric(OwnGen$SeasNum)
dim(OwnGen) # dim(OwnGen)=[72,18] up to here. 6 stations x 12 months = 72 rows.
############# Add WWIF and WCIF attributes ###############
# Get WWIC and WCIF from "Life cycle water use for electricity generation - a review and harmonization of literature estimates" (NREL 2012)
WIF<-read.xlsx(file="/Users/elliotcohen/Dropbox/Data/Electricity/US/NREL/WWIF_WCIF_for_Elec_Gen-NREL_2012.xlsx",sheetName="Compiled", colIndex=c(1:11), as.data.frame=TRUE,header=TRUE)
# Incorporate known conditions at OwnGen PPs. For example, open-Loop cooling at BTPS, Simple GT (not CC) at GT plant, etc...
OwnGen1<-OwnGen
OwnGen2<-OwnGen
# Operation-phase Water Withdrwal Intensities given in gal/MWh for the following technology types:
# coal-PC with cooling tower
# coal-pc with open-loop cooling
# NGCC with cooling tower
# NGCT, cooling=NA
# Hydroelectric power station (generic - all types), cooling=NA
OwnGen1$metric<-as.factor("Withdrawals")
min<-OwnGen1
mean<-OwnGen1
max<-OwnGen1
# OwnGen1$metric<-as.factor("Withdrawals")
# min<-OwnGen1
# mean<-OwnGen1
# max<-OwnGen1
min$stat<-as.factor("min")
n=dim(min)[1]
for (i in 1:n){
if(min$Station[i]=="BTPS") {min$WI.gal[i]<-WIF[20,9]} else
if(min$Station[i]=="Rajghat") {min$WI.gal[i]<-WIF[19,9]} else
if(min$Station[i]=="Bawana") {min$WI.gal[i]<-WIF[46,9]} else
if(min$Station[i]=="Pragati") {min$WI.gal[i]<-WIF[46,9]} else
if(min$Station[i]=="Rithala") {min$WI.gal[i]<-WIF[46,9]} else
if(min$Station[i]=="GT") {min$WI.gal[i]<-WIF[51,9]} }
# Add mean WWIF attribute
mean$stat<-as.factor("mean")
for (i in 1:n){
if(mean$Station[i]=="BTPS") {mean$WI.gal[i]<-WIF[20,8]} else
if(mean$Station[i]=="Rajghat") {mean$WI.gal[i]<-WIF[19,8]} else
if(mean$Station[i]=="Bawana") {mean$WI.gal[i]<-WIF[46,8]} else
if(mean$Station[i]=="Pragati") {mean$WI.gal[i]<-WIF[46,8]} else
if(mean$Station[i]=="Rithala") {mean$WI.gal[i]<-WIF[46,8]} else
if(mean$Station[i]=="GT") {mean$WI.gal[i]<-WIF[51,8]} }
# Add max WWIF attribute
max$stat<-as.factor("max")
for (i in 1:n){
if(max$Station[i]=="BTPS") {max$WI.gal[i]<-WIF[20,10]} else
if(max$Station[i]=="Rajghat") {max$WI.gal[i]<-WIF[19,10]} else
if(max$Station[i]=="Bawana") {max$WI.gal[i]<-WIF[46,10]} else
if(max$Station[i]=="Pragati") {max$WI.gal[i]<-WIF[46,10]} else
if(max$Station[i]=="Rithala") {max$WI.gal[i]<-WIF[46,10]} else
if(max$Station[i]=="GT") {max$WI.gal[i]<-WIF[51,10]} }
OwnGen1<-rbind(min,mean,max)
## NOW REPEAT FOR CONSUMPTION
# Operation-phase Water Consumption Intensities given in gal/MWh for the following technology types:
# BTPS-PC with cooling tower
# NGCC with cooling tower
# Hydroelectric (generic - all types), cooling=NA
# Nuclear (generic - all types) with cooling tower
OwnGen2$metric<-as.factor("Consumption")
min<-OwnGen2
mean<-OwnGen2
max<-OwnGen2
# Add min WCIF attribute
min$stat<-as.factor("min")
n=dim(min)[1]
for (i in 1:n){
if(min$Station[i]=="BTPS") {min$WI.gal[i]<-WIF[5,9]} else
if(min$Station[i]=="Rajghat") {min$WI.gal[i]<-WIF[4,9]} else
if(min$Station[i]=="Bawana") {min$WI.gal[i]<-WIF[34,9]} else
if(min$Station[i]=="Pragati") {min$WI.gal[i]<-WIF[34,9]} else
if(min$Station[i]=="Rithala") {min$WI.gal[i]<-WIF[34,9]} else
if(min$Station[i]=="GT") {min$WI.gal[i]<-WIF[39,9]} }
# Add mean WCIF attribute
mean$stat<-as.factor("mean")
for (i in 1:n){
if(mean$Station[i]=="BTPS") {mean$WI.gal[i]<-WIF[5,8]} else
if(mean$Station[i]=="Rajghat") {mean$WI.gal[i]<-WIF[4,8]} else
if(mean$Station[i]=="Bawana") {mean$WI.gal[i]<-WIF[34,8]} else
if(mean$Station[i]=="Pragati") {mean$WI.gal[i]<-WIF[34,8]} else
if(mean$Station[i]=="Rithala") {mean$WI.gal[i]<-WIF[34,8]} else
if(mean$Station[i]=="GT") {mean$WI.gal[i]<-WIF[39,8]} }
# Add max WCIF attribute
max$stat<-as.factor("max")
for (i in 1:n){
if(max$Station[i]=="BTPS") {max$WI.gal[i]<-WIF[5,10]} else
if(max$Station[i]=="Rajghat") {max$WI.gal[i]<-WIF[4,10]} else
if(max$Station[i]=="Bawana") {max$WI.gal[i]<-WIF[34,10]} else
if(max$Station[i]=="Pragati") {max$WI.gal[i]<-WIF[34,10]} else
if(max$Station[i]=="Rithala") {max$WI.gal[i]<-WIF[34,10]} else
if(max$Station[i]=="GT") {max$WI.gal[i]<-WIF[39,10]} }
OwnGen2<-rbind(min,mean,max)
OwnGen3<-rbind(OwnGen1,OwnGen2)
OwnGen<-OwnGen3
OwnGen$WI.liter<-OwnGen$WI.gal*3.78
# re-arrange factor levels for ideal plotting...
OwnGen$stat=factor(OwnGen$stat, levels(OwnGen$stat)[c(3,2,1)])
# Compute the WF for each PP
# (GWh)*(10^3MWH/GWh)*(L/MWh)*(ML/10^6 L) Million Liters (ML), per month
OwnGen$WF<-OwnGen$Monthly.Gen.MU*(10^3)*OwnGen$WI.liter/(10^6) #units: ML
# Plot stationwise Delhi Own Generation
OwnGen$stn_code<-OwnGen$Station
# Unecessary...
# #Create a custom color scale
# myColors <- rainbow(length(levels(merge$Fuel)))
# names(myColors) <- levels(merge$Fuel)
# colScale <- scale_colour_manual(name = "Fuel",values = myColors)
#
# # Single plot
# Stationwise<-ggplot(OwnGen,aes(x=Date,y=Monthly.Gen.MU, colour=Fuel, linetype=stn_code)) + geom_line()
# Stationwise + colScale
# Facet-wrap
Stationwise<-ggplot(OwnGen,aes(x=Date,y=Monthly.Gen.MU, colour=Fuel)) + geom_line()
# Free
Stationwise + facet_wrap(~stn_code, scale="free_y") + scale_x_date(labels = date_format("%b")) + labs(title="Electricity Generation within Delhi (in-boundary") + scale_y_continuous(name="Monthly Energy Supplied (GWh)")
# Fixed_y
Stationwise + facet_wrap(~stn_code, scale="fixed") + scale_x_date(labels = date_format("%b"), name="Month") + labs(title="Delhi's in-boundary electricity production\n(all sources, 2011-12)") + scale_y_continuous(name="GWh per month")
OwnGen_fixed<-Stationwise + facet_wrap(~stn_code, scale="fixed") + scale_x_date(labels = date_format("%b"), name="Month") + labs(title="Delhi's in-boundary electricity production\n(all sources, 2011-12)") + scale_y_continuous(name="GWh per month"); OwnGen_fixed
# WWF and WCF of In-Boundary Electricity Generation (mean estimates only).
# Units: Million Liters Water (MLw)
IBoundWF<-ggplot(OwnGen,aes(x=Date,y=WF,colour=metric,linetype=stat)) + geom_line() + facet_wrap(~stn_code, scale="free_y") + labs(title="In-Boundary Water Footprint\nof Electricity Generation In Delhi, Disaggregated by Source") + scale_y_continuous(name="Million liters water per month") + scale_x_date(labels = date_format("%b"),name="Month"); IBoundWF
# # Units: Billion Liters Water (BLw)
# WF.mean<-subset(OwnGen, stat=="mean")
# WF.meanp<-ggplot(WF.mean,aes(x=Date,y=WF/10^3,group=metric,linetype=metric, colour=Fuel)) + geom_line()
#
# OwnGenWFp<-WF.meanp + facet_wrap(~stn_code) + scale_x_date(labels = date_format("%b"),name="Month") + labs(title="Water withdrawal and consumption\nassociated with Delhi's in-boundary electricity production") + scale_y_continuous(name="Billion liters water per month")
# OwnGenWFp
#
# # Plot Withdrawals - min, mean and max estimates.
# # Units: Million Liters Water (MLw)
# Withdrawals<-subset(OwnGen, metric=="Withdrawals")
# p<-ggplot(Withdrawals,aes(x=Date,y=WF,colour=Fuel,linetype=stat)) + geom_line()
# InBoundWWF<-p + facet_wrap(~stn_code, scale="fixed") + scale_x_date(labels = date_format("%b")) + labs(title="In-Boundary Water Withdrawal Footprint\nof Delhi's Own Generation, Disaggregated By Source") + scale_y_continuous(name="Million Liters Freshwater")
# InBoundWWF
#
# # Repeat for Consumption
# # Units: Million Liters Water (MLw)
# Consumption<-subset(OwnGen, metric=="Consumption")
# p<-ggplot(Consumption,aes(x=Date,y=WF,colour=Fuel,linetype=stat)) + geom_line()
# InBoundWCF<-p + facet_wrap(~stn_code, scale="fixed") + scale_x_date(labels = date_format("%b")) + labs(title="In-Boundary Water Consumption Footprint\nof Delhi's Own Generation, Disaggregated By Source") + scale_y_continuous(name="Million Liters Freshwater")
# InBoundWCF
#
# ## Now plot both withdrawals and consumption...
# # Units: BIllion Liters Water (BLw)
# OwnGenWF<-ggplot(OwnGen,aes(x=Date,y=WF/10^3,colour=Fuel,linetype=stat)) + geom_line() + facet_wrap(~stn_code + metric) + scale_x_date(labels = date_format("%b")) + labs(title="Water Withdrawal and Consumption Requirements\nof Power Plants Located In Delhi") + scale_y_continuous(name="Billion liters water per month")
# Total WF of Delhi Own Gen
TotalIBWF<-ddply(OwnGen, .(year,month_id,POSIXct,Date,metric,stat),summarize,WF=sum(WF))
TotalIBWF$Scale<-"In-boundary"
TotalIBWFp<-ggplot(TotalIBWF, aes(x=Date,y=WF/10^3,linetype=stat)) + geom_line() + facet_wrap(~metric,scale="free") + scale_x_date(labels = date_format("%b"),name="Month", breaks="2 months") + scale_y_continuous(name=ylab2) + labs(title="In-boundary water footprint of Delhi own generaiton\nSummation of all sources (note change in y-scale)")
TotalIBWFp
# Total WF (Showing min, mean and max estimates for both In-boundary and Trans-boundary)
TotalWF<-rbind(TotalIBWF,TotalTBWF)
p<-ggplot(TotalWF, aes(x=Date,y=WF/10^3,linetype=stat,colour=Scale)) + geom_line() + facet_wrap(~metric, scale="free") + scale_y_continuous(name=ylab2); p
p<-ggplot(TotalWF, aes(x=Date,y=WF,linetype=stat,colour=metric)) + geom_line() + facet_wrap(~Scale + metric, scale="fixed"); p
# Show mean condition only
mean<-subset(TotalWF,stat=="mean")
# Show IB and TB seperately
TotalWFmean<-ggplot(mean, aes(x=Date,y=WF/10^3,colour=metric, linetype=Scale)) + geom_line() + scale_x_date(labels = date_format("%b"), name=xlab1) + labs(title="Total Water Footprint of Delhi's Electricity Supply\n[mean estimates only; note change in y-scale]") + scale_y_continuous(name=ylab2, breaks=c(10,20,30,40,50,60)) + facet_wrap(~Scale, scale="free");
TotalWFmean
# Per capita Withdrawals and consumption
TotalWF$percap<-TotalWF$WF/23 #Million liters divided by millions people = liters per capita
percap<-ggplot(TotalWF,aes(x=Date,y=percap/30,colour=metric,linetype=stat)) + geom_line() + facet_wrap(~Scale + metric, scale="free"); percap
# Show combined (IB+TB)
# Total WF (Showing min, mean and max combined estiamtes (IB+TB)
SumWF<-ddply(TotalWF, .(year,month_id,POSIXct,Date,metric,stat),summarize,WF=sum(WF))
p2<-ggplot(SumWF, aes(x=Date,y=WF,linetype=stat,colour=metric)) + geom_line() + facet_wrap(~metric) + scale_x_date(labels = date_format("%b"), breaks="2 month", name="Month") + scale_y_continuous(name=ylab2); p2
SumWFmean<-subset(SumWF, stat=="mean")
SumWFmeanp<-ggplot(SumWFmean,aes(x=Date,y=WF/10^3,colour=metric)) + geom_line()
p3<-SumWFmeanp + scale_x_date(labels = date_format("%b"), breaks="2 month", name="Month") + labs(title="Total water footprint of Delhi's electricity supply") + scale_y_continuous(name=ylab2); p3
SumWF$percap<-SumWF$WF/23 #Million liters divided by million people = liters per person
Sumpercap<-ggplot(SumWF,aes(x=Date,y=percap/30,colour=metric,linetype=stat)) + geom_line() + facet_wrap(~metric, scale="free"); Sumpercap
# mean2<-subset(SumWF,stat=="mean")
# meanW<-subset(mean2,metric=="Withdrawals"); range(meanW$WF)
# meanC<-subset(mean2,metric=="Consumption"); range(meanC$WF)
# add a line showing municipal supply comparison
muniW<-850*3.78 # MLD produced (withdrawals slightly higher)
pop<-16753235/10^6 # million people
muniWpc<-muniW/pop #liters per person per day (production ~ withdrawals)
muniCpc<-50*3.78 #liters "consumed: per person per day
muni<-rep(96.390,12) #Delhi Municipal Supply according to 850MGD figure reported in 2012 Delhi Statistical Handbook
muni<-as.data.frame(muni)
muni$units<-rep("BL",12)
muni$metric<-as.factor(rep("Municipal Supply",12))
muni$Date<-as.Date(levels(as.factor(merge$Date)))
#muni$muni<-factor(muni$muni[c(2,3,1)])
SumWFmeanp + geom_line(aes(x=muni$Date,y=muni$muni,colour=muni$metric))
# End Delhi_OwnGen.R
##########################################################
#########################################################
#########################################################
# NRPC_Schedule_Drawal_15min_1y.R
setwd("/Users/elliotcohen/Dropbox/data/Electricity/CEA/Rcommands")
library(xlsx)
library(plyr)
library(ggplot2)
library(scales)
library(gdata)
library(chron)
# Multiple plot function
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
require(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
# 15-min interval energy use data aggregated by State/UT. Original units = LU. "c" data frame converted to MU.
############### Actual Drawal (15-min) ###################
# Import Northern Region Schedule-Drawal-UI data...
Act.drl=read.xlsx(file="/Users/elliotcohen/Dropbox/data/Electricity/CEA/WorkingDocs/Schedule-Drawal-UI_15min_2012-13.xls",sheetIndex=2,colIndex=c(1:98),as.data.frame=TRUE,header=TRUE)
#Act.drl=Act.drl[7:371,] # Take one full year's data
#day of the week index (1=Monday, 2=Tuesday, etc...)
dayIND<-c(rep(1,96),rep(2,96),rep(3,96),rep(4,96),rep(5,96),rep(6,96),rep(7,96))
table(dayIND) #96 timeslices each day
#number of days in the time series
d=dim(Act.drl)[1]/length(levels(Act.drl$Seb_Name))
#sequence from zero to number of seconds in the time series (~1 yr) in 15-min intervals.
seq<-seq(0,(d*24*60*60)-(15*60),15*60)
#initialize ts on first day of data
time<-as.POSIXct(seq,origin="2012-04-01", tz='IST')
#Grab Delhi data
Act.drl.Delhi=subset(Act.drl, Seb_Name=="DELHI")
#grab the values of Act.drl.Delhi (in units of MU)
a<-Act.drl.Delhi[,3:98]
a<-as.data.frame(t(a))
#stack days into one long vector
a<-stack(a)
a$dayIND<-dayIND
a$POSIXlt<-time
a$var<-c(rep("Actual", length(a)))