import pandas as pd
import numpy as np
from bokeh.io import output_file, show, vform
from bokeh.plotting import *
from bokeh.charts import *
from bokeh.models import *
from bokeh.models.widgets import CheckboxButtonGroup

###### -- Difined color palette-- ########
Fblue = "#3fa0d4"
Fdark_green = "#47994b"
Flight_green = "#88ba14"
Fgrey_blue = "#437c92"
Fturquoise ="#0aaba4"
Fsand ="#dabe86"
Fbrown ="#59392a"
fred ="#C90119"
Fsand20 ="#f7f2e7"
###### -- end -- ########


###### -- Import and defined xlsx data -- ########
###### -- Import -- ########
with pd.ExcelFile('*_RawData.xlsx') as xlsx:
    df1 = pd.read_excel(xlsx, 'Day')
    df2 = pd.read_excel(xlsx, 'Night')
###### -- end -- ########
###### -- build a dataset where multiple columns measure the same thing -- ########
###### -- Day -- ########
data1 = dict(Pressure=df1["Pressure"],
            Depth=df1["Depth"],
            Temperature=df1["Temperature"],
            Salinity=df1["Salinity"],
            Conductivity=df1["Conductivity"],
            Density=df1["Density"],
            DescentRate=df1["Descent Rate"],
            SoundVelocity=df1["Sound Velocity"],
            Oxygenraw=df1["Oxygen raw"],
            Oxygenmgl=df1["Oxygen [mg/l]"],
            Oxygenpercent=df1["Oxygen [%]"],
            Flag=df1["Flag"]
            )
###### -- end -- ########
###### -- Night -- ########
data2 = dict(Pressure=df2["Pressure"],
            Depth=df2["Depth"],
            Temperature=df2["Temperature"],
            Salinity=df2["Salinity"],
            Conductivity=df2["Conductivity"],
            Density=df2["Density"],
            DescentRate=df2["Descent Rate"],
            SoundVelocity=df2["Sound Velocity"],
            Oxygenraw=df2["Oxygen raw"],
            Oxygenmgl=df2["Oxygen [mg/l]"],
            Oxygenpercent=df2["Oxygen [%]"],
            Flag=df2["Flag"]
            )
###### -- end -- ########
###### -- Need correction auto -- ########
#source = ColumnDataSource(data1)

TOOLS = 'crosshair,pan,wheel_zoom,box_zoom,reset,hover,save'

plot_options = dict(background_fill_color=Fsand20, width=400, height=400, title=None, tools=TOOLS)

###### -- end -- ########

###### -- Set up checkboxes button to show/hide lines -- ########
def checkbox_button_group_handler(active):
	if checkbox_button_group.value == 'Downcast':
		source = ColumnDataSource(df1.loc[df['Flag'] == 'Down'])		
	if checkbox_button_group.value == 'Upcast':
		source = ColumnDataSource(df1.loc[df['Flag'] == 'Up'])		
	else:
		source = ColumnDataSource(data1)

checkbox_button_group = CheckboxButtonGroup(
        labels=["Downcast", "Upcast"], active=[1, 0])

checkbox_button_group.on_click(checkbox_button_group_handler)

b = vform(checkbox_button_group)
###### -- end -- ########

###### -- build figures -- ######## 
###### -- First one -- ########
line0 = figure(**plot_options)
line0.line('Temperature', 'Depth', source=source, color=Fblue)
line0.xaxis.axis_label = "Temperature [°C]"
line0.yaxis.axis_label = "Depth [m]"
line0.y_range.bounds = 'auto'
###### -- end -- ########
 
###### -- linked panning is expressed by sharing ranges between plots.  -- ########
line1 = figure(y_range=line0.y_range, **plot_options)
line1.line('Salinity', 'Depth', source=source, color=Flight_green)
line1.xaxis.axis_label = "Salinity [PSU]"
line1.yaxis.axis_label = "Depth [m]"

line2 = figure(y_range=line0.y_range, **plot_options)
line2.line('Conductivity', 'Depth', source=source, color=Fturquoise)
line2.xaxis.axis_label = "Conductivity [S/m]"
line2.yaxis.axis_label = "Depth [m]"

line3 = figure(y_range=line0.y_range, **plot_options)
line3.line('Density', 'Depth', source=source, color=Fbrown)
line3.xaxis.axis_label = "Density [kg/m^3]"
line3.yaxis.axis_label = "Depth [m]"
###### -- end -- ########

###### -- Grid plot option -- ########
p = gridplot([[line0, line1],
             [line2, line3]]
        )
###### -- end -- ########

###### -- Hover tool option -- ########
hover = p.select(dict(type=HoverTool))
hover.mode = 'mouse'
hover.tooltips = [
    ("Depth", "$y{1.1}"), # this will format as 1-decimal float
    ("Measure", "$x{1.1}") # this will format as 1-decimal float
]
###### -- end -- ########

###### -- Export to HTLM -- ########
output_file("Test.html", title="Test example")
###### -- end -- ########

###### -- Show plot -- ########
show(vform(b, p))
###### -- end -- ########
