function [ TE ] = TEBA( DIPL,SLT,NS )
%TEBA ELECTRON TEMPERATURES TE(1) TO TE(4) AT ALTITUDES
%
%      SUBROUTINE TEBA(DIPL,SLT,NS,TE) 
% CALCULATES ELECTRON TEMPERATURES TE(1) TO TE(4) AT ALTITUDES
% 300, 400, 1400 AND 3000 KM FOR DIP-LATITUDE DIPL/DEG AND 
% LOCAL SOLAR TIME SLT/H USING THE BRACE-THEIS-MODELS (J. ATMOS.
% TERR. PHYS. 43, 1317, 1981); NS IS SEASON IN NORTHERN
% HEMISOHERE: IS=1 SPRING, IS=2 SUMMER ....
% ALSO CALCULATED ARE THE TEMPERATURES AT 400 KM ALTITUDE FOR
% MIDNIGHT (TE(5)) AND NOON (TE(6)).   

%      DIMENSION C(4,2,81),A(82),TE(6)
%      COMMON    /CONST/UMR      /const1/humr,dumr
  persistent C NI NL NM;
  if isempty(C)
    NI = 81;
    NL = 8;
    NM = 8;
    C11 = [ ...
       .3100E+1,-.3215E-2, .2440E+0,-.4613E-3,-.1711E-1, .2605E-1, ...
      -.9546E-1, .1794E-1, .1270E-1, .2791E-1, .1536E-1,-.6629E-2, ...
      -.3616E-2, .1229E-1, .4147E-3, .1447E-2,-.4453E-3,-.1853E+0, ...
      -.1245E-1,-.3675E-1, .4965E-2, .5460E-2, .8117E-2,-.1002E-1, ...
       .5466E-3,-.3087E-1,-.3435E-2,-.1107E-3, .2199E-2, .4115E-3, ...
       .6061E-3, .2916E-3,-.6584E-1, .4729E-2,-.1523E-2, .6689E-3, ...
       .1031E-2, .5398E-3,-.1924E-2,-.4565E-1, .7244E-2,-.8543E-4, ...
       .1052E-2,-.6696E-3,-.7492E-3, .4405E-1, .3047E-2, .2858E-2, ...
      -.1465E-3, .1195E-2,-.1024E-3, .4582E-1, .8749E-3, .3011E-3, ...
       .4473E-3,-.2782E-3, .4911E-1,-.1016E-1, .2700E-2,-.9304E-3, ...
      -.1202E-2, .2210E-1, .2566E-2,-.1220E-3, .3987E-3,-.5744E-1, ...
       .4408E-2,-.3497E-2, .8300E-3,-.3536E-1,-.8813E-2, .2423E-2, ...
      -.2994E-1,-.1929E-2,-.5268E-3,-.2228E-1, .3385E-2, ...
       .4130E-1, .4876E-2, .2692E-1, .1684E-2];
    C12 = [.313654E1,.6796E-2,.181413,.8564E-1, ...
      -.32856E-1,-.3508E-2,-.1438E-1,-.2454E-1,.2745E-2,.5284E-1, ...
      .1136E-1,-.1956E-1,-.5805E-2,.2801E-2,-.1211E-2,.4127E-2, ...
      .2909E-2,-.25751,-.37915E-2,-.136E-1,-.13225E-1,.1202E-1, ...
      .1256E-1,-.12165E-1,.1326E-1,-.7123E-1,.5793E-3,.1537E-2, ...
      .6914E-2,-.4173E-2,.1052E-3,-.5765E-3,-.4041E-1,-.1752E-2, ...
      -.542E-2,-.684E-2,.8921E-3,-.2228E-2,.1428E-2,.6635E-2,-.48045E-2, ...
      -.1659E-2,-.9341E-3,.223E-3,-.9995E-3,.4285E-1,-.5211E-3, ...
      -.3293E-2,.179E-2,.6435E-3,-.1891E-3,.3844E-1,.359E-2,-.8139E-3, ...
      -.1996E-2,.2398E-3,.2938E-1,.761E-2,.347655E-2,.1707E-2,.2769E-3, ...
      -.157E-1,.983E-3,-.6532E-3,.929E-4,-.2506E-1,.4681E-2,.1461E-2, ...
      -.3757E-5,-.9728E-2,.2315E-2,.6377E-3,-.1705E-1,.2767E-2, ...
      -.6992E-3,-.115E-1,-.1644E-2,.3355E-2,-.4326E-2,.2035E-1,.2985E-1];
    C21 = [.3136E1,.6498E-2,.2289,.1859E-1,-.3328E-1, ...
      -.4889E-2,-.3054E-1,-.1773E-1,-.1728E-1,.6555E-1,.1775E-1, ...
      -.2488E-1,-.9498E-2,.1493E-1,.281E-2,.2406E-2,.5436E-2,-.2115, ...
      .7007E-2,-.5129E-1,-.7327E-2,.2402E-1,.4772E-2,-.7374E-2, ...
      -.3835E-3,-.5013E-1,.2866E-2,.2216E-2,.2412E-3,.2094E-2,.122E-2 ...
      ,-.1703E-3,-.1082,-.4992E-2,-.4065E-2,.3615E-2,-.2738E-2, ...
      -.7177E-3,.2173E-3,-.4373E-1,-.375E-2,.5507E-2,-.1567E-2, ...
      -.1458E-2,-.7397E-3,.7903E-1,.4131E-2,.3714E-2,.1073E-2, ...
      -.8991E-3,.2976E-3,.2623E-1,.2344E-2,.5608E-3,.4124E-3,.1509E-3, ...
      .5103E-1,.345E-2,.1283E-2,.7238E-3,-.3464E-4,.1663E-1,-.1644E-2, ...
      -.71E-3,.5281E-3,-.2729E-1,.3556E-2,-.3391E-2,-.1787E-3,.2154E-2, ...
      .6476E-2,-.8282E-3,-.2361E-1,.9557E-3,.3205E-3,-.2301E-1, ...
      -.854E-3,-.1126E-1,-.2323E-2,-.8582E-2,.2683E-1];
    C22 = [.3144E1,.8571E-2,.2539,.6937E-1,-.1667E-1, ...
      .2249E-1,-.4162E-1,.1201E-1,.2435E-1,.5232E-1,.2521E-1,-.199E-1, ...
      -.7671E-2,.1264E-1,-.1551E-2,-.1928E-2,.3652E-2,-.2019,.5697E-2, ...
      -.3159E-1,-.1451E-1,.2868E-1,.1377E-1,-.4383E-2,.1172E-1, ...
      -.5683E-1,.3593E-2,.3571E-2,.3282E-2,.1732E-2,-.4921E-3,-.1165E-2 ...
      ,-.1066,-.1892E-1,.357E-2,-.8631E-3,-.1876E-2,-.8414E-4,.2356E-2, ...
      -.4259E-1,-.322E-2,.4641E-2,.6223E-3,-.168E-2,-.1243E-3,.7393E-1, ...
      -.3143E-2,-.2362E-2,.1235E-2,-.1551E-2,.2099E-3,.2299E-1,.5301E-2 ...
      ,-.4306E-2,-.1303E-2,.7687E-5,.5305E-1,.6642E-2,-.1686E-2, ...
      .1048E-2,.5958E-3,.4341E-1,-.8819E-4,-.333E-3,-.2158E-3,-.4106E-1 ...
      ,.4191E-2,.2045E-2,-.1437E-3,-.1803E-1,-.8072E-3,-.424E-3, ...
      -.26E-1,-.2329E-2,.5949E-3,-.1371E-1,-.2188E-2,.1788E-1, ...
      .6405E-3,.5977E-2,.1333E-1];
    C31 = [.3372E1,.1006E-1,.1436,.2023E-2,-.5166E-1, ...
      .9606E-2,-.5596E-1,.4914E-3,-.3124E-2,-.4713E-1,-.7371E-2, ...
      -.4823E-2,-.2213E-2,.6569E-2,-.1962E-3,.3309E-3,-.3908E-3, ...
      -.2836,.7829E-2,.1175E-1,.9919E-3,.6589E-2,.2045E-2,-.7346E-2 ...
      ,-.89E-3,-.347E-1,-.4977E-2,.147E-2,-.2823E-5,.6465E-3, ...
      -.1448E-3,.1401E-2,-.8988E-1,-.3293E-4,-.1848E-2,.4439E-3, ...
      -.1263E-2,.317E-3,-.6227E-3,.1721E-1,-.199E-2,-.4627E-3, ...
      .2897E-5,-.5454E-3,.3385E-3,.8432E-1,-.1951E-2,.1487E-2, ...
      .1042E-2,-.4788E-3,-.1276E-3,.2373E-1,.2409E-2,.5263E-3, ...
      .1301E-2,-.4177E-3,.3974E-1,.1418E-3,-.1048E-2,-.2982E-3, ...
      -.3396E-4,.131E-1,.1413E-2,-.1373E-3,.2638E-3,-.4171E-1, ...
      -.5932E-3,-.7523E-3,-.6883E-3,-.2355E-1,.5695E-3,-.2219E-4, ...
      -.2301E-1,-.9962E-4,-.6761E-3,.204E-2,-.5479E-3,.2591E-1, ...
      -.2425E-2,.1583E-1,.9577E-2];
    C32 = [.3367E1,.1038E-1,.1407,.3622E-1,-.3144E-1, ...
      .112E-1,-.5674E-1,.3219E-1,.1288E-2,-.5799E-1,-.4609E-2, ...
      .3252E-2,-.2859E-3,.1226E-1,-.4539E-2,.1310E-2,-.5603E-3, ...
      -.311,-.1268E-2,.1539E-1,.3146E-2,.7787E-2,-.143E-2,-.482E-2 ...
      ,.2924E-2,-.9981E-1,-.7838E-2,-.1663E-3,.4769E-3,.4148E-2, ...
      -.1008E-2,-.979E-3,-.9049E-1,-.2994E-2,-.6748E-2,-.9889E-3, ...
      .1488E-2,-.1154E-2,-.8412E-4,-.1302E-1,-.4859E-2,-.7172E-3, ...
      -.9401E-3,.9101E-3,-.1735E-3,.7055E-1,.6398E-2,-.3103E-2, ...
      -.938E-3,-.4E-3,-.1165E-2,.2713E-1,-.1654E-2,.2781E-2, ...
      -.5215E-5,.2258E-3,.5022E-1,.95E-2,.4147E-3,.3499E-3, ...
      -.6097E-3,.4118E-1,.6556E-2,.3793E-2,-.1226E-3,-.2517E-1, ...
      .1491E-3,.1075E-2,.4531E-3,-.9012E-2,.3343E-2,.3431E-2, ...
      -.2519E-1,.3793E-4,.5973E-3,-.1423E-1,-.132E-2,-.6048E-2, ...
      -.5005E-2,-.115E-1,.2574E-1];
    C41 = [.3574E1,.0,.7537E-1,.0,-.8459E-1, ...
      0.,-.294E-1,0.,.4547E-1,-.5321E-1,0.,.4328E-2,0.,.6022E-2, ...
      .0,-.9168E-3,.0,-.1768,.0,.294E-1,.0,.5902E-3,.0,-.9047E-2, ...
      .0,-.6555E-1,.0,-.1033E-2,.0,.1674E-2,.0,.2802E-3,-.6786E-1 ...
      ,.0,.4193E-2,.0,-.6448E-3,.0,.9277E-3,-.1634E-1,.0,-.2531E-2 ...
      ,.0,.193E-4,.0,.528E-1,.0,.2438E-2,.0,-.5292E-3,.0,.1555E-1 ...
      ,.0,-.3259E-2,.0,-.5998E-3,.3168E-1,.0,.2382E-2,.0,-.4078E-3 ...
      ,.2312E-1,.0,.1481E-3,.0,-.1885E-1,.0,.1144E-2,.0,-.9952E-2 ...
      ,.0,-.551E-3,-.202E-1,.0,-.7283E-4,-.1272E-1,.0,.2224E-2, ...
      .0,-.251E-2,.2434E-1];
    C42 = [.3574E+1,-.5639E-2, .7094E-1, ...
          -.3347E-1,-.8610E-1,-.2877E-1,-.3154E-1,-.2847E-2, .1235E-1, ...
          -.5966E-1,-.3236E-2, .3795E-3,-.8634E-3, .3377E-2,-.1071E-3, ...
          -.2151E-2,-.4057E-3,-.1783E+0, .1260E-1, .2835E-1,-.2420E-2, ...
           .3002E-2,-.4684E-2,-.6756E-2,-.7493E-3,-.6147E-1,-.5636E-2, ...
          -.1234E-2,-.1613E-2,-.6353E-4,-.2503E-3,-.1729E-3,-.7148E-1, ...
           .5326E-2, .4006E-2, .6484E-3,-.1046E-3,-.6034E-3,-.9435E-3, ...
          -.2385E-2, .6853E-2, .1510E-2, .1319E-2, .9049E-4,-.1999E-3, ...
           .3976E-1, .2802E-2,-.1030E-2, .5599E-3,-.4791E-3,-.8460E-4, ...
           .2683E-1, .4270E-2, .5911E-3, .2987E-3,-.2080E-3, .1396E-1, ...
          -.1922E-2,-.1063E-2, .3803E-3, .1343E-3, .1771E-1,-.1038E-2, ...
          -.4645E-3,-.2481E-3,-.2251E-1,-.2900E-2,-.3977E-3,-.5160E-3, ...
          -.8079E-2,-.1528E-2, .3060E-3,-.1582E-1,-.8536E-3, .1565E-3, ...
          -.1252E-1, .2319E-3, .4311E-2, .1024E-2, .1296E-5, .1790E-1];
    for J=1:NI
      C(1,1,J) = C11(J);
      C(1,2,J) = C12(J);
      C(2,1,J) = C21(J);
      C(2,2,J) = C22(J);
      C(3,1,J) = C31(J);
      C(3,2,J) = C32(J);
      C(4,1,J) = C41(J);
      C(4,2,J) = C42(J);
    end
  end
  TE = zeros(6,1);
  if NS < IRI2012.FALL % SPRING, SUMMER
    IS=NS;
  elseif NS > IRI2012.FALL % WINTER
    IS=2;
    DIPL=-DIPL;
  else % FALL
    IS=1;
  end
  COLAT=IRI2012.UMR*(90.-DIPL);
  AZ=IRI2012.humr*SLT;
  A = IRI2012.SPHARM(NL,NM,COLAT,AZ);
  if IS == 2
     KEND=3;
  else
     KEND=4;
  end
  for K=1:KEND      
    STE=0.;
    for I=1:NI
      STE=STE+A(I)*C(K,IS,I);
    end
    TE(K)=10.^STE;
  end
  if IS == 2
     DIPL=-DIPL;
     COLAT=IRI2012.UMR*(90.-DIPL);
     A = IRI2012.SPHARM(NL,NM,COLAT,AZ);
     STE=0.;
     for I=1:NI
       STE=STE+A(I)*C(4,2,I);
     end
     TE(4)=10.^STE;
  end

  %---------- TEMPERATURE AT 400KM AT MIDNIGHT AND NOON
  for J=1:2
    STE=0.;
    AZ=IRI2012.humr*(J-1)*12.;
    A = IRI2012.SPHARM(NL,NM,COLAT,AZ);
    for I=1:NI
      STE=STE+A(I)*C(2,IS,I);
    end
    TE(J+4)=10.^STE;
  end

end

