Talk:Energy balance in Basel

From Testiwiki
Jump to: navigation, search

Data to equations script

options(digits = 15)

t1 <- "Flow;Activity;Use;Fuel;Unit;2010;2020
Heating Supply;Heating Supply;Input;Others;TJ;1746.810171953100;2277.39543879580000
Heating Supply;Heating Supply;Input;Oil products;TJ;205.858802660488;268.38743312948000
Heating Supply;Heating Supply;Input;Natural gas;TJ;2028.691519994430;2644.89690324695000
Heating Supply;Heating Supply;Input;Electricity input;TJ;42.709295157778;55.68204006835690
Heating Supply;Heating Supply;Input;Environmental heat;TJ;43.563481060933;56.79568086972410
Heating Supply;Heating Supply;Output;Electricity output;TJ;107.627423797599;140.31874097225900
Heating Supply;Heating Supply;Output;Heat;TJ;3175.009002029180;4139.40285868165000
Heating Supply;Heating Supply;Output;Loss;TJ;784.996844999951;1023.43589645640000
Losses;Losses;Output;Electricity;TJ;1148.285868352080;1627.66517538361000
Losses;Losses;Output;Oil products;TJ;9.179016720464;9.31046082615613
Losses;Losses;Output;Natural gas;TJ;19.619120807397;13.77640430886830
Losses;Losses;Output;Heat;TJ;451.010156866131;588.00234312184900"

t2 <- "Year;Activity;Fuel;Unit;2010;2020
2004;Import;Others;TJ;1880.921283064210;2425.25293879580
2004;Import;Oil products;TJ;4326.217419402300;4447.74984842623
2004;Import;Natural gas;TJ;7080.098835246270;6191.95847582505
2004;Import;Environmental heat;TJ;55.938481060933;73.47099336972
2004;Import;Solar energy;TJ;17.625000000000;15.29718750000
2004;Import;Electricity;TJ;6919.447456490840;9815.51878161507
2004;Recovery of energy;Electricity;TJ;50.965586548236;72.24238550163
2004;Recovery of energy;Heat;TJ;174.001154836949;157.40948444020"

t3 <- "Year;Activity;Fuel;Unit;2010;2020
2004;Households;Oil products;TJ;473.955555555556000;379.260000000000000
2004;Households;Natural gas;TJ;836.343750000000000;1005.893437500000000
2004;Households;Heat;TJ;932.000000000000000;1541.295000000000000
2004;Households;Electricity;TJ;1344.109589041100000;2037.586130136990000
2004;Households;Environmental heat;TJ;7.875000000000000;10.611562500000000
2004;Households;Solar energy;TJ;10.125000000000000;13.643437500000000
2004;Industry;Oil products;TJ;215.384615384615000;67.846153846153900
2004;Industry;Natural gas;TJ;2535.444444444440000;1029.857500000000000
2004;Industry;Heat;TJ;333.000000000000000;367.132500000000000
2004;Industry;Electricity;TJ;1341.964285714290000;2515.176562500000000
2004;Industry;Others;TJ;120.111111111111000;132.422500000000000
2004;Commerce;Oil products;TJ;514.576271186441000;495.115932203390000
2004;Commerce;Natural gas;TJ;1652.000000000000000;1471.074230769230000
2004;Commerce;Heat;TJ;1633.000000000000000;1800.382500000000000
2004;Commerce;Electricity;TJ;2940.971428571430000;3505.320000000000000
2004;Commerce;Others;TJ;14.000000000000000;15.435000000000000
2004;Commerce;Environmental heat;TJ;4.500000000000000;6.063750000000000
2004;Commerce;Solar energy;TJ;7.500000000000000;1.653750000000000
2004;Transport;Oil products;TJ;2907.263157894740000;3227.829868421050000
2004;Transport;Natural gas;TJ;8.000000000000000;26.460000000000000
2004;Transport;Electricity;TJ;260.000000000000000;286.650000000000000"

t1 <- read.table(text = t1, header = TRUE, sep = ";")

t2 <- read.table(text = t2, header = TRUE, sep = ";")

t3 <- read.table(text = t3, header = TRUE, sep = ";")

y <- tapply(t1$X2010, t1[c("Activity", "Fuel")], I)

k1 <- (y[1,"Others"] + 
	y[1,"Natural gas"] + 
	y[1,"Electricity input"] + 
	y[1,"Environmental heat"]) / y[1,"Oil products"]

k2 <- (y[1,"Others"] + 
	y[1,"Oil products"] + 
	y[1,"Electricity input"] + 
	y[1,"Environmental heat"]) / y[1,"Natural gas"]

k3 <- (y[1,"Others"] + 
	y[1,"Oil products"] + 
	y[1,"Natural gas"] + 
	y[1,"Environmental heat"]) / y[1,"Electricity input"]

k4 <- (y[1,"Others"] + 
	y[1,"Oil products"] + 
	y[1,"Natural gas"] + 
	y[1,"Electricity input"]) / y[1,"Environmental heat"]

k5 <- y[1, "Heat"] / y[1, "Loss"]
k6 <- y[1, "Electricity output"] / y[1, "Loss"]

k7 <- y[2, "Electricity"] / y[1, "Electricity output"]
k8 <- y[2, "Oil products"] / y[1, "Oil products"]
k9 <- y[2, "Natural gas"] / y[1, "Natural gas"]
k10 <- y[2, "Heat"] / y[1, "Heat"]

c1 <- t3$X2010[t3$Activity == "Households" & t3$Fuel == "Electricity"]
c2 <- t3$X2010[t3$Activity == "Industry" & t3$Fuel == "Electricity"]
c3 <- t3$X2010[t3$Activity == "Commerce" & t3$Fuel == "Electricity"]
c4 <- t3$X2010[t3$Activity == "Transport" & t3$Fuel == "Electricity"]
c5 <- t3$X2010[t3$Activity == "Households" & t3$Fuel == "Heat"]
c6 <- t3$X2010[t3$Activity == "Industry" & t3$Fuel == "Heat"]
c7 <- t3$X2010[t3$Activity == "Commerce" & t3$Fuel == "Heat"]
c8 <- y[1, "Heat"]
c9 <- t2$X2010[t2$Activity == "Recovery of energy" & t2$Fuel == "Electricity"]

Equations1 <- c("H_other_in + H_oil_in + H_gas_in + H_electricity_in + H_environment_in = H_electricity_out + H_heat_out + H_loss",
paste("H_other_in + H_gas_in + H_electricity_in + H_environment_in =", k1, "* H_oil_in"), 
paste("H_other_in + H_oil_in + H_electricity_in + H_environment_in =", k2, "* H_gas_in"), 
paste("H_other_in + H_oil_in + H_gas_in + H_environment_in =", k3, "* H_electricity_in"), 
paste("H_other_in + H_oil_in + H_gas_in + H_electricity_in =", k4, "* H_environment_in"), 
paste("H_heat_out =", k5, "* H_loss"),
paste("H_electricity_out =", k6, "* H_loss"),
paste("Loss_electricity =", k7, "* H_electricity_out"),
paste("Loss_oil =", k8, "* H_oil_in"),
paste("Loss_gas =", k9, "* H_gas_in"),
paste("Loss_heat =", k10, "* H_heat_out"),
"Import_electricity + H_electricity_out + Recovery_electricity = Loss_electricity + Household_electricity + Industry_electricity + Commerce_electricity + Transport_electricity",
"Recovery_heat + H_heat_out = Loss_heat + Household_heat + Industry_heat + Commerce_heat",
paste("Household_electricity =", c1),
paste("Industry_electricity =", c2),
paste("Commerce_electricity =", c3),
paste("Transport_electricity =", c4),
paste("Household_heat =", c5),
paste("Industry_heat =", c6),
paste("Commerce_heat =", c7),
paste("H_heat_out =", c8),
paste("Recovery_electricity =", c9)
)

y <- tapply(t1$X2020, t1[c("Activity", "Fuel")], I)

k1 <- (y[1,"Others"] + 
	y[1,"Natural gas"] + 
	y[1,"Electricity input"] + 
	y[1,"Environmental heat"]) / y[1,"Oil products"]

k2 <- (y[1,"Others"] + 
	y[1,"Oil products"] + 
	y[1,"Electricity input"] + 
	y[1,"Environmental heat"]) / y[1,"Natural gas"]

k3 <- (y[1,"Others"] + 
	y[1,"Oil products"] + 
	y[1,"Natural gas"] + 
	y[1,"Environmental heat"]) / y[1,"Electricity input"]

k4 <- (y[1,"Others"] + 
	y[1,"Oil products"] + 
	y[1,"Natural gas"] + 
	y[1,"Electricity input"]) / y[1,"Environmental heat"]

k5 <- y[1, "Heat"] / y[1, "Loss"]
k6 <- y[1, "Electricity output"] / y[1, "Loss"]

k7 <- y[2, "Electricity"] / y[1, "Electricity output"]
k8 <- y[2, "Oil products"] / y[1, "Oil products"]
k9 <- y[2, "Natural gas"] / y[1, "Natural gas"]
k10 <- y[2, "Heat"] / y[1, "Heat"]

c1 <- t3$X2020[t3$Activity == "Households" & t3$Fuel == "Electricity"]
c2 <- t3$X2020[t3$Activity == "Industry" & t3$Fuel == "Electricity"]
c3 <- t3$X2020[t3$Activity == "Commerce" & t3$Fuel == "Electricity"]
c4 <- t3$X2020[t3$Activity == "Transport" & t3$Fuel == "Electricity"]
c5 <- t3$X2020[t3$Activity == "Households" & t3$Fuel == "Heat"]
c6 <- t3$X2020[t3$Activity == "Industry" & t3$Fuel == "Heat"]
c7 <- t3$X2020[t3$Activity == "Commerce" & t3$Fuel == "Heat"]
c8 <- y[1, "Heat"]
c9 <- t2$X2020[t2$Activity == "Recovery of energy" & t2$Fuel == "Electricity"]

Equations2 <- c("H_other_in + H_oil_in + H_gas_in + H_electricity_in + H_environment_in = H_electricity_out + H_heat_out + H_loss",
	paste("H_other_in + H_gas_in + H_electricity_in + H_environment_in =", k1, "* H_oil_in"), 
	paste("H_other_in + H_oil_in + H_electricity_in + H_environment_in =", k2, "* H_gas_in"), 
	paste("H_other_in + H_oil_in + H_gas_in + H_environment_in =", k3, "* H_electricity_in"), 
	paste("H_other_in + H_oil_in + H_gas_in + H_electricity_in =", k4, "* H_environment_in"), 
	paste("H_heat_out =", k5, "* H_loss"),
	paste("H_electricity_out =", k6, "* H_loss"),
	paste("Loss_electricity =", k7, "* H_electricity_out"),
	paste("Loss_oil =", k8, "* H_oil_in"),
	paste("Loss_gas =", k9, "* H_gas_in"),
	paste("Loss_heat =", k10, "* H_heat_out"),
	"Import_electricity + H_electricity_out + Recovery_electricity = Loss_electricity + Household_electricity + Industry_electricity + Commerce_electricity + Transport_electricity",
	"Recovery_heat + H_heat_out = Loss_heat + Household_heat + Industry_heat + Commerce_heat",
	paste("Household_electricity =", c1),
	paste("Industry_electricity =", c2),
	paste("Commerce_electricity =", c3),
	paste("Transport_electricity =", c4),
	paste("Household_heat =", c5),
	paste("Industry_heat =", c6),
	paste("Commerce_heat =", c7),
	paste("H_heat_out =", c8),
	paste("Recovery_electricity =", c9)
)

out <- #paste(
	paste(
		rep(
			c(2010, 2020), 
			c(length(Equations1), length(Equations2))
		), 
		c(Equations1, Equations2),
		0,
		sep = "|"
	)#, 
	#collapse = "\n"
#)

write(out, "basel.txt")