Included an option for return dictionary when output file is None

This commit is contained in:
Peter Yefi 2022-08-18 16:45:55 -04:00
parent 04c56587a8
commit 175d772c24
8 changed files with 37 additions and 22 deletions

View File

@ -6,7 +6,7 @@ Copyright © 2022 Concordia CERC group
Project Coder Peter Yefi peteryefi@gmail.com
"""
from exports.energy_systems.heat_pump_export import HeatPumpExport
from typing import List, Tuple, Union
from typing import List, Dict, Union
class AirSourceHPExport(HeatPumpExport):
@ -28,7 +28,6 @@ class AirSourceHPExport(HeatPumpExport):
template_path = (base_path / tmp_file)
super().__init__(base_path, city, output_path, template_path, demand_path)
def _extract_model_coff(self, hp_model: str, data_type='heat') -> Union[List, None]:
"""
Extracts heat pump coefficient data for a specific
@ -45,7 +44,7 @@ class AirSourceHPExport(HeatPumpExport):
return energy_system.air_source_hp.cooling_capacity_coff
return None
def execute_insel(self, user_input, hp_model, data_type):
def execute_insel(self, user_input, hp_model, data_type) -> Union[Dict, None]:
"""
Runs insel and produces output files
Runs insel and write the necessary files
@ -56,7 +55,6 @@ class AirSourceHPExport(HeatPumpExport):
:param data_type: a string that indicates whether
insel should run for heat or cooling performance
:return:
:return:
"""
capacity_coeff = self._extract_model_coff(hp_model, data_type)
super(AirSourceHPExport, self)._run_insel(user_input, capacity_coeff, 'air_source.insel')
return super(AirSourceHPExport, self)._run_insel(user_input, capacity_coeff, 'air_source.insel')

View File

@ -5,7 +5,7 @@ Copyright © 2022 Concordia CERC group
Project Coder Peter Yefi peteryefi@gmail.com
"""
import os
from typing import List, Tuple, Union, Dict
from typing import List, Union, Dict
import yaml
from string import Template
import pandas as pd
@ -28,7 +28,7 @@ class HeatPumpExport:
self._base_path = base_path
self._output_path = output_path
def _run_insel(self, user_input: Dict, capacity_coeff: List, filename: str) -> None:
def _run_insel(self, user_input: Dict, capacity_coeff: List, filename: str) -> Union[Dict, None]:
"""
Runs insel and write the necessary files
:param user_input: a dictionary containing the user
@ -60,7 +60,7 @@ class HeatPumpExport:
# Writer headers to csv output files generated by insel
self._write_insel_output_headers()
# User output
self._get_user_out_put()
return self._get_user_out_put()
except IOError as err:
print("I/O exception: {}".format(err))
finally:
@ -224,15 +224,30 @@ class HeatPumpExport:
self._input_data["a10"] = a_coeff[9]
self._input_data["a11"] = a_coeff[10]
def _get_user_out_put(self):
def _get_user_out_put(self) -> Union[Dict, None]:
"""
Extracts monthly electricity demand and fossil fuel consumption
from output files generated by insel
:return:
:return: Dict for json output
"""
demand_data = 'fileOut8'
fossil_data = 'fileOut4'
fossil_index = 2
demand_index = 2
electricity_df = pd.read_csv(self._input_data['fileOut8'].strip("'")).iloc[:, 2]
fossil_df = pd.read_csv(self._input_data['fileOut4'].strip("'")).iloc[:, 2]
if self._output_path is None:
demand_data = 'fileOut10'
fossil_data = 'fileOut9'
demand_index = 5
electricity_df = pd.read_csv(self._input_data[demand_data].strip("'")).iloc[:, demand_index]
fossil_df = pd.read_csv(self._input_data[fossil_data].strip("'")).iloc[:, fossil_index]
if self._output_path is None:
return {
'hourly_electricity_demand': electricity_df.values.tolist(),
'daily_fossil_consumption': fossil_df.values.tolist()
}
data = [electricity_df, fossil_df]
df = pd.concat(data, axis=1)
@ -240,3 +255,5 @@ class HeatPumpExport:
s = pd.Series(["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec", "Total"])
df = df.set_index([s])
df.to_csv(self._output_path)

View File

@ -6,7 +6,7 @@ Copyright © 2022 Concordia CERC group
Project Coder Peter Yefi peteryefi@gmail.com
"""
from exports.energy_systems.heat_pump_export import HeatPumpExport
from typing import List, Tuple, Union
from typing import List, Dict, Union
class WaterToWaterHPExport(HeatPumpExport):
@ -41,7 +41,7 @@ class WaterToWaterHPExport(HeatPumpExport):
return energy_system.water_to_water_hp.power_demand_coff
return None
def execute_insel(self, user_input, hp_model):
def execute_insel(self, user_input, hp_model) -> Union[Dict, None]:
"""
Runs insel and produces output files
Runs insel and write the necessary files
@ -52,4 +52,4 @@ class WaterToWaterHPExport(HeatPumpExport):
:return:
"""
pow_demand_coeff = self._extract_model_coff(hp_model)
super(WaterToWaterHPExport, self)._run_insel(user_input, pow_demand_coeff, 'w2w.insel')
return super(WaterToWaterHPExport, self)._run_insel(user_input, pow_demand_coeff, 'w2w.insel')

View File

@ -48,10 +48,10 @@ class EnergySystemsExportFactory:
:return: None
"""
if source == 'air':
AirSourceHPExport(self._base_path, self._city, self._output_path, self._sim_type, self._demand_path)\
return AirSourceHPExport(self._base_path, self._city, self._output_path, self._sim_type, self._demand_path)\
.execute_insel(self._user_input, self._hp_model, self._data_type)
elif source == 'water':
WaterToWaterHPExport(self._base_path, self._city, self._output_path, self._sim_type, self._demand_path)\
return WaterToWaterHPExport(self._base_path, self._city, self._output_path, self._sim_type, self._demand_path)\
.execute_insel(self._user_input, self._hp_model)
def export(self, source='air'):

View File

@ -57,11 +57,11 @@ class TestEnergySystemsFactory(TestCase):
self.assertEqual(df.iloc[0, 1], 1867715.88)
def test_air_source_parallel_heat_pump_export(self):
EnergySystemsExportFactory(city=self._city, user_input=user_input, hp_model='018',
output_path=self._output_path, sim_type=1).export()
df = pd.read_csv(self._output_path)
self.assertEqual(df.shape, (13, 3))
self.assertEqual(df.iloc[0, 1], 22155602.0)
output = EnergySystemsExportFactory(city=self._city, user_input=user_input, hp_model='018',
output_path=None, sim_type=1).export()
self.assertEqual(output["hourly_electricity_demand"][0], 38748.5625)
self.assertIsNotNone(output["daily_fossil_consumption"])
self.assertEqual(len(output["hourly_electricity_demand"]), 8760)
def tearDown(self) -> None:
try: