ClearAll[spherical,rcap,thetacap,phicap, t, p]
spherical[t_,p_]:=CoordinateTransformData["Spherical"->"Cartesian","OrthonormalBasisRotation",{1,t,p}];
rcap[t_,p_]=spherical[t,p]//First;
thetacap[t_,p_]=spherical[t,p][[2]];
phicap[t_,p_]=spherical[t,p]//Last;
(*rcap = {Sin[#1] Cos[#2], Sin[#1] Sin[#2], Cos[#1]} & ;
thetacap = {Cos[#1] Cos[#2], Cos[#1] Sin[#2], -Sin[#1]} &;
phicap = {-Sin[#2], Cos[#2], 0 } & ;
*)
(*checks*)
(*{
rcap[θ, ϕ]. thetacap[θ, ϕ]// Simplify,
thetacap[θ, ϕ]. phicap[θ, ϕ],
phicap[θ, ϕ]. rcap[θ, ϕ],
rcap[θ, ϕ]\[Cross] thetacap[θ, ϕ] -phicap[θ, ϕ]// Simplify } // TableForm*)


ClearAll[ asz, toff, axes, m, ef, hf, k, t,p, a, b, logscale] ;


asz = 1 ;
toff = 0.1 ;
ath = 0.1 ;
axes = Graphics3D[{
Red,Arrow[Tube[{{0,0,0},{asz,0,0}}] , ath],
Blue,Arrow[Tube[{{0,0,0},{0,asz,0}}] , ath],
Darker[Green, .8],Arrow[Tube[{{0,0,0},{0,0,asz}}] , ath],
Text[ "(Subscript[e, 1])",  {asz + toff,0,0} ],
Text[ "(Subscript[e, 2])",  {0,asz + toff,0} ],
Text[ "(Subscript[e, 3])",  {0,0,asz + toff} ]
}] ;

X[k_, t_, p_] = k Sin[t] Cos[p] ;
Y[k_, t_, p_] = k Sin[t] Sin[p] ;

m[t_, p_, k_, a_, b_] = (Cos[X[k, t,p] a/2]/(X[k, t,p]^2 - Pi^2/a^2))(2Sin[Y[k, t,p] b]/Y[k, t,p]) ;
(*m[t_, p_, k_, a_, b_] =Cos[t Cos[p]]^2 ;*)

ef[t_, p_, k_, a_, b_] = (Cos[t] Cos[p] phicap[t, p] + Sin[p]thetacap[t, p]) m[t, p, k, a, b] ;


hf[t_, p_, k_, a_, b_] = (Cos[t] Cos[p] thetacap[t, p] - Sin[p]phicap[t, p]) m[t, p, k, a, b] ;
hf[θ, ϕ, k, a, b] // Simplify // TraditionalForm

(*Assumes that x ≤1, minDb is negative (like -40 say)*)
logscale[x_, minDb_] = If[ x < 10^(minDb/ 10),minDb,10 Log10[x] ]/(-minDb) + 1 ;

(*Plot a function assumed to be in the [0,1] interval on a dB scale polar plot, with radial tick markers at -10 n dB from 0 to -40.

http://mathematica.stackexchange.com/a/79364/10

example:

PolarDbPlot[ Abs[Cos[#]] & ]
*)
PolarDbPlot := PolarPlot[ 
logscale[#1[t],-40],
{t, 0, Pi},
PolarAxesOrigin->{0,1 },
PolarGridLines->{Automatic,{1/4,2/4,3/4}},
PolarAxes->True,
PlotStyle->Thick,PolarTicks->{Table[{N[Pi/6 i],ToString[30 i]<>"°"},{i,-5,6}],{{1,0},{3/4,-10},{2/4,-20},{1/4,-30},{0,-40}}}
] & ;

Manipulate[
Module[{k, c, mv,m ,minDb, r, tmax, pmax},
c = 3 10^8 ;
k = 2 Pi f 10^9/ c ;

mv= NMaximize[{Norm[ef[θ, ϕ, k, a, b]],0 <=θ <= Pi,0 <=ϕ ≤ 2Pi},{ θ, ϕ } ] ;
m = mv // First ;
tmax  = θ /. (mv // Last) ;
pmax  = ϕ /. (mv // Last) ;
kcap = rcap[ tmax, pmax ] ;
ecap = ef[ tmax, pmax, k, a, b] ;
ncap = Cross[kcap, ecap] ;

minDb = -40 ;
r = 1.2 ;

(*Column[{
(*ef[θ, ϕ, k, a, b] // Simplify // TraditionalForm,*)
Row[{"max = ",mv // First, ", @(θ,ϕ) = (", θ /. (mv // Last), ", ", ϕ /. (mv // Last), ")"}],
Row[
{
PolarDbPlot[ 
(Norm[ef[#, pmax, k, a, b]]/m )^2& ]
,
PolarDbPlot[ 
(Norm[ef[#, 1/10^10, k, a, b]]/m )^2& ]
,
PolarDbPlot[ (Norm[ef[#, Pi/2, k, a, b]]/m )^2& ]
,
PolarPlot[ Norm[ef[tmax, p, k, a, b]]/m,
{p, 0, 2 Pi},
PolarAxesOrigin->{0,1 },
PolarGridLines->Automatic,PolarAxes->True,PolarTicks->{Table[{N[Pi/6 i],ToString[30 i]<>"°"},{i,-5,6}],Automatic}
],
Plot[ Norm[ef[tmax, p, k, a, b]]/m,
{p, 0, 2 Pi}
,Ticks -> {Table[i Pi/2, {i, 0, 4}]}
, Epilog -> {Point[{pmax, Norm[ef[tmax,pmax, k, a, b]]/m}]}
]

}]
,
PolarPlot[ Norm[ef[t, pmax, k, a, b]]/m,
{t, 0,  Pi},
PolarAxesOrigin->{0,1 },
PolarGridLines->Automatic,
PolarAxes->True,
PolarTicks->{Table[{N[Pi/6 i],ToString[30 i]<>"°"},{i,-5,6}],Automatic},
PlotStyle->Thick
]
,

SphericalPlot3D[(Norm[ef[θ, ϕ, k, a, b]]/m)^2,{θ,0,Pi},{ϕ,0,2Pi},
PlotRange -> Full,
AxesLabel -> {x, y, z}
,RegionFunction->Function[{x,y,z},ncap.{x,y,z}<0]
]


(*,
 ,
SphericalPlot3D[Norm[hf[θ, ϕ, k, a, b]]/m,{θ,0,Pi},{ϕ,0,2Pi},
PlotRange -> Full,
AxesLabel -> {x, y, z}
]*)
}] (*Column*)*)
Show[SphericalPlot3D[
logscale[(Norm[ef[θ, ϕ, k, a, b]]/m)^2, -40],
{θ,0,Pi},
{ϕ,0,2Pi},
PlotRange -> {{-r, r}, {-r, r}, {-r, r}},
AxesLabel -> {x, y, z}
], axes]
],
{{a, 0.1}, 0.01, 1},
{{b, 0.1}, 0.01, 1},
{{f, 9.8, "f (GHz)"}, 1, 20}
]

p = DynamicModule[{a=0.1`,b=0.1`,f=9.8`},Module[{k$,c$,mv$,m$,minDb$,r$,tmax$,pmax$},c$=3 (10^8);k$=((2 π f (10^9))/c$);mv$=NMaximize[{Norm[ef[θ,ϕ,k$,a,b]],0≤θ≤π,0≤ϕ≤2 π},{θ,ϕ}];m$=First[mv$];tmax$=θ/. Last[mv$];pmax$=ϕ/. Last[mv$];kcap=rcap[tmax$,pmax$];ecap=ef[tmax$,pmax$,k$,a,b];ncap=kcap\[Cross]ecap;minDb$=-40;r$=1.2`;Show[SphericalPlot3D[logscale[(((((Norm[ef[θ,ϕ,k$,a,b]])/m$)))^2),-40],{θ,0,π},{ϕ,0,2 π},PlotRange->{{-r$,r$},{-r$,r$},{-r$,r$}},AxesLabel->{x,y,z}],axes]]]

<<peeters` ;
(*peeters`setGitDir[ "notes\\blogit" ] *)
peeters`setGitDir[ "figures\\ece1229" ]

peeters`exportForLatex["sphericalPolarPlotFig6", p]