cd ..
global d;
d=dir ('plan-*');


for i=2:size(d,1)
    cd (d(i).name);
    op = dir ('*-optodes');
    cd (op.name)
  OptodeSlopesv3();
end

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

%Port LinFit=[slope of lin fit(umol/hr), y-int(of lin fit),y-int, last y val, first
%time record, last time record, r-square of lin fit, duration of experiment
%(in hours)

%Stbd LinFit=[slope of lin fit(umol/hr), y-int(of lin fit),y-int, last y val, first
%time record, last time record, r-square of lin fit, duration of experiment
%(in hours), 

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;
    StbdSlopes(i,:)=StbdLinFit;
    
end

figure;
plot(PortSlopes(:,5),PortSlopes(:,1),'bx');
hold on;
plot(StbdSlopes(:,5),StbdSlopes(:,1),'rx');
datetick('x','mmmyyyy','keepticks','keeplimits')
legend('Port','Stbd')
xlabel('Date');ylabel('Slope (um/hr)');

figure;
plot(PortSlopes(:,5),(PortSlopes(:,4)-PortSlopes(:,3))/(mean(PortSlopes(:,8))),'gx');hold on;
plot(PortSlopes(:,5),PortSlopes(:,1),'bx');

hold on;
plot(StbdSlopes(:,5),StbdSlopes(:,1),'rx');
datetick('x','mmmyyyy','keepticks','keeplimits')
legend('Port','Stbd')
xlabel('Date');ylabel('Slope (um/hr)');


% PortInsertDepth=data;
% PortInsertDate=datenum(textdata(3:end,1));
% save('PortInsertData.mat', 'PortInsertDate','PortInsertDepth');

load StbdInsertData
load PortInsertData
%10.21Liters is chamber vol before knife edge then add knife vol pi*r^2*h
%%%%%%%%%   ID of chamber (to knife edge) 29.2 cm
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;

figure;
plot(StbdSlopes(:,5),StbdCmbrVolLiter,'rx');
hold on; 
plot(PortSlopes(:,5),PortCmbrVolLiter(1:size(PortSlopes,1)),'bx');
plot(StbdSlopes(:,5), 10.21*ones(size(StbdSlopes,1)),'k');
datetick('x','mmmyyyy','keepticks','keeplimits');
xlabel('Date');ylabel('Chamber Vol (Liters)');legend('Stbd','Port');
title('Chamber Vol (L) vs. Date');
saveas(gcf,strcat('ChamberVols','.tiff'));

StbdPrctConsumed=zeros((size(StbdSlopes,1)),1);
for i= 1: size(StbdSlopes,1),
    StbdPrctConsumed(i)=(StbdSlopes(i,1).*StbdSlopes(i,8)/StbdSlopes(i,3))*-100;
end

PortPrctConsumed=zeros((size(PortSlopes,1)),1);
for i= 1: size(PortSlopes,1),
    PortPrctConsumed(i)=(PortSlopes(i,1).*PortSlopes(i,8)/PortSlopes(i,3))*-100;
end

figure;
plot(StbdSlopes(:,5),StbdPrctConsumed,'rx');
hold on;
plot(PortSlopes(:,5),PortPrctConsumed,'bx');
legend('Stbd','Port');
xlabel('Date');ylabel('%Consumed per chamber');
datetick('x','mmmyyyy','keepticks','keeplimits');
saveas(gcf,strcat('PercentConsumed','.tiff'));

StbdConPerSqrMtr = (StbdSlopes(:,1)/(pi*(diameter/2)^2))*10000;
%%%%%   mmol/m^2day
StbdConPerSqrMtrPerDay = StbdConPerSqrMtr*24/1000;
StbdOrCa = StbdConPerSqrMtrPerDay/0.0959;


PortConPerSqrMtr = (PortSlopes(:,1)/(pi*(diameter/2)^2))*10000;
%%%%%   mmol/m^2day
PortConPerSqrMtrPerDay = PortConPerSqrMtr*24/1000;
PortOrCa = PortConPerSqrMtrPerDay/0.0959;

figure;
plot(StbdSlopes(:,5),StbdOrCa,'rx');
hold on;
plot(PortSlopes(:,5),PortOrCa,'bx');
datetick('x','mmmyyyy','keepticks','keeplimits');
legend('Stbd','Port');
xlabel('Date');ylabel('Carbon per square meter per day (mg/m^2day)');
title('Organic Carbon Consumed vs. Time');
