
for i=1:14,
   
  OptodeSlopesv4(i);
end

cd MatlabData

close all;
clear all;

l=dir('*v2.mat');


PortSlopes=ones(size(l,1),8);
StbdSlopes=ones(size(l,1),8);
for i=1:size(l,1)
    load(l(i).name);
    PortSlopes(i,:)=PortLinFit;
   
    
end

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

figure;
plot(PortSlopes(:,5),PortSlopes(:,1),'bx');
hold on;
plot(StbdSlopes(:,5),StbdSlopes(:,1),'kx');
datetick('x','mm/dd/yy','keepticks','keeplimits');
legend('Port','Stbd');
xlabel('Date');ylabel('Slope of Linear Fit (umol/l*hr)');

StbdInsertDepth= 0.88;
PortInsertDepth=0.88;

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

PortCmbrVol = 10210+PortInsertDepth*pi*(diameter/2)^2; %in cm^3
PortCmbrVolLiter = PortCmbrVol./1000; %in liters



PortO2PerChamber=zeros((size(PortSlopes,1)),1); %%%%umol/chamber
for i= 1: size(PortSlopes,1),
    PortO2PerChamber(i)=(PortSlopes(i,3)-PortSlopes(i,4))*PortCmbrVolLiter;
    
end

StbdO2PerChamber=zeros((size(StbdSlopes,1)),1); %%%%umol/chamber
for i= 1: size(StbdSlopes,1),
    StbdO2PerChamber(i)=(StbdSlopes(i,3)-StbdSlopes(i,4))*StbdCmbrVolLiter;
    
end



%PortO2PerChamberPerIncubation= PortO2PerChamber/48;
PortO2PerChamberPerIncubation=zeros((size(PortSlopes,1)),1);%%%umol/hr
for i= 1: size(PortSlopes,1),
    PortO2PerChamberPerIncubation(i)=-1*PortSlopes(i,1)*PortCmbrVolLiter;
    
end

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

figure;
plot(PortSlopes(:,5),PortO2PerChamberPerIncubation,'bx');
hold on;
plot(StbdSlopes(:,5),StbdO2PerChamberPerIncubation,'gx');
datetick('x','mmmyyyy','keepticks','keeplimits');
legend('Port','Stbd');
xlabel('Date');ylabel('Oxygen Consumber per chamber per incubation per hour (umol/hr)');



figure;
plot(PortSlopes(:,5),PortSlopes(:,1),'bx');
hold on;
plot(StbdSlopes(:,5),StbdSlopes(:,1),'gx');
datetick('x','mmmyyyy','keepticks','keeplimits');
legend('Port','Stbd');
xlabel('Date');ylabel('Slope of Linear Fit (umol/l*hr)');



figure;
plot(PortSlopes(:,5),PortO2PerChamber,'bx');
hold on;
plot(StbdSlopes(:,5),StbdO2PerChamber,'kx');
datetick('x','mmmyyyy','keepticks','keeplimits');
legend('Port','Stbd');
xlabel('Date');ylabel('Oxygen Consumber per chamber per incubation (umol)');

%%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

PortO2PerSquareMeterPerHour=PortO2PerChamberPerIncubation/ChamberArea; %umol/m2hr
StbdO2PerSquareMeterPerHour=StbdO2PerChamberPerIncubation/ChamberArea; %umol/m2hr

figure;
plot(PortSlopes(:,5),PortO2PerSquareMeterPerHour,'bx');
hold on;
plot(StbdSlopes(:,5),StbdO2PerSquareMeterPerHour,'kx');
datetick('x','mmmyyyy','keepticks','keeplimits');
legend('Port','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
PortO2PerSquareMeterPerDayInMMol=PortO2PerSquareMeterPerHour*24/1000;
StbdO2PerSquareMeterPerDayInMMol=StbdO2PerSquareMeterPerHour*24/1000;

figure;
plot(PortSlopes(:,5),PortO2PerSquareMeterPerDayInMMol,'bx');
hold on;
plot(StbdSlopes(:,5),StbdO2PerSquareMeterPerDayInMMol,'kx');
datetick('x','mmmyyyy','keepticks','keeplimits');
legend('Port','Stbd');
xlabel('Date');ylabel('O2 Consumed Per Square Meter Per Day (mmol/m^2day)');


PortCarbonPerSquareMeterPerDay=PortO2PerSquareMeterPerDayInMMol/0.0959;%mgC/m2day
StbdCarbonPerSquareMeterPerDay=StbdO2PerSquareMeterPerDayInMMol/0.0959;%mgC/m2day

figure;
plot(PortSlopes(:,5),PortCarbonPerSquareMeterPerDay,'rx');
hold on;
plot(StbdSlopes(:,5),StbdCarbonPerSquareMeterPerDay,'bx'); 

datetick('x','mm/dd/yy','keepticks','keeplimits');
axis tight;
legend('Port','Stbd');
xlabel('Date');ylabel('Carbon Consumed Per Square Meter Per Day (mgC/m^2day)');
saveas(gcf,strcat('Pulse65.tiff'));

CarbonDataAll=[StbdSlopes(:,5),StbdCarbonPerSquareMeterPerDay,PortCarbonPerSquareMeterPerDay];
% 


fid = fopen('RoverCarbonData_Pulse65.txt','wt');
fprintf(fid,'Carbon per sq meter per day\n');
fprintf(fid,'Date\t Stbd\t Port\n');
for i=1:size(CarbonDataAll,1),
fprintf(fid,'%s\t %d\t %d\n',datestr(CarbonDataAll(i,1)),CarbonDataAll(i,2),CarbonDataAll(i,3));
end
fclose(fid);


