# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Window 1
#
# * [locations Discovery Service - Oceans 2.0 API - Ocean Networks Canada](https://wiki.oceannetworks.ca/display/O2A/locations+Discovery+Service)
# * [https://data.oceannetworks.ca/apiproxy/devices?method=get&deviceCategoryCode=BPR&dateFrom=2019-07-01T00:00:00.000Z&dateTo=2020-06-30T23:59:59.999Z](https://data.oceannetworks.ca/apiproxy/devices?method=get&deviceCategoryCode=BPR&dateFrom=2019-07-01T00:00:00.000Z&dateTo=2020-06-30T23:59:59.999Z)
# * [https://data.oceannetworks.ca/apiproxy/locations?method=get&deviceCategoryCode=BPR&dateFrom=2019-07-01T00:00:00.000Z&dateTo=2020-06-30T23:59:59.999Z](https://data.oceannetworks.ca/apiproxy/locations?method=get&deviceCategoryCode=BPR&dateFrom=2019-07-01T00:00:00.000Z&dateTo=2020-06-30T23:59:59.999Z)
# * [https://data.oceannetworks.ca/apiproxy/deployments?method=get&deviceCategoryCode=BPR&dateFrom=2020-01-01T00:00:00.000Z](https://data.oceannetworks.ca/apiproxy/deployments?method=get&deviceCategoryCode=BPR&dateFrom=2020-01-01T00:00:00.000Z)
# * [Data Search: Ocean Networks Canada - Oceans 2.0](https://data.oceannetworks.ca/DataSearch?location=KEMF&deviceCategory=BPR)
# * [Device Details: Ocean Networks Canada - Oceans 2.0](https://data.oceannetworks.ca/DeviceListing?DeviceId=10703)
# * [https://data.oceannetworks.ca/DeviceSensorService?deviceId=10703](https://data.oceannetworks.ca/DeviceSensorService?deviceId=10703)
# * [Data Preview: Ocean Networks Canada - Oceans 2.0](https://data.oceannetworks.ca/home?TREETYPE=1&LOCATION=29&DEVICECATEGORY=5&TIMECONFIG=2)
# * [Plotting Utility: Ocean Networks Canada - Oceans 2.0](https://data.oceannetworks.ca/PlottingUtility)
# * [Device Console: Ocean Networks Canada - Oceans 2.0](https://data.oceannetworks.ca/DC?TREETYPE=10&OBSERVATORY=8&STATION=102&DEVICE=10401&DEVICE=10513&DEVICE=12501&TAB=Device%20Control)
# * [Plotting Utility: Ocean Networks Canada - Oceans 2.0](https://data.oceannetworks.ca/PlottingUtility?TREETYPE=26&LOCATION=30&DEVICECATEGORY=3&SENSOR=9082&DATEFROM=18-May-2014%2020:50:15&DATETO=14-Feb-2020%2002:28:03)
# * [Plot (500×300)](https://data.oceannetworks.ca/Plot?sensorId=4176&plotwidth=500&plotheight=300)
# * [BeakerX](https://beakerx.com/)
# * [[OC-75] National Tsunami Warning Center support - DMAS Jira](https://jira.oceannetworks.ca/browse/OC-75)
#

# ### Heading
# * dfds
# * fafds
#

# +
# %matplotlib notebook
import matplotlib.pyplot as plt
import requests
import numpy as np
import pandas as pd

from get_ONC_token import get_ONC_token
token = get_ONC_token()


# +
def geo_dist(x1, x2):
    #print(x1)
    # approximate radius of earth in km
    R = 6373.0

    lat1 = np.radians(x1[0])
    lon1 = np.radians(x1[1])
    lat2 = np.radians(x2[0])
    lon2 = np.radians(x2[1])

    dlon = lon2 - lon1
    dlat = lat2 - lat1

    a = np.sin(dlat / 2)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon / 2)**2
    c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1 - a))

    return R * c



# +
x1 = np.array([[48.,-128.],[47,-128],[47,-128]])
x2 = np.array([[48.,-127.],[47,-127],[48,-128]])

geo_dist(x1, x2)

# -

# https://stackabuse.com/hierarchical-clustering-with-python-and-scikit-learn/
import scipy.cluster.hierarchy as shc
from scipy.spatial.distance import pdist

# +
url = 'https://data.oceannetworks.ca/api/deployments'
deployments = []

for deviceCategoryCode in ['BPR', 'CORK', 'TILTMTR', 'CTD']:
    params = {'method' : 'get',
              'token' : token,
             'deviceCategoryCode' : deviceCategoryCode,
             'dateFrom' : '2020-01-01T00:00:00.000Z'}

    r = requests.get(url, params = params)
    deps = r.json()
    for dep in deps:
        dep['deviceCategoryCode'] = deviceCategoryCode
        deployments.append(dep)



# +
url = 'https://data.oceannetworks.ca/api/locations'
locations = []

for deviceCategoryCode in ['BPR', 'CORK', 'TILTMTR', 'CTD']:
    params = {'method' : 'get',
              'token' : token,
             'deviceCategoryCode' : deviceCategoryCode,
             'dateFrom' : '2020-01-01T00:00:00.000Z'}

    r = requests.get(url, params = params)
    locs = r.json()
    for loc in locs:
        loc['deviceCategoryCode'] = deviceCategoryCode
        loc['bbox'] = None
        locations.append(loc)

# -

locations

# +
url = 'https://data.oceannetworks.ca/api/locations'
params = {'method' : 'getTree', 'token' : token}
r = requests.get(url, params = params)
tree = r.json()

def buildTree(root,nodes={},name_path=''):
    for child in root['children']:
        new_path = name_path + '/' + child['locationName']
        nodes[child['locationCode']]=(new_path, child['description'])
        if child['children']:
            buildTree(child,nodes,new_path)
    return nodes
        
        
locTree = buildTree(tree[0])
locPaths = pd.DataFrame.from_dict(locTree,orient='index',columns=['path','description'])
# -

df['path'] = df.join(locPaths['path'],on='locationCode')['path']

df['locationCode']

locs = pd.DataFrame(locations)
locs
#locs
#locations
#r.url

#df['locationName'] = locs.loc[df['locationCode'].values]['locationName'].values
#locs.loc[df['locationCode'].values]['locationName']
#len(df['locationCode'].values)
#len(locs)


#locs.set_index(['locationCode','deviceCategoryCode']).join(df.set_index(['locationCode','deviceCategoryCode']))
#
df


# +
df = pd.DataFrame(deployments)
df = df[df['lon']<-120.]
df =df[df['locationName'].str.contains('Patrol') == False]
df['locationName'] = df.set_index(['locationCode','deviceCategoryCode']).join(locs.set_index(['locationCode','deviceCategoryCode'])['locationName'])['locationName'].values
dep_coords = df[['lat','lon']].values


# -

y = pdist(dep_coords, metric = geo_dist)
z = shc.linkage(y)
c = shc.fcluster(z, 1., criterion='distance')

#df = pd.DataFrame(deployments)
df['cluster'] = c

for i in range(1,c.max()+1):
    print('---- %d ----' % i)
    print(df[df['cluster'] == i][['locationCode','deviceCategoryCode','deviceCode','locationName']])
    print('----------\n')
        

df.sort_values('cluster').sort_values('deviceCategoryCode')


ax = plt.gca()
plt.yscale('log')
plt.ylim((0.001,1000))
dend = shc.dendrogram(z,labels=(df['locationCode'].str.cat(df['deviceCategoryCode'],sep="_")).values)
plt.ylabel('Distance between stations (km)')


# https://ipyleaflet.readthedocs.io/en/latest/api_reference/layers_control.html
from ipyleaflet import Map, Marker, basemaps, MarkerCluster, LayerGroup,Circle,LayersControl,GeoJSON,WMSLayer


m = Map(center=(48,-128), zoom=5,basemap=basemaps.Esri.WorldImagery)
display(m)
m.layout.width = '100%'
m.layout.height = '800px'


# +
markers = []
for idx in range(len(df)):
    row = df.iloc[idx]
    markers.append(Marker(location = (row['lat'], row['lon']),
                          title = '%s %s' % (row['locationCode'],row['deviceCategoryCode'])))
    
#a = Marker(location=(48.,-129.))
#m.add_layer(a)
# -

m.add_layer(MarkerCluster(markers=markers, title='Hi!!!'))

m.add_layer(LayerGroup(layers=markers, name='circles'))

m.add_control(LayersControl(position='topright'))

markers = []
for idx in range(len(df)):
    row = df.iloc[idx]
    markers.append(Circle(location = (row['lat'], row['lon']),
                          title = row.to_string()))
    

r = requests.get('https://www.oceannetworks.ca/sites/all/libraries/dmas/earthquake/RingSpur.geoJson')
cable_ring = GeoJSON(data = r.json(), name='cable')
m.add_layer(cable_ring)

r = requests.get('https://www.oceannetworks.ca/sites/all/libraries/dmas/earthquake/PB2002_boundaries.json')
cable_ring = GeoJSON(data = r.json(), name='plate boundaries')
m.add_layer(cable_ring)

#https://www.gmrt.org/services/
wms = WMSLayer(
    url='https://www.gmrt.org/services/mapserver/wms_merc?request=GetCapabilities&service=WMS&version=1.3.0',
    layers='GMRT',
    #format='image/png',
    #transparent=True,
    #attribution='Weather data © 2012 IEM Nexrad'
)
m.add_layer(wms)

from IPython.core.display import display, HTML
HTML(df.sort_values(['cluster','deviceCategoryCode']).to_html())

df[~df['locationName'].str.contains('Patrol')]


basemaps.get()

StarcoderdataJupyterScriptsDedupFiltered