%Uses Local On-Site Measurements
%The function expects a Data vector that contains the following information
%1 [Launch Altitude;
%2  Local Tempurature; (F)
%3  Local Pressure; (mbar)
%4  Balloon Temperature; (F)
%5  Balloon Drag Coeffinent 
%6  Tank Pressure; (PSI) (total delta)
%7  Tank Temperature; (F)
%8  Weight (lbs)
%  
% The next input is a vector for burst diameter in meters
%
% Using this data the function will generate the lift expected at the
% launch altitude and a vector of altitudes corrisponding to burst diameter
% And ascent rate in meters per second
function [lift alt speed liftdelta fill] = LocalMethod(Data,Dburst)
%% Input Conversion Layer
LaunchAlt_m = Data(1); %expected in meters, no conversion nessisary
Temp_K = (Data(2)-32)*5/9+273.15; %expecting Fahrenheit, converting to Kelvin
Pressure_Pa = Data(3)*100; %expecting milibars, converted to Pascals
BT_K = (Data(4)-32)*5/9+273.15; %expecting Fahrenheit, converting to Kelvin
C = Data(5); %unitless
tank_Pa = Data(6)*6894.75728; %expecting PSI, converting to pascals.
tankTemp_K = (Data(7)-32)*5/9+273.15; %expecting Fahrenheit, converting to Kelvin
weight_N = Data(8)/0.22480894244; %expecting pounds, converting to Newtons
%Dburst = Dburst; %expected in meters, no conversion nessisary


%% Math Layer
[A T P D] = StandAtmo1976(LaunchAlt_m,Temp_K,Pressure_Pa);
g0 = 9.80665; %m/s^2
MolarHelium = 4.002602;
Rbar = 8314.4598;

vtank = 43.61/1000; %liters/1000 = cubic meters for a K sized tank
BV = tank_Pa/Pressure_Pa * BT_K/tankTemp_K * vtank;
pHe = (Pressure_Pa.*MolarHelium)./(BT_K.*Rbar); %M for He and R value
pAir = interp1(A,D,LaunchAlt_m); %Getting air density from table
lift = BV.*(pAir-pHe).*g0;
Vneeded = weight_N/(g0*(pAir-pHe));
%what to fill to
fill = (Vneeded/vtank)*(tankTemp_K/BT_K)*Pressure_Pa;

%I am not happy with the for loops in this section, but the interp1
%function does not accpet vector inputs for the third input
Tburst = 273*ones(length(Dburst),1); %pre alocated the size of the intermediate values
Vburst = zeros(length(Dburst),1);
Pburst = zeros(length(Dburst),1);
alt_m = zeros(length(Dburst),1);

for m = 1:5; %iterations, max effective number any more doesn't change answers much

    for n = 1:length(Dburst)
        Vburst(n) = 4./3.*pi.*(Dburst(n)./2).^3; %cubic meters, volume based on burst diameter assuming spherical balloon
        Pburst(n) = Pressure_Pa.*BV./Vburst(n)*(Tburst(n)/BT_K); %using ideal gas law, pressure at
    end

    for n = 1:length(Dburst)
        alt_m(n) = interp1(P,A,Pburst(n));%interpolating from the table altitdue from burst pressure
    end

    for n = 1:length(Dburst)
        Tburst(n) = interp1(A,T,alt_m(n)); %getting the temperature from the altitude it burstrs at
    end

end

liftdelta = lift-weight_N;
%A lot of other preditiction programs seems to assume that the acsent rate
%is constant, so I will assume that the acsent rate at launch is constant
area = pi*(BV./pi*3/4).^(2/3);
speed = sqrt((2*(liftdelta))/(pAir*area*C));

%% Output Conversion Layer
lift = lift*0.22480894244; %converting the newtons of lift to pounds
alt_ft = alt_m*3.280839895; %converting altitude in meters to feet
%speed = speed %no conversion needed
liftdelta = liftdelta*0.22480894244; %converting the newtons to pounds
fill = fill/6894.75728; %converting from pascals to PSI

%% Output Additions
alt = [alt_ft alt_m];

end 