from edisgo.grid.network import Network, Scenario, TimeSeries, Results
from edisgo.flex_opt import reinforce_grid
import os
import pickle
import pandas as pd
from ast import literal_eval
import numpy as np

timeseries = TimeSeries()
scenario = Scenario(timeseries=timeseries)

network = Network.import_from_ding0(
    os.path.join('data', 'ding0_grids__3545.pkl'),
    id='Test grid',
    scenario=scenario
)
# pickle.dump(network, open('test_network.pkl', 'wb'))
# network = pickle.load(open('test_network.pkl', 'rb'))

# export to pypsa
# network.analyze(mode='mv')

# for now create results object
# ToDo: Werte in DataFrame als List oder Array?
results = Results()
results.pfa_edges = pd.read_csv('Exemplary_PyPSA_line_results.csv',
                                index_col=0,
                                converters={'p0': literal_eval,
                                            'q0': literal_eval,
                                            'p1': literal_eval,
                                            'q1': literal_eval})
results.pfa_edges['p0'] = results.pfa_edges['p0'].apply(lambda x: np.array(x))
results.pfa_edges['q0'] = results.pfa_edges['q0'].apply(lambda x: np.array(x))
results.pfa_edges['p1'] = results.pfa_edges['p1'].apply(lambda x: np.array(x))
results.pfa_edges['q1'] = results.pfa_edges['q1'].apply(lambda x: np.array(x))
results.pfa_nodes = pd.read_csv('Exemplary_PyPSA_bus_results.csv', index_col=0,
                                converters={'v_mag_pu': literal_eval})
results.pfa_nodes['v_mag_pu'] = results.pfa_nodes['v_mag_pu'].apply(
    lambda x: np.array(x))

# # MV generators
# gens = network.mv_grid.graph.nodes_by_attribute('generator')
# print('Generators in MV grid incl. aggregated generators from MV and LV')
# print('Type\tSubtype\tCapacity in kW')
# for gen in gens:
#     print("{type}\t{sub}\t{capacity}".format(
#         type=gen.type, sub=gen.subtype, capacity=gen.nominal_capacity))
# 
# # Load located in aggregated LAs
# print('\n\nAggregated load in LA adds up to\n')
# if network.mv_grid.graph.nodes_by_attribute('load'):
#     [print('\t{0}: {1} MWh'.format(
#         _,
#         network.mv_grid.graph.nodes_by_attribute('load')[0].consumption[_] / 1e3))
#         for _ in ['retail', 'industrial', 'agricultural', 'residential']]
# else:
#     print("O MWh")

reinforce_grid.reinforce_grid(network, results)

# liste aller lv grids
# [_ for _ in network.mv_grid.lv_grids]

# nx.draw_spectral(list(network.mv_grid.lv_grids)[0].graph)

# ToDo: wie halten wir fest, welche Betriebsmittel erneuert wurden, um hinterher Kosten berechnen zu können?
# ToDo: Parameter bei Komponenten einführen mit dem man feststellen kann, ob die Komponente bereits in einer ersten Maßnahme verstärkt oder ausgebaut wurde
# ToDo: config mit Standardbetriebsmitteln?
# ToDo: Abbruchkriterium einführen - Anzahl paralleler lines


@@ -642,16 +642,18 @@
  Array?%0A
+# 
 results 
@@ -664,16 +664,18 @@
 sults()%0A
+# 
 results.
@@ -734,16 +734,18 @@
 s.csv',%0A
+# 
         
@@ -773,32 +773,34 @@
    index_col=0,%0A
+# 
                 
@@ -839,32 +839,34 @@
 : literal_eval,%0A
+# 
                 
@@ -905,32 +905,34 @@
 : literal_eval,%0A
+# 
                 
@@ -979,16 +979,18 @@
 l_eval,%0A
+# 
         
@@ -1038,32 +1038,34 @@
  literal_eval%7D)%0A
+# 
 results.pfa_edge
@@ -1119,32 +1119,34 @@
 x: np.array(x))%0A
+# 
 results.pfa_edge
@@ -1200,32 +1200,34 @@
 x: np.array(x))%0A
+# 
 results.pfa_edge
@@ -1281,32 +1281,34 @@
 x: np.array(x))%0A
+# 
 results.pfa_edge
@@ -1370,16 +1370,18 @@
 ray(x))%0A
+# 
 results.
@@ -1452,16 +1452,18 @@
 _col=0,%0A
+# 
         
@@ -1525,16 +1525,18 @@
 _eval%7D)%0A
+# 
 results.
@@ -1596,16 +1596,18 @@
 .apply(%0A
+# 
     lamb
@@ -2350,16 +2350,18 @@
  MWh%22)%0A%0A
+# 
 reinforc

Comment reinforce stuff