%Uses Data from the ASOS
%The function expects a Data vector that contains the following information
%1 [Launch Altitude;
%2  AWOS Tempurature; (C)
%3  AWOS Pressure; (inHg)
%4  AWOS Density Altitude
%5  Balloon Temperature; (F)
%6  Balloon Drag Coeffient
%7  Tank Pressure; (PSI) (total delta)
%8  Tank Temperature; (F)
%9  Weight (lbs)
%  
% The next input is a vector for burst diameter in meters (Dburst)
%
% Using this data the function will generate the lift expected at the
% launch altitude and a vector of altitudes corrisponding to burst diameter

function [lift alt speed liftdelta fill] = AWOSMethod(Data,Dburst)
%% Input Conversion Layer
LaunchAlt_m = Data(1); %expected in meters, no conversion nessisary
Temp_K = Data(2)+273.15; %expecting Celcius, converting to Kelvin
Pressure_Pa = Data(3)*3386.3886667; %expecting inches of mercury, converted to Pascals
DensityAlt_m = Data(4)/3.280839895; %expecting feet, convtered to meters
BT_K = (Data(5)-32)*5/9+273.15; %expecting Fahrenheit, converting to Kelvin
C = Data(6); %unitless
tank_Pa = Data(7)*6894.75728; %expecting PSI, converting to pascals.
tankTemp_K = (Data(8)-32)*5/9+273.15; %expecting Fahrenheit, converting to Kelvin
weight_N = Data(9)/0.22480894244; %expecting pounds, converting to newtons
%Dburst = Dburst; %expected in meters, no conversion nessisary

%% Math Layer
[A T P D] =StandAtmo1976(0,288.15,101325); %Standard table
g0 = 9.80665;
MolarHelium = 4.002602;
Rbar = 8314.4598;

% I am at LaunchAlt elevation and the AWOS pressure reading is Pressure
% the pressure altitude you are at is altpressure and your pressure is
% PressureA
altadjust_m = interp1(P,A,Pressure_Pa);
PressureA_Pa = interp1(A,P,(LaunchAlt_m+altadjust_m));


vtank = 43.61/1000; %liters/1000 = cubic meters for a K sized tank
BV = tank_Pa/PressureA_Pa * BT_K/tankTemp_K * vtank;
pHe = (PressureA_Pa.*MolarHelium)./(BT_K.*Rbar); %M for He and R value
pAir = interp1(A,D,DensityAlt_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 the burst pressure is
    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
alt_m = alt_m+(altadjust_m); %adjustment for pressure altitude

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