partial implementation meb workflow in the api

This commit is contained in:
Guille Gutierrez 2024-04-29 07:09:11 +02:00
parent c414de2635
commit 31ebe48f68

View File

@ -1,11 +1,14 @@
import json import json
import os import os
import shutil
import subprocess import subprocess
from pathlib import Path from pathlib import Path
from flask_restful import Resource
import hub.helpers.constants as cte
from flask import Response, request 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.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.construction_factory import ConstructionFactory
from hub.imports.geometry_factory import GeometryFactory from hub.imports.geometry_factory import GeometryFactory
from hub.imports.results_factory import ResultFactory 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') year_of_construction_field = request.args.get('year_of_construction_field')
function_field = request.args.get('function_field') function_field = request.args.get('function_field')
function_dictionary = Config.function_dictionary(request.args.get('function_dictionary_name')) function_dictionary = Config.function_dictionary(request.args.get('function_dictionary_name'))
return GeometryFactory('geojson', return GeometryFactory('geojson',
path=file_path, path=file_path,
height_field=height_field, height_field=height_field,
@ -42,6 +44,10 @@ class InselMonthlyEnergyBalance(Resource, Config):
function_field=function_field, function_field=function_field,
function_to_hub=function_dictionary).city function_to_hub=function_dictionary).city
@staticmethod
def _citygml(file_path):
raise NotImplementedError
def post(self): def post(self):
""" """
API call for performing the monthly energy balance workflow API call for performing the monthly energy balance workflow
@ -53,20 +59,43 @@ class InselMonthlyEnergyBalance(Resource, Config):
if _session is None: if _session is None:
return Response(json.dumps({'error': 'unauthorized'}), status=403) return Response(json.dumps({'error': 'unauthorized'}), status=403)
else: else:
tmp_path = (self._tmp_path / token).resolve()
os.mkdir(tmp_path)
geometry_file = request.files['geometry_file'] geometry_file = request.files['geometry_file']
if not self._allowed_extensions(geometry_file): if not self._allowed_extensions(geometry_file):
return Response(json.dumps({'error': 'Unsupported media type'}), status=415, headers=token) return Response(json.dumps({'error': 'Unsupported media type'}), status=415, headers=token)
filename = secure_filename(geometry_file.filename) 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) geometry_file.save(file_path)
if self._file_extension == 'geojson': if self._file_extension == 'geojson':
self._city = InselMonthlyEnergyBalance._geojson(file_path) self._city = InselMonthlyEnergyBalance._geojson(file_path)
else:
self._city = InselMonthlyEnergyBalance._citygml(file_path)
construction_handler = request.args.get('construction_handler') construction_handler = request.args.get('construction_handler')
usage_handler = request.args.get('usage_handler') usage_handler = request.args.get('usage_handler')
WeatherFactory('epw', self._city).enrich() WeatherFactory('epw', self._city).enrich()
ConstructionFactory(construction_handler, self._city).enrich() ConstructionFactory(construction_handler, self._city).enrich()
UsageFactory(usage_handler, self._city).enrich() UsageFactory(usage_handler, self._city).enrich()
ExportsFactory('sra', self._city, self._tmp_path).export() ExportsFactory('sra', self._city, tmp_path).export()
sra_file = str((self._tmp_path / f'{self._city.name}_sra.xml').resolve()) sra_file = str((tmp_path / f'{self._city.name}_sra.xml').resolve())
subprocess.run([self.sra, sra_file], stdout=subprocess.DEVNULL) 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)