global d;
d=dir ('plan-01-long*');

%finds each site folder and goes into the optode data 
 for i=1:size(d,1)
    cd (d(i).name);
    op = dir ('*-optodes');
    cd (op.name)
    %OptodeSlopesv3();
    OptodeSlopesSubRef();
end


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

%save('PortInsertData.mat', 'PortInsertDate','PortInsertDepth');

%Port/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)


%not used
%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,elapsed time before first drift (hrs), duration of experiment
%(in hours), mean diff of lin data, std of diff of linear data

%for Station M 11/11 dont use last two
% PortSlopes=ones(size(l,1)-2,8);
% StbdSlopes=ones(size(l,1)-1,11);
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

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


%   csvwrite('PortSlopes.csv',PortSlopes)
start=2;
figure;
plot(PortSlopes(start:end,5),PortSlopes(start:end,1),'bx');
hold on;
plot(StbdSlopes(start:end,5),StbdSlopes(start:end,1),'rx');
datetick('x','mmmyyyy','keepticks','keeplimits')
legend('Port','Stbd')
xlabel('Date');ylabel('Slope (um/hr)');
saveas(gcf,strcat('Slopes_Port_Stbd','.tiff'));


figure;
plot(PortSlopes(:,5),PortSlopes(:,8),'bx');
hold on;
plot(StbdSlopes(:,5),StbdSlopes(:,8),'rx');
datetick('x','mmmyyyy','keepticks','keeplimits')
legend('Port','Stbd')
xlabel('Date');ylabel('Experiment Duration (Hrs)');
saveas(gcf,strcat('Duration_Port_Stbd','.tiff'));


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)');


figure;
hist(StbdSlopes(:,8));
xlabel('Hour Drift Began');ylabel('Frequency');
saveas(gcf,strcat('StbdDrift_Hist','.tiff'));

figure;
plot(StbdSlopes(:,10)-StbdSlopes(:,1),'bo');

figure; plot(PortSlopes(:,1),StbdSlopes(:,1),'x')
hold on;plot([-1 0],[ -1 0],'r--');
xlabel('Port Slope (umol/hr)');ylabel('Stbd Slope (umol/hr)');
axis([-1 0 -1 0]);

figure;
plot(PortSlopes(:,5),PortSlopes(:,7),'bx');
hold on;
plot(StbdSlopes(:,5),StbdSlopes(:,7),'rx');
datetick('x','mmmyyyy','keepticks','keeplimits')
xlabel('Date');ylabel('R^2 for Lin Reg');
legend('Port','Stbd');
saveas(gcf,strcat('R^2_SCOCLinRegAbrev','.tiff'));

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

% StbdInsertDepth=data;
% StbdInsertDate=datenum(textdata(3:end,1));
% save('StbdInsertData.mat', 'StbdInsertDate','StbdInsertDepth');

StbdInsertDepth=INSERTIONDEPTHA;
StbdInsertDate=Date1;
save('StbdInsertData.mat', 'StbdInsertDate','StbdInsertDepth');

PortInsertDepth=INSERTIONDEPTHA;
PortInsertDate=Date1;
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');
legend('Port');
title('Chamber Vol (L) vs. Date');
saveas(gcf,strcat('ChamberVols','.tiff'));


%%%%%%%%%%% O2 consumed per chamber
%%%%%%%%%%% slope (umol/hr)*experiment durations (hr)/initial conc (umol)*100
StbdPrctConsumed=zeros((size(StbdSlopes,1)),1);
for i= 1: size(StbdSlopes,1),
    StbdPrctConsumed(i)=(StbdSlopes(i,1).*StbdSlopes(i,9)/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'));

%%%%%%%%%O2 Consumed per meter of sea floor per hour
%%%%% = slope (umol./hr)* area of chamber (cm^2) *10,000 cm^2/1m^2 =
%%%%% umol/hr*m^2

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');
saveas(gcf,strcat('MgConsumedPerSqmtrPerDay','.tiff'));


figure;
[AX,H1,H2]=plotyy(StbdSlopes(:,5),StbdConPerSqrMtrPerDay,StbdSlopes(:,5),StbdOrCa);
set(H1,'Marker','x','LineStyle','none');
set(H2,'Marker','x','LineStyle','none');
set(AX, 'xTickLabel','')
datetick('x','mmmyyyy','keepticks','keeplimits');
set(get(AX(1),'YLabel'),'String','O2 Conc per square meter per day (mmol/m^2day)');
set(get(AX(2),'YLabel'),'String','Carbon per square meter per day (mg/m^2day)');
xlabel('Date');
set(get(AX(1),'MarkerType'),'String','x');
title('Stbd Organic Carbon Consumed vs. Time');

saveas(gcf,strcat('MgMmolConsumedPerSqmtrPerDay','.tiff'));



fid = fopen('StbdTimeStamps.txt','wt');
for i=1:size(StbdSlopes,1),
fprintf(fid,'%s\t %s\n',datestr(StbdSlopes(i,5)),datestr(StbdInsertDate(i)));
end
fclose(fid);

StbdCmbrVol= 10210+StbdInsertDepth*pi*(diameter/2)^2; %in cm^3
plot(StbdSlopes(:,5),StbdCmbrVol(1:size(StbdSlopes,1))/1000,'x');
hold on; plot(StbdSlopes(:,5), 10.21*ones(size(StbdSlopes,1)),'r');
datetick('x','mmmyyyy','keepticks','keeplimits');
xlabel('Date');ylabel('Stbd Chamber Vol (Liters)');


%%%%%%%%%%%o2 consumber per chamber
%slope (umol/hr)*experiment durations (hr)/initial conc (umol)*100
StbdPrctConsumed=zeros((size(StbdSlopes,1)),1);
for i= 1: size(StbdSlopes,1),
    StbdPrctConsumed(i)=(StbdSlopes(i,1).*StbdSlopes(i,9)/StbdSlopes(i,3))*100;
end
figure;
plot(StbdSlopes(:,5),StbdPrctConsumed,'x');
xlabel('Date');ylabel('%Consumed per chamber');
datetick('x','mmmyyyy','keepticks','keeplimits');

%%%%%%%%%O2 Consumed per meter of sea floor per hour
%%%%% = slope (umol./hr)* area of chamber (cm^2) *10,000 cm^2/1m^2 =
%%%%% umol/hr*m^2
StbdConPerSqrMtr = (StbdSlopes(:,1)/(pi*(diameter/2)^2))*10000;
%%%%%   mmol/m^2day
StbdConPerSqrMtrPerDay = StbdConPerSqrMtr*24/1000;
StbdOrCa = StbdConPerSqrMtrPerDay/0.0959;

