From 31ebe48f68bd7091d36c099c458b31f558ae6c2e Mon Sep 17 00:00:00 2001 From: Guille Date: Mon, 29 Apr 2024 07:09:11 +0200 Subject: [PATCH] partial implementation meb workflow in the api --- .../workflow/insel_montly_energy_balance.py | 43 ++++++++++++++++--- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/hub_api/workflow/insel_montly_energy_balance.py b/hub_api/workflow/insel_montly_energy_balance.py index 5dd48dd..3952f3d 100644 --- a/hub_api/workflow/insel_montly_energy_balance.py +++ b/hub_api/workflow/insel_montly_energy_balance.py @@ -1,11 +1,14 @@ import json import os +import shutil import subprocess from pathlib import Path -from flask_restful import Resource + +import hub.helpers.constants as cte from flask import Response, request +from flask_restful import Resource +from hub.exports.energy_building_exports_factory import EnergyBuildingsExportsFactory from hub.exports.exports_factory import ExportsFactory -from hub.helpers.data.montreal_function_to_hub_function import MontrealFunctionToHubFunction from hub.imports.construction_factory import ConstructionFactory from hub.imports.geometry_factory import GeometryFactory from hub.imports.results_factory import ResultFactory @@ -34,7 +37,6 @@ class InselMonthlyEnergyBalance(Resource, Config): year_of_construction_field = request.args.get('year_of_construction_field') function_field = request.args.get('function_field') function_dictionary = Config.function_dictionary(request.args.get('function_dictionary_name')) - return GeometryFactory('geojson', path=file_path, height_field=height_field, @@ -42,6 +44,10 @@ class InselMonthlyEnergyBalance(Resource, Config): function_field=function_field, function_to_hub=function_dictionary).city + @staticmethod + def _citygml(file_path): + raise NotImplementedError + def post(self): """ API call for performing the monthly energy balance workflow @@ -53,20 +59,43 @@ class InselMonthlyEnergyBalance(Resource, Config): if _session is None: return Response(json.dumps({'error': 'unauthorized'}), status=403) else: + tmp_path = (self._tmp_path / token).resolve() + os.mkdir(tmp_path) geometry_file = request.files['geometry_file'] if not self._allowed_extensions(geometry_file): return Response(json.dumps({'error': 'Unsupported media type'}), status=415, headers=token) filename = secure_filename(geometry_file.filename) - file_path = os.path.join(self._tmp_path, filename) + file_path = os.path.join(tmp_path, filename) geometry_file.save(file_path) if self._file_extension == 'geojson': self._city = InselMonthlyEnergyBalance._geojson(file_path) + else: + self._city = InselMonthlyEnergyBalance._citygml(file_path) construction_handler = request.args.get('construction_handler') usage_handler = request.args.get('usage_handler') WeatherFactory('epw', self._city).enrich() ConstructionFactory(construction_handler, self._city).enrich() UsageFactory(usage_handler, self._city).enrich() - ExportsFactory('sra', self._city, self._tmp_path).export() - sra_file = str((self._tmp_path / f'{self._city.name}_sra.xml').resolve()) + ExportsFactory('sra', self._city, tmp_path).export() + sra_file = str((tmp_path / f'{self._city.name}_sra.xml').resolve()) subprocess.run([self.sra, sra_file], stdout=subprocess.DEVNULL) - ResultFactory('sra', self._city, self._tmp_path).enrich() + ResultFactory('sra', self._city, tmp_path).enrich() + EnergyBuildingsExportsFactory('insel_monthly_energy_balance', self._city, tmp_path).export() + for building in self._city.buildings: + insel_path = (tmp_path / f'{building.name}.insel') + subprocess.run([self.insel, str(insel_path)]) + ResultFactory('insel_monthly_energy_balance', self._city, tmp_path).enrich() + results = {} + for building in self._city.buildings: + results[building] = { + 'monthly_heating_demand': building.heating_demand[cte.MONTH], + 'yearly_heating_demand': building.heating_demand[cte.YEAR], + 'monthly_cooling_demand': building.cooling_demand[cte.MONTH], + 'yearly_cooling_demand': building.cooling_demand[cte.YEAR], + 'monthly_lighting_peak_load': building.lighting_peak_load[cte.MONTH], + 'yearly_lighting_peak_load': building.lighting_peak_load[cte.YEAR], + 'monthly_appliances_peak_load': building.appliances_peak_load[cte.MONTH], + 'yearly_appliances_peak_load': building.appliances_peak_load[cte.YEAR] + } + shutil.rmtree(tmp_path) + return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)