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

close all;
clear all;

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


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

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

% I=find(PortSlopes(:,7)>0.97);
% 
% PortSlopesNoLeaks=PortSlopes(I,:);
% figure;
% plot(PortSlopes(:,5),PortSlopes(:,1),'bx');
% hold on;
% plot(StbdSlopes(:,5),StbdSlopes(:,1),'rx');
% plot(PortSlopesNoLeaks(:,5),PortSlopesNoLeaks(:,1),'go');
% datetick('x','mmmyyyy','keepticks','keeplimits')
% legend('Port','Stbd','Stbd (no leak)')
% xlabel('Date');ylabel('Slope (um/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; %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(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

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



%%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,'bx');
hold on;
plot(PortSlopes(I,5),PortCarbonPerSquareMeterPerDay(I),'ko');
plot(StbdSlopes(:,5),StbdCarbonPerSquareMeterPerDay,'rx'); 

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


% 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)];
% CarbonDataAll=[StbdSlopes(:,5),StbdCarbonPerSquareMeterPerDay,PortCarbonPerSquareMeterPerDay];
StbdCarbonData=[StbdSlopes(:,5),StbdCarbonPerSquareMeterPerDay];
PortCarbonData=[PortSlopes(I,5),PortCarbonPerSquareMeterPerDay(I)];
% 


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

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

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

fid = fopen('RoverAnnualCarbonData.txt','wt');
fprintf(fid,'Stbd Date          Stbd Carbon     Port Date           Port Carbon\n');
for i=1:size(CarbonDataPulse59,1),
fprintf(fid,'%s\t %2.2f\t %s\t %2.2f\n',datestr(CarbonDataPulse59(i,1)),CarbonDataPulse59(i,2),datestr(CarbonDataPulse59(i,3)),CarbonDataPulse59(i,4));
end

fclose(fid);
