% Auto-generated by MATLAB on 25-Apr-2024 13:40:56
%% Set up the Import Options and import the data

clear all

opts = delimitedTextImportOptions("NumVariables", 57);

% Specify range and delimiter
opts.DataLines = [1, Inf];
opts.Delimiter = "\t";

% Specify column names and types
opts.VariableNames = ["STX", "Date", "Time", "State", "Comment", "Pressure", "BelPos", "PumpMilliamps", "PumpMilliVolts", "PumpMicroWatts", "PumpMilliSeconds", "PumpUCoulombs", "PumpNanoJoules", "HotelMilliamps", "HotelMilliVolts", "HotelMicroWatts", "HotelMilliSeconds", "HotelUCoulombs", "HotelNanoJoules", "pumpMotorMsg", "CTDTemp", "CTDSal", "RawPress", "LPFPress", "LPFVel", "DiffVel", "Diff2Vel", "SlopeLPFPress", "CTDSamplePeriod", "LPS331Press", "LPS331Temp", "SHT21Humid", "SHT21Temp", "pidSP", "pidPV", "pidY1", "pidY2", "pidI", "pid_v", "pid_u", "pid_uCPS", "pid_h", "FLBB", "Topt", "Tphase", "Rphase", "NO3", "pHVrs", "OCRtext", "OCRCh1", "OCRCh2", "OCRCh3", "OCRCH4", "GPS", "IridModem", "MSC", "VarName57"];
opts.VariableTypes = ["categorical", "datetime", "datetime", "categorical", "string", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "string", "string", "string", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "string", "string", "string", "string", "double", "string", "string", "double", "double", "double", "double", "string", "string", "string", "string"];

% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";

% Specify variable properties
opts = setvaropts(opts, ["Comment", "pumpMotorMsg", "CTDTemp", "CTDSal", "FLBB", "Topt", "Tphase", "Rphase", "pHVrs", "OCRtext", "GPS", "IridModem", "MSC", "VarName57"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["STX", "State", "Comment", "pumpMotorMsg", "CTDTemp", "CTDSal", "FLBB", "Topt", "Tphase", "Rphase", "pHVrs", "OCRtext", "GPS", "IridModem", "MSC", "VarName57"], "EmptyFieldRule", "auto");
opts = setvaropts(opts, "Date", "InputFormat", "dd-MMM-yyyy");
opts = setvaropts(opts, "Time", "InputFormat", "HH:mm:ss.SSS");
opts = setvaropts(opts, "NO3", "TrimNonNumeric", true);
opts = setvaropts(opts, "NO3", "ThousandsSeparator", ",");
dataTable.Time.Format = "HH:mm:ss.SSS";

% Import the data
dataTable = readtable("\\atlas\projectlibrary\CPF\CPF003\Data\TankTests\2023-11-2T23-24-9\CPF003.001V3.eng", opts);
clear opts

DT = dataTable.Date + timeofday(dataTable.Time);
dataTable = addvars(dataTable, DT, 'After', 'Time');
dataTable.Time = timeofday(dataTable.Time)

dtDiff = dataTable.Time(4) - dataTable.Time(3)
seconds(dtDiff)

%

nonNaN = ~isnan(dataTable.PumpMilliVolts);
pumpVolts = dataTable.PumpMilliVolts(nonNaN) / 1000.0;
figure
plot(pumpVolts)
title('Pump Volts')

nonNaN = ~isnan(dataTable.PumpMilliamps);
pumpAmps = dataTable.PumpMilliamps(nonNaN) / 1000.0;
figure
plot(pumpAmps)
title('Pump Amps')

nonNaN = ~isnan(dataTable.PumpMilliSeconds);
pumpSeconds = dataTable.PumpMilliSeconds(nonNaN) / 1000.0;
figure
plot(pumpSeconds)
title('Pump Seconds')

nonNaN = ~isnan(dataTable.PumpNanoJoules);
pumpJoules = dataTable.PumpNanoJoules(nonNaN) / 10^9;
figure
plot(pumpJoules)
title('Pump Joules')

% Double check that joules measured by the LTC2946 are n the right order of magnitude
for i=1:length(pumpSeconds) - 1
    instPumpJoulesCalc(i) = pumpVolts(i) * pumpAmps(i) * (pumpSeconds(i+1) - pumpSeconds(i));
end
figure
plot(instPumpJoulesCalc)
title('Calculated Instantaneous Pump Joules')

pumpJoulesCalcSum = cumsum(pumpJoulesCalc);
plot(pumpJoulesCalcSum)
title('Calculated Pump Joules')

%

nonNaN = ~isnan(dataTable.HotelNanoJoules);
hotelJoules = dataTable.HotelNanoJoules(nonNaN) / 10^9;
dt = dataTable.DT(nonNaN);

plot(dt, pumpJoules, dt, hotelJoules)
title('Pump and Hotel Joules')

%

sleepSeconds = 64 * 60;
sleepJoules = 2;
sleepVolts = 28.0;

sleepAmps = sleepJoules / (sleepSeconds * sleepVolts)

%

% This is just a guess for the short term
% Needs to be calculated from eng files
wakeFromSleepJoules = 5;

%

% This is from Dana's estimate just for the short term
% Needs to be calculated from eng files
% ISUS/NO3 + SBE41 + pH + FLBB + Optode + OCR (guess)
joulesPerSample = 3.62 + 3.04 + 0.16 + 0.1 +0.5;

%

profileDepth = 200;
numProfiles = 100;
samplesPerProfile = 20;
descendSpeedMPS = 0.2;
ascendSpeedMPS = 0.1;
totalProfileSeconds = (profileDepth / descendSpeedMPS) + (profileDepth / ascendSpeedMPS)

numAnchorSamples = 4;
anchorTime = (numAnchorSamples - 1) * sleepSeconds;

anchorJoules = ((numAnchorSamples - 1) * wakeFromSleepJoules) + (numAnchorSamples * joulesPerSample) ...
    + (numAnchorSamples - 1) * (sleepSeconds * sleepVolts * sleepAmps) 

%

descendJoules = 100;
PVMaintenanceJoules = 10 * profileDepth;
anchorJoules = 100;
deanchorJoules = 500;
ascendJoules = 100;
surfaceOpsJoules = 1000;
totalSampleJoules = samplesPerProfile * joulesPerSample;
profileJoules = descendJoules + (2 * PVMaintenanceJoules) + anchorJoules...
    + deanchorJouiles + ascendJoules + surfaceOpsJoules + totalSampleJoules

%

missionJoules = (numProfiles * profileJoules) descendJoules + (2 * PVMaintenanceJoules) + anchorJoules + deanchorJoules + surfaceOpsJouls
missionTime 

%

numBatteries := 32;
batteryVolts - 28;
batteryAmpHours = 3.3;
selfDischargePrecentPerYear = 0.03;
batteryJoules = 