(* Spherical Harmonics Texture on Yin and Yang grids *)
(* by Akira Kageyama, Kobe Univ. 2013.05.26. *)

SetDirectory[NotebookDirectory[]]

Nθ=60
Nϕ=3*(Nθ-1)+1  (* Do not change this relation: Nϕ-1 = 3(Nθ-1) *)

targetL = 20
targetM = 20

fileNameBase = "Ylm_"<>"l"<>IntegerString[targetL]<>"m"<>IntegerString[targetM]

dθ=π/2/(Nθ-1)
dϕ=dθ
θmin=π/4
ϕmin=-3 π/4 
list = {}
Do[θ=θmin+dθ*j;
Do[ϕ=ϕmin+dϕ*k;
Ylm=SphericalHarmonicY[targetL,targetM,θ,ϕ];
ReYlm=Re[N[Ylm,30]];
GrayScale = 0.5*(ReYlm+1);
list =Append[list,GrayScale],
{k,0,Nϕ-1}
],
{j,0,Nθ-1}
]
i=Image[Partition[list,Nϕ],ColorSpace->"Grayscale"]
Export[fileNameBase<>"_yin.jpg",i]


list = {}
Do[θ=θmin+dθ*j;
Do[ϕ=ϕmin+dϕ*k;
x = Sin[θ] Cos[ϕ];
y = Sin[θ] Sin[ϕ];
z = Cos[θ];
xyang = -x;
yyang =  z;
zyang =  y;
θyang = ArcCos[zyang];
ϕyang = If[Abs[xyang]<0.00000001&& Abs[yyang] <0.00000001,
                    0,  
                    ArcTan[xyang,yyang]]; 
Ylm=SphericalHarmonicY[targetL,targetM,θyang,ϕyang];
 ReYlm=Re[N[Ylm,30]];
 GrayScale=0.5*(ReYlm+1)*255;
GrayScale = 0.5*(ReYlm+1);
list =Append[list,GrayScale],
{k,0,Nϕ-1}
],
{j,0,Nθ-1}
]
i=Image[Partition[list,Nϕ],ColorSpace->"Grayscale"]
Export[fileNameBase<>"_yang.jpg",i]