%Burst at 92,600 ft
%28252m and 27978m from APIRS
%Bach's Box Data: 
%Time,       Temp,  Humidity, Dewpoint, Pressure, Altitude, Shifted Alt, Voltage, Calc Voltage, Dust Intensity, Lidar distance
%13:52:18,  -8.62,  4.20,     -44.78,   18.14,    23712.75, 23741.16,    158.00,  0.77,         0.03,           58486321040
%amtemp, amhumidity, dew_point, bmptemp, bmppressure, bmpaltitude, bmprealaltitude, bmppressuresealvl, pressure mpl, temperature mpl, time,        date,      fix, fixquality, latitude,   longitude,  speed, angle, GPSaltitude, satellites ,distance ,bearing ,free memory 
%-14.80, 2.20,       -56.42,    -13.70,  12.19,       25200.02,    25241.28,        12.34,             11.08,        -12.25,          13:51:7:984, 4/24/2016, 1,   2.00,       3924.1547N, 7720.7480W, 33.37, 84.66, 23323.30, 10.00 ,13683933.00 ,90.05 ,390 

%% Lifting payload Approximation
%Constants
PapPSI = 6894.75728; %Pascals per PSI
lbpN = 4.4482216283; %pounds per Newton
Rbar = 8314.4598; %m^3 Pa K^-1 Kmol^-1 (Liter-kPa interchangable)
PapHg = 3386.3886667; %Pascals per inch mercery

%Air Metrics
Lalt = 175; %meters
AirTemp = 08+273.15; %Degrees Fahrenheit ((XX - 32)*5/9)
AirPres = 30.15 *PapHg; %inches Hg converted to Pa
[PAlt AltPres] = AltAdjustAWOS(Lalt,AirPres);

%Ballon Metrics
tanks = [2550 200; 2450 350]*PapPSI; %pressure difference PSI converted to Pa
BP = AltPres; % The balloon pressure is assumed to be atmosphereic at filling
BalloonTemp =(( 55 -32)*5/9+273.15); %degrees Fahrenheit converted to Kelvin

%Calculations
pair = (AltPres)*28.97/( AirTemp*Rbar);
[lift Vballoon] = balloonlift(tanks,BP,BalloonTemp,pair,AirTemp);
liftweight = lift/lbpN;

%% Fill to lift payload
payload = (2.99+2.4+2.5+1.4+3.28+.5+1.5+3.6+1)*1.1; %pounds
payNew = payload*lbpN; %converting Payload from lbs to Newtons
Pend = balloonpressure(payNew,BP,BalloonTemp,pair)/PapPSI; %answer in Pascals, converted to PSI


%% Balloon Drag and Assent Rate
difflift = liftweight-payload;

%% Balloon Burst Altitude
Dburst = [10.5:.01:11]; %burst diameter in meters
alt = balloonburst(Dburst,Vballoon,BP,BalloonTemp);
feet = (alt+(Lalt-PAlt))*3.280839895; 

figure(1)
hold on
plot(Dburst,feet,'r')
grid on 
hold off

%% Results
liftweight
Pend 
difflift

%% Bach's Box Burst diameter calculation

BachP = 12.34*100; %Pressure in Pascals converted from Hpa
BachT = -14.80+273.15; %Tempurature converted to Kelvin
BachV = Vballoon*(BP)/(BachP)*(BachT)/(BalloonTemp); %Calculation of Pressure
BachD = 2*(3/4*BachV/pi)^(1/3);

%% Additional Analysis
tankP = (2550-100+ 2450-350)*PapPSI;
HeMass = HeliumMass(tankP,295);
MassAlt = burstmass(11,HeMass);
Massfeet = (MassAlt)*3.280839895