# Tm laskentakoodi perustuu KEKO ekolaskuriin. 
# SYKEn internet-sivulla olevan KEKO ekolaskurin osoite on www.ymparisto.fi/keko 
# Tt viittausta KEKOon ja SYKEen internet-sivun osoitetta ei saa poistaa koodin alusta 
# vaikka koodia muokattaisiin.
# -*- coding: utf-8 -*-

# constants for KEKO Building calculations
# For array type constants, import numpy
import numpy
from numpy import array, zeros, ones, loadtxt

### 1 x 1 constants
### for calculations some initializing parameters
# Piling constants
pilingDistance = 1.5 # Paalutusvli [m] D157
pilingDepth = 10 # paalutussyvyys [m] E157
pilingMassPerM = 0.216 # paino [t/m] F157

# For variable initializing or loops
nEne = 4 # heating, hot water, electricity, cooling
nR = 8
nR2 = 24 # rakenneosien lkm
nC = 7
nC2 = 3
nBtypes = 8 # okt, rt,kt,....
nBtypesR = 6 # kt,...
# For Annual calculations
n_calcYears = 50
# INITIALIZE variables
constMats = [] # init
Rest = [] # init
###################################################
##### ------- Array type constants --------------##
###################################################
# muuntokerroin kerrosalasta bruttoalaksi
floorAtoGrossA = array([1.1,1.03,1.06,1.1,1.1,1.1,1.03,1.06]) # K148:K155
#-- Anturan tietoja
# Anturan poikkileikkaus, perus/vaativa perustus [mm]
baseW = array([500.,700.]) # E149:E150
baseH = array([800.,1000.]) # F149:F150
baseTperM3 = 2.5
# -- Kaivanto
barrowPitC = array([1.5919502617801,1.9901832460733]) # ave, demanding, [t] E161:F161
# -- Tytt
fillingC = array([0.394633507853403,0.493455497382199]) # ave, demanding, [t] E165:F165
## Energy efficiency levels: D40, E40, G40
enEffLevel = array([1.0, 1.015, 1.05]) # norms, low-energy, passive
## Vaativan perustuksen kertoimet: Q30:Q37
demFoundConsts = array([1.03,1.03,1.03,1.03,1.03,1.03,1.03,1.03])
## Energiakorjauksen kertoimet R30:R37
eneRepConsts = array([.5,.5,.5,.5,.5,.5,.5,.5])
## Kytttavan muutos kertoimet S30:S37
altUseConsts = array([.5,.5,.5,.5,.5,.5,.5,.5])
# Kytttavan muutos, energiatehokkuus, kertoimet T330:T337
altUseConsts2 = array([1.,1.,1.,1.,1.,1.,1.,.0])
## INITIALIZE some variables
barrowPit = []
fillingAll = []
altOutput = [] #
altOutputFin = []
eRepOutputFin = []


###################################################
##### ------- Read from text file(s) --------------##
###################################################
### ---OBS pitk nm lukea sittenkin jossain muualla?
##
# 1
# Omakotitalojen ja rivitalojen rakentamisen materiaalikertoimet
# KEKO-laskuri_vrikoodattu_Rakennukset_....xlsx: J99:J122
# CdetachedAttachedH = [24 x 3] from CdetachedAttachedHouses.txt
# rows = MAAOSAT: LOUHINTA/MAANKAIVU,MAAOSAT: TYTT,
# TUENNAT JA VAHVISTUKSET: POHJARAKENTEET JA VAHVISTUS,
# TALO-OSAT: PERUSTUKSET (ANTURAT, SOKKELI/PERUSMUURI)
# PERUSTUKSET: ALAPOHJA, RUNKO: KANTAVAT VLISEINT
# RUNKO: PILARIT, RUNKO: PALKIT,RUNKO: VLIPOHJAT,RUNKO: YLPOHJAT
# ULKOSEINT, SEINIEN SISPUOLI,IKKUNAT,VESIKATE,ULKOTASOT: PARVEKKEET TMS.
# VLISEINT, PORTAAT,LATTIOIDEN PINTAVALU,LATTIAPINNOITTEET,ALAKATOT/SISKATOT
# OVET,TALOTEKNIIKKA,HISSIT,RAKENTAMISEEN KULUNUT ENERGIA
# columns = puurakenteinen, puurunko tiiliverhoilu, tiilinen
CdetachedAttachedH = loadtxt("CdetachedAttachedHouses.txt") # [?]

# 2
# Kerrostalojen ja muiden rakennuste materiaalikertoimet
# E103:G122 (kt) ja K103:M122 (muut)
# CapartmentOtherB = [20 x 6] from CapartmentOtherBuildings.txt
# rows = PERUSTUKSET: ALAPOHJA, RUNKO: KANTAVAT VLISEINT
# RUNKO: PILARIT, RUNKO: PALKIT,RUNKO: VLIPOHJAT,RUNKO: YLPOHJAT
# ULKOSEINT, SEINIEN SISPUOLI,IKKUNAT,VESIKATE,ULKOTASOT: PARVEKKEET TMS.
# VLISEINT, PORTAAT,LATTIOIDEN PINTAVALU,LATTIAPINNOITTEET,ALAKATOT/SISKATOT
# OVET,TALOTEKNIIKKA,HISSIT,RAKENTAMISEEN KULUNUT ENERGIA
# columns = VTT Puurunko, VTT normirunko,VTT raskas runko (kt),
# VTT Metlan toimistorakennus puu (runko ja vaippa),
# Sinivuori 2004 toimisto/yliopisto sis laboratorio,
# Sinivuori 2004 yliopisto/toimisto
CapartmentOtherB = loadtxt("CapartmentOtherBuildings.txt") # [?]

# 3
# Materiaalityyppien CO2-kertoimet
# N30:P37
# matCO2coefs = [8x3] from matCO2coefs.txt
# rows = Kerrostalot (AK),Rivitalot (AR),Omakoti- ja paritalot (AO, AP)
# Toimistorakennukset,Liikerakennukset,Palvelurakennukset,
# Teollisuus- ja muut tuotantorakennukset, Muut rakennukset
# columns = ?
matCO2coefs = loadtxt("matCO2coefs.txt") # [?]

# 4
# Rakennusten energiankulutus
# CheatingBtypesYears = [7x10] from CheatingBtypesYears.txt
# rows = Kerrostalot (AK),Rivitalot (AR),Omakoti- ja paritalot (AO, AP)
# Toimistorakennukset,Liikerakennukset,Palvelurakennukset,
# Teollisuus- ja muut tuotantorakennukset
# columns = -1960, 1960-1970,1970-1980,1980-1990,1990-2000,2000-2010,
# 2010-2012,2013 Normitaso,Matalaenergia,Passiivi
CheatingBtypesYears = loadtxt("CheatingBtypesYears.txt") # kWh/m2/a

# 5
# Rakennusten LKV energiankulutus
# CheatingWBtypesYears = [8x10] from CheatingWBtypesYears.txt
# rows = Kerrostalot (AK),Rivitalot (AR),Omakoti- ja paritalot (AO, AP)
# Toimistorakennukset,Liikerakennukset,Palvelurakennukset,
# Teollisuus- ja muut tuotantorakennukset, muut rakennukset
# columns = -1960, 1960-1970,1970-1980,1980-1990,1990-2000,2000-2010,
# 2010-2012,2013 Normitaso,Matalaenergia,Passiivi
CheatingWBtypesYears = loadtxt("CheatingWBtypesYears.txt") # kWh/m2/a

# 6
# Rakennusten shkenergiankulutus
# CelectricityBtypesYears = [7x10] from CelectricityBtypesYears.txt
# rows = Kerrostalot (AK),Rivitalot (AR),Omakoti- ja paritalot (AO, AP)
# Toimistorakennukset,Liikerakennukset,Palvelurakennukset,
# Teollisuus- ja muut tuotantorakennukset
# columns = -1960, 1960-1970,1970-1980,1980-1990,1990-2000,2000-2010,
# 2010-2012,2013 Normitaso,Matalaenergia,Passiivi
CelectricityBtypesYears = loadtxt("CelectricityBtypesYears.txt") # kWh/m2/a

# 7
# Rakennusten jhdytysenergiankulutus
# CcoolingBtypesYears = [7x10] from CcoolingBtypesYears.txt
# rows = Kerrostalot (AK),Rivitalot (AR),Omakoti- ja paritalot (AO, AP)
# Toimistorakennukset,Liikerakennukset,Palvelurakennukset,
# Teollisuus- ja muut tuotantorakennukset
# columns = -1960, 1960-1970,1970-1980,1980-1990,1990-2000,2000-2010,
# 2010-2012,2013 Normitaso,Matalaenergia,Passiivi
CcoolingBtypesYears = loadtxt("CcoolingBtypesYears.txt") # kWh/m2/a

# 8
# Materiaalien uusiutuva/uusiutumaton
# CrenTot = [x2] from CrenewableNtot.txt
# rows = kt Puurunko; kt, normirunko; kt, raskas runko;
# Puurakenteinen omakotitalo; Puurakenteinen tiiliverhoilu omakotitalo;
# tiilinen omakotitalo; toimistorakennus puu (runko ja vaippa);
# toimisto/yliopisto, sis laboratorio; yliopisto/toimisto
# columns = renewable, non-renewable
CrenTot = loadtxt("CrenewableNtot.txt") #  [?]

# 9
# Rakennusten kunnossapito, pstt
# CbuildingMaintenance = [51x4] from CbuildingMaintenanceY.txt
# rows = years: 0, 1, ...50 
# columns = apartment house, attached house, office, tertiarybuildings
CbuildingMaintenance = loadtxt("CbuildingMaintenanceY.txt") #  [?]

# -*- coding: utf-8 -*-

# constants for KEKO Energy calculations
# For array type constants, import numpy
import numpy
from numpy import array, zeros, size

#--- 1 x 1 constants
n_col = 2   # For output saving number of
            # columns (for average and marginal emissions factors)
nStockTypes = 2 # old and new building stock
#--- Array type constants
flags = ["average", "marginal"] # emission factor "flags"
calc_years = range(51)#array([10,20,30,40,50]) # calculation years
#--- Initialize needed parameters for the loops and output writing
n_years = size(calc_years) # number of years (calc_years from Energy constants)
CumEmElectricity = []#zeros((n_years,n_col)) # initialize output variable for electricity
# Energy and heating requirement
heatRequirementAll = [] # ...will be [2x11]
eneRequirementAll = [] # ...will be [2x9]
# Heating, electricity and cooling variables
emHeating = [] # EmissionsHeating, for saving J115, M115, Z115, AC115
emEle = [] # Emissions electricity, old: K115, N115, new: AA115, AD115
emCool = [] # Emissions cooling, old: L 115,O115 new: AB115, AE115
# Outputs C168:C170 and C217:C219
heatingOutput = []
electricityOutput = []
coolingOutput = []
# At year 50
heatingEND = []
electricityEND = []
coolingEND = []
# Cumulative result variables
CumEmElectricityALL = []
CumEmHeatingALL = []
CumEmCoolingALL = []
EmissionsAllCUM = []
# Annual results
emAllYear = []

# -*- coding: utf-8 -*-

# constants for KEKO Infrastructure calculations
# For array type constants, import numpy
import numpy
from numpy import array, zeros, loadtxt

#################################################
### ------- 1x1 type constants --------------##
#################################################
# The following assist calculations
nStatus = 2 # before and after
nRoadTypes = 9 # 
nRoadMatTypes = 4 # asfalttibetoni, murske, sora, hiekka
nFoundTypes = 3 # poor, average, good
nCalcYears = 50 
# The following are from KEKO excel
DensityRock = 2.625 # t/m3, D157 
MinableVolumeConversion = 1.2 # conversion between gross and net voume, D155
ExcavationConstructionEmissions = 0.11 # t CO2e/m3, I161
ExcavationOtherEmissions = 0.11 # t CO2e/m3, I163
NetworkUseEmissionCoefficient = 7.5e-4 #[-] used in cells F185:F187   

#################################################
### ------- Array type constants --------------##
#################################################
# Kerroin rakennusten rakentamisen pst- sek LV-mrist
# rows = shkverkko, tietoliikenneverkko; kaukolmpverkko;
# vesijohtoverkko, jtevesiverkko, hulevesi/sadevesiverkko
TechnicalNetworkCoefficients = array([0.005,0.005,0.015]) # [-] D169,D171,D172
#################################################
### ------- Read from text file(s) --------------##
#################################################
#

#1
# Tierakentamisen materiaalikulutus
# Rows represent the data for different types of roads 
# and their structure (6x4) [t/m2]
# Road types = streets, connecting road, seututie, valtatie,
# mottoritie, pyrtie/jalkakytv
# Structure types (4) = paving, road base, sub-base,protective course
# Columns represent foundation conditions (good, average, poor)
# laskenta_INFRA, F41:H64. MatRoadBuilding = [24x3]
MatRoadBuilding = loadtxt("RoadBuildingMaterialConst.txt")

#2
# INFRA-CO2
# Infra CO2 vakiot
# rows = moottoritie, valtatie, seututie, yhdystie,
# 2-raiteinen shkistetty, 1-raiteinen shkistetty
# columns = building, use&maintenance
# infraCO2consts = [6x2], [tCO2/km]
# laskenta_INFRA, D24:E29
infraCO2consts = loadtxt("infraCO2consts.txt")

# 3
# Infra materiaalivakiot
# infraMatConsts = [3x2], [kg/m2]
# Rows kadut, jalkakytvt, viheralueet
# columns = building, use&maintenance
# laskenta_INFRA, D30:E32
infraMatConsts = loadtxt("infraMatConsts.txt")

# 4
# C_Mat_RailConst = [8 x 1], [t/railway m]
# Constants for mass per railway meter 
# Rows = superstructure, earth construction, substructure, wires,
# bridges, tunnels, other structures, railway yeards
# laskenta_INFRA: D72:D81
C_Mat_RailConst = loadtxt("RailWayMatConsts.txt")

# -*- coding: utf-8 -*-

# constants for KEKO Energy calculations
# For array type constants, import numpy
import numpy
from numpy import array, zeros, ones

# 1 x 1 constants

n_calcYears = 50 # number of calculation years for cumulative/emission calculation
#################################################
### ------- Array type constants --------------##
#################################################
# Kuntaseutu (KS) keskiarvo [m2/kem2] laskenta LUONTO J114:J117
KS_consts = array([3.10569391095771,2.6621460358843,1.99747571567981,0.27723963188926]) 
KS_values = array([1.0,0.4,0.06]) # [-] laskenta LUONTO M102:M105
KS_comp_values = array([-2.241027362302,-32.0741781883322,-2.03961810397015]) # [?] K92:K94
X_values = array([1,2,3]) # [-] laskenta LUONTO J102:J104

# -----NPP vakioarvot [9x1]
# kerrostaloalueet, pientaloalueet, keskustatoimintojen ja palveluiden alueet,
# loma-asumisen ja matkailun alueet, liikennealueet, erityisalueet,
#maatalouden alueet, vesialueet. NPP t, [BM/ha,v],
NPP_constants = array([0.5,1.4,0.5,0.5,2.0,0.5,0.9,8.0,1.0]) # F54:F64

# NPPs for virkistysalueet [7x1]
# NPP t, [BM/ha,v]
NPPs_recreationArea = array([3.1,8.9,8.9,8.9,0.1,0.2,0.8]) # H73:H80

# -----Biodiversiteetti vakioarvot [9x1]
# kerrostaloalueet, pientaloalueet, keskustatoimintojen ja palveluiden alueet,
# loma-asumisen ja matkailun alueet, liikennealueet, erityisalueet,
# maatalouden alueet, vesialueet. BDI - factor
BDI_constants = array([6.0,14.0,6.0,6.0,12.0,6.0,6.0,12.0,9.5]) # R54:R64, poislukien R58:59

# BDI for recreation areas [7x1] [BDI factor]
BDIs_recreationArea = array([12.0,15.0,15.0,14.0,10.0,8.0,10.0]) # T73:T80

# ------Lpisevyyskertoimet (permeability)
# 1. set for residential areas, 2.set for other areas
# Calculational factor
perm_consts = array(([15.729,9.9811],[10.211,35.648])) # see equations in AD54:AE57

# And for the rest of areas
# Perm factor
perm_rest_c = array(([0.9,0.4,1.0,1.0,1.0])) # AD54:AE64
# Permeability constants for recreation area
perm_consts_recArea = ones(([2,7])) # AF73:AG80

# --- Hiililaskenta
CinLand = array([55.,6.,60.,60.,60.,500.,50.]) # Hiilimr maassa [t/ha] AX57:AX64
CtoCO2 = 3.66666666666667 # conversion from C to CO2, BA50
LandCshare = 1 # Maan hiilest poistuva osuus, BA52
CsinkFactor = 15 *1e-2 # BC48 = 15 * 1e4 *1e-6 = 15*1e-2 [tC/ha,a]
amountTree = array([50.,90.,180.,10.,0.,0.]) # [m3/ha], [6x1], AU58:AU64
biomassExpansionFactor = 0.736 # [t/m3], constant for all types, AV58:AV64
CshareInBiomass = 0.5 # [%], BA49
BiomassCshare = 1 # Kasvillisuuden hiilest poistuva osuus, BA51

# -*- coding: utf-8 -*-

# constants for KEKO Result calculations
# For array type constants, import numpy
import numpy
from numpy import array, zeros, ones, loadtxt

### 1 x 1 constants
### for calculations some initializing parameters
# Piling constants

C_co2 = 1.76 # Kasvihuonekaasupstt kerrosnelit kohti H21
C_materials = array([2.18, 1.46, 0.]) # buildings + infrastructure

# -*- coding: utf-8 -*-

# constants for KEKO Transport calculations
# For array type constants, import numpy
import numpy
from numpy import array, zeros, ones, loadtxt

# 1 x 1 constants
# for calculations some initializing parameters
nAreaTypes = 5
nTransportModes = 8
nZones = 7
nDistGroups = 3
n_calcYears = 50
nToLoop = range(2) # before, after
# KEKO-specific
shareDieselBus = 1 # [%] , laskenta_LIIKENNE_nyky L192
shareGasBus = 0 # [%] , laskenta_LIIKENNE_nyky L193
shareAsDriver = 0.6955 # Henkilauton jakautuminen, kuljettajana in eqs. G158:H161
shareAsPassenger = 0.3045 # Henkilauton jakautuminen, matkustajana in eqs. G158:H161
#################################################
### ------- Array type constants --------------##
#################################################
# kerroin kaupunkiseutu- ja etisyysvalinnan perusteella
# koko maan keskiarvo [km]
factorAveFin = array([1.059,0.730,20.796,9.105,2.963,0.235,2.672,3.846]) # E173:L173
#################################################
### ------- Read from text file(s) --------------##
#################################################
# 

# 1
# Autokilometrit eri etisyysvyhykkeill kaupunkiseuduilla
# G143:J149, read from txt file carKmInZonesAll.txt
# carKmInZones = [7x4]; n rows = 7 (city types) n cols = 4 (zone types)
# ROWS: Helsinki, Tampere&Turku, city pop 80-200*1e3,
# urban area pop 40-80*1e3, urban area pop 25-40*1e3,
# urban area pop 15-25*1e3, rural area
# COLUMNS: inner core area, outer core area, inner kehysalue,
# outer kehysalue
carKmInZones = loadtxt("carKmInZonesAll.txt") # [km]

# 2
# Matkasuorite, [km/hl/vrk]
# Eri kaupunkiseuduilla asuvien matkat
# E68:M75, read from txt file tripsInCities.txt
# tripsInCities = [8x9]
# Rows: Helsinki, Tampere&Turku, city pop 80-200*1e3,
# urban area pop 40-80*1e3, urban area pop 25-40*1e3,
# urban area pop 15-25*1e3, rural area
# Columns: walking, bike, car (as driver), car (as passenger),
# bus, metro&tram, train, other, all
tripsInCities = loadtxt("tripsInCities.txt") # [km]

# 3
# Eri aluetehokkuusalueilla asuvien matkat, [km/hl/vrk]
# E81:L87, read from txt file tripsInCities.txt
# tripsByAreaDens = [7x9]
# Rows: area density <.02, .02<=dens<.08, .08<=dens<.16,
# .16<=dens <.32, 0.332 <=dens
# Columns: walking, bike, car (as driver), car (as passenger),
# bus, metro&tram, train, other, all
tripsByAreaDens = loadtxt("tripsByAreaDensity.txt") # [km]

# 4
# Eri yhdyskuntarakenteen vyhykkeill asuvien matkat, [km/hl/vrk]
# E92:L99, read from txt file tripsInZones.txt
# tripsInZones = [8x9]
# Rows: jalankulkuvyhyke, alakeskus, jalankulun reunavyhyke,
# intensiivinen JL-vyhyke, joukkoliikennevyhyke, autovyhyke,
# ei vyhykkeell, kaikki
# Columns: walking, bike, car (as driver), car (as passenger),
# bus, metro&tram, train, other, all
tripsInZones = loadtxt("tripsInZones.txt") # [km]

# 5
# Matka supermarkettiin, [km/hl/vrk]
# E92:L99, read from txt file tripsSupermarket.txt
# tripsSupermarket = [4x9]
# Rows: 500m, 1 km, no in the zone, all
# Columns: walking, bike, car (as driver), car (as passenger),
# bus, metro&tram, train, other, all
tripsSupermarket = loadtxt("tripsSupermarket.txt") # [km]

# 6
# Suomen keskimrinen pst ja energiankulutus henkilkilometri kohden
# (VTT LIPASTO 24.4.2009, 25.4.2012 ja 7.8.2012)
# H213:I219, read from txt file LIPASTO_data.txt
# CO2energyPerHkm = [7x2]
# Rows: passenger cars, diesel bus, gas bus, train, tarm, walking,
# biking etc.
# Columns: CO2 eq [g/hkm], Energy [kWh/hkm]
CO2energyPerHkm = loadtxt("LIPASTO_data.txt") # [km]

# 7
# Tekninen tehostumiskerroin
# H213:I219, read from txt file techEffFactors.txt
# techEffFactors = [51x1], D259:BA259, laskenta_LIIKENNE_tuleva
techEffFactors = loadtxt("techEffFactors.txt") # [km]



