%% temppawf107.m is designed to be called by the temppaw#.m models.
%% It calculates the required fractional percentage change in the
%% the base temperatures and gradients due to F10.7. The current
%% values should be multiplied by the output from temppawf107.m to 
%% reflex these changes. 
%% Currently it uses the results from pwtpfit8.m, based on fits to
%% FLIP profiles. It also returns the base temperature and 
%% gradient obtained from the pwtpfit8.m fit.
%%
%% 4/2/2003 - based on some results from Chris Wilford, I have decided
%% to restrict the minimum sl used by temmpawf107.m to 0.15 to stop 
%% unrealistically large gradient corrections at night. Note that the 
%% FLIP fits used to determine the polynominal coefficents had a minimum
%% value of ~0.15.
%%
%% Author: Phillip A. Webb
%% Last updated: June 9 2004                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
%%

function [t0dc,t0nc,g0dc,g0nc,t0d,t0n,g0d,g0n]=temppawf107(sl,f107)

% sl = sL parameter of point of interest.
% f107 = daily F10.7 solar flux for point of interest
% t0dc = fractional correction for day base temperature
% t0nc = fractional correction for night base temperature
% g0dc = fractional correction for day temperature gradient
% g0nc = fractional correction for night temperature gradient
% t0d = day base temperature (K)
% t0n = night base temperature (K)
% g0d = day base gradient (K/km)
% g0n = night base gradient (K/km)

%Base F10.7 value used
f107b=120;

%Set a minimum sL
sl(sl<0.15)=0.15;

%Required values from pwtpfit11.m.
load pwf107
  
%Determine the required corrections by calculating the factional percentage 
%difference between the temperature and gradient differences at F10.7=90 and
%the point of interest. 
t0dc1=polyval(pptd1,sl).*f107+polyval(pptd0,sl);
t0dc2=polyval(pptd1,sl)*f107b+polyval(pptd0,sl);
t0dc=t0dc1./t0dc2;
t0d=t0dc1;
t0nc1=polyval(pptn1,sl).*f107+polyval(pptn0,sl);
t0nc2=polyval(pptn1,sl)*f107b+polyval(pptn0,sl);
t0nc=t0nc1./t0nc2;
t0n=t0nc1;
g0dc1=polyval(ppgd1,sl).*f107+polyval(ppgd0,sl);
g0dc2=polyval(ppgd1,sl)*f107b+polyval(ppgd0,sl);
g0dc=g0dc1./g0dc2;
g0d=g0dc1;
g0nc1=polyval(ppgn1,sl).*f107+polyval(ppgn0,sl);
g0nc2=polyval(ppgn1,sl)*f107b+polyval(ppgn0,sl);
g0nc=g0nc1./g0nc2;
g0n=g0nc1;

%Add in a correction so the that day and night values agree at sL=1 (this is not
%currently done by the fit from pwtpfit8.m). Use a sine weighting for points
%above sL>0.9.
poic=sl>0.9;
if any(any(poic))
 t0dc0=t0dc;
 t0nc0=t0nc;
 g0dc0=g0dc;
 g0nc0=g0nc;
 t0d0=t0d;
 t0n0=t0n;
 g0d0=g0d;
 g0n0=g0n;
 wt=sin(((sl(poic)-0.9)/0.2).*(pi/2)).^2;
 t0dc(poic)=(1-wt).*t0dc0(poic)+wt.*t0nc0(poic);
 t0nc(poic)=(1-wt).*t0nc0(poic)+wt.*t0dc0(poic);
 g0dc(poic)=(1-wt).*g0dc0(poic)+wt.*g0nc0(poic);
 g0nc(poic)=(1-wt).*g0nc0(poic)+wt.*g0dc0(poic);
 t0d(poic)=(1-wt).*t0d0(poic)+wt.*t0n0(poic);
 t0n(poic)=(1-wt).*t0n0(poic)+wt.*t0d0(poic);
 g0d(poic)=(1-wt).*g0d0(poic)+wt.*g0n0(poic);
 g0n(poic)=(1-wt).*g0n0(poic)+wt.*g0d0(poic);
end
