function Hcalibrated=ALTBaron58(Hindicated,Vindicated,flap)
%function Hcalibrated=ALTBaron58(Hindicated,Vindicated,flap)
% Correct indicated altitude for instrument error and static position error.
% Data is extracted from Pilots Operating Handbook Baron 58, page 5-17 (Normal System).
% It is assumed that the Beechcraft Baron 58 has zero instrument correction.
% Therefore 
%             Hinstrumentcorrected (Hic) = Hindicated (Hi)
% Given below are the position error corrections to get Hcalibrated. 
% They are a function of Hindicated, Vindicated, and flap position.
%             Hcalibrated=f(Hindicated,Vindicated,flap)
% Hindicated is indicated pressure altitude in geopotential feet.
% Hcalibrated is calibrated pressure altitude in geopotential feet.
% Vindicated is indicated airspeed in knots.
%                   flap=0   for no flap
%                   flap=0.5 for half flap
%                   flap=1   for full flap
% For accuracy, Vindicated must be in the range 60-200 knots for the no flap case. 
% For accuracy, Vindicated must be in the range 60-130 knots for the full flap case.
% For accuracy, Hindicated must be in the range 0-20000 ft for the no flap case.
% For accuracy, Hindicated must be in the range 0-10000 ft for the full flap case.
% Use of this function for the half flap case is somewhat questionable.
% This function uses the 2D table lookup function interp2.

% Table of data for flap up case.
%         H=0                        H=20000 ft
DHz =    [-1.973                     -4.02
         -2.19241555225359         -4.73122994652406
         -2.41183110450718         -5.44245989304813
         -2.96606596701649         -6.18772796739684
          -3.5267856071964          -6.9350534895568
         -3.75419095477387                    -7.513
         -3.97479396984925         -8.09005797101449
         -4.35593103448276         -8.79295652173913
         -4.73868965517241         -9.49585507246377
                    -5.138         -10.0782193877551
                    -5.538         -10.6542397959184
                    -5.758                  -11.3827
                    -5.978                  -12.1127
                   -6.7095         -13.0204522613065
                    -7.441                    -13.93];
% Each row is for a different airspeed as indicated by Vfu below.					
Vfu=60:10:200;		% Knots			
Hx=[0,20000]; % a row vector, geopotential feet
Vy=Vfu';            % a column vector
% End of table data

% Keep Vindicated and Hindicated in the range of the table
Vnoflap=Vindicated;
kk=find(Vindicated<60);  Vnoflap(kk)=60;
kk=find(Vindicated>200); Vnoflap(kk)=200;
Hnoflap=Hindicated;
kk=find(Hindicated<0);       Hnoflap(kk)=0;
kk=find(Hindicated>20000);   Hnoflap(kk)=20000;
dh=interp2(Hx,Vy,DHz,Hnoflap,Vnoflap);
HcalibratedNoFlap=Hindicated+dh;

% Table of data for flap down case.
%         H=0                      H=10000 ft 
DHz =   [     17.47                      24.3
          10.7023943661972          14.9361386138614
           3.9260534124629          5.57227722772278
         -3.56949554896143                  -4.88785
         -11.0650445103858                 -15.37035
         -19.3646153846154                   -26.745
         -27.6773076923077         -38.1290196078431
                    -36.11                    -49.08];
% Each row is for a different airspeed as indicated by Vfd below.
Vfd=60:10:130;  % Knots
Hx=[0,10000];   % a row vector, geopotential feet
Vy=Vfd';       % a column vector
% End of table data

% Keep Vindicated and Hindicated in the range of the table
Vflap=Vindicated;
kk=find(Vindicated<60);  Vflap(kk)=60;
kk=find(Vindicated>130); Vflap(kk)=130;
Hflap=Hindicated;
kk=find(Hindicated<0);       Hflap(kk)=0;
kk=find(Hindicated>10000);   Hflap(kk)=10000;
dh=interp2(Hx,Vy,DHz,Hflap,Vflap);
HcalibratedFlap=Hindicated+dh;

Hcalibrated=(1-flap).*HcalibratedNoFlap+flap.*HcalibratedFlap;
