transformers = [pp_coal, pp_lig, pp_gas, pp_oil, pp_chp]
renew_sources = [pv, wind_on]
sinks = [demand_th, demand_el]

components = transformers + renew_sources + sinks
entities = components + buses

simulation = es.Simulation(solver='glpk', timesteps=timesteps,
                           stream_solver_output=True,
                           objective_name='minimize_costs')
energysystem = es.EnergySystem(entities=entities, simulation=simulation)

om = OptimizationModel(energysystem=energysystem)

om.solve(solver='gurobi', debug=True, tee=True, duals=False)
pp.results_to_objects(om)

components = transformers + renew_sources


if __name__ == "__main__":
    def plot_dispatch(bus_to_plot):
        # plotting: later as multiple pdf with pie-charts and topology?
        import numpy as np
        import matplotlib as mpl
        import matplotlib.cm as cm

        plot_data = renew_sources+transformers

        # data preparation
        x = np.arange(len(timesteps))
commodities = [rgas]
storages = [storage]
sinks = [demand]

# groupt components
components = transformers + renewable_sources + storages + sinks + commodities

# create list of all entities
entities = components + buses

# TODO: other solver libraries should be passable
simulation = es.Simulation(solver="glpk", timesteps=timesteps, stream_solver_output=True)

energysystem = es.EnergySystem(entities=entities, simulation=simulation)

energysystem.optimize()

# write results back to objects
pp.results_to_objects(energysystem.optimization_model)


if __name__ == "__main__":
    import postprocessing as pp

    data = renewable_sources + transformers + storages

    pp.plot_dispatch("bel", timesteps, data, storage, demand)
    #    pp.plot_dispatchplt.show()

    pp.print_results("bel", data, demand, transformers, storage, energysystem)
    for pp in region.renew_pps:
        entities.append(pp)
        components.append(pp)
    for pp in region.sinks:
        entities.append(pp)
        components.append(pp)

timesteps = [t for t in range(876)]

om = OptimizationModel(entities=entities, timesteps=timesteps,
                       options={'invest': False, 'slack': {
                           'excess': True, 'shortage': True}})

om.solve(solver='gurobi', debug=True, tee=False, duals=True)

postprocessing.results_to_objects(om)

df = pd.DataFrame()
for c in components:
    for k in c.results["out"].keys():
        df[c.uid] = c.results["out"][k]

#for t in transmissions:
#    for k in t.results["out"].keys():
#        df[t.uid] = t.results["out"][k]
#for c in components:
#    for k in c.results["in"].keys():
#        df[c.uid] = c.results["in"][k]
#
#for c in components:
#    c.calc_emissions()