monthly_energy_balance/insel/monthly_demand_calculation.py

67 lines
2.5 KiB
Python

"""
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
from hub.hub_logger import logger
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)
if Path(full_path_out).is_file():
building.heating['month'], building.cooling['month'] = self._demand(full_path_out)
else:
building.heating['month'] = pd.DataFrame([0] * 12, columns=['INSEL'])
building.cooling['month'] = pd.DataFrame([0] * 12, columns=['INSEL'])
logger.error(f'Building {building.name} could not be processed. Heating and cooling set to 0\n')
sys.stderr.write(f'Building {building.name} could not be processed. Heating and cooling set to 0\n')
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
@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