# Spherical ContoursΒΆ

import numpy as np
from scipy import special as sp
from matplotlib import pyplot as plt
import s3dlib.surface as s3d
import s3dlib.cmap_utilities as cmu

#.. Sphereical Harmonics, contant R contours

# 1. Define functions to examine ....................................

def sphHar(rtp) :
r, theta, phi = rtp
m, l = 2,3
r = sp.sph_harm(m, l, theta, phi).real
return r, theta, phi

def sphHar_absR(rtp) :
r, theta, phi = sphHar(rtp)
return np.abs(r), theta, phi

rtp = s3d.SphericalSurface.coor_convert(xyz,False)
return sphHar(rtp)[0]

# 2. Setup and map surfaces .........................................
rez, nCnt = 5, 15
binmap = cmu.binary_cmap()
bkr = cmu.stitch_cmap(b2k,k2r)

sph_23_pos = s3d.SphericalSurface(rez, basetype='octa', cmap=binmap,lw=0)
sph_23_pos.map_cmap_from_op( lambda rtp : sphHar(rtp)[0] )
sph_23_pos.set_surface_alpha(0.2)
sph_23_pos.map_geom_from_op(sphHar_absR)

lines = sph_23_pos.contourLineSet(nCnt,coor=2)

# 3. Construct figure, add surfaces, and plot .....................
minmax = (-.25,0.25)
fig = plt.figure(figsize=plt.figaspect(0.8))
ax2 = plt.axes(projection='3d')
ax2.set(xlim=minmax, ylim=minmax, zlim=minmax )
ax2.set_title('Spherical Harmonics (2,3)\n'+r'R=|f($\theta$,$\varphi$)|'+' contours')
cbar = plt.colorbar(lines.stcBar_ScalarMappable(2*nCnt,'w'), ax=ax2,  shrink=0.6 )