close all;
clear all;
l=dir('*.mat');

StbdSlopes=ones(size(l,1)-5,4);
for i=1:size(l,1)-5
    load(l(i).name);
    StbdSlopes(i,:)=StbdLinFit; 
    
end

figure;
plot(StbdSlopes(:,3),StbdSlopes(:,1),'kx');
datetick('x','mm/dd','keepticks','keeplimits');
legend('Stbd');
xlabel('Date');ylabel('Slope of Linear Fit (umol/l*hr)');

load StbdInsertionsDepth;
load StbdInsertionsDate;

diameter=29.2;
StbdCmbrVol= 10210+StbdInsertionsDepth*pi*(diameter/2)^2; %in cm^3
StbdCmbrVolLiter=StbdCmbrVol./1000;


StbdO2PerChamberPerIncubation=zeros((size(StbdSlopes,1)),1);%%%umol/hr
for i= 1: size(StbdSlopes,1),
    StbdO2PerChamberPerIncubation(i)=-1*StbdSlopes(i,1)*StbdCmbrVolLiter(i);
    
end

figure;
plot(StbdSlopes(:,3),StbdO2PerChamberPerIncubation,'gx');
datetick('x','mm/dd','keepticks','keeplimits');
legend('Stbd');
xlabel('Date');ylabel('Oxygen Consumber per chamber per incubation per hour (umol/hr)');

%%Convert to Oxygen consummed per square meter of seafloor per day
%%%% O2/IncubationHr *24hr/day*area of sea floor in cm2 (pi*r2)*10000 (convert from cm2
%%%% to m2)/1000 to go from umol to mmol
ChamberArea=(pi*(diameter/2)^2)/10000;  %meter square

StbdO2PerSquareMeterPerHour=StbdO2PerChamberPerIncubation/ChamberArea; %umol/m2hr

figure;
plot(StbdSlopes(:,3),StbdO2PerSquareMeterPerHour,'kx');
datetick('x','mm/dd','keepticks','keeplimits');
legend('Stbd');
xlabel('Date');ylabel('O2 Consumed Per Square Meter Per Hour (umol/m^2hr)');

% convert from umol to mmol -> divide by 1000
% convert from hour to day -> multiply by 24
StbdO2PerSquareMeterPerDayInMMol=StbdO2PerSquareMeterPerHour*24/1000;

figure;
plot(StbdSlopes(:,3),StbdO2PerSquareMeterPerDayInMMol,'kx');
datetick('x','mm/dd','keepticks','keeplimits');
legend('Port','Stbd');
xlabel('Date');ylabel('O2 Consumed Per Square Meter Per Day (mmol/m^2day)');


StbdCarbonPerSquareMeterPerDay=StbdO2PerSquareMeterPerDayInMMol/0.0959;%mgC/m2day

figure;
plot(StbdSlopes(:,3),StbdCarbonPerSquareMeterPerDay,'x'); 

datetick('x','mm/dd','keepticks','keeplimits');
axis tight;
legend('Stbd');
xlabel('Date');ylabel('Carbon Consumed Per Square Meter Per Day (mgC/m^2day)');

CarbonData=[StbdSlopes(:,3),StbdCarbonPerSquareMeterPerDay];

fid = fopen('RoverPulse58StbdCarbonData.txt','wt');
fprintf(fid,'Stbd Date          Stbd Carbon (mgC/m2day)\n');

for i=1:size(CarbonData,1),
fprintf(fid,'%s\t %2.2f\n',datestr(CarbonData(i,1)),CarbonData(i,2));
end
fclose(fid);