%matplotlib inline

from IPython.display import display, HTML

import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (14.0, 8.0)

import numpy as np
from datetime import datetime

from dtocean_core import start_logging
from dtocean_core.core import Core
from dtocean_core.menu import DataMenu, ModuleMenu, ProjectMenu, ThemeMenu
from dtocean_core.pipeline import Tree, _get_connector
from dtocean_core.extensions import StrategyManager

# Bring up the logger
start_logging()

def html_list(x):
    message = "<ul>"
    for name in x:
        message += "<li>{}</li>".format(name)
    message += "</ul>"
    return message
def html_dict(x):
    message = "<ul>"
    for name, status in x.iteritems():
        message += "<li>{}: <b>{}</b></li>".format(name, status)
    message += "</ul>"
    return message
def html_variable(core, project, variable):
    value = variable.get_value(core, project)
    metadata = variable.get_metadata(core)
    name = metadata.title
    units = metadata.units
    message = "<b>{}:</b> {}".format(name, value)
    if units:
        message += " ({})".format(units[0])
    return message

"""
Explanation: Floating Wave Scenario Analysis
End of explanation
"""


new_core = Core()
project_menu = ProjectMenu()
module_menu = ModuleMenu()
theme_menu = ThemeMenu()
data_menu = DataMenu()
pipe_tree = Tree()

"""
Explanation: Create the core, menus and pipeline tree
The core object carrys all the system information and is operated on by the other classes
End of explanation
"""


project_title = "DTOcean"  
new_project = project_menu.new_project(new_core, project_title)

"""
Explanation: Create a new project and tree
End of explanation
"""


options_branch = pipe_tree.get_branch(new_core, new_project, "System Type Selection")
variable_id = "device.system_type"
my_var = options_branch.get_input_variable(new_core, new_project, variable_id)
my_var.set_raw_interface(new_core, "Wave Floating")
my_var.read(new_core, new_project)

"""
Explanation: Set the device type
End of explanation
"""


project_menu.initiate_pipeline(new_core, new_project)

"""
Explanation: Initiate the pipeline
This step will be important when the database is incorporated into the system as it will effect the operation of the pipeline.
End of explanation
"""


names = module_menu.get_available(new_core, new_project)
message = html_list(names)
HTML(message)

"""
Explanation: Discover available modules
End of explanation
"""


module_menu.activate(new_core, new_project, 'Hydrodynamics')
module_menu.activate(new_core, new_project, 'Electrical Sub-Systems')
module_menu.activate(new_core, new_project, 'Mooring and Foundations')

"""
Explanation: Activate some modules
Note that the order of activation is important and that we can't deactivate yet!
End of explanation
"""


names = theme_menu.get_available(new_core, new_project)
message = html_list(names)
HTML(message)

theme_menu.activate(new_core, new_project, "Economics")

# Here we are expecting Hydrodynamics
assert _get_connector(new_project, "modules").get_current_interface_name(new_core, new_project) == "Hydrodynamics"

from aneris.utilities.analysis import get_variable_network, count_atomic_variables

req_inputs, opt_inputs, outputs, req_inter, opt_inter = get_variable_network(new_core.control,
                                                                             new_project.get_pool(),
                                                                             new_project.get_simulation(),
                                                                             "modules")

req_inputs[req_inputs.Type=="Shared"].reset_index()

shared_req_inputs = req_inputs[req_inputs.Type=="Shared"]
len(shared_req_inputs["Identifier"].unique())

count_atomic_variables(shared_req_inputs["Identifier"].unique(),
                       new_core.data_catalog,
                       "labels",
                       ["TableData",
                        "TableDataColumn",
                        "IndexTable",
                        "LineTable",
                        "LineTableColumn",
                        "TimeTable",
                        "TimeTableColumn"])

opt_inputs[opt_inputs.Type=="Shared"].reset_index()

shared_opt_inputs = opt_inputs[opt_inputs.Type=="Shared"]
len(shared_opt_inputs["Identifier"].unique())

count_atomic_variables(shared_opt_inputs["Identifier"].unique(),
                       new_core.data_catalog,
                       "labels",
                       ["TableData",
                        "TableDataColumn",
                        "IndexTable",
                        "LineTable",
                        "LineTableColumn",
                        "TimeTable",
                        "TimeTableColumn"])

req_inter

len(req_inter["Identifier"].unique())

count_atomic_variables(req_inter["Identifier"].unique(),
                       new_core.data_catalog,
                       "labels",
                       ["TableData",
                        "TableDataColumn",
                        "IndexTable",
                        "LineTable",
                        "LineTableColumn",
                        "TimeTable",
                        "TimeTableColumn"])

opt_inter

len(opt_inter["Identifier"].unique())

count_atomic_variables(opt_inter["Identifier"].unique(),
                       new_core.data_catalog,
                       "labels",
                       ["TableData",
                        "TableDataColumn",
                        "IndexTable",
                        "LineTable",
                        "LineTableColumn",
                        "TimeTable",
                        "TimeTableColumn"])

hyrdo_req_inputs = req_inputs.loc[req_inputs['Interface'] == 'Hydrodynamics']
len(hyrdo_req_inputs["Identifier"].unique())

count_atomic_variables(hyrdo_req_inputs["Identifier"].unique(),
                       new_core.data_catalog,
                       "labels",
                       ["TableData",
                        "TableDataColumn",
                        "IndexTable",
                        "LineTable",
                        "LineTableColumn",
                        "TimeTable",
                        "TimeTableColumn"])

hyrdo_opt_inputs = opt_inputs.loc[opt_inputs['Interface'] == 'Hydrodynamics']
len(hyrdo_opt_inputs["Identifier"].unique())

count_atomic_variables(hyrdo_opt_inputs["Identifier"].unique(),
                       new_core.data_catalog,
                       "labels",
                       ["TableData",
                        "TableDataColumn",
                        "IndexTable",
                        "LineTable",
                        "LineTableColumn",
                        "TimeTable",
                        "TimeTableColumn"])

electro_req_inputs = req_inputs.loc[req_inputs['Interface'] == 'Electrical Sub-Systems']
len(electro_req_inputs["Identifier"].unique())

count_atomic_variables(electro_req_inputs["Identifier"].unique(),
                       new_core.data_catalog,
                       "labels",
                       ["TableData",
                        "TableDataColumn",
                        "IndexTable",
                        "LineTable",
                        "LineTableColumn",
                        "TimeTable",
                        "TimeTableColumn"])

electro_opt_inputs = opt_inputs.loc[opt_inputs['Interface'] == 'Electrical Sub-Systems']
len(electro_opt_inputs["Identifier"].unique())

count_atomic_variables(electro_opt_inputs["Identifier"].unique(),
                       new_core.data_catalog,
                       "labels",
                       ["TableData",
                        "TableDataColumn",
                        "IndexTable",
                        "LineTable",
                        "LineTableColumn",
                        "TimeTable",
                        "TimeTableColumn"])

moorings_req_inputs = req_inputs.loc[req_inputs['Interface'] == 'Mooring and Foundations']
len(moorings_req_inputs["Identifier"].unique())

count_atomic_variables(moorings_req_inputs["Identifier"].unique(),
                       new_core.data_catalog,
                       "labels",
                       ["TableData",
                        "TableDataColumn",
                        "IndexTable",
                        "LineTable",
                        "LineTableColumn",
                        "TimeTable",
                        "TimeTableColumn"])

moorings_opt_inputs = opt_inputs.loc[opt_inputs['Interface'] == 'Mooring and Foundations']
len(moorings_opt_inputs["Identifier"].unique())

count_atomic_variables(moorings_opt_inputs["Identifier"].unique(),
                       new_core.data_catalog,
                       "labels",
                       ["TableData",
                        "TableDataColumn",
                        "IndexTable",
                        "LineTable",
                        "LineTableColumn",
                        "TimeTable",
                        "TimeTableColumn"])

total_req_inputs = req_inputs.loc[req_inputs['Interface'] != 'Shared']
len(total_req_inputs["Identifier"].unique())

count_atomic_variables(total_req_inputs["Identifier"].unique(),
                       new_core.data_catalog,
                       "labels",
                       ["TableData",
                        "TableDataColumn",
                        "IndexTable",
                        "LineTable",
                        "LineTableColumn",
                        "TimeTable",
                        "TimeTableColumn"])

"""
Explanation: Activate the Economics and Reliability themes
End of explanation
"""


