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)
    OptodeSlopesv4();
  
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 (uM), last y val (uM), 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


%Plot slope vs. time
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)');
saveas(gcf,strcat('Slopes_Port_Stbd','.tiff'));

% Compare linear fit to slope calculation for accuracy
% Not sure why mean is used in the denominator
figure;
plot(PortSlopes(:,5),(PortSlopes(:,4)-PortSlopes(:,3))/(mean(PortSlopes(:,8))),'gx');hold on;
plot(PortSlopes(:,5),PortSlopes(:,1),'bx');

hold on;

Stbd_Slope_Calc=(StbdSlopes(:,4)-StbdSlopes(:,3))/mean(StbdSlopes(:,8));
Stbd_Slope_Error=Stbd_Slope_Calc-StbdSlopes(:,1);
figure;
plot(StbdSlopes(:,5),Stbd_Slope_Error);
datetick('x','mmmyyyy','keepticks','keeplimits')
legend('Lin Fit Compare Error')
xlabel('Date');ylabel('Slope (uM/hr)');


% R^2 for linear fit vs.time

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


%%%%%%% Load insertion data
% Save data to mat file for future use
StbdInsertDepth=INSERTIONDEPTHA;
StbdInsertDate=Date1;
save('StbdInsertData.mat', 'StbdInsertDate','StbdInsertDepth');
% % 
PortInsertDepth=INSERTIONDEPTHA;
PortInsertDate=Date1;
save('PortInsertData.mat', 'PortInsertDate','PortInsertDepth');
% 

% Clear all and load insertion data
load StbdInsertData
load PortInsertData

reply = input('Do you want to use last incubation? Y/N :','s');
       if reply == 'N'
          StbdSlopes=StbdSlopes(1:end-1,:);
          PortSlopes=PortSlopes(1:end-1,:);
       end
%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;                   %in liters

PortCmbrVol = 10210+PortInsertDepth*pi*(diameter/2)^2; %in cm^3
PortCmbrVolLiter = PortCmbrVol./1000;                  %in liters



% Plot Volume
figure;
plot(StbdSlopes(:,5),StbdCmbrVolLiter,'rx');
hold on; 
plot(PortSlopes(:,5),PortCmbrVolLiter,'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'));


%%%%%%%%%O2 Consumed per meter of sea floor per hour
%%%%%%%%%%% Convert concentration from Molarity to umoles to mmoles to Carbon
%%%%% = slope ((uMol/L)/hr)*volume (L)* 24 hr/day* area of chamber (cm^2) * 1m^2/10,000 cm^2 =
%%%%% umol/hr*m^2

% First convert from uMol to umol. uMol= umole/liter, slope in uM/hr thus multiply by volume in L
% results in umol/hr
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

ChamberArea=(pi*(diameter/2)^2)/10000;  %meter square

% divide by chamber area to get consumed per square meter
PortO2PerSquareMeterPerHour=PortO2PerChamberPerIncubation/ChamberArea; %umol/m2hr
StbdO2PerSquareMeterPerHour=StbdO2PerChamberPerIncubation/ChamberArea; %umol/m2hr

% convert from umol to mmol -> divide by 1000
% convert from hour to day -> multiply by 24
PortO2PerSquareMeterPerDayInmMol=PortO2PerSquareMeterPerHour*24/1000;
StbdO2PerSquareMeterPerDayInmMol=StbdO2PerSquareMeterPerHour*24/1000;

%convert from mmol O2 to mgC by dividing by 0.0959

PortOrCa=PortO2PerSquareMeterPerDayInmMol/0.0959;%mgC/m2day
StbdOrCa=StbdO2PerSquareMeterPerDayInmMol/0.0959;%mgC/m2day

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_Stbd','.tiff'));

% New data structure: columns are Date/time experiment began, Org Carbon in
% mgC/m^2day

StbdCarbonData=[StbdSlopes(:,5),StbdOrCa];
PortCarbonData=[PortSlopes(:,5),PortOrCa];


 

PulseNum=num2str(input('What is the pulse number?'));

save(strcat('PulseCarbonData',PulseNum,'.mat'), 'StbdCarbonData','PortCarbonData');

fid = fopen(strcat('RoverAnnualCarbonDataPulse',PulseNum,'.txt'),'wt');
fprintf(fid,strcat('Experiement Started:',datestr(StbdSlopes(1,5)),'\n'));
fprintf(fid,'Dates listed are the start of the experiment\n');
fprintf(fid,'Units are (mg/m^2day) \n');
fprintf(fid,'Stbd Date          Stbd Carbon     Port Date           Port Carbon\n');
for i=1:size(StbdCarbonData,1),
fprintf(fid,'%s\t %2.2f\t %s\t %2.2f\n',datestr(StbdCarbonData(i,1)),StbdCarbonData(i,2),datestr(PortCarbonData(i,1)),PortCarbonData(i,2));
end

fclose(fid);


