# Path: rivus/main/rivus.py
# COLORS = { # (R,G,B) tuples with range (0-255)
#     # defaults
#     'base': (192, 192, 192),
#     'building': (192, 192, 192),
#     'decoration': (128, 128, 128),
#     # commodities
#     'Heat': (230, 112, 36),
#     'Cool': (0, 0, 255),
#     'Elec': (255, 170, 0),
#     'Demand': (0, 255, 0),
#     'Gas': (128, 64, 0),
#     # buildings
#     'industrial': (240, 198, 116),
#     'residential': (181, 189, 104),
#     'commercial': (129, 162, 190),
#     'basin': (110, 75, 56),
#     'chapel': (177, 121, 91),
#     'church': (177, 121, 91),
#     'farm': (202, 178, 214),
#     'farm_auxiliary': (106, 61, 154),
#     'garage': (253, 191, 111),
#     'greenhouse': (255, 127, 0),
#     'hospital': (129, 221, 190),
#     'hotel': (227, 26, 28),
#     'house': (181, 189, 104),
#     'office': (129, 162, 190),
#     'public': (129, 162, 190),
#     'restaurant': (227, 26, 28),
#     'retail': (129, 162, 190),
#     'school': (29, 103, 214),
#     'warehouse': (98, 134, 6),
# }
# def read_excel(filepath):
# def create_model(data, vertex, edge, peak_multiplier=None,
#                  hub_only_in_edge=True):
#     def multiply_by_area_demand(series, column):
# def peak_satisfaction_rule(m, i, j, co, t):
# def edge_equation_rule(m, i, j, co, t):
# def arc_flow_by_capacity_rule(m, i, j, co, t):
# def arc_flow_unidirectionality_rule(m, i, j, co, t):
# def arc_unidirectionality_rule(m, i, j, co, t):
# def edge_capacity_rule(m, i, j, co):
# def hub_supply_rule(m, i, j, co, t):
# def hub_output_by_capacity_rule(m, i, j, h, t):
# def hub_capacity_rule(m, i, j, h):
# def vertex_equation_rule(m, v, co, t):
# def source_vertices_rule(m, v, co, t):
# def commodity_maximum_rule(m, co):
# def process_throughput_by_capacity_rule(m, v, p, t):
# def process_capacity_min_rule(m, v, p):
# def process_capacity_max_rule(m, v, p):
# def process_input_rule(m, v, p, co, t):
# def process_output_rule(m, v, p, co, t):
# def def_costs_rule(m, cost_type):
# def obj_rule(m):
# def hub_balance(m, i, j, co, t):
# def flow_balance(m, v, co, t):
# def process_balance(m, v, co, t):
# def find_matching_edge(m, i, j):
# def line_length(line):
# def pairs(lst):
# def get_entity(instance, name):
# def get_entities(instance, names):
# def list_entities(instance, entity_type):
#     def filter_by_type(entity, entity_type):
# def get_onset_names(entity):
# def get_constants(prob):
# def get_timeseries(prob):
# def plot(prob, commodity, plot_demand=False, mapscale=False, tick_labels=True,
#          annotations=True, buildings=None, shapefiles=None, decoration=True,
#          boundary=False):
#                         def _calc_xytext_offset(line):
# def result_figures(prob, file_basename, buildings=None, shapefiles=None):
# def report(prob, filename):
# def save_log(result, filename):
# def save(prob, filepath):
# def load(filepath):


import glob
import os
import sys
from rivus.main import rivus

def replot(directory):
    """Recreate result figures for all pickled rivus results in directory

    Args:
        directory: a directory with 1 or multiple pickled rivus instances

    Returns:
        Nothing
    """
    glob_pattern = os.path.join(directory, '*.pgz')
    pickle_filenames = glob.glob(glob_pattern)

    data_dir = os.path.join('data', os.path.basename(directory).split('-')[0])
    # if directory = 'result/moosh' try to find a suitable building shapefile
    # in 'data/moosh'
    buildings = None
    building_filename = os.path.join(data_dir, 'building')
    if os.path.exists(building_filename+'.shp'):
        buildings = (building_filename, False)  # if True, color buildings

    # if data/.../to_edge exists, paint it
    shapefiles = None
    to_edge_filename = os.path.join(data_dir, 'to_edge')
    if os.path.exists(to_edge_filename+'.shp'):
        shapefiles = [{'name': 'to_edge',

# Path: rivus/main/rivus.py
# COLORS = { # (R,G,B) tuples with range (0-255)
#     # defaults
#     'base': (192, 192, 192),
#     'building': (192, 192, 192),
#     'decoration': (128, 128, 128),
#     # commodities
#     'Heat': (230, 112, 36),
#     'Cool': (0, 0, 255),
#     'Elec': (255, 170, 0),
#     'Demand': (0, 255, 0),
#     'Gas': (128, 64, 0),
#     # buildings
#     'industrial': (240, 198, 116),
#     'residential': (181, 189, 104),
#     'commercial': (129, 162, 190),
#     'basin': (110, 75, 56),
#     'chapel': (177, 121, 91),
#     'church': (177, 121, 91),
#     'farm': (202, 178, 214),
#     'farm_auxiliary': (106, 61, 154),
#     'garage': (253, 191, 111),
#     'greenhouse': (255, 127, 0),
#     'hospital': (129, 221, 190),
#     'hotel': (227, 26, 28),
#     'house': (181, 189, 104),
#     'office': (129, 162, 190),
#     'public': (129, 162, 190),
#     'restaurant': (227, 26, 28),
#     'retail': (129, 162, 190),
#     'school': (29, 103, 214),
#     'warehouse': (98, 134, 6),
# }
# def read_excel(filepath):
# def create_model(data, vertex, edge, peak_multiplier=None,
#                  hub_only_in_edge=True):
#     def multiply_by_area_demand(series, column):
# def peak_satisfaction_rule(m, i, j, co, t):
# def edge_equation_rule(m, i, j, co, t):
# def arc_flow_by_capacity_rule(m, i, j, co, t):
# def arc_flow_unidirectionality_rule(m, i, j, co, t):
# def arc_unidirectionality_rule(m, i, j, co, t):
# def edge_capacity_rule(m, i, j, co):
# def hub_supply_rule(m, i, j, co, t):
# def hub_output_by_capacity_rule(m, i, j, h, t):
# def hub_capacity_rule(m, i, j, h):
# def vertex_equation_rule(m, v, co, t):
# def source_vertices_rule(m, v, co, t):
# def commodity_maximum_rule(m, co):
# def process_throughput_by_capacity_rule(m, v, p, t):
# def process_capacity_min_rule(m, v, p):
# def process_capacity_max_rule(m, v, p):
# def process_input_rule(m, v, p, co, t):
# def process_output_rule(m, v, p, co, t):
# def def_costs_rule(m, cost_type):
# def obj_rule(m):
# def hub_balance(m, i, j, co, t):
# def flow_balance(m, v, co, t):
# def process_balance(m, v, co, t):
# def find_matching_edge(m, i, j):
# def line_length(line):
# def pairs(lst):
# def get_entity(instance, name):
# def get_entities(instance, names):
# def list_entities(instance, entity_type):
#     def filter_by_type(entity, entity_type):
# def get_onset_names(entity):
# def get_constants(prob):
# def get_timeseries(prob):
# def plot(prob, commodity, plot_demand=False, mapscale=False, tick_labels=True,
#          annotations=True, buildings=None, shapefiles=None, decoration=True,
#          boundary=False):
#                         def _calc_xytext_offset(line):
# def result_figures(prob, file_basename, buildings=None, shapefiles=None):
# def report(prob, filename):
# def save_log(result, filename):
# def save(prob, filepath):
# def load(filepath):


# Path: replot.py
import glob
import os
import sys
from rivus.main import rivus


def replot(directory):
    """Recreate result figures for all pickled rivus results in directory

    Args:
        directory: a directory with 1 or multiple pickled rivus instances

    Returns:
        Nothing
    """
    glob_pattern = os.path.join(directory, '*.pgz')
    pickle_filenames = glob.glob(glob_pattern)

    data_dir = os.path.join('data', os.path.basename(directory).split('-')[0])
    # if directory = 'result/moosh' try to find a suitable building shapefile
    # in 'data/moosh'
    buildings = None
    building_filename = os.path.join(data_dir, 'building')
    if os.path.exists(building_filename+'.shp'):
        buildings = (building_filename, False)  # if True, color buildings

    # if data/.../to_edge exists, paint it
    shapefiles = None
    to_edge_filename = os.path.join(data_dir, 'to_edge')
    if os.path.exists(to_edge_filename+'.shp'):
        shapefiles = [{'name': 'to_edge',

                       'color': rivus.to_rgb(192, 192, 192),