function [ TT ] = GLOB7S( context, P, K )
%GLOB7S VERSION OF GLOBE FOR LOWER ATMOSPHERE
%      FUNCTION GLOB7S(P)
%-----------------------------------------------------------------------
%      VERSION OF GLOBE FOR LOWER ATMOSPHERE 10/26/99
%      P
%      K column index into P matrix
%      GLOB7S temperature (K)
%-----------------------------------------------------------------------

%      REAL LONG
%      COMMON/LPOLY/PLG(9,4),CTLOC,STLOC,C2TLOC,S2TLOC,C3TLOC,S3TLOC,
%     $ IYR,DAY,DF,DFA,APD,APDF,APT(4),LONG
%      COMMON/CSW/SW(25),ISW,SWC(25)
%c      DIMENSION P(1),T(14)
%      DIMENSION P(150),T(14)
%      SAVE
  persistent PSET DAYL P14 P18 P32 P39 CD14 CD18 CD32 CD39 CD14dSec CD18dSec CD32dSec CD39dSec;
  if isempty(PSET)
    PSET = 2.;
    DAYL = -1.;
    P14 = -1000.;
    P18 = -1000.;
    P32 = -1000.;
    P39 = -1000.;
    CD14 = 0.0;
    CD18 = 0.0;
    CD32 = 0.0;
    CD39 = 0.0;
    CD14dSec = 0.0;
    CD18dSec = 0.0;
    CD32dSec = 0.0;
    CD39dSec = 0.0;
  end
  T = zeros(CIRA.TERDIURNAL_SW,CIRA.DerLast);
  TT = zeros(1,CIRA.DerLast);
  %       CONFIRM PARAMETER SET
  if P(100, K) == 0
    P(100, K)=PSET;
  end
  if P(100, K) ~= PSET
    if context.KONSOL > 0
      fprintf(context.KONSOL,'WRONG PARAMETER SET FOR GLOB7S %10.1f ~= %10.1f\n',...
        PSET,P(100, K));
    end
    return;
  end
  DAYdSec = 1.0/86400.0;
  if context.DAY ~= DAYL || P32 ~= P(32, K)
    CD32=cos(context.DR*(context.DAY-P(32, K)));
    CD32dSec=-sin(context.DR*(context.DAY-P(32, K)))*context.DR*DAYdSec;
  end
  if context.DAY ~= DAYL || P18 ~= P(18, K)
    CD18=cos(2.*context.DR*(context.DAY-P(18, K)));
    CD18dSec=-sin(2.*context.DR*(context.DAY-P(18, K)))*2.*context.DR*DAYdSec;
  end
  if context.DAY ~= DAYL || P14 ~= P(14, K)
    CD14=cos(context.DR*(context.DAY-P(14, K)));
    CD14dSec=-sin(context.DR*(context.DAY-P(14, K)))*context.DR*DAYdSec;
  end
  if context.DAY ~= DAYL || P39 ~= P(39, K)
    CD39=cos(2.*context.DR*(context.DAY-P(39, K)));
    CD39dSec=-sin(2.*context.DR*(context.DAY-P(39, K)))*2.*context.DR*DAYdSec;
  end
  DAYL=context.DAY;
  P32=P(32, K);
  P18=P(18, K);
  P14=P(14, K);
  P39=P(39, K);
  PLG = context.PLG;
  PLGdLat = context.PLGdLat;
  SW = context.SW;
  SWC = context.SWC;
  %
  %       F10.7
  T(CIRA.F107_SW,CIRA.Der0)=P(22, K)*context.DFA;
  %       TIME INDEPENDENT
  order = 1;
  T(CIRA.TIME_INDEP_SW,CIRA.Der0)=  P(2, K)*PLG(3,order) ...
                                   +P(3, K)*PLG(5,order) ...
                                  +P(23, K)*PLG(7,order) ...
                                  +P(27, K)*PLG(2,order) ...
                                  +P(15, K)*PLG(4,order) ...
                                  +P(60, K)*PLG(6,order);
  T(CIRA.TIME_INDEP_SW,CIRA.DerLat)=  P(2, K)*PLGdLat(3,order) ...
                                     +P(3, K)*PLGdLat(5,order) ...
                                    +P(23, K)*PLGdLat(7,order) ...
                                    +P(27, K)*PLGdLat(2,order) ...
                                    +P(15, K)*PLGdLat(4,order) ...
                                    +P(60, K)*PLGdLat(6,order);
  %       SYMMETRICAL ANNUAL
  T(CIRA.SYM_ANNUAL_SW,CIRA.Der0)=(P(19, K) ...
                                  +P(48, K)*PLG(3,order) ...
                                  +P(30, K)*PLG(5,order))*CD32;
  T(CIRA.SYM_ANNUAL_SW,CIRA.DerLat)=(P(48, K)*PLGdLat(3,order) ...
                                    +P(30, K)*PLGdLat(5,order))*CD32;
  T(CIRA.SYM_ANNUAL_SW,CIRA.DerSec)=(P(19, K) ...
                                  +P(48, K)*PLG(3,order) ...
                                  +P(30, K)*PLG(5,order))*CD32dSec;
  %       SYMMETRICAL SEMIANNUAL
  T(CIRA.SYM_SEMIANNUAL_SW,CIRA.Der0)=(P(16, K) ...
                                      +P(17, K)*PLG(3,order) ...
                                      +P(31, K)*PLG(5,order))*CD18;
  T(CIRA.SYM_SEMIANNUAL_SW,CIRA.DerLat)=(P(17, K)*PLGdLat(3,order) ...
                                        +P(31, K)*PLGdLat(5,order))*CD18;
  T(CIRA.SYM_SEMIANNUAL_SW,CIRA.DerSec)=(P(16, K) ...
                                      +P(17, K)*PLG(3,order) ...
                                      +P(31, K)*PLG(5,order))*CD18dSec;
  %       ASYMMETRICAL ANNUAL
  T(CIRA.ASYM_ANNUAL_SW,CIRA.Der0)=(P(10, K)*PLG(2,order) ...
                                   +P(11, K)*PLG(4,order) ...
                                   +P(21, K)*PLG(6,order))*CD14;
  T(CIRA.ASYM_ANNUAL_SW,CIRA.DerLat)=(P(10, K)*PLGdLat(2,order) ...
                                     +P(11, K)*PLGdLat(4,order) ...
                                     +P(21, K)*PLGdLat(6,order))*CD14;
  T(CIRA.ASYM_ANNUAL_SW,CIRA.DerSec)=(P(10, K)*PLG(2,order) ...
                                   +P(11, K)*PLG(4,order) ...
                                   +P(21, K)*PLG(6,order))*CD14dSec;
  %       ASYMMETRICAL SEMIANNUAL
  T(CIRA.ASYM_SEMIANNUAL_SW,CIRA.Der0)=(P(38, K)*PLG(2,order))*CD39;
  T(CIRA.ASYM_SEMIANNUAL_SW,CIRA.DerLat)=(P(38, K)*PLGdLat(2,order))*CD39;
  T(CIRA.ASYM_SEMIANNUAL_SW,CIRA.DerSec)=(P(38, K)*PLG(2,order))*CD39dSec;
  %        DIURNAL
  T71 = CD14*SWC(CIRA.ASYM_ANNUAL_SW);
  T71dSec = CD14dSec*SWC(CIRA.ASYM_ANNUAL_SW);
  if SW(CIRA.DIURNAL_SW) ~= 0
    order = 2;
    T(CIRA.DIURNAL_SW,CIRA.Der0) = ((P(4, K)*PLG(2,order) ...
                                  + P(12, K)*PLG(3,order)*T71 ...
                                   + P(5, K)*PLG(4,order))*context.CTLOC ...
                                   +(P(7, K)*PLG(2,order) ...
                                  + P(13, K)*PLG(3,order)*T71 ...
                                   + P(8, K)*PLG(4,order))*context.STLOC);
    T(CIRA.DIURNAL_SW,CIRA.DerLat) = ((P(4, K)*PLGdLat(2,order) ...
                                    + P(12, K)*PLGdLat(3,order)*T71 ...
                                     + P(5, K)*PLGdLat(4,order))*context.CTLOC ...
                                     +(P(7, K)*PLGdLat(2,order) ...
                                    + P(13, K)*PLGdLat(3,order)*T71 ...
                                     + P(8, K)*PLGdLat(4,order))*context.STLOC);
    T(CIRA.DIURNAL_SW,CIRA.DerSec) = ((P(12, K)*PLG(3,order)*T71dSec)*context.CTLOC ...
                                   +(P(13, K)*PLG(3,order)*T71dSec)*context.STLOC);
    T(CIRA.DIURNAL_SW,CIRA.DerSTL) = ((P(4, K)*PLG(2,order) ...
                                  + P(12, K)*PLG(3,order)*T71 ...
                                   + P(5, K)*PLG(4,order))*context.CTLOCdSTL ...
                                   +(P(7, K)*PLG(2,order) ...
                                  + P(13, K)*PLG(3,order)*T71 ...
                                   + P(8, K)*PLG(4,order))*context.STLOCdSTL);
  end
  %        SEMIDIURNAL
  if SW(CIRA.SEMIDIURNAL_SW) ~= 0
    order = 3;
    T(CIRA.SEMIDIURNAL_SW,CIRA.Der0) = ((P( 6, K)*PLG(3,order) ...
                                       + P(24, K)*PLG(4,order)*T71 ...
                                       + P(42, K)*PLG(5,order) ...
                                       + P(36, K)*PLG(6,order)*T71)*context.C2TLOC ...
                                       +(P( 9, K)*PLG(3,order) ...
                                       + P(34, K)*PLG(4,order)*T71 ...
                                       + P(43, K)*PLG(5,order) ...
                                       + P(37, K)*PLG(6,order)*T71)*context.S2TLOC);
    T(CIRA.SEMIDIURNAL_SW,CIRA.DerLat) = ((P( 6, K)*PLGdLat(3,order) ...
                                         + P(24, K)*PLGdLat(4,order)*T71 ...
                                         + P(42, K)*PLGdLat(5,order) ...
                                         + P(36, K)*PLGdLat(6,order)*T71)*context.C2TLOC ...
                                         +(P( 9, K)*PLGdLat(3,order) ...
                                         + P(34, K)*PLGdLat(4,order)*T71 ...
                                         + P(43, K)*PLGdLat(5,order) ...
                                         + P(37, K)*PLGdLat(6,order)*T71)*context.S2TLOC);
    T(CIRA.SEMIDIURNAL_SW,CIRA.DerSec) = ((P(24, K)*PLG(4,order)*T71dSec ...
                                       + P(36, K)*PLG(6,order)*T71dSec)*context.C2TLOC ...
                                       +(P(34, K)*PLG(4,order)*T71dSec ...
                                       + P(37, K)*PLG(6,order)*T71dSec)*context.S2TLOC);
    T(CIRA.SEMIDIURNAL_SW,CIRA.DerSTL) = ((P( 6, K)*PLG(3,order) ...
                                       + P(24, K)*PLG(4,order)*T71 ...
                                       + P(42, K)*PLG(5,order) ...
                                       + P(36, K)*PLG(6,order)*T71)*context.C2TLOCdSTL ...
                                       +(P( 9, K)*PLG(3,order) ...
                                       + P(34, K)*PLG(4,order)*T71 ...
                                       + P(43, K)*PLG(5,order) ...
                                       + P(37, K)*PLG(6,order)*T71)*context.S2TLOCdSTL);
  end
  %        TERDIURNAL
  if SW(CIRA.TERDIURNAL_SW) ~= 0
    order = 4;
    T(CIRA.TERDIURNAL_SW,CIRA.Der0) = P(40, K)*PLG(4,order)*context.S3TLOC ...
                                     +P(41, K)*PLG(4,order)*context.C3TLOC;
    T(CIRA.TERDIURNAL_SW,CIRA.DerLat) = P(40, K)*PLGdLat(4,order)*context.S3TLOC ...
                                       +P(41, K)*PLGdLat(4,order)*context.C3TLOC;
    T(CIRA.TERDIURNAL_SW,CIRA.DerSTL) = P(40, K)*PLG(4,order)*context.S3TLOCdSTL ...
                                     +P(41, K)*PLG(4,order)*context.C3TLOCdSTL;
  end
  %       MAGNETIC ACTIVITY
  if SW(CIRA.DAILY_AP_SW) ~= 0
    s = SWC(CIRA.TIME_INDEP_SW);
    if SW(CIRA.DAILY_AP_SW) == 1
      T(CIRA.DAILY_AP_SW,CIRA.Der0)=context.APDF*(P(33, K)+P(46, K)*PLG(3,1)*s);
      T(CIRA.DAILY_AP_SW,CIRA.DerLat)=context.APDF*(P(46, K)*PLGdLat(3,1)*s);
    elseif SW(CIRA.DAILY_AP_SW) == -1
      T(CIRA.DAILY_AP_SW,CIRA.Der0)=(P(51, K)*context.APT(1,CIRA.Der0) ...
                          +P(97, K)*PLG(3,1)*context.APT(1,CIRA.Der0)*s);
      T(CIRA.DAILY_AP_SW,CIRA.DerLat)=(P(51, K)*context.APT(1,CIRA.DerLat) ...
                          +P(97, K)*PLGdLat(3,1)*context.APT(1,CIRA.Der0)*s ...
                          +P(97, K)*PLG(3,1)*context.APT(1,CIRA.DerLat)*s);
    end
  end
  if SW(CIRA.ALL_UTLONG_EFF_SW) ~= 0 && ...
     SW(CIRA.LONG_SW) ~= 0 && ...
     context.LONG > -1000.
    c82 = cos(context.DR*(context.DAY-P(82, K)));
    c87 = cos(2.*context.DR*(context.DAY-P(87, K)));
    c85 = cos(context.DR*(context.DAY-P(85, K)));
    c89 = cos(2.*context.DR*(context.DAY-P(89, K)));
    c82dSec = -sin(context.DR*(context.DAY-P(82, K)))*context.DR*DAYdSec;
    c87dSec = -sin(2.*context.DR*(context.DAY-P(87, K)))*2.*context.DR*DAYdSec;
    c85dSec = -sin(context.DR*(context.DAY-P(85, K)))*context.DR*DAYdSec;
    c89dSec = -sin(2.*context.DR*(context.DAY-P(89, K)))*2.*context.DR*DAYdSec;
    fd = (1.+PLG(2,1)*( ...
            P(81, K)*SWC(CIRA.ASYM_ANNUAL_SW)*c82 ...
           +P(86, K)*SWC(CIRA.ASYM_SEMIANNUAL_SW)*c87) ...
           +P(84, K)*SWC(CIRA.SYM_ANNUAL_SW)*c85 ...
           +P(88, K)*SWC(CIRA.SYM_SEMIANNUAL_SW)*c89);
    fddLat = PLGdLat(2,1)*( ...
            P(81, K)*SWC(CIRA.ASYM_ANNUAL_SW)*c82 ...
           +P(86, K)*SWC(CIRA.ASYM_SEMIANNUAL_SW)*c87);
    fddSec = PLG(2,1)*( ...
            P(81, K)*SWC(CIRA.ASYM_ANNUAL_SW)*c82dSec ...
           +P(86, K)*SWC(CIRA.ASYM_SEMIANNUAL_SW)*c87dSec) ...
           +P(84, K)*SWC(CIRA.SYM_ANNUAL_SW)*c85dSec ...
           +P(88, K)*SWC(CIRA.SYM_SEMIANNUAL_SW)*c89dSec;
    ca =  P(75, K)*PLG(2,2) ...
         +P(65, K)*PLG(3,2) ...
         +P(76, K)*PLG(4,2) ...
         +P(66, K)*PLG(5,2) ...
         +P(77, K)*PLG(6,2) ...
         +P(67, K)*PLG(7,2);
    cadLat =  P(65, K)*PLGdLat(3,2) ...
             +P(66, K)*PLGdLat(5,2) ...
             +P(67, K)*PLGdLat(7,2) ...
             +P(75, K)*PLGdLat(2,2) ...
             +P(76, K)*PLGdLat(4,2) ...
             +P(77, K)*PLGdLat(6,2);
    sa =  P(91, K)*PLG(3,2) ...
         +P(92, K)*PLG(5,2) ...
         +P(93, K)*PLG(7,2) ...
         +P(78, K)*PLG(2,2) ...
         +P(79, K)*PLG(4,2) ...
         +P(80, K)*PLG(6,2);
    sadLat =  P(91, K)*PLGdLat(3,2) ...
             +P(92, K)*PLGdLat(5,2) ...
             +P(93, K)*PLGdLat(7,2) ...
             +P(78, K)*PLGdLat(2,2) ...
             +P(79, K)*PLGdLat(4,2) ...
             +P(80, K)*PLGdLat(6,2);
    %        LONGITUDINAL
    cl = cos(CIRA.DGTR*context.LONG);
    sl = sin(CIRA.DGTR*context.LONG);
    T(CIRA.LONG_SW,CIRA.Der0)= fd*(ca*cl+sa*sl);
    T(CIRA.LONG_SW,CIRA.DerLat)= fddLat*(ca*cl+sa*sl) ...
      +fd*(cadLat*cl+sadLat*sl);
    T(CIRA.LONG_SW,CIRA.DerLon)= fd*(-ca*sl+sa*cl)*CIRA.DGTR;
    T(CIRA.LONG_SW,CIRA.DerSec)= fddSec*(ca*cl+sa*sl);
  end
  for d=CIRA.Der0:CIRA.DerLast
    TT(1,d)=0.;
    for I=CIRA.F107_SW:CIRA.TERDIURNAL_SW
      TT(1,d)=TT(1,d)+abs(SW(I))*T(I,d);
    end
  end

end

