%% tempbrac.m calls the FORTRAN code from the IRI for the
%% Brace and Thesis electron temperature model. For each 
%% point it returns the temperature at 300km, 400km, 1400km
%% and 3000km. For input of size m x n the output array is
%% (mxn) x 4, where the four columns correspond to the
%% above altitudes respectively. 
%%

function tmpe=tempbrac(latmatm,timl,nss)

% latmatm = geomagnetic latitude of points of interest (rads).
% timl = local time of points of interest (fractional hours). 
% nss = season in northern hemisphere (1 = spring, 2 = summer, 3 = fall, 4 = winter)
% 
% tmpe = electron temperatures at point of interest (K).

cd iri2000

%Save to file the required parameters
fid=fopen('data.txt','w');       
fprintf(fid,'%f %e %d\r\n',[latmatm(:)';timl(:)';nss(:)']);
fclose(fid);

%call Fortran subrountine in IRI directory to calculate temperatures using Brace and Thesis electron temperature model.
!pwbractp                     

%load in results from IRI
fid=fopen('res.txt','r');
tmpe=fscanf(fid,'%e %e %e %e',[4 inf]);
fclose(fid);

delete res.txt 

tmpe=tmpe';                           %required electron temperatures  
                  
cd ..
