
l=dir('*v2.mat');
%load data from each incubation
%do not use last entry as data was incomplete
PortSlopes=ones(size(l,1)-1,8);
for i=1:size(l,1)-1
    load(l(i).name);
    PortSlopes(i,:)=PortLinFit;
   
    
end

%load data with insertion depths for each incubation
load PortInsertData

%%%%%%%%%   ID of chamber (to knife edge) 29.2 cm
diameter=29.2;


%10.21Liters is chamber vol before knife edge then add knife vol pi*r^2*h
PortCmbrVol = 10210+PortInsertDepth*pi*(diameter/2)^2; %in cm^3
PortCmbrVolLiter = PortCmbrVol./1000;



%O2 per chamber per hour is the slope of the linear fit of the O2 consumed
%(umol/Lhr) * volume of chamber (L) = umol/hr
PortO2PerChamberPerHour=zeros((size(PortSlopes,1)),1);%%%umol/hr

for i= 1: size(PortSlopes,1),
    PortO2PerChamberPerHour(i)=-1*(PortSlopes(i,1)*PortCmbrVolLiter(i));
    
end

%%Convert to Oxygen consummed to O2 per square meter of seafloor per day
%%%% O2 consumed per chamber per hours *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=PortO2PerChamberPerHour/ChamberArea; %umol/m2hr



% convert from umol to mmol -> divide by 1000
% convert from hour to day -> multiply by 24
PortO2PerSquareMeterPerDayInMMol=PortO2PerSquareMeterPerHour*24/1000; %mmol//m^2day


PortCarbonPerSquareMeterPerDay=PortO2PerSquareMeterPerDayInMMol/0.0959;%mgC/m2day


figure;
plot(PortSlopes(:,5),PortCarbonPerSquareMeterPerDay,'ro');
datetick('x','mmmyyyy','keepticks','keeplimits');
legend('Port');
xlabel('Date');ylabel('Carbon Consumed Per Square Meter Per Day (mgC/m^2day)');


ExperimentDates=zeros((size(PortSlopes,1)),2);%%%datestrings

for i= 1: size(PortSlopes,1),
    ExperimentDates(i,1)=addtodate(PortSlopes(i,5),-8,'hour');
    ExperimentDates(i,2)=addtodate(PortSlopes(i,6),-8,'hour');
end


%Print Data to file
PortCarbonData=[ExperimentDates,PortSlopes(:,1),PortCarbonPerSquareMeterPerDay,zeros(size(PortSlopes,1),1)];

fid = fopen('Pulse61PortCarbonandInsertData.txt','wt');
fprintf(fid,'Optode Start Date\t Optode Finish Date\t slope (umol/Lhr)\t carbon comsumed(mgC/m^2day)\t InsertDate\t InsertDepth (cm)\n');

for i=1:size(PortCarbonData,1),
fprintf(fid,'%s\t %s\t %d\t %d\t %s\t %d\n',datestr(PortCarbonData(i,1)),datestr(PortCarbonData(i,2)),PortCarbonData(i,3),PortCarbonData(i,4),datestr(PortInsertDate(i)),PortInsertDepth(i));
end
fclose(fid);


%%%%%%%%%%%%%%%%%%%%%%%%% STBD %%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

%load data with insertion depths for each incubation
load StbdInsertData

%%%%%%%%%   ID of chamber (to knife edge) 29.2 cm
diameter=29.2;


%10.21Liters is chamber vol before knife edge then add knife vol pi*r^2*h
StbdCmbrVol = 10210+StbdInsertDepth*pi*(diameter/2)^2; %in cm^3
StbdCmbrVolLiter = StbdCmbrVol./1000;



%O2 per chamber per hour is the slope of the linear fit of the O2 consumed
%(umol/Lhr) * volume of chamber (L) = umol/hr
StbdO2PerChamberPerHour=zeros((size(StbdSlopes,1)),1);%%%umol/hr

for i= 1: size(StbdSlopes,1),
    StbdO2PerChamberPerHour(i)=-1*(StbdSlopes(i,1)*StbdCmbrVolLiter(i));
    
end

%%Convert to Oxygen consummed to O2 per square meter of seafloor per day
%%%% O2 consumed per chamber per hours *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=StbdO2PerChamberPerHour/ChamberArea; %umol/m2hr



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


StbdCarbonPerSquareMeterPerDay=StbdO2PerSquareMeterPerDayInMMol/0.0959;%mgC/m2day


figure;
plot(StbdSlopes(:,5),StbdCarbonPerSquareMeterPerDay,'ro');
datetick('x','mmmyyyy','keepticks','keeplimits');
legend('Stbd');
xlabel('Date');ylabel('Carbon Consumed Per Square Meter Per Day (mgC/m^2day)');


ExperimentDates=zeros((size(StbdSlopes,1)),2);%%%datestrings

for i= 1: size(StbdSlopes,1),
    ExperimentDates(i,1)=addtodate(StbdSlopes(i,5),-8,'hour');
    ExperimentDates(i,2)=addtodate(StbdSlopes(i,6),-8,'hour');
end


%Print Data to file
StbdCarbonData=[ExperimentDates,StbdSlopes(:,1),StbdCarbonPerSquareMeterPerDay,zeros(size(StbdSlopes,1),1)];

fid = fopen('Pulse61StbdCarbonandInsertData.txt','wt');
fprintf(fid,'Optode Start Date\t Optode Finish Date\t slope (umol/Lhr)\t carbon comsumed(mgC/m^2day)\t InsertDate\t InsertDepth (cm)\n');

for i=1:size(StbdCarbonData,1),
fprintf(fid,'%s\t %s\t %d\t %d\t %s\t %d\n',datestr(StbdCarbonData(i,1)),datestr(StbdCarbonData(i,2)),StbdCarbonData(i,3),StbdCarbonData(i,4),datestr(StbdInsertDate(i)),StbdInsertDepth(i));
end
fclose(fid);

figure;
plot(StbdSlopes(:,5),StbdCarbonPerSquareMeterPerDay,'rx');
datetick('x','mm/dd/yy','keepticks','keeplimits');
hold on;
plot(PortSlopes(:,5),PortCarbonPerSquareMeterPerDay,'bx');
legend('Stbd','Port');
xlabel('Date');ylabel('Carbon Consumed Per Square Meter Per Day (mgC/m^2day)');

