from planet4 import io
from planet4 import markings

imgid = markings.ImageID('APF0000pr4')

%matplotlib ipympl
import seaborn as sns
sns.set_context('notebook')

imgid.plot_blotches()

imgid.plot_fans()

blotches = imgid.get_blotches()
fans = imgid.get_fans()

"""
Explanation: <h1>Table of Contents<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"><li><ul class="toc-item"><li><span><a href="#fan-graphic" data-toc-modified-id="fan-graphic-0.1">fan graphic</a></span></li></ul></li><li><span><a href="#Blotches-/-Ellipses" data-toc-modified-id="Blotches-/-Ellipses-1">Blotches / Ellipses</a></span><ul class="toc-item"><li><span><a href="#Shapely-fans" data-toc-modified-id="Shapely-fans-1.1">Shapely fans</a></span></li></ul></li></ul></div>
End of explanation
"""


sns.set_style('white')

import matplotlib.image as mplimg

img = mplimg.imread('/Users/klay6683/Dropbox/data/planet4/images/50e73ae05e2ed211dc00016d.jpg')

fig, ax = plt.subplots()
ax.imshow(img, origin='upper')

p4id = markings.ImageID('APF0000il4')

p4id.plot_fans()

fig, ax = plt.subplots()
p4id.data.angle.hist(ax=ax)

fig, ax = plt.subplots()
for angle in range(15, 360, 35):
    hiangle = 360 - angle
    fan = markings.Fan(pd.Series(dict(x=400, y=400, angle=angle, distance=100, spread=10)))
    fan.plot(color='g', ax=ax)
    markings.set_subframe_size(ax)
    fan.add_mean_wind_pointer(ax)
    ax.text(*fan.wind_pointer_end, f'P4: {angle}, Hi: {hiangle}',
            horizontalalignment='left',
            verticalalignment='center')

base = np.array([0.5,0.5])
length = 0.3
fig, ax = plt.subplots()
ax.set_aspect('equal')
for angle in range(0, 360, 30):
    p1 = markings.rotate_vector([length, 0], angle-15)
    p2 = markings.rotate_vector([length, 0], angle+15)
    vectors = np.vstack((base+p1, base, base+p2, base+p1))
    line = mlines.Line2D(vectors[:, 0], vectors[:, 1])
    ax.add_line(line)

    circle_base = p1-p2
    print(vectors)
    print(circle_base)
    center = base + p2 + 0.5*circle_base
    radius = 0.5 * LA.norm(circle_base)
    theta1 = degrees(arctan2(*circle_base[::-1]))
    theta2 = theta1+180
    wedge = mpatches.Wedge(center, radius, theta1, theta2,width=0.01*radius)
    ax.add_patch(wedge)

degrees(arctan2(circle_base[1],circle_base[0]))

arctan2?

markings.diffangle(p1, p2, rads=False)

base + markings.rotate_vector([0.3, 0], 45)

circle_base = array([1,-1])
0.5*circle_base

degrees(arctan2(*-circle_base))

markings.diffangle(p1, p2, rads=False)

diff = degrees(py_ang(p1, p2))
angle_to_p1 = degrees(py_ang([1,0],p1))
angle = angle_to_p1 + diff/2.0
angle

vectors = np.vstack((p1, base, p2))
vectors

vectors[:,0]

import math

%timeit math.cos(0.2)

%timeit np.cos(0.2)

"""
Explanation: fan graphic
End of explanation
"""


from planet4 import markings

data = {'x':200, 'y':200, 'angle':10, 'radius_1':200, 'radius_2':100}

p4id = markings.ImageID('brt')

blotch = markings.Blotch(pd.Series(data), with_center=True, lw=2)
fig, ax = plt.subplots()
ax.set_aspect('equal','datalim')
p4id.show_subframe(ax=ax)
blotch.plot(ax=ax)
# plt.gca().autoscale_view()

blotch.to_shapely()

"""
Explanation: Blotches / Ellipses
End of explanation
"""


d = {'x':[100], 'y':[200], 'spread':[35], 'angle':[25], 'distance':[250]}

from planet4.markings import Fan
from shapely import geometry as geom

fan = Fan(pd.DataFrame(d).squeeze())

fan_geo = fan.to_shapely()

fan_geo.area

%matplotlib ipympl

circle = geom.Point(*fan.semi_circle_center).buffer(fan.radius)
circle

np.array(circle.boundary).shape

"""
Explanation: Shapely fans
End of explanation
"""


plt.figure()
plt.scatter(*circle.boundary.xy, s=1)

"""
Explanation: The default buffering of shapely creates 66 elements here, therefore I will chose numsegments = 100 for now in the to_shapely method for Fans.
End of explanation
"""


