clear joules

IEEnergyAvailable = 6 * 28.8 * 3.4 * 3600; %numBatteries * volts * amps*hrs * secondsPerHour
EC3B0076EnergyAvailable = 32 * 3.67 * 31.0 * 3600;
EC3B0036EnergyAvailable = 32 * 3.67 * 26.0 * 3600;


%for 1 sample during anchor
anchorJoules1 = 2743.08;
anchorDT1 = datetime('jun-09-2022 00:35:04', 'InputFormat', 'MMM-dd-yyyy HH:mm:ss');
anchorJoules2 = 3007.93;
anchrDT1 = datetime('jun-09-2022 00:39:26', 'InputFormat', 'MMM-dd-yyyy HH:mm:ss');

joulesPerAnchorSample = anchorJoules2-anchorJoules1;

initPlatformJ = 1;
initMissionJ = 1;
setBellows0J = 1;
recovery0J = 1;
preMissionDelayJ = 1;
descendActiveW = 1;
descendInactiveW = 1;
descendDutyCycle = 0.5;      %ratio of descendActiveJ / descendInactiveJ
descendDepth = 100;         % meters
descendPV = 0.2;            % meters per second
anchorNonsampleW = 1;
anchorSampleJ = 1;
anchorTimeMinutes = 70;
anchorSamplePeriodMinutes = 15;
anchorSampleDurationMinutes = 4;
ascendBackgroundJ = 1;
ascendSampleJ = 1;
ascendSampleDepthBin = 5;

surfaceOps1PumpStartJ = 15754185582838 / 1e9;
surfaceOps1PumpEndJ = 15754857139650 / 1e9;
surfaceOps1HotelStartJ = 7524941829922 / 1e9;
surfaceOps1HotelEndJ = 8322361820577 / 1e9;
joules(1).pump = surfaceOps1PumpEndJ - surfaceOps1PumpStartJ;
joules(1).hotel = surfaceOps1HotelEndJ - surfaceOps1HotelStartJ;
joules(1).total = joules(1).pump + joules(1).hotel;
joules(1).state = "surfaceOps1";
joulesState(1) = "surfaceOps1";

initProfilePumpEndJ = 10744909005 / 1e9;
initProfileHotelEndJ = 183093249441.7 / 1e9;
joules(2).pump = initProfilePumpEndJ - 0;
joules(2).hotel = initProfileHotelEndJ - 0;
joules(2).total = joules(2).pump + joules(2).hotel;
joules(2).state = "initProfile";
joulesState(2) = "initProfile";

submergePumpEndJ = 6512757970534 / 1e9;
submergeHotelEndJ = 1098948999602 / 1e9;
joules(3).pump = submergePumpEndJ - initProfilePumpEndJ;
joules(3).hotel =  submergeHotelEndJ - initProfileHotelEndJ;
joules(3).total = joules(3).pump + joules(3).hotel;
joules(3).state = "submerge";
joulesState(3) = "submerge";

descendPumpEndJ = 7201909571829 / 1e9;
descendHotelEndJ = 1433733501919 / 1e9;
descendSeconds = (334614.3309 - 252342.0631) / 1000.0;
joules(4).pump = descendPumpEndJ - submergePumpEndJ;
joules(4).hotel =  descendHotelEndJ - submergeHotelEndJ;
joules(4).total = joules(4).pump + joules(4).hotel;
joules(4).state = "descend"
joulesState(4) = "descend"

anchorPumpEndJ = 7350189316096 /1e9;
anchorHotelEndJ = 4984818478915 /1e9;
joules(5).pump = anchorPumpEndJ - descendPumpEndJ;
joules(5).hotel =  anchorHotelEndJ - descendHotelEndJ;
joules(5).total = joules(5).pump + joules(5).hotel;
joules(5).state = "anchor";
joulesState(5) = "anchor";

startCPPumpEndJ = 7350189316096 / 1e9;
startCPHotelEndJ = 4995321627467 / 1e9;
joules(6).pump = startCPPumpEndJ - anchorPumpEndJ;
joules(6).hotel =  startCPHotelEndJ - anchorHotelEndJ;
joules(6).total = joules(6).pump + joules(6).hotel;
joules(6).state = "startCP";
joulesState(6) = "startCP";

deAnchorPumpEndJ = 11189479614873 / 1e9;
deAnchorHotelEndJ = 6151084333432 / 1e9;
joules(7).pump = deAnchorPumpEndJ - startCPPumpEndJ;
joules(7).hotel =  deAnchorHotelEndJ - startCPHotelEndJ;
joules(7).total = joules(7).pump + joules(7).hotel;
joules(7).state = "deAnchor";
joulesState(7) = "deAnchor";

ascendPumpEndJ = 11545270414295 / 1e9;
ascendHotelEndJ = 6345083665514 / 1e9;
ascendSeconds = (4875246.692 - 4839406.282) / 1000.0;
joules(8).pump = ascendPumpEndJ - deAnchorPumpEndJ;
joules(8).hotel =  ascendHotelEndJ - deAnchorHotelEndJ;
joules(8).total = joules(8).pump + joules(8).hotel;
joules(8).state = "ascend";
joulesState(8) = "ascend";

setBellowsPumpEndJ = 15424182565027 / 1e9;
setBellowsHotelEndJ = 7343191694106 / 1e9;
joules(9).pump = setBellowsPumpEndJ - ascendPumpEndJ;
joules(9).hotel =  setBellowsHotelEndJ - ascendHotelEndJ;
joules(9).total = joules(9).pump + joules(9).hotel;
joules(9).state = "setBellows";
joulesState(9) = "setBellows";

dumpCPPumpEndJ = 15662450922209 / 1e9;
dumpCPHotelEndJ = 7486622798184 / 1e9;
joules(10).pump = dumpCPPumpEndJ - setBellowsPumpEndJ;
joules(10).hotel =  dumpCPHotelEndJ - setBellowsHotelEndJ;
joules(10).total = joules(10).pump + joules(10).hotel;
joules(10).state = "dumpCP";
joulesState(10) = "dumpCP";

surfaceOps2PumpEndJ = 15667017508536 / 1e9;
surfaceOps2HotelEndJ = 8123352674672 / 1e9;
joules(11).pump = surfaceOps2PumpEndJ - dumpCPPumpEndJ;
joules(11).hotel =  surfaceOps2HotelEndJ - dumpCPHotelEndJ;
joules(11).total = joules(11).pump + joules(11).hotel;
joules(11).state = "surfaceOps2";
joulesState(11) = "surfaceOps2";

close all;
x = categorical(joulesState);
x = reordercats(x,joulesState);
y = [joules.pump; joules.hotel; joules.total];
barChart = bar(x, y);
xtips1 = barChart(3).XEndPoints;
ytips1 = barChart(3).YEndPoints;
labels1 = compose('%.0f', barChart(3).YData);
text(xtips1,ytips1,labels1,'HorizontalAlignment','center',...
    'VerticalAlignment','bottom')

totalJ = sum([joules.total]);
percentHotelJ = sum([joules.hotel]) / totalJ;
percentPumpJ = sum([joules.pump]) / totalJ;
string1 = "%Pump: ";
string2 = "%Hotel: ";
title(compose('Total Joules this profile: %.0f  %s %0.f  %s %0.f', ...
                totalJ, string1, percentPumpJ * 100, string2, percentHotelJ * 100))
legend('Pump Joules', 'Hotel Joules', 'Total Joules')

numProfiles = 100;
energyAvailable = 0.8 * 11,000,000;
sleepPower = 0.7;
missionDuration = (numProfiles * totalJ) + 




% numProfiles = 1;
% 
% missionInitJ  = initPlatformJ + initMissionJ + setBellows0J + recovery0J + preMissionDelayJ;
% 
% descendTime = (descendDepth / descendPV)
% descendJ = submergeJ + (descendTime * descendDutyCycle * descendActiveW) + (descendTime * (1-descendDutyCycle) * descendInactiveW); 
% 
% anchorIdleTimeSeconds = (anchorSamplePeriodMinutes - anchorSampleDurationMinutes) * 60.0;
% numAnchorSamples = floor(anchorTimeMinutes / anchorSamplePeriodMinutes);
% finalSeconds = (anchorTimeMinutes - (numAnchorSamples * anchorSampleDurationMinutes)) * 60.0;
% anchorJ = deanchorJ + (anchorIdleTimeSeconds * anchorIdleW) + (numAnchorSamples * anchorSampleJ) + (finalSeconds * anchorNonsampleW);














