%% tehfn.m (teh function) generates the electron temperatures from
%% Titheridge eqn 13 for the given values of T0 and G0 at the required
%% heights. Note that the user can input the base height, ie not
%% always set to 400km as in Titheridge paper.
%%

function tmpe=tehfn(t0,g0,altmat,lsh,h0)

% t0 = T0 base temperature value at h0 altitude (K)
% g0 = G0 temperature gradient at h0 altitude (K/km)
% altmat = altitude matrix of point of interest (km)
% lsh = L-shell of point of interest
% h0 = base height (km)
% tmpe = electron temperature at dismat

re=6371;                                                         %Earth radi (km)
r0=1+h0/re;                                                      %r0 parameter
rh=1+altmat/re;                                                  %rh parameter
hlog=log10(altmat/h0);
bh=(0.05./(2*lsh-r0)).*(88+hlog.*(10.5-hlog));                   %eqn 13b
dhr=((lsh-1)*re-h0)./r0.^2-((lsh-1)*re-altmat)./rh.^2;           %eqn 13 bracket term
dhr2=bh.*(g0./t0).*dhr;
dhr2(dhr2<-1)=-1;                                                %minimum temperature catch (corresponds to temp of 0)
tmpe=t0.*(1+dhr2).^(2/7);                                        %average electro temperature, eqn 13

