|
| 1 | +import tempfile |
| 2 | +from pathlib import Path |
| 3 | + |
1 | 4 | import numpy as np |
| 5 | +import pandas as pd |
2 | 6 | from django.core.exceptions import PermissionDenied |
3 | 7 | from django.template.loader import get_template |
4 | 8 | from django.db.models import Count, Value, F, Q, Case, When |
5 | 9 | from django.db.models.functions import Concat, Replace |
6 | 10 | from django.http.response import Http404, HttpResponse |
| 11 | +from oemof.datapackage import datapackage |
| 12 | +from oemof.eesyplan.datapackage.energy_system import create_energy_system_from_dp |
| 13 | + |
7 | 14 | from dashboard.helpers import * |
8 | 15 | from dashboard.models import ( |
9 | 16 | AssetsResults, |
|
64 | 71 | import datetime |
65 | 72 | import logging |
66 | 73 | import traceback |
67 | | -from projects.helpers import parameters_helper |
| 74 | +from projects.helpers import parameters_helper, add_timeseries_to_database_datapackage |
| 75 | + |
| 76 | +import oemof.eesyplan.postprocessing.graphs as eesyplan_graphs |
68 | 77 |
|
69 | 78 | logger = logging.getLogger(__name__) |
70 | 79 |
|
@@ -1223,21 +1232,30 @@ def scenario_visualize_sankey(request, scen_id, ts=None): |
1223 | 1232 | is False |
1224 | 1233 | ): |
1225 | 1234 | raise PermissionDenied |
1226 | | - if ts is not None: |
1227 | | - ts = int(ts) |
1228 | | - results_json = report_item_render_to_json( |
1229 | | - report_item_id="sankey", |
1230 | | - data=REPORT_GRAPHS[GRAPH_SANKEY]( |
1231 | | - simulation=scenario.simulation, |
1232 | | - energy_vector=scenario.energy_vectors, |
1233 | | - timestep=ts, |
1234 | | - ), |
1235 | | - title="Sankey", |
1236 | | - report_item_type=GRAPH_SANKEY, |
1237 | | - ) |
1238 | 1235 |
|
| 1236 | + # TODO this sankey implementation does not allow for single timesteps |
| 1237 | + # if ts is not None: |
| 1238 | + # ts = int(ts) |
| 1239 | + rebuilt_dp = add_timeseries_to_database_datapackage(scenario) |
| 1240 | + |
| 1241 | + # TODO these flows do not generate the correct sankey result yet |
| 1242 | + with tempfile.TemporaryDirectory(prefix="dp_") as td: |
| 1243 | + temp_path = Path(td) |
| 1244 | + dp_path = datapackage.rebuild_dp_from_json(rebuilt_dp, temp_path) |
| 1245 | + es = create_energy_system_from_dp(dp_path) |
| 1246 | + |
| 1247 | + qs = FancyResults.objects.filter(simulation=scenario.simulation) |
| 1248 | + |
| 1249 | + if qs.exists(): |
| 1250 | + flows = { |
| 1251 | + (asset, bus): json.loads(flow_data) |
| 1252 | + for asset, bus, flow_data in qs.values_list("asset", "bus", "flow_data") |
| 1253 | + } |
| 1254 | + flows_df = pd.DataFrame(flows) |
| 1255 | + flows_df.index = scenario.get_timestamps()[: len(flows_df)] |
| 1256 | + fig, links_df = eesyplan_graphs.sankey(flows=flows_df, es=es) |
1239 | 1257 | return JsonResponse( |
1240 | | - results_json, status=200, content_type="application/json", safe=False |
| 1258 | + fig.to_dict(), status=200, content_type="application/json", safe=False |
1241 | 1259 | ) |
1242 | 1260 |
|
1243 | 1261 |
|
|
0 commit comments