function [ TINF,context,T ] = GLOBE7( context, YRD,SEC,LAT,LONG,TLOC,F107A,F107,AP,P,m )
%GLOBE7 CALCULATE G(L) FUNCTION
%     FUNCTION GLOBE7(YRD,SEC,LAT,LONG,TLOC,F107A,F107,AP,P)
%-----------------------------------------------------------------------
%       CALCULATE G(L) FUNCTION 
%       Upper Thermosphere Parameters
%       YRD YYYDDD year, day in year concatenated integer
%       SEC seconds in day
%       LAT geodetic latitude (degrees north)
%       LONG longitude (degrees east)
%       TLOC solar time (hours)
%       F107A average flux at 10.7m wavelength
%       F107 flux at 10.7m wavelength
%       AP Ap index
%       P
%-----------------------------------------------------------------------

%      REAL LAT, LONG
%c      DIMENSION P(1),SV(25),AP(1)
%      DIMENSION P(150),SV(25),AP(7)
%      COMMON/TTEST/TINF,GB,ROUT,T(15)
%      COMMON/CSW/SW(25),ISW,SWC(25)
%      COMMON/LPOLY/PLG(9,4),CTLOC,STLOC,C2TLOC,S2TLOC,C3TLOC,S3TLOC,
%     $ IYR,DAY,DF,DFA,APD,APDF,APT(4),XLONG
%      SAVE
  persistent DAYL P14 P18 P32 P39 CD14 CD18 CD32 CD39 CD14dSec CD18dSec CD32dSec CD39dSec;
  if isempty(DAYL)
    DAYL = -1.;
    P32 = -1000.0;
    P18 = -1000.0;
    P14 = -1000.0;
    P39 = -1000.0;
    CD32 = 0.0;
    CD18 = 0.0;
    CD14 = 0.0;
    CD39 = 0.0;
    CD32dSec = 0.0;
    CD18dSec = 0.0;
    CD14dSec = 0.0;
    CD39dSec = 0.0;
  end
  if context.ISW ~= CIRA.ISWinitialized
    context = context.TSELEC(CIRA.allSwitchesOn);
  end
  T = zeros(CIRA.TERDIURNAL_SW,CIRA.DerLast);
  SW = context.SW;
  SWC = context.SWC;
  if SW(CIRA.DAILY_AP_SW) < 0
    SW9=-1.;
  else
    SW9 = 1;
  end
  IYR = floor(double(YRD)/CIRA.YRDSHIFT);
  context.DAY = double(YRD) - IYR*CIRA.YRDSHIFT + double(SEC)/86400.0;
  DAYdSec = 1.0/86400.0;
  context.LONG = double(LONG);
  %      Eq. A22 (remainder of code)
  if context.XL ~= LAT
    [PLG,PLGdLat] = CIRA.LegendrePolynomials( LAT, CIRA.DGTR, CIRA.maxLegDeg, CIRA.maxLegOrd );
    context.PLG = PLG;
    context.PLGdLat = PLGdLat;
    context.XL=double(LAT);
  else % already calculated
    PLG = context.PLG;
    PLGdLat = context.PLGdLat;
  end
  if context.TLL ~= TLOC
    if SW(CIRA.DIURNAL_SW) ~= 0 || ...
       SW(CIRA.SEMIDIURNAL_SW) ~= 0 || ...
       SW(CIRA.TERDIURNAL_SW) ~= 0
      [ PCH, PCHdT ] = CIRA.ChebyshevPolynomials( TLOC, context.HR, 3 );
      context.STLOC = -PCHdT(1+1,1)/context.HR;
      context.S2TLOC = PCH(1+1,2)*context.STLOC;
      context.S3TLOC = PCH(2+1,2)*context.STLOC;
      context.CTLOC  = PCH(1+1,1);
      context.C2TLOC = PCH(2+1,1);
      context.C3TLOC = PCH(3+1,1);
      context.STLOCdSTL = PCH(1+1,1)*context.HR;
      context.S2TLOCdSTL = PCHdT(1+1,2)*context.STLOC + PCH(1+1,2)*context.STLOCdSTL;
      context.S3TLOCdSTL = PCHdT(2+1,2)*context.STLOC + PCH(2+1,2)*context.STLOCdSTL;
      context.CTLOCdSTL  = PCHdT(1+1,1);
      context.C2TLOCdSTL = PCHdT(2+1,1);
      context.C3TLOCdSTL = PCHdT(3+1,1);
      context.TLL = double(TLOC);
    end
  end
  if context.DAY ~= DAYL || P(14,m) ~= P14
    CD14=cos(context.DR*(context.DAY-P(14,m)));
    CD14dSec=-sin(context.DR*(context.DAY-P(14,m)))*context.DR*DAYdSec;
  end
  if context.DAY ~= DAYL || P(18,m) ~= P18
    CD18=cos(2.*context.DR*(context.DAY-P(18,m)));
    CD18dSec=-sin(2.*context.DR*(context.DAY-P(18,m)))*2.*context.DR*DAYdSec;
  end
  if context.DAY ~= DAYL || P(32,m) ~= P32
    CD32=cos(context.DR*(context.DAY-P(32,m)));
    CD32dSec=-sin(context.DR*(context.DAY-P(32,m)))*context.DR*DAYdSec;
  end
  if context.DAY ~= DAYL || P(39,m) ~= P39
    CD39=cos(2.*context.DR*(context.DAY-P(39,m)));
    CD39dSec=-sin(2.*context.DR*(context.DAY-P(39,m)))*2.*context.DR*DAYdSec;
  end
  DAYL = context.DAY;
  P14 = P(14,m);
  P18 = P(18,m);
  P32 = P(32,m);
  P39 = P(39,m);
  
  % F10.7 EFFECT
  DF = double(F107) - double(F107A);
  DFA = double(F107A) - context.defaultF107;
  context.DFA = DFA;
  SW1 = CIRA.F107_SW;
  % solar EUV dependence
  T(SW1,CIRA.Der0) = (P(20,m)*(1.+ P(60,m)*DFA) + P(21,m)*DF)*DF ...
                    + (P(22,m) + P(30,m)*DFA)*DFA;
  F1 = 1. + (P(48,m)*DFA+(P(20,m)+P(21,m)*DF)*DF)*SWC(SW1);
  F2 = 1. + (P(50,m)*DFA+(P(20,m)+P(21,m)*DF)*DF)*SWC(SW1);
  % TIME INDEPENDENT
  order = 0+1;
  T(CIRA.TIME_INDEP_SW,CIRA.Der0) = P(27,m)*PLG(2,order) ...
             + (P(2,m)+ P(15,m)*DFA*SWC(SW1))*PLG(3,order) ...
                                  + P( 3,m)*PLG(5,order) ...
                                  + P(23,m)*PLG(7,order);
  T(CIRA.TIME_INDEP_SW,CIRA.DerLat) = P(27,m)*PLGdLat(2,order) ...
              + (P(2,m) + P(15,m)*DFA*SWC(SW1))*PLGdLat(3,order) ...
                                     + P(3,m)*PLGdLat(5,order) ...
                                    + P(23,m)*PLGdLat(7,order);
  % SYMMETRICAL ANNUAL
  T(CIRA.SYM_ANNUAL_SW,CIRA.Der0) = P(19,m) * CD32;
  T(CIRA.SYM_ANNUAL_SW,CIRA.DerSec) = P(19,m) * CD32dSec;
  %        SYMMETRICAL SEMIANNUAL
  T(CIRA.SYM_SEMIANNUAL_SW,CIRA.Der0) = (P(16,m)+P(17,m)*PLG(3,order))*CD18;
  T(CIRA.SYM_SEMIANNUAL_SW,CIRA.DerLat) = (P(17,m)*PLGdLat(3,order))*CD18;
  T(CIRA.SYM_SEMIANNUAL_SW,CIRA.DerSec) = (P(16,m)+P(17,m)*PLG(3,order))*CD18dSec;
  %        ASYMMETRICAL ANNUAL
  T(CIRA.ASYM_ANNUAL_SW,CIRA.Der0) =  F1*(P(10,m)*PLG(2,order) ...
                                         +P(11,m)*PLG(4,order))*CD14;
  T(CIRA.ASYM_ANNUAL_SW,CIRA.DerLat) =  F1*(P(10,m)*PLGdLat(2,order) ...
                                           +P(11,m)*PLGdLat(4,order))*CD14;
  T(CIRA.ASYM_ANNUAL_SW,CIRA.DerSec) =  F1*(P(10,m)*PLG(2,order) ...
                                           +P(11,m)*PLG(4,order))*CD14dSec;
  %         ASYMMETRICAL SEMIANNUAL
  T(CIRA.ASYM_SEMIANNUAL_SW,CIRA.Der0) = P(38,m)*PLG(2,order)*CD39;
  T(CIRA.ASYM_SEMIANNUAL_SW,CIRA.DerLat) = P(38,m)*PLGdLat(2,order)*CD39;
  T(CIRA.ASYM_SEMIANNUAL_SW,CIRA.DerSec) = P(38,m)*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 = 1+1;
    T(CIRA.DIURNAL_SW,CIRA.Der0) = F2* ...
       ( (P(4,m)*PLG(2,order) ...
       + P(12,m)*PLG(3,order)*T71 ...
        + P(5,m)*PLG(4,order) ...
       + P(28,m)*PLG(6,order))*context.CTLOC ...
       + (P(7,m)*PLG(2,order) ...
       + P(13,m)*PLG(3,order)*T71 ...
        + P(8,m)*PLG(4,order) ...
       + P(29,m)*PLG(6,order))*context.STLOC);
    T(CIRA.DIURNAL_SW,CIRA.DerLat) = F2* ...
       ((P(4,m)*PLGdLat(2,order) ...
      + P(12,m)*PLGdLat(3,order)*T71 ...
       + P(5,m)*PLGdLat(4,order) ...
      + P(28,m)*PLGdLat(6,order))*context.CTLOC ...
      + (P(7,m)*PLGdLat(2,order) ...
      + P(13,m)*PLGdLat(3,order)*T71 ...
       + P(8,m)*PLGdLat(4,order) ...
      + P(29,m)*PLGdLat(6,order))*context.STLOC);
    T(CIRA.DIURNAL_SW,CIRA.DerSec) = F2* ...
       ( (P(12,m)*PLG(3,order)*T71dSec)*context.CTLOC ...
       + (P(13,m)*PLG(3,order)*T71dSec)*context.STLOC);
    T(CIRA.DIURNAL_SW,CIRA.DerSTL) = F2* ...
       ( (P(4,m)*PLG(2,order) ...
       + P(12,m)*PLG(3,order)*T71 ...
        + P(5,m)*PLG(4,order) ...
       + P(28,m)*PLG(6,order))*context.CTLOCdSTL ...
       + (P(7,m)*PLG(2,order) ...
       + P(13,m)*PLG(3,order)*T71 ...
        + P(8,m)*PLG(4,order) ...
       + P(29,m)*PLG(6,order))*context.STLOCdSTL);
  end
  %        SEMIDIURNAL
  if SW(CIRA.SEMIDIURNAL_SW) ~= 0
    order = 2+1;
    T(CIRA.SEMIDIURNAL_SW,CIRA.Der0) = F2* ...
       ( (P(6,m)*PLG(3,order) ...
       + P(24,m)*PLG(4,order)*T71 ...
       + P(42,m)*PLG(5,order) ...
       + P(36,m)*PLG(6,order)*T71)*context.C2TLOC ...
        +(P(9,m)*PLG(3,order) ...
       + P(34,m)*PLG(4,order)*T71 ...
       + P(43,m)*PLG(5,order) ...
       + P(37,m)*PLG(6,order)*T71)*context.S2TLOC);
    T(CIRA.SEMIDIURNAL_SW,CIRA.DerLat) = F2* ...
       ( (P(6,m)*PLGdLat(3,order) ...
       + P(24,m)*PLGdLat(4,order)*T71 ...
       + P(42,m)*PLGdLat(5,order) ...
       + P(36,m)*PLGdLat(6,order)*T71)*context.C2TLOC ...
        +(P(9,m)*PLGdLat(3,order) ...
       + P(34,m)*PLGdLat(4,order)*T71 ...
       + P(43,m)*PLGdLat(5,order) ...
       + P(37,m)*PLGdLat(6,order)*T71)*context.S2TLOC);
    T(CIRA.SEMIDIURNAL_SW,CIRA.DerSec) = F2* ...
       ( (P(24,m)*PLG(4,order)*T71dSec ...
       + P(36,m)*PLG(6,order)*T71dSec)*context.C2TLOC ...
        +(P(34,m)*PLG(4,order)*T71dSec ...
       + P(37,m)*PLG(6,order)*T71dSec)*context.S2TLOC);
    T(CIRA.SEMIDIURNAL_SW,CIRA.DerSTL) = F2* ...
       ( (P(6,m)*PLG(3,order) ...
       + P(24,m)*PLG(4,order)*T71 ...
       + P(42,m)*PLG(5,order) ...
       + P(36,m)*PLG(6,order)*T71)*context.C2TLOCdSTL ...
        +(P(9,m)*PLG(3,order) ...
       + P(34,m)*PLG(4,order)*T71 ...
       + P(43,m)*PLG(5,order) ...
       + P(37,m)*PLG(6,order)*T71)*context.S2TLOCdSTL);
  end
  %        TERDIURNAL
  if SW(CIRA.TERDIURNAL_SW) ~= 0
    order = 3+1;
    T(CIRA.TERDIURNAL_SW,CIRA.Der0) = F2* ...
       ((P(40,m)*PLG(4,order) ...
       +(P(94,m)*PLG(5,order) ...
       + P(47,m)*PLG(7,order))*T71)*context.S3TLOC ...
       +(P(41,m)*PLG(4,order) ...
       +(P(95,m)*PLG(5,order) ...
       + P(49,m)*PLG(7,order))*T71)*context.C3TLOC);
    T(CIRA.TERDIURNAL_SW,CIRA.DerLat) = F2* ...
       ((P(40,m)*PLGdLat(4,order) ...
       +(P(94,m)*PLGdLat(5,order) ...
       + P(47,m)*PLGdLat(7,order))*T71)*context.S3TLOC ...
       +(P(41,m)*PLGdLat(4,order) ...
       +(P(95,m)*PLGdLat(5,order) ...
       + P(49,m)*PLGdLat(7,order))*T71)*context.C3TLOC);
    T(CIRA.TERDIURNAL_SW,CIRA.DerSec) = F2* ...
       (((P(94,m)*PLG(5,order) ...
        + P(47,m)*PLG(7,order))*T71dSec)*context.S3TLOC ...
       +((P(95,m)*PLG(5,order) ...
        + P(49,m)*PLG(7,order))*T71dSec)*context.C3TLOC);
    T(CIRA.TERDIURNAL_SW,CIRA.DerSTL) = F2* ...
       ((P(40,m)*PLG(4,order) ...
       +(P(94,m)*PLG(5,order) ...
       + P(47,m)*PLG(7,order))*T71)*context.S3TLOCdSTL ...
       +(P(41,m)*PLG(4,order) ...
       +(P(95,m)*PLG(5,order) ...
       + P(49,m)*PLG(7,order))*T71)*context.C3TLOCdSTL);
  end
  %          MAGNETIC ACTIVITY BASED ON DAILY AP

  if SW9 ~= -1.
    order = 0+1;
    order2 = 1+1;
    APD=double(AP(CIRA.DAILY_AP))-context.defaultAp;
    P44=P(44,m);
    P45=P(45,m);
    if P44 == 0
      APDF = APD;
    elseif P44 < 0
      P44 = 1.0e-5;
      APDF = APD+(P45-1.)*(P44*APD^2/2);
    else
      APDF = APD+(P45-1.)*(APD+(exp(-P44*APD)-1.)/P44);
    end
    context.APDF = APDF;
    if SW(CIRA.DAILY_AP_SW) ~= 0
      c125 = SWC(CIRA.DIURNAL_SW)*cos(context.HR*(context.TLL-P(125,m)));
      c125dSTL = -SWC(CIRA.DIURNAL_SW)*sin(context.HR*(context.TLL-P(125,m)))*context.HR;
      T(CIRA.DAILY_AP_SW,CIRA.Der0)=APDF*(P(33,m) ...
         + P(101,m)*PLG(2,order)*T71 ...
          + P(46,m)*PLG(3,order) ...
         + P(102,m)*PLG(4,order)*T71 ...
          + P(35,m)*PLG(5,order) ...
         + P(103,m)*PLG(6,order)*T71 ...
        + (P(122,m)*PLG(2,order2) ...
        +  P(123,m)*PLG(4,order2) ...
        +  P(124,m)*PLG(6,order2))*c125);
      T(CIRA.DAILY_AP_SW,CIRA.DerLat)=APDF*( ...
         + P(101,m)*PLGdLat(2,order)*T71 ...
          + P(46,m)*PLGdLat(3,order) ...
         + P(102,m)*PLGdLat(4,order)*T71 ...
          + P(35,m)*PLGdLat(5,order) ...
         + P(103,m)*PLGdLat(6,order)*T71 ...
        + (P(122,m)*PLGdLat(2,order2) ...
        +  P(123,m)*PLGdLat(4,order2) ...
        +  P(124,m)*PLGdLat(6,order2))*c125);
      T(CIRA.DAILY_AP_SW,CIRA.DerSec)=APDF*( ...
         + P(101,m)*PLG(2,order)*T71dSec ...
         + P(102,m)*PLG(4,order)*T71dSec ...
         + P(103,m)*PLG(6,order)*T71dSec);
      T(CIRA.DAILY_AP_SW,CIRA.DerSTL)=APDF*( ...
          (P(122,m)*PLG(2,order2) ...
        +  P(123,m)*PLG(4,order2) ...
        +  P(124,m)*PLG(6,order2))*c125dSTL);
    end
  elseif P(52,m) ~= 0
    if context.XL < 0
      sn = -1;
    else
      sn = 1;
    end
    a = 0.5*60.*360.*abs(P(52,m));
    b = 1.+P(139,m)*(context.defaultLatitude-sn*context.XL);
    db = -P(139,m)*sn;
    f = a/b;
    if f < 1.0e-5
      EXP1=.99999;
      EXP1dLat = 0.0;
      %EXP1dLat = EXP1*f*db/b;
    else
      EXP1 = exp(-f);
      EXP1dLat = EXP1*f*db/b;
    end
    [APT1,APT1dEX]=CIRA.SG0(EXP1, P(25,m), P(26,m), AP, context.defaultAp);
    APT1dLat = APT1dEX*EXP1dLat;
    context.APT(1,CIRA.Der0)=APT1;
    context.APT(1,CIRA.DerLat)=APT1dLat;
    %      context.APT(2,CIRA.Der0)=CIRA.SG2(EXP1, P, AP)
    %      context.APT(3,CIRA.Der0)=CIRA.SG0(EXP2, P, AP)
    %      context.APT(4,CIRA.Der0)=CIRA.SG2(EXP2, P, AP)
    if SW(CIRA.DAILY_AP_SW) ~= 0
      order = 0+1;
      order2 = 1+1;
      a = P(51,m) ...
        + P(126,m)*PLG(2,order)*T71 ...
         + P(97,m)*PLG(3,order) ...
        + P(127,m)*PLG(4,order)*T71 ...
         + P(55,m)*PLG(5,order) ...
        + P(128,m)*PLG(6,order)*T71;
      adLat = P(126,m)*PLGdLat(2,order)*T71 ...
             + P(97,m)*PLGdLat(3,order) ...
            + P(127,m)*PLGdLat(4,order)*T71 ...
             + P(55,m)*PLGdLat(5,order) ...
            + P(128,m)*PLGdLat(6,order)*T71;
      adSec = P(126,m)*PLG(2,order)*T71dSec ...
            + P(127,m)*PLG(4,order)*T71dSec ...
            + P(128,m)*PLG(6,order)*T71dSec;
      ca = (P(129,m)*PLG(2,order2) ...
          + P(130,m)*PLG(4,order2) ...
          + P(131,m)*PLG(6,order2))*SWC(CIRA.DIURNAL_SW);
      cadLat = (P(129,m)*PLGdLat(2,order2) ...
              + P(130,m)*PLGdLat(4,order2) ...
              + P(131,m)*PLGdLat(6,order2))*SWC(CIRA.DIURNAL_SW);
      c132 = cos(context.HR*(context.TLL-P(132,m)));
      c132dSTL = -sin(context.HR*(context.TLL-P(132,m)))*context.HR;
      T(CIRA.DAILY_AP_SW,CIRA.Der0) = APT1*(a+ca*c132);
      T(CIRA.DAILY_AP_SW,CIRA.DerLat) = APT1dLat*(a+ca*c132)...
        +APT1*(adLat+cadLat*c132);
      T(CIRA.DAILY_AP_SW,CIRA.DerSec) = APT1*adSec;
      T(CIRA.DAILY_AP_SW,CIRA.DerSTL) = APT1*(ca*c132dSTL);
    end
  end
  if SW(CIRA.ALL_UTLONG_EFF_SW) ~= 0 && context.LONG > -1000.
    %        LONGITUDINAL
    if SW(CIRA.LONG_SW) ~= 0
      order = 1+1;
      fd = 1. + P(81,m)*DFA*SWC(CIRA.F107_SW);
      ca = (P(104,m) + P(110,m)*T71)*PLG(2,order) ...
                         + P(65,m)*PLG(3,order) ...
         + (P(105,m) + P(111,m)*T71)*PLG(4,order) ...
                         + P(66,m)*PLG(5,order) ...
         + (P(106,m) + P(112,m)*T71)*PLG(6,order) ...
                         + P(67,m)*PLG(7,order);
      cadLat = P(65,m)*PLGdLat(3,order) ...
             + P(66,m)*PLGdLat(5,order) ...
             + P(67,m)*PLGdLat(7,order) ...
            + P(104,m)*PLGdLat(2,order) ...
            + P(105,m)*PLGdLat(4,order) ...
            + P(106,m)*PLGdLat(6,order) ...
           + (P(110,m)*PLGdLat(2,order) ...
            + P(111,m)*PLGdLat(4,order) ...
            + P(112,m)*PLGdLat(6,order))*T71;
      cadSec = (P(110,m)*T71dSec)*PLG(2,order) ...
             + (P(111,m)*T71dSec)*PLG(4,order) ...
             + (P(112,m)*T71dSec)*PLG(6,order);
      sa = P(91,m)*PLG(3,order) ...
         + P(92,m)*PLG(5,order) ...
         + P(93,m)*PLG(7,order) ...
        + P(107,m)*PLG(2,order) ...
        + P(108,m)*PLG(4,order) ...
        + P(109,m)*PLG(6,order) ...
        +(P(113,m)*PLG(2,order) ...
        + P(114,m)*PLG(4,order) ...
        + P(115,m)*PLG(6,order))*T71;
      sadLat = P(91,m)*PLGdLat(3,order) ...
             + P(92,m)*PLGdLat(5,order) ...
             + P(93,m)*PLGdLat(7,order) ...
            + P(107,m)*PLGdLat(2,order) ...
            + P(108,m)*PLGdLat(4,order) ...
            + P(109,m)*PLGdLat(6,order) ...
            +(P(113,m)*PLGdLat(2,order) ...
            + P(114,m)*PLGdLat(4,order) ...
            + P(115,m)*PLGdLat(6,order))*T71;
      sadSec = (P(113,m)*PLG(2,order) ...
              + P(114,m)*PLG(4,order) ...
              + P(115,m)*PLG(6,order))*T71dSec;
      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)= fd*(cadLat*cl+sadLat*sl);
      T(CIRA.LONG_SW,CIRA.DerLon)= fd*(-ca*sl+sa*cl)*CIRA.DGTR;
      T(CIRA.LONG_SW,CIRA.DerSec)= fd*(cadSec*cl+sadSec*sl);
    end
    %        UT AND MIXED UT,LONGITUDE
    if SW(CIRA.UT_MIXED_UTLONG_SW) ~= 0
      order = 1;
      a = (1. + P(96,m)*PLG(2,order))*(1. + P(82,m)*DFA*SWC(CIRA.F107_SW));
      adLat = (P(96,m)*PLGdLat(2,order))*(1. + P(82,m)*DFA*SWC(CIRA.F107_SW));
      b = 1.+P(120,m)*PLG(2,order)*T71;
      bdLat = (P(120,m)*PLGdLat(2,order)*T71);
      bdSec = P(120,m)*PLG(2,order)*T71dSec;
      c = P(69,m)*PLG(2,order) ...
        + P(70,m)*PLG(4,order) ...
        + P(71,m)*PLG(6,order);
      cdLat = P(69,m)*PLGdLat(2,order) ...
            + P(70,m)*PLGdLat(4,order) ...
            + P(71,m)*PLGdLat(6,order);
      c72 = cos(context.SR*(double(SEC)-P(72,m)));
      c72dSec = -sin(context.SR*(double(SEC)-P(72,m)))*context.SR;
      T(CIRA.UT_MIXED_UTLONG_SW,CIRA.Der0)=a*b*c*c72;
      T(CIRA.UT_MIXED_UTLONG_SW,CIRA.DerLat)=(adLat*b*c+a*bdLat*c+a*b*cdLat)*c72;
      T(CIRA.UT_MIXED_UTLONG_SW,CIRA.DerLon) = 0.0;
      T(CIRA.UT_MIXED_UTLONG_SW,CIRA.DerSec)=a*bdSec*c*c72 + a*b*c*c72dSec;
      order = 3;
      fd = SWC(CIRA.LONG_SW)*(1.+P(138,m)*DFA*SWC(CIRA.F107_SW));
      ca = P(77,m)*PLG(4,order) ...
          +P(78,m)*PLG(6,order) ...
          +P(79,m)*PLG(8,order);
      cadLat = P(77,m)*PLGdLat(4,order) ...
              +P(78,m)*PLGdLat(6,order) ...
              +P(79,m)*PLGdLat(8,order);
      c80 = cos(context.SR*(double(SEC)-P(80,m))+2.*CIRA.DGTR*context.LONG);
      s80 = sin(context.SR*(double(SEC)-P(80,m))+2.*CIRA.DGTR*context.LONG);
      c80dSec = -s80*context.SR;
      c80dLon = -s80*2.*CIRA.DGTR;
      T(CIRA.UT_MIXED_UTLONG_SW,CIRA.Der0)=T(CIRA.UT_MIXED_UTLONG_SW,CIRA.Der0) ...
        +fd*ca*c80;
      T(CIRA.UT_MIXED_UTLONG_SW,CIRA.DerLat)=T(CIRA.UT_MIXED_UTLONG_SW,CIRA.DerLat) ...
        +fd*cadLat*c80;
      T(CIRA.UT_MIXED_UTLONG_SW,CIRA.DerLon)=T(CIRA.UT_MIXED_UTLONG_SW,CIRA.DerLon) ...
        +fd*ca*c80dLon;
      T(CIRA.UT_MIXED_UTLONG_SW,CIRA.DerSec)=T(CIRA.UT_MIXED_UTLONG_SW,CIRA.DerSec) ...
        +fd*ca*c80dSec;
    end
    %        UT,LONGITUDE MAGNETIC ACTIVITY
    if SW(CIRA.MIXED_APUTLONG_SW) ~= 0
      if SW9 ~= -1.
        fd = APDF*SWC(CIRA.LONG_SW);
        ca1 = (P( 61,m)*PLG(3,2) ...
             + P( 62,m)*PLG(5,2) ...
             + P( 63,m)*PLG(7,2))*(1. + P(121,m)*PLG(2,1));
        ca1dLat = (P( 61,m)*PLGdLat(3,2) ...
                 + P( 62,m)*PLGdLat(5,2) ...
                 + P( 63,m)*PLGdLat(7,2))*(1. + P(121,m)*PLG(2,1)) ...
           +(P( 61,m)*PLG(3,2)+ P( 62,m)*PLG(5,2) + P( 63,m)*PLG(7,2)) ...
           *(P(121,m)*PLGdLat(2,1));
        ca2 = (P(116,m)*PLG(2,2) ...
              +P(117,m)*PLG(4,2) ...
              +P(118,m)*PLG(6,2))*T71;
        ca2dLat = (P(116,m)*PLGdLat(2,2) ...
                  +P(117,m)*PLGdLat(4,2) ...
                  +P(118,m)*PLGdLat(6,2))*T71;
        ca2dSec = (P(116,m)*PLG(2,2) ...
              +P(117,m)*PLG(4,2) ...
              +P(118,m)*PLG(6,2))*T71dSec;
        ct = (P( 84,m)*PLG(2,1) ...
            +P( 85,m)*PLG(4,1) ...
            +P( 86,m)*PLG(6,1))*APDF*SWC(CIRA.UT_MIXED_UTLONG_SW);
        ctdLat = (P( 84,m)*PLGdLat(2,1) ...
            +P( 85,m)*PLGdLat(4,1) ...
            +P( 86,m)*PLGdLat(6,1))*APDF*SWC(CIRA.UT_MIXED_UTLONG_SW);
        c64 = cos(CIRA.DGTR*(context.LONG-P( 64,m)));
        s64 = sin(CIRA.DGTR*(context.LONG-P( 64,m)));
        c119 = cos(CIRA.DGTR*(context.LONG-P(119,m)));
        s119 = sin(CIRA.DGTR*(context.LONG-P(119,m)));
        c76 = cos(context.SR*(double(SEC)-P( 76,m)));
        c76dSec = -sin(context.SR*(double(SEC)-P( 76,m)))*context.SR;
        T(CIRA.MIXED_APUTLONG_SW,CIRA.Der0)=fd*ca1*c64+fd*ca2*c119+ct*c76;
        T(CIRA.MIXED_APUTLONG_SW,CIRA.DerLat)=fd*ca1dLat*c64+fd*ca2dLat*c119+ctdLat*c76;
        T(CIRA.MIXED_APUTLONG_SW,CIRA.DerLon)=-fd*(ca1*s64+ca2*s119)*CIRA.DGTR;
        T(CIRA.MIXED_APUTLONG_SW,CIRA.DerSec)=fd*ca2dSec*c119+ct*c76dSec;
      elseif P(52,m) ~= 0
        fd = APT1*SWC(CIRA.LONG_SW);
        fddLat = APT1dLat*SWC(CIRA.LONG_SW);
        ca1 = (P(53,m)*PLG(3,2) ...
              +P(99,m)*PLG(5,2) ...
              +P(68,m)*PLG(7,2))*(1.+P(133,m)*PLG(2,1));
        ca1dLat = (P(53,m)*PLGdLat(3,2) ...
                  +P(99,m)*PLGdLat(5,2) ...
                  +P(68,m)*PLGdLat(7,2))*(1.+P(133,m)*PLG(2,1)) ...
            +(P(53,m)*PLG(3,2)+P(99,m)*PLG(5,2)+P(68,m)*PLG(7,2))*(P(133,m)*PLGdLat(2,1));
        ca2 = (P(134,m)*PLG(2,2) ...
              +P(135,m)*PLG(4,2) ...
              +P(136,m)*PLG(6,2))*T71;
        ca2dLat = (P(134,m)*PLGdLat(2,2) ...
                  +P(135,m)*PLGdLat(4,2) ...
                  +P(136,m)*PLGdLat(6,2))*T71;
        ca2dSec = (P(134,m)*PLG(2,2) ...
              +P(135,m)*PLG(4,2) ...
              +P(136,m)*PLG(6,2))*T71dSec;
        ct = (P(56,m)*PLG(2,1) ...
             +P(57,m)*PLG(4,1) ...
             +P(58,m)*PLG(6,1))*APT1*SWC(CIRA.UT_MIXED_UTLONG_SW);
        ctdLat = (P(56,m)*PLGdLat(2,1) ...
                 +P(57,m)*PLGdLat(4,1) ...
                 +P(58,m)*PLGdLat(6,1))*APT1*SWC(CIRA.UT_MIXED_UTLONG_SW) ...
                +(P(56,m)*PLG(2,1) ...
                 +P(57,m)*PLG(4,1) ...
                 +P(58,m)*PLG(6,1))*APT1dLat*SWC(CIRA.UT_MIXED_UTLONG_SW);
        c98 = cos(CIRA.DGTR*(context.LONG-P(98,m)));
        s98 = sin(CIRA.DGTR*(context.LONG-P(98,m)));
        c137 = cos(CIRA.DGTR*(context.LONG-P(137,m)));
        s137 = sin(CIRA.DGTR*(context.LONG-P(137,m)));
        c59 = cos(context.SR*(double(SEC)-P(59,m)));
        c59dSec = -sin(context.SR*(double(SEC)-P(59,m)))*context.SR;
        T(CIRA.MIXED_APUTLONG_SW,CIRA.Der0)=fd*ca1*c98+fd*ca2*c137+ct*c59;
        T(CIRA.MIXED_APUTLONG_SW,CIRA.DerLat)= ...
           (fddLat*ca1+fd*ca1dLat)*c98+(fddLat*ca2+fd*ca2dLat)*c137+ctdLat*c59;
        T(CIRA.MIXED_APUTLONG_SW,CIRA.DerLon)= -fd*(ca1*s98+ca2*s137)*CIRA.DGTR;
        T(CIRA.MIXED_APUTLONG_SW,CIRA.DerSec)=fd*ca2dSec*c137+ct*c59dSec;
      end
    end
  end
  %  PARMS NOT USED: 83, 90,100,140-150
  TINF = zeros(1,CIRA.DerLast);
  TINF(1,CIRA.Der0)=P(31,m);
  for d=CIRA.Der0+1:CIRA.DerLast
    TINF(1,d)=0.0;
  end
  for I = CIRA.F107_SW:CIRA.TERDIURNAL_SW
    for d=CIRA.Der0:CIRA.DerLast
      TINF(1,d) = TINF(1,d) + abs(SW(I))*T(I,d);
    end
  end
end
