<filename>plot_test/test_plot_2.py
'''
========================
3D surface (solid color)
========================

Demonstrates a very basic plot of a 3D surface using a solid color.
'''

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

from tanager_feeder.utils import cos, sin

data = [
    (30.0, 80.0, 90.0),
    (30.0, 75.0, 90.0),
    (30.0, 70.0, 90.0),
    (30.0, 65.0, 90.0),
    (30.0, 60.0, 90.0),
    (30.0, 55.0, 90.0),
    (30.0, 50.0, 90.0),
    (30.0, 45.0, 90.0),
    (30.0, 40.0, 90.0),
    (30.0, 35.0, 90.0),
    (30.0, 30.0, 90.0),
    (30.0, 25.0, 90.0),
    (30.0, 20.0, 90.0),
    (30.0, 15.0, 90.0),
    (30.0, 10.0, 90.0),
    (30.0, 5.0, 90.0),
    (30.0, 0.0, 90.0),
    (30.0, 0.0, 75.0),
    (30.0, 5.0, 75.0),
    (30.0, 10.0, 75.0),
    (30.0, 15.0, 75.0),
    (30.0, 20.0, 75.0),
    (30.0, 25.0, 75.0),
    (30.0, 30.0, 75.0),
    (30.0, 35.0, 75.0),
    (30.0, 40.0, 75.0),
    (30.0, 45.0, 75.0),
    (30.0, 50.0, 75.0),
    (30.0, 55.0, 75.0),
    (30.0, 60.0, 75.0),
    (30.0, 65.0, 75.0),
    (30.0, 70.0, 75.0),
    (30.0, 75.0, 75.0),
    (30.0, 80.0, 75.0),
    (30.0, 80.0, 60.0),
    (30.0, 75.0, 60.0),
    (30.0, 70.0, 60.0),
    (30.0, 65.0, 60.0),
    (30.0, 60.0, 60.0),
    (30.0, 55.0, 60.0),
    (30.0, 50.0, 60.0),
    (30.0, 45.0, 60.0),
    (30.0, 40.0, 60.0),
    (30.0, 35.0, 60.0),
    (30.0, 30.0, 60.0),
    (30.0, 25.0, 60.0),
    (30.0, 20.0, 60.0),
    (30.0, 15.0, 60.0),
    (30.0, 10.0, 60.0),
    (30.0, 5.0, 60.0),
    (30.0, 0.0, 60.0),
    (30.0, 0.0, 45.0),
    (30.0, 5.0, 45.0),
    (30.0, 10.0, 45.0),
    (30.0, 15.0, 45.0),
    (30.0, 20.0, 45.0),
    (30.0, 25.0, 45.0),
    (30.0, 30.0, 45.0),
    (30.0, 35.0, 45.0),
    (30.0, 40.0, 45.0),
    (30.0, 45.0, 45.0),
    (30.0, 50.0, 45.0),
    (30.0, 55.0, 45.0),
    (30.0, 60.0, 45.0),
    (30.0, 65.0, 45.0),
    (30.0, 70.0, 45.0),
    (30.0, 75.0, 45.0),
    (30.0, 80.0, 45.0),
    (30.0, 80.0, 30.0),
    (30.0, 75.0, 30.0),
    (30.0, 70.0, 30.0),
    (30.0, 65.0, 30.0),
    (30.0, 60.0, 30.0),
    (30.0, 55.0, 30.0),
    (30.0, 50.0, 30.0),
    (30.0, 45.0, 30.0),
    (30.0, 40.0, 30.0),
    (30.0, 35.0, 30.0),
    (30.0, 30.0, 30.0),
    (30.0, 25.0, 30.0),
    (30.0, 20.0, 30.0),
    (30.0, 15.0, 30.0),
    (30.0, 10.0, 30.0),
    (30.0, 5.0, 30.0),
    (30.0, 0.0, 30.0),
    (30.0, 0.0, 15.0),
    (30.0, 5.0, 15.0),
    (30.0, 10.0, 15.0),
    (30.0, 15.0, 15.0),
    (30.0, 20.0, 15.0),
    (30.0, 25.0, 15.0),
    (30.0, 30.0, 15.0),
    (30.0, 35.0, 15.0),
    (30.0, 40.0, 15.0),
    (30.0, 45.0, 15.0),
    (30.0, 50.0, 15.0),
    (30.0, 55.0, 15.0),
    (30.0, 60.0, 15.0),
    (30.0, 65.0, 15.0),
    (30.0, 70.0, 15.0),
    (30.0, 75.0, 15.0),
    (30.0, 80.0, 15.0),
    (30.0, 80.0, 0.0),
    (30.0, 75.0, 0.0),
    (30.0, 70.0, 0.0),
    (30.0, 65.0, 0.0),
    (30.0, 60.0, 0.0),
    (30.0, 55.0, 0.0),
    (30.0, 50.0, 0.0),
    (30.0, 45.0, 0.0),
    (30.0, 40.0, 0.0),
    (30.0, 35.0, 0.0),
    (30.0, 25.0, 0.0),
    (30.0, 20.0, 0.0),
    (30.0, 15.0, 0.0),
    (30.0, 10.0, 0.0),
    (30.0, 5.0, 0.0),
    (30.0, 0.0, 0.0),
    (30.0, 5.0, 180.0),
    (30.0, 10.0, 180.0),
    (30.0, 15.0, 180.0),
    (30.0, 20.0, 180.0),
    (30.0, 25.0, 180.0),
    (30.0, 30.0, 180.0),
    (30.0, 35.0, 180.0),
    (30.0, 40.0, 180.0),
    (30.0, 45.0, 180.0),
    (30.0, 50.0, 180.0),
    (30.0, 55.0, 180.0),
    (30.0, 60.0, 180.0),
    (30.0, 65.0, 180.0),
    (30.0, 70.0, 180.0),
    (30.0, 75.0, 180.0),
    (30.0, 80.0, 180.0),
    (30.0, 80.0, 165.0),
    (30.0, 75.0, 165.0),
    (30.0, 70.0, 165.0),
    (30.0, 65.0, 165.0),
    (30.0, 60.0, 165.0),
    (30.0, 55.0, 165.0),
    (30.0, 50.0, 165.0),
    (30.0, 45.0, 165.0),
    (30.0, 40.0, 165.0),
    (30.0, 35.0, 165.0),
    (30.0, 30.0, 165.0),
    (30.0, 25.0, 165.0),
    (30.0, 20.0, 165.0),
    (30.0, 15.0, 165.0),
    (30.0, 10.0, 165.0),
    (30.0, 5.0, 165.0),
    (30.0, 0.0, 165.0),
    (30.0, 0.0, 150.0),
    (30.0, 5.0, 150.0),
    (30.0, 10.0, 150.0),
    (30.0, 15.0, 150.0),
    (30.0, 20.0, 150.0),
    (30.0, 25.0, 150.0),
    (30.0, 30.0, 150.0),
    (30.0, 35.0, 150.0),
    (30.0, 40.0, 150.0),
    (30.0, 45.0, 150.0),
    (30.0, 50.0, 150.0),
    (30.0, 55.0, 150.0),
    (30.0, 60.0, 150.0),
    (30.0, 65.0, 150.0),
    (30.0, 70.0, 150.0),
    (30.0, 75.0, 150.0),
    (30.0, 80.0, 150.0),
    (30.0, 80.0, 135.0),
    (30.0, 75.0, 135.0),
    (30.0, 70.0, 135.0),
    (30.0, 65.0, 135.0),
    (30.0, 60.0, 135.0),
    (30.0, 55.0, 135.0),
    (30.0, 50.0, 135.0),
    (30.0, 45.0, 135.0),
    (30.0, 40.0, 135.0),
    (30.0, 35.0, 135.0),
    (30.0, 30.0, 135.0),
    (30.0, 25.0, 135.0),
    (30.0, 20.0, 135.0),
    (30.0, 15.0, 135.0),
    (30.0, 10.0, 135.0),
    (30.0, 5.0, 135.0),
    (30.0, 0.0, 135.0),
    (30.0, 0.0, 120.0),
    (30.0, 5.0, 120.0),
    (30.0, 10.0, 120.0),
    (30.0, 15.0, 120.0),
    (30.0, 20.0, 120.0),
    (30.0, 25.0, 120.0),
    (30.0, 30.0, 120.0),
    (30.0, 35.0, 120.0),
    (30.0, 40.0, 120.0),
    (30.0, 45.0, 120.0),
    (30.0, 50.0, 120.0),
    (30.0, 55.0, 120.0),
    (30.0, 60.0, 120.0),
    (30.0, 65.0, 120.0),
    (30.0, 70.0, 120.0),
    (30.0, 75.0, 120.0),
    (30.0, 80.0, 120.0),
    (30.0, 80.0, 105.0),
    (30.0, 75.0, 105.0),
    (30.0, 70.0, 105.0),
    (30.0, 65.0, 105.0),
    (30.0, 60.0, 105.0),
    (30.0, 55.0, 105.0),
    (30.0, 50.0, 105.0),
    (30.0, 45.0, 105.0),
    (30.0, 40.0, 105.0),
    (30.0, 35.0, 105.0),
    (30.0, 30.0, 105.0),
    (30.0, 25.0, 105.0),
    (30.0, 20.0, 105.0),
    (30.0, 15.0, 105.0),
    (30.0, 10.0, 105.0),
    (30.0, 5.0, 105.0),
    (30.0, 0.0, 105.0),
]

reflectance_list = [
    0.805684,
    0.870814,
    0.9112600000000001,
    0.943568,
    0.968318,
    0.9849159999999999,
    1.000224,
    1.0141959999999999,
    1.024488,
    1.0335940000000001,
    1.0427700000000002,
    1.0503019999999998,
    1.0561019999999999,
    1.061442,
    1.06514,
    1.067282,
    1.0641500000000002,
    1.0640880000000001,
    1.0663359999999997,
    1.06389,
    1.059596,
    1.05416,
    1.047818,
    1.0397459999999998,
    1.0294659999999998,
    1.01957,
    1.008654,
    0.9935240000000001,
    0.977514,
    0.959784,
    0.9339299999999999,
    0.90016,
    0.858244,
    0.789354,
    0.7761340000000001,
    0.846076,
    0.890474,
    0.925694,
    0.952636,
    0.971668,
    0.988964,
    1.004756,
    1.01646,
    1.02711,
    1.037936,
    1.046412,
    1.0530199999999998,
    1.059202,
    1.0635020000000002,
    1.065898,
    1.0637940000000001,
    1.064208,
    1.0663,
    1.0637400000000001,
    1.059878,
    1.054054,
    1.047156,
    1.0385680000000002,
    1.027352,
    1.015794,
    1.0031820000000002,
    0.986654,
    0.9688100000000001,
    0.949226,
    0.9210040000000002,
    0.884652,
    0.83978,
    0.765868,
    0.7575799999999999,
    0.8320839999999998,
    0.8797039999999999,
    0.9172399999999999,
    0.946504,
    0.9672360000000001,
    0.9864599999999999,
    1.0043000000000002,
    1.0176100000000001,
    1.029752,
    1.0415459999999999,
    1.050254,
    1.056378,
    1.061364,
    1.0647959999999999,
    1.0671300000000001,
    1.06499,
    1.065826,
    1.068158,
    1.0662680000000002,
    1.063794,
    1.060086,
    1.055842,
    1.048642,
    1.036226,
    1.0225020000000002,
    1.0073,
    0.98848,
    0.968328,
    0.9463820000000001,
    0.916076,
    0.8775700000000001,
    0.82927,
    0.751222,
    0.746038,
    0.8244999999999999,
    0.87447,
    0.91443,
    0.9457379999999999,
    0.968008,
    0.9892960000000001,
    1.009306,
    1.0261900000000002,
    1.045416,
    1.064698,
    1.063866,
    1.0657520000000003,
    1.0676539999999999,
    1.0690719999999998,
    1.066528,
    1.069938,
    1.073272,
    1.075138,
    1.0764299999999998,
    1.076966,
    1.0767419999999999,
    1.076474,
    1.075158,
    1.0707239999999998,
    1.066786,
    1.062028,
    1.05125,
    1.0370679999999999,
    1.0191240000000001,
    0.9840540000000001,
    0.9192560000000001,
    0.8991439999999999,
    0.963176,
    1.001946,
    1.0214820000000002,
    1.037988,
    1.0504660000000001,
    1.056624,
    1.0626039999999999,
    1.0677539999999999,
    1.070054,
    1.0713279999999998,
    1.0726600000000002,
    1.0729,
    1.072806,
    1.073032,
    1.0704,
    1.067676,
    1.068478,
    1.070608,
    1.0723019999999999,
    1.071598,
    1.0699480000000001,
    1.067478,
    1.063748,
    1.0604679999999997,
    1.0559220000000002,
    1.049026,
    1.0421559999999999,
    1.034024,
    1.0195440000000002,
    1.00162,
    0.979408,
    0.939376,
    0.8708,
    0.8431140000000001,
    0.9087780000000001,
    0.952788,
    0.9779500000000001,
    0.999752,
    1.016732,
    1.0270319999999997,
    1.036618,
    1.045634,
    1.05117,
    1.056414,
    1.061472,
    1.065066,
    1.067942,
    1.071158,
    1.0706120000000001,
    1.069372,
    1.070332,
    1.0706779999999998,
    1.070236,
    1.0670499999999998,
    1.062686,
    1.0571360000000003,
    1.050212,
    1.0438079999999998,
    1.036112,
    1.026106,
    1.015464,
    1.0035779999999999,
    0.985088,
    0.96311,
    0.936314,
    0.892502,
    0.8222479999999999,
    0.806732,
    0.8725099999999999,
    0.9191099999999999,
    0.9476279999999999,
    0.972116,
    0.9922340000000001,
    1.0057620000000003,
    1.018056,
    1.029748,
    1.037886,
    1.045328,
    1.052948,
    1.0587339999999998,
    1.0641219999999998,
    1.0694599999999999,
    1.0709579999999999,
    0.6647879999999999,
]

print(len(data))
print(len(reflectance_list))

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')


# Make data
azimuths = np.linspace(0, 359, 360)
emissions = np.linspace(0, 89, 90)

for j, az in enumerate(azimuths):
    azimuths[j] = int(az)
for j, e in enumerate(emissions):
    emissions[j] = int(e)

#
# # datasets = {}
#
R = {}
# reflectance_list = []

print(len(data))
print(len(reflectance_list))

for i, tup in enumerate(data):
    if tup[0] == 30.0:
        e = int(tup[1])
        az = int(tup[2])
        if e < 0:
            az = az+180
            e = np.abs(e)
        if az not in R:
            R[az] = {}
        else:
            R[az][e] = reflectance_list[i]
        # reflectance_list.append(1)

reflectance_list = np.array(reflectance_list)
avg = np.mean(reflectance_list)
norm = mpl.colors.Normalize(vmin=np.min(reflectance_list),vmax=np.max(reflectance_list))

x = []
y = []
z = []
r = []

scatter_x = []
scatter_y = []
scatter_z = []

winnowed_az = []
winnowed_e = []

for i, az in enumerate(azimuths):
    if az in R:
        winnowed_az.append(az)
        for j, e in enumerate(emissions):
            if e in R[az]:
                if e not in winnowed_e:
                    winnowed_e.append(e)
winnowed_az = sorted(winnowed_az)
winnowed_e = sorted(winnowed_e)

for i, az in enumerate(winnowed_az):
    x.append([])
    y.append([])
    z.append([])
    r.append([])

    for j, e in enumerate(winnowed_e):
        if e in R[az]:
            x[-1].append(cos(az)*sin(e)*R[az][e])
            y[-1].append(sin(az)*sin(e)*R[az][e])
            z[-1].append(cos(e)*R[az][e])
            r[-1].append(R[az][e])

            scatter_x.append(cos(az)*sin(e)*R[az][e])
            scatter_y.append(sin(az)*sin(e)*R[az][e])
            scatter_z.append(cos(e)*R[az][e])
        #To have nicely behaved 2D arrays, all should represent the same az, e pairs (I think)
        else:
            x[-1].append(None)
            y[-1].append(None)
            z[-1].append(None)
            r[-1].append(None)

for j in range(len(r)):
    for k in range(len(r[j])):
        if r[j][k] is None:
            close_rs = []
            search_index = j
            while search_index >= 0 and r[search_index][k] is None:
                search_index -= 1
            if search_index > -1:
                close_rs.append(r[search_index][k])

            search_index = j
            while search_index < len(r) and r[search_index][k] is None:
                search_index += 1
            if search_index < len(r):
                close_rs.append(r[search_index][k])

            search_index = k
            while search_index >= 0 and r[j][search_index] is None:
                search_index -= 1
            if search_index > -1:
                close_rs.append(r[j][search_index])

            search_index = k
            while search_index < len(r[j]) and r[j][search_index] is None:
                search_index += 1

            if search_index < len(r[j]):
                close_rs.append(r[j][search_index])

            avg = np.mean(close_rs)
            az = winnowed_az[j]
            e = winnowed_e[k]

            x[j][k] = cos(az)*sin(e)*avg
            y[j][k] = sin(az)*sin(e)*avg
            z[j][k] = cos(e)*avg
            r[j][k] = avg


x = np.array(x)
y = np.array(y)
z = np.array(z)

jet = plt.cm.jet
colors = []
num_az = len(winnowed_az)
num_e = len(winnowed_e)

for i in range(num_az):
    colors.append([])
    for j in range(num_e):
        vals_to_avg = [norm(r[i][j])]
        if i < num_az - 1:
            vals_to_avg.append(norm(r[i+1][j]))
            if j < num_e - 1:
                vals_to_avg.append(norm(r[i][j+1]))
                vals_to_avg.append(norm(r[i+1][j+1]))
            else:
                pass # e values do not wrap around (it's a hemisphere, not a sphere)
        elif winnowed_az[i] > 330:  # az values can wrap around.
            vals_to_avg.append(norm(r[0][j]))
            if j < num_e - 1:
                vals_to_avg.append(norm(r[i][j + 1]))
                vals_to_avg.append(norm(r[0][j + 1]))
            else:
                pass # e values do not wrap around (it's a hemisphere, not a sphere)
        val = np.mean(vals_to_avg)
        colors[i].append(jet(val))

# u = np.linspace(0, 2 * np.pi, 30)
# v = np.linspace(0, np.pi/2, 15)
# back_x = np.outer(np.cos(u), np.sin(v))
# back_y = np.outer(np.sin(u), np.sin(v))
# back_z = np.outer(np.ones(np.size(u)), 0)
# backdrop = ax.plot_surface(back_x, back_y, back_z, alpha=0.8, zorder=0)

# Plot the surface
ax.plot_surface(x, y, z,
    linewidth=1, alpha=1, facecolors=colors, zorder=100)


ax.scatter(scatter_x, scatter_y, scatter_z, s=1, c='black', zorder=200)

x = []
y = []
z = []
for az in winnowed_az:
    x.append([])
    y.append([])
    z.append([])
    for e in winnowed_e:
        x[-1].append(cos(az)*sin(e))
        y[-1].append(sin(az)*sin(e))
        z[-1].append(0)

x, y, z = np.array(x), np.array(y), np.array(z)

surf = ax.plot_surface(x, y, z,
    linewidth=1, alpha=1, zorder=0)

rline = 1.8
azline = 0
iline = 0
xline = [0, cos(azline) * sin(iline) * rline]
yline = [0, sin(azline) * sin(iline) * rline]
zline = [0, cos(iline) * rline]
ax.plot3D(xline, yline, zline, 'darkorange', linewidth=4)

ax.set_ylabel("y")
ax.set_xlabel("x")
ax.grid(False)
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
ax.axis("off")
ax.text2D(0.15, 0.85, "Title", fontsize=18, transform=ax.transAxes)
ax.auto_scale_xyz([-0.8, 0.8], [-0.8, 0.8], [0, 1.6])

print(np.max(reflectance_list))

import matplotlib.cm as cm
m = cm.ScalarMappable(cmap=jet, norm=norm)

cbar_ax = fig.add_axes([0.75, 0.3, 0.035, 0.45])
colorbar = fig.colorbar(m,  cax=cbar_ax)

pos1 = ax.get_position()  # get the original position
pos2 = [pos1.x0 - 0.1, pos1.y0, pos1.width, pos1.height]
ax.set_position(pos2)

ax.text2D(1, 0.35, f"Reflectance", fontsize=16, rotation=90, transform=ax.transAxes)

plt.show()

<filename>plot_test/test_plot_2.py
'''
========================
3D surface (solid color)
========================

Demonstrates a very basic plot of a 3D surface using a solid color.
'''

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

from tanager_feeder.utils import cos, sin

data = [
    (30.0, 80.0, 90.0),
    (30.0, 75.0, 90.0),
    (30.0, 70.0, 90.0),
    (30.0, 65.0, 90.0),
    (30.0, 60.0, 90.0),
    (30.0, 55.0, 90.0),
    (30.0, 50.0, 90.0),
    (30.0, 45.0, 90.0),
    (30.0, 40.0, 90.0),
    (30.0, 35.0, 90.0),
    (30.0, 30.0, 90.0),
    (30.0, 25.0, 90.0),
    (30.0, 20.0, 90.0),
    (30.0, 15.0, 90.0),
    (30.0, 10.0, 90.0),
    (30.0, 5.0, 90.0),
    (30.0, 0.0, 90.0),
    (30.0, 0.0, 75.0),
    (30.0, 5.0, 75.0),
    (30.0, 10.0, 75.0),
    (30.0, 15.0, 75.0),
    (30.0, 20.0, 75.0),
    (30.0, 25.0, 75.0),
    (30.0, 30.0, 75.0),
    (30.0, 35.0, 75.0),
    (30.0, 40.0, 75.0),
    (30.0, 45.0, 75.0),
    (30.0, 50.0, 75.0),
    (30.0, 55.0, 75.0),
    (30.0, 60.0, 75.0),
    (30.0, 65.0, 75.0),
    (30.0, 70.0, 75.0),
    (30.0, 75.0, 75.0),
    (30.0, 80.0, 75.0),
    (30.0, 80.0, 60.0),
    (30.0, 75.0, 60.0),
    (30.0, 70.0, 60.0),
    (30.0, 65.0, 60.0),
    (30.0, 60.0, 60.0),
    (30.0, 55.0, 60.0),
    (30.0, 50.0, 60.0),
    (30.0, 45.0, 60.0),
    (30.0, 40.0, 60.0),
    (30.0, 35.0, 60.0),
    (30.0, 30.0, 60.0),
    (30.0, 25.0, 60.0),
    (30.0, 20.0, 60.0),
    (30.0, 15.0, 60.0),
    (30.0, 10.0, 60.0),
    (30.0, 5.0, 60.0),
    (30.0, 0.0, 60.0),
    (30.0, 0.0, 45.0),
    (30.0, 5.0, 45.0),
    (30.0, 10.0, 45.0),
    (30.0, 15.0, 45.0),
    (30.0, 20.0, 45.0),
    (30.0, 25.0, 45.0),
    (30.0, 30.0, 45.0),
    (30.0, 35.0, 45.0),
    (30.0, 40.0, 45.0),
    (30.0, 45.0, 45.0),
    (30.0, 50.0, 45.0),
    (30.0, 55.0, 45.0),
    (30.0, 60.0, 45.0),
    (30.0, 65.0, 45.0),
    (30.0, 70.0, 45.0),
    (30.0, 75.0, 45.0),
    (30.0, 80.0, 45.0),
    (30.0, 80.0, 30.0),
    (30.0, 75.0, 30.0),
    (30.0, 70.0, 30.0),
    (30.0, 65.0, 30.0),
    (30.0, 60.0, 30.0),
    (30.0, 55.0, 30.0),
    (30.0, 50.0, 30.0),
    (30.0, 45.0, 30.0),
    (30.0, 40.0, 30.0),
    (30.0, 35.0, 30.0),
    (30.0, 30.0, 30.0),
    (30.0, 25.0, 30.0),
    (30.0, 20.0, 30.0),
    (30.0, 15.0, 30.0),
    (30.0, 10.0, 30.0),
    (30.0, 5.0, 30.0),
    (30.0, 0.0, 30.0),
    (30.0, 0.0, 15.0),
    (30.0, 5.0, 15.0),
    (30.0, 10.0, 15.0),
    (30.0, 15.0, 15.0),
    (30.0, 20.0, 15.0),
    (30.0, 25.0, 15.0),
    (30.0, 30.0, 15.0),
    (30.0, 35.0, 15.0),
    (30.0, 40.0, 15.0),
    (30.0, 45.0, 15.0),
    (30.0, 50.0, 15.0),
    (30.0, 55.0, 15.0),
    (30.0, 60.0, 15.0),
    (30.0, 65.0, 15.0),
    (30.0, 70.0, 15.0),
    (30.0, 75.0, 15.0),
    (30.0, 80.0, 15.0),
    (30.0, 80.0, 0.0),
    (30.0, 75.0, 0.0),
    (30.0, 70.0, 0.0),
    (30.0, 65.0, 0.0),
    (30.0, 60.0, 0.0),
    (30.0, 55.0, 0.0),
    (30.0, 50.0, 0.0),
    (30.0, 45.0, 0.0),
    (30.0, 40.0, 0.0),
    (30.0, 35.0, 0.0),
    (30.0, 25.0, 0.0),
    (30.0, 20.0, 0.0),
    (30.0, 15.0, 0.0),
    (30.0, 10.0, 0.0),
    (30.0, 5.0, 0.0),
    (30.0, 0.0, 0.0),
    (30.0, 5.0, 180.0),
    (30.0, 10.0, 180.0),
    (30.0, 15.0, 180.0),
    (30.0, 20.0, 180.0),
    (30.0, 25.0, 180.0),
    (30.0, 30.0, 180.0),
    (30.0, 35.0, 180.0),
    (30.0, 40.0, 180.0),
    (30.0, 45.0, 180.0),
    (30.0, 50.0, 180.0),
    (30.0, 55.0, 180.0),
    (30.0, 60.0, 180.0),
    (30.0, 65.0, 180.0),
    (30.0, 70.0, 180.0),
    (30.0, 75.0, 180.0),
    (30.0, 80.0, 180.0),
    (30.0, 80.0, 165.0),
    (30.0, 75.0, 165.0),
    (30.0, 70.0, 165.0),
    (30.0, 65.0, 165.0),
    (30.0, 60.0, 165.0),
    (30.0, 55.0, 165.0),
    (30.0, 50.0, 165.0),
    (30.0, 45.0, 165.0),
    (30.0, 40.0, 165.0),
    (30.0, 35.0, 165.0),
    (30.0, 30.0, 165.0),
    (30.0, 25.0, 165.0),
    (30.0, 20.0, 165.0),
    (30.0, 15.0, 165.0),
    (30.0, 10.0, 165.0),
    (30.0, 5.0, 165.0),
    (30.0, 0.0, 165.0),
    (30.0, 0.0, 150.0),
    (30.0, 5.0, 150.0),
    (30.0, 10.0, 150.0),
    (30.0, 15.0, 150.0),
    (30.0, 20.0, 150.0),
    (30.0, 25.0, 150.0),
    (30.0, 30.0, 150.0),
    (30.0, 35.0, 150.0),
    (30.0, 40.0, 150.0),
    (30.0, 45.0, 150.0),
    (30.0, 50.0, 150.0),
    (30.0, 55.0, 150.0),
    (30.0, 60.0, 150.0),
    (30.0, 65.0, 150.0),
    (30.0, 70.0, 150.0),
    (30.0, 75.0, 150.0),
    (30.0, 80.0, 150.0),
    (30.0, 80.0, 135.0),
    (30.0, 75.0, 135.0),
    (30.0, 70.0, 135.0),
    (30.0, 65.0, 135.0),
    (30.0, 60.0, 135.0),
    (30.0, 55.0, 135.0),
    (30.0, 50.0, 135.0),
    (30.0, 45.0, 135.0),
    (30.0, 40.0, 135.0),
    (30.0, 35.0, 135.0),
    (30.0, 30.0, 135.0),
    (30.0, 25.0, 135.0),
    (30.0, 20.0, 135.0),
    (30.0, 15.0, 135.0),
    (30.0, 10.0, 135.0),
    (30.0, 5.0, 135.0),
    (30.0, 0.0, 135.0),
    (30.0, 0.0, 120.0),
    (30.0, 5.0, 120.0),
    (30.0, 10.0, 120.0),
    (30.0, 15.0, 120.0),
    (30.0, 20.0, 120.0),
    (30.0, 25.0, 120.0),
    (30.0, 30.0, 120.0),
    (30.0, 35.0, 120.0),
    (30.0, 40.0, 120.0),
    (30.0, 45.0, 120.0),
    (30.0, 50.0, 120.0),
    (30.0, 55.0, 120.0),
    (30.0, 60.0, 120.0),
    (30.0, 65.0, 120.0),
    (30.0, 70.0, 120.0),
    (30.0, 75.0, 120.0),
    (30.0, 80.0, 120.0),
    (30.0, 80.0, 105.0),
    (30.0, 75.0, 105.0),
    (30.0, 70.0, 105.0),
    (30.0, 65.0, 105.0),
    (30.0, 60.0, 105.0),
    (30.0, 55.0, 105.0),
    (30.0, 50.0, 105.0),
    (30.0, 45.0, 105.0),
    (30.0, 40.0, 105.0),
    (30.0, 35.0, 105.0),
    (30.0, 30.0, 105.0),
    (30.0, 25.0, 105.0),
    (30.0, 20.0, 105.0),
    (30.0, 15.0, 105.0),
    (30.0, 10.0, 105.0),
    (30.0, 5.0, 105.0),
    (30.0, 0.0, 105.0),
]

reflectance_list = [
    0.805684,
    0.870814,
    0.9112600000000001,
    0.943568,
    0.968318,
    0.9849159999999999,
    1.000224,
    1.0141959999999999,
    1.024488,
    1.0335940000000001,
    1.0427700000000002,
    1.0503019999999998,
    1.0561019999999999,
    1.061442,
    1.06514,
    1.067282,
    1.0641500000000002,
    1.0640880000000001,
    1.0663359999999997,
    1.06389,
    1.059596,
    1.05416,
    1.047818,
    1.0397459999999998,
    1.0294659999999998,
    1.01957,
    1.008654,
    0.9935240000000001,
    0.977514,
    0.959784,
    0.9339299999999999,
    0.90016,
    0.858244,
    0.789354,
    0.7761340000000001,
    0.846076,
    0.890474,
    0.925694,
    0.952636,
    0.971668,
    0.988964,
    1.004756,
    1.01646,
    1.02711,
    1.037936,
    1.046412,
    1.0530199999999998,
    1.059202,
    1.0635020000000002,
    1.065898,
    1.0637940000000001,
    1.064208,
    1.0663,
    1.0637400000000001,
    1.059878,
    1.054054,
    1.047156,
    1.0385680000000002,
    1.027352,
    1.015794,
    1.0031820000000002,
    0.986654,
    0.9688100000000001,
    0.949226,
    0.9210040000000002,
    0.884652,
    0.83978,
    0.765868,
    0.7575799999999999,
    0.8320839999999998,
    0.8797039999999999,
    0.9172399999999999,
    0.946504,
    0.9672360000000001,
    0.9864599999999999,
    1.0043000000000002,
    1.0176100000000001,
    1.029752,
    1.0415459999999999,
    1.050254,
    1.056378,
    1.061364,
    1.0647959999999999,
    1.0671300000000001,
    1.06499,
    1.065826,
    1.068158,
    1.0662680000000002,
    1.063794,
    1.060086,
    1.055842,
    1.048642,
    1.036226,
    1.0225020000000002,
    1.0073,
    0.98848,
    0.968328,
    0.9463820000000001,
    0.916076,
    0.8775700000000001,
    0.82927,
    0.751222,
    0.746038,
    0.8244999999999999,
    0.87447,
    0.91443,
    0.9457379999999999,
    0.968008,
    0.9892960000000001,
    1.009306,
    1.0261900000000002,
    1.045416,
    1.064698,
    1.063866,
    1.0657520000000003,
    1.0676539999999999,
    1.0690719999999998,
    1.066528,
    1.069938,
    1.073272,
    1.075138,
    1.0764299999999998,
    1.076966,
    1.0767419999999999,
    1.076474,
    1.075158,
    1.0707239999999998,
    1.066786,
    1.062028,
    1.05125,
    1.0370679999999999,
    1.0191240000000001,
    0.9840540000000001,
    0.9192560000000001,
    0.8991439999999999,
    0.963176,
    1.001946,
    1.0214820000000002,
    1.037988,
    1.0504660000000001,
    1.056624,
    1.0626039999999999,
    1.0677539999999999,
    1.070054,
    1.0713279999999998,
    1.0726600000000002,
    1.0729,
    1.072806,
    1.073032,
    1.0704,
    1.067676,
    1.068478,
    1.070608,
    1.0723019999999999,
    1.071598,
    1.0699480000000001,
    1.067478,
    1.063748,
    1.0604679999999997,
    1.0559220000000002,
    1.049026,
    1.0421559999999999,
    1.034024,
    1.0195440000000002,
    1.00162,
    0.979408,
    0.939376,
    0.8708,
    0.8431140000000001,
    0.9087780000000001,
    0.952788,
    0.9779500000000001,
    0.999752,
    1.016732,
    1.0270319999999997,
    1.036618,
    1.045634,
    1.05117,
    1.056414,
    1.061472,
    1.065066,
    1.067942,
    1.071158,
    1.0706120000000001,
    1.069372,
    1.070332,
    1.0706779999999998,
    1.070236,
    1.0670499999999998,
    1.062686,
    1.0571360000000003,
    1.050212,
    1.0438079999999998,
    1.036112,
    1.026106,
    1.015464,
    1.0035779999999999,
    0.985088,
    0.96311,
    0.936314,
    0.892502,
    0.8222479999999999,
    0.806732,
    0.8725099999999999,
    0.9191099999999999,
    0.9476279999999999,
    0.972116,
    0.9922340000000001,
    1.0057620000000003,
    1.018056,
    1.029748,
    1.037886,
    1.045328,
    1.052948,
    1.0587339999999998,
    1.0641219999999998,
    1.0694599999999999,
    1.0709579999999999,
    0.6647879999999999,
]

print(len(data))
print(len(reflectance_list))

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')


# Make data
azimuths = np.linspace(0, 359, 360)
emissions = np.linspace(0, 89, 90)

for j, az in enumerate(azimuths):
    azimuths[j] = int(az)
for j, e in enumerate(emissions):
    emissions[j] = int(e)

#
# # datasets = {}
#
R = {}
# reflectance_list = []

print(len(data))
print(len(reflectance_list))

for i, tup in enumerate(data):
    if tup[0] == 30.0:
        e = int(tup[1])
        az = int(tup[2])
        if e < 0:
            az = az+180
            e = np.abs(e)
        if az not in R:
            R[az] = {}
        else:
            R[az][e] = reflectance_list[i]
        # reflectance_list.append(1)

reflectance_list = np.array(reflectance_list)
avg = np.mean(reflectance_list)
norm = mpl.colors.Normalize(vmin=np.min(reflectance_list),vmax=np.max(reflectance_list))

x = []
y = []
z = []
r = []

scatter_x = []
scatter_y = []
scatter_z = []

winnowed_az = []
winnowed_e = []

for i, az in enumerate(azimuths):
    if az in R:
        winnowed_az.append(az)
        for j, e in enumerate(emissions):
            if e in R[az]:
                if e not in winnowed_e:
                    winnowed_e.append(e)
winnowed_az = sorted(winnowed_az)
winnowed_e = sorted(winnowed_e)

for i, az in enumerate(winnowed_az):
    x.append([])
    y.append([])
    z.append([])
    r.append([])

    for j, e in enumerate(winnowed_e):
        if e in R[az]:
            x[-1].append(cos(az)*sin(e)*R[az][e])
            y[-1].append(sin(az)*sin(e)*R[az][e])
            z[-1].append(cos(e)*R[az][e])
            r[-1].append(R[az][e])

            scatter_x.append(cos(az)*sin(e)*R[az][e])
            scatter_y.append(sin(az)*sin(e)*R[az][e])
            scatter_z.append(cos(e)*R[az][e])
        #To have nicely behaved 2D arrays, all should represent the same az, e pairs (I think)
        else:
            x[-1].append(None)
            y[-1].append(None)
            z[-1].append(None)
            r[-1].append(None)

for j in range(len(r)):
    for k in range(len(r[j])):
        if r[j][k] is None:
            close_rs = []
            search_index = j
            while search_index >= 0 and r[search_index][k] is None:
                search_index -= 1
            if search_index > -1:
                close_rs.append(r[search_index][k])

            search_index = j
            while search_index < len(r) and r[search_index][k] is None:
                search_index += 1
            if search_index < len(r):
                close_rs.append(r[search_index][k])

            search_index = k
            while search_index >= 0 and r[j][search_index] is None:
                search_index -= 1
            if search_index > -1:
                close_rs.append(r[j][search_index])

            search_index = k
            while search_index < len(r[j]) and r[j][search_index] is None:
                search_index += 1

            if search_index < len(r[j]):
                close_rs.append(r[j][search_index])

            avg = np.mean(close_rs)
            az = winnowed_az[j]
            e = winnowed_e[k]

            x[j][k] = cos(az)*sin(e)*avg
            y[j][k] = sin(az)*sin(e)*avg
            z[j][k] = cos(e)*avg
            r[j][k] = avg


x = np.array(x)
y = np.array(y)
z = np.array(z)

jet = plt.cm.jet
colors = []
num_az = len(winnowed_az)
num_e = len(winnowed_e)

for i in range(num_az):
    colors.append([])
    for j in range(num_e):
        vals_to_avg = [norm(r[i][j])]
        if i < num_az - 1:
            vals_to_avg.append(norm(r[i+1][j]))
            if j < num_e - 1:
                vals_to_avg.append(norm(r[i][j+1]))
                vals_to_avg.append(norm(r[i+1][j+1]))
            else:
                pass # e values do not wrap around (it's a hemisphere, not a sphere)
        elif winnowed_az[i] > 330:  # az values can wrap around.
            vals_to_avg.append(norm(r[0][j]))
            if j < num_e - 1:
                vals_to_avg.append(norm(r[i][j + 1]))
                vals_to_avg.append(norm(r[0][j + 1]))
            else:
                pass # e values do not wrap around (it's a hemisphere, not a sphere)
        val = np.mean(vals_to_avg)
        colors[i].append(jet(val))

# u = np.linspace(0, 2 * np.pi, 30)
# v = np.linspace(0, np.pi/2, 15)
# back_x = np.outer(np.cos(u), np.sin(v))
# back_y = np.outer(np.sin(u), np.sin(v))
# back_z = np.outer(np.ones(np.size(u)), 0)
# backdrop = ax.plot_surface(back_x, back_y, back_z, alpha=0.8, zorder=0)

# Plot the surface
ax.plot_surface(x, y, z,
    linewidth=1, alpha=1, facecolors=colors, zorder=100)


ax.scatter(scatter_x, scatter_y, scatter_z, s=1, c='black', zorder=200)

x = []
y = []
z = []
for az in winnowed_az:
    x.append([])
    y.append([])
    z.append([])
    for e in winnowed_e:
        x[-1].append(cos(az)*sin(e))
        y[-1].append(sin(az)*sin(e))
        z[-1].append(0)

x, y, z = np.array(x), np.array(y), np.array(z)

surf = ax.plot_surface(x, y, z,
    linewidth=1, alpha=1, zorder=0)

rline = 1.8
azline = 0
iline = 0
xline = [0, cos(azline) * sin(iline) * rline]
yline = [0, sin(azline) * sin(iline) * rline]
zline = [0, cos(iline) * rline]
ax.plot3D(xline, yline, zline, 'darkorange', linewidth=4)

ax.set_ylabel("y")
ax.set_xlabel("x")
ax.grid(False)
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
ax.axis("off")
ax.text2D(0.15, 0.85, "Title", fontsize=18, transform=ax.transAxes)
ax.auto_scale_xyz([-0.8, 0.8], [-0.8, 0.8], [0, 1.6])

print(np.max(reflectance_list))

import matplotlib.cm as cm
m = cm.ScalarMappable(cmap=jet, norm=norm)

cbar_ax = fig.add_axes([0.75, 0.3, 0.035, 0.45])
colorbar = fig.colorbar(m,  cax=cbar_ax)

pos1 = ax.get_position()  # get the original position
pos2 = [pos1.x0 - 0.1, pos1.y0, pos1.width, pos1.height]
ax.set_position(pos2)

ax.text2D(1, 0.35, f"Reflectance", fontsize=16, rotation=90, transform=ax.transAxes)

plt.show()

======================== 3D surface (solid color) ========================  Demonstrates a very basic plot of a 3D surface using a solid color. # Make data # # # datasets = {} # # reflectance_list = [] # reflectance_list.append(1) #To have nicely behaved 2D arrays, all should represent the same az, e pairs (I think) # e values do not wrap around (it's a hemisphere, not a sphere) # az values can wrap around. # e values do not wrap around (it's a hemisphere, not a sphere) # u = np.linspace(0, 2 * np.pi, 30) # v = np.linspace(0, np.pi/2, 15) # back_x = np.outer(np.cos(u), np.sin(v)) # back_y = np.outer(np.sin(u), np.sin(v)) # back_z = np.outer(np.ones(np.size(u)), 0) # backdrop = ax.plot_surface(back_x, back_y, back_z, alpha=0.8, zorder=0) # Plot the surface # get the original position