peak_demands/insel/monthly_demand_calculation.py

65 lines
2.2 KiB
Python
Raw Normal View History

"""
Monthly demand calculation using the monthly energy balance methodology based on the norm...
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2020 Project Author Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca
"""
from pathlib import Path
import pandas as pd
import sys
import csv
from insel.insel import Insel
class MonthlyDemandCalculation(Insel):
def __init__(self, city, main_path, weather_format):
super().__init__(main_path)
self._city = city
self._weather_format = weather_format
def results(self):
for building in self._city.buildings:
file_name = building.name + '.out'
full_path_out = Path(self._path / file_name).resolve()
full_path_out.parent.mkdir(parents=True, exist_ok=True)
try:
building.heating['month'], building.cooling['month'] = self._demand(full_path_out)
heating_year = 0
for value in building.heating['month']['INSEL']:
if value == 'NaN':
value = '0'
heating_year += float(value)
yearly_heating = pd.DataFrame([heating_year], columns=['INSEL'])
building.heating['year'] = yearly_heating
cooling_year = 0
for value in building.cooling['month']['INSEL']:
if value == 'NaN':
value = '0'
cooling_year += float(value)
yearly_cooling = pd.DataFrame([cooling_year], columns=['INSEL'])
building.cooling['year'] = yearly_cooling
except ValueError:
print(sys.exc_info()[1])
print('Building ' + building.name + ' could not be processed')
continue
@staticmethod
def _demand(insel_outputs_path):
heating = []
cooling = []
with open(Path(insel_outputs_path).resolve()) as csv_file:
csv_reader = csv.reader(csv_file)
for line in csv_reader:
demand = str(line).replace("['", '').replace("']", '').split()
for i in range(0, 2):
if demand[i] != 'NaN':
aux = float(demand[i])*1000 # kWh to Wh
demand[i] = str(aux)
heating.append(demand[0])
cooling.append(demand[1])
monthly_heating = pd.DataFrame(heating, columns=['INSEL'])
monthly_cooling = pd.DataFrame(cooling, columns=['INSEL'])
return monthly_heating, monthly_cooling