% Function to estimate: aircraft true mach number, air density, speed of sound 
% temperature and density altitude 

% Inputs:

% alt                   = altitude (m)
% ias                  = indicated airspeed (knots)
% deltaTemp      = degrees from ISA condition (deg. Celsius)

% Outputs: 
% mtrue                 = true mach number (dim)
% a_alt                  = speed of sound at altitude (m/s)
% temp                  = air temperature degrees  (deg. Kelvin)
% dAlt                   = density altitude (meters)

% Calls: atmosphere

% Programmer: T. Trani
% date: 09/10/06

function [mtrue,a_alt,density,temp,dAlt] = densityAltitudeoffISA(deltaTemp,alt,ias)

load atmosphere.m;			% loads ISA atmospheric tables
rho_zero = 1.225;           % density at sea level 

h = atmosphere(:,1);		% vector with values of altitude
t = atmosphere(:,2);		% vector with values of temperature
r = atmosphere(:,3);		% vector with values of density
a = atmosphere(:,4);		% vector with values of speed
p = atmosphere(:,5);		% vector with values of pressure

rho 	= interp1(h,r,alt,'pchip');             % interpolates to get density
a_alt 	= interp1(h,a,alt,'pchip');             % gets speed of sound
temp 	= interp1(h,t,alt,'pchip') + deltaTemp;	% gets temperature + ISA
press 	= interp1(h,p,alt,'pchip');             % gets temperature

% Compute the pressure ratio (delta)

delta = press./p(1);         % pressure ratio (dim)
theta = temp/t(1);   % temperature ratio (dim)
sigma = delta./theta;        % density ratio (dim)
density = sigma * r(1);     % density for density altitude calculation (kg/m-m-m)

% Calculate the density altitude (meters)

dAlt = interp1(r,h,density,'pchip');

% Compute mach number and speed of sound

mtrue 	= sqrt(5 * ((rho_zero./density .*((1 + 0.2 .* (ias./661.5).^2)...
			.^3.5 -1) + 1).^0.286 -1));
        
a_alt 	= interp1(h,a,dAlt,'pchip');	% gets speed of sound

