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

%Stbd LinFit=[slope of lin fit(umol/liter*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
close all;
clear all;

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


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

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

% PortSlopes=ones(size(l,1),4);
% StbdSlopes=ones(size(l,1),4);
% for i=1:size(l,1)
%     load(l(i).name);
%     PortSlopes(i,:)=PortLinFit;
%     StbdSlopes(i,:)=StbdLinFit; 
%     
% end
% figure;
% plot(PortSlopes(:,3),PortSlopes(:,1),'bx');
% hold on;
% plot(StbdSlopes(:,3),StbdSlopes(:,1),'kx');
% datetick('x','mmmyyyy','keepticks','keeplimits');
% legend('Port','Stbd');
% xlabel('Date');ylabel('Slope of Linear Fit (umol/l*hr)');

%%%%%%%%%%% O2 consumber per chamber
%%%%%%%%%%% slope (umol/hr)*experiment durations (hr)/initial conc (umol)*100

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;



%O2 per chamber is the (initial conc (umol/liter)- final conc (umol/liter))*chamber
%vol (liters)= O2 in umol/chamber
PortO2PerChamber=zeros((size(PortSlopes,1)),1); %%%%umol/chamber
for i= 1: size(PortSlopes,1),
    PortO2PerChamber(i)=(PortSlopes(i,3)-PortSlopes(i,4))*PortCmbrVolLiter(i);
    
end

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

PortO2PerChamberPerHour=zeros((size(PortSlopes,1)),1);%%%umol/hr
%o2 per chamber is then divided by the total incubation time which is
%roughly 48 hours
for i= 1: size(PortSlopes,1),
    PortO2PerChamberPerHour(i)=PortO2PerChamber(i)/PortSlopes(i,8);
    
end

PortO2PerChamberPerIncubation=zeros((size(PortSlopes,1)),1);%%%umol/hr
for i= 1: size(PortSlopes,1),
    PortO2PerChamberPerIncubation(i)=-1*PortSlopes(i,1)*PortCmbrVolLiter(i);
    
end
% 
% 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(PortSlopes(:,5),PortO2PerChamberPerIncubation,'bx');
hold on;
plot(PortSlopes(:,5),PortO2PerChamberPerHour,'gx');

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

load JuneData

figure;
plot(PortSlopes(:,5),-1*PortSlopes(:,1),'b*');
hold on;
plot(StbdSlopes(:,5),-1*StbdSlopes(:,1),'g*');
plot(JunePortSlopes(:,3),-1*JunePortSlopes(:,1),'bx');
plot(JuneStbdSlopes(:,3),-1*JuneStbdSlopes(:,1),'gx');
datetick('x','mm/dd/yy','keepticks','keeplimits');
legend('Port','Stbd');
xlabel('Date');ylabel('Respiration Rate (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,'gx'); 

datetick('x','mmmyyyy','keepticks','keeplimits');
axis tight;
legend('Port','Stbd');
xlabel('Date');ylabel('Carbon Consumed Per Square Meter Per Day (mgC/m^2day)');
load FVGR;
plot(FVGR(:,1),FVGR(:,4),'ro');

SlopeData=[StbdSlopes(:,5),StbdSlopes(:,1),ones(size(StbdSlopes,1),1);PortSlopes(:,5),PortSlopes(:,1),zeros(size(PortSlopes,1),1);JuneStbdSlopes(:,3),JuneStbdSlopes(:,1),ones(size(JuneStbdSlopes,1),1);JunePortSlopes(:,3),JunePortSlopes(:,1),zeros(size(JunePortSlopes,1),1)];
fid = fopen('RoverSlopeData.txt','wt');
for i=1:size(SlopeData,1),
fprintf(fid,'%s\t %d\t %d\n',datestr(SlopeData(i,1)),SlopeData(i,2:3));
end
fclose(fid);

CarbonData=[StbdSlopes(:,5),StbdCarbonPerSquareMeterPerDay,ones(size(StbdSlopes,1),1);PortSlopes(:,5),PortCarbonPerSquareMeterPerDay,zeros(size(PortSlopes,1),1)];

fid = fopen('RoverCarbonData.txt','wt');
for i=1:size(CarbonData,1),
fprintf(fid,'%s\t %d\t %d\n',datestr(CarbonData(i,1)),CarbonData(i,2:3));
end
fclose(fid);
