%% tempiri.m calls the IRI for the given points of interest and returns 
%% the ion, electron and neutral temperatures. Note that it does not do 
%% any checks to make sure that the input data is valid.
%%
%% 12/2/2002 - upgrade to use the IRI2000, and allow the user to choose between the
%%             Brace and Thesis temperature model (original) or the newer Truhlik et al.
%%             model.
%%

function [tmpe,tmpi]=tempiri(lonmat,latmat,dismat,timmat,year,month,day,mods)

% lonmat = geographic longitude of points of interest (rads).
% latmat = geographic latitude of points of interest (rads).
% dismat = radial distance of points of interest (km).
% timmat = univerisal time (UT) of points of interest. 
% year = year matrix.
% month = month matrix.
% day = day matrix.
% mods = temperature model switch: 0 = Brace and Thesis, 1 = Truhlik et al. (scalar)
%
% tmpi = ion temperatures at point of interest (K).
% tmpe = electron temperatures at point of interest (K).

re=6371;                                %Earth radi (km)
loni=longch2(lonmat);                   %convert negative longitudes into positive values (add 2pi, required format of IRI)
lati=longch2(latmat);                   %convert negative latitudes into positive values (add 2pi, required format of IRI)
dnoi=daynum(day,month,year);            %determine day number 
altmat=dismat-re;                       %convert radial distance to altitude

%Determine the required daily F107 (needed if mods==1)
[kp,kpmin,apday,f107]=solarval(timmat,dnoi,year);

cd iri2000

%Save to file the required parameters
fid=fopen('data.txt','w');       
fprintf(fid,'%d \r\n',mods);                     
fprintf(fid,'%d %d %f %f %e %e %e\r\n',[year(:)';dnoi(:)';lati(:)';loni(:)';timmat(:)';altmat(:)';f107(:)']);
fclose(fid);

%call Fortran subrountine in IRI directory to calculate temperatures
!pwiritmp                     

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

delete res.txt 

tmpe=reshape(resm(1,:),size(altmat));                           %required electron temperatures  
tmpi=reshape(resm(2,:),size(altmat));                           %required ion temperatures
                  
cd ..
