import plotar
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path
import requests
import json

"""
Explanation: Welcome to plotAR
End of explanation
"""


from sklearn import datasets
iris = datasets.load_iris()

plotar.plotar(iris.data, iris.target)

plotar.controller(height=500)

"""
Explanation: Iris
End of explanation
"""


url = 'https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv'
gap = pd.read_csv(url)

gap

data_json = plotar.linear(gap.query("continent=='Europe'"), xyz=['gdpPercap','year','lifeExp'],
              col='country', size='pop', return_data=True)

with open('examples/gapminder.json', 'w') as f:
    json.dump(data_json, f)

"""
Explanation: Just scan the QR-Code with your mobile device and open the URL - this will open that keyboard in your browser. Tap on the the AR-Icon to step in the Augmented Reality.
What should happen here is, that your mobile device connects to this very Jupyter server! For that to work plotar tries to guess a URL that also works from you mobile: 
Troubleshooting: If you cannot connect to this server consider the following steps:
* Enable connections from outside by (re-)starting Jupyter: jupyter lab --ip="*"
* Try to have your mobile device to be on the same network as your desktop.
WARNING Jupyter is secured by default to have a non-guessable token to get some level of security, but still you probably do not use HTTPS, so anybody intercepting the traffic between you mobile device and your desktop can see all your data! This might be ok in your home network or in a company enterprise - be cautious!
Traffic with mybinder.org actually is secured by HTTPS and the token.
GAPminder
End of explanation
"""


from bs4 import BeautifulSoup

url = 'https://d-one.ai/team'
res = requests.get(url)

pd.set_option("max_colwidth", 400)

soup = BeautifulSoup(res.text, 'html.parser')
x = soup.find_all("div", class_="details")
team = pd.DataFrame( dict(name=_.find_all('h3')[0].text, text=_.find_all('p')[0].text) for _ in x )
team = team.drop_duplicates('name')
team

"""
Explanation: D ONE Team
We scrape the http://d-one.ai/team webpage and extract some features on the team member's description
End of explanation
"""


team['n_sent'] = team.text.str.replace(r'[^.]','', regex=True).str.len()
team['n_word'] = team.text.str.replace(r'[^ ]','', regex=True).str.len()+1

"""
Explanation: Extract two approximate counts: number of words and number of sentence (the latter actuall fails e.g. if many Abbreviations are used :-| )
End of explanation
"""


years = team.text.apply(lambda x: ([2000] + [_ for _ in x.split() if _.startswith("20")])[-1])
team['year_start'] = years.astype(str).str.rstrip('.').astype(int)
team['year_start'].value_counts(dropna=False)

team['dr'] = team.name.str.startswith("Dr.")
team['dr'].value_counts(dropna=False)

team

team.columns

data_json = plotar.plotar(team, xyz=['n_word', 'n_sent', 'year_start'], col='dr', label='name', size=0.2, return_data=True)

with open('examples/d-one-team.json', 'w') as f:
    json.dump(data_json, f)

"""
Explanation: Extract the last mentioned year - usually that is, when people started. If we do not find one, take 2000 as a default value - that is before the company was founded!
End of explanation
"""


url = 'https://data.geo.admin.ch/ch.swisstopo.digitales-hoehenmodell_25/data.zip'
file_name = 'DHM200.asc'
file = Path("tmp") / file_name

"""
Explanation: CH - surface of Switzerland
We use the Swisstopo Digital Height Model 200m grid to draw a surface of Swizterland.
End of explanation
"""


if not file.exists():
    from io import BytesIO
    from zipfile import ZipFile
    import shutil
    print(f"Downloading {url} to {file} ...")
    zipfile = ZipFile(BytesIO(requests.get(url).content))
    with open(file, 'wb') as f:
        shutil.copyfileobj(zipfile.open("DHM200.asc"), f)
    print("Done")
else:
    print(f"{file} is ready")

%%time
y_head = {k: float(v) for k,v in np.genfromtxt(file, dtype=str, max_rows=6)}
print(y_head)
y = np.genfromtxt(file, skip_header=6, skip_footer=1)
y.shape

n,m = [int(y_head[_]) for _ in ['NCOLS','NROWS']]
n,m

img = y.flatten()[:n*(m-1)].reshape((m-1,n))

"""
Explanation: Download Zip file, unzip the part we need to file:
End of explanation
"""


factor = 2

img = img[::factor,::factor]

xvec = np.arange(img.shape[1]) * y_head['CELLSIZE'] * factor
yvec = np.arange(img.shape[0]) * y_head['CELLSIZE'] * factor

"""
Explanation: This file is actually rather big - if you want you can make it smaller by setting factor to e.g. 5, 10, 20. We set it to 2 for mybinder.org
End of explanation
"""


img[img<0] = 400

"""
Explanation: Impute negative, i.e. NA values
End of explanation
"""


i = plt.imshow(img, interpolation='none');

plt.hist(img.flatten(), 100);

data_json = plotar.surfacevr(img, x=xvec, y=yvec, return_data=True)

with open('examples/CH-full.json', 'w') as f:
    json.dump(data_json, f)

"""
Explanation: Quickly draw it here so we understand whats happening:
End of explanation
"""


from skyfield.api import Loader

load = Loader("./tmp/")

ts = load.timescale()
t = ts.utc(2021, 2, 26, 15, 19)

"""
Explanation: Planets
We visualize the position of the Planets in the solar system at some time using the skyfield package.
End of explanation
"""


planets = load('de421.bsp')  # ephemeris DE421

planet_names = [ _[-1] for i,_ in planets.names().items() if 0 < i < 100 ]
print(len(planet_names))
planet_names

"""
Explanation: Note: on mybinder.org unfortunately ftp-downloads are blocked so this will run into a timeout. We are preparing a workaround.
End of explanation
"""


planet_sizes = {
'Mercury': '0.383',
'Venus': '0.949',
'Earth': '1.000',
'Mars': '0.532',
'Jupiter': '11.20',
'Saturn': '9.449',
'Uranus': '4.007',
'Neptune': '3.883',
}

planets_xyz = np.array([planets[_].at(t).ecliptic_xyz().au for _ in planet_names ])

data = plotar.plotar(planets_xyz, #col=planet_names, label=np.array(planet_names),
              return_data=True)

with open('examples/planets.json', 'w') as f:
    json.dump(data, f)

"""
Explanation: https://en.wikipedia.org/wiki/Planet
Name    Equatorial

diameter [i]    Mass [i]    Semi-major axis (AU)    Orbital period
(years) Inclination
to Sun's equator (°)    Orbital
eccentricity    Rotation period
(days)  Confirmed
moons   Axial tilt (°)  Rings   Atmosphere
1.  Mercury 0.383   0.06    0.39    0.24    3.38    0.206   58.65   0   0.10    no  minimal
2.  Venus   0.949   0.81    0.72    0.62    3.86    0.007   −243.02 0   177.30  no  CO2, N2
3.  Earth   1.000   1.00    1.00    1.00    7.25    0.017   1.00    1   23.44   no  N2, O2, Ar
4.  Mars    0.532   0.11    1.52    1.88    5.65    0.093   1.03    2   25.19   no  CO2, N2, Ar
5.  Jupiter 11.209  317.83  5.20    11.86   6.09    0.048   0.41    79  3.12    yes H2, He
6.  Saturn  9.449   95.16   9.54    29.45   5.51    0.054   0.44    82  26.73   yes H2, He
7.  Uranus  4.007   14.54   19.19   84.02   6.48    0.047   −0.72   27  97.86   yes H2, He, CH4
8.  Neptune 3.883   17.15   30.07   164.79  6.43    0.009   0.67    14  29.60   yes H2, He, CH4j
End of explanation
"""


