function []= OptodesSlopes()
%clear all;
DELIMITER = ',';
HEADERLINES = 1;

% Import the file
newData1 = importdata('port_optode.csv', DELIMITER, HEADERLINES);

% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
    assignin('base', vars{i}, newData1.(vars{i}));
end

portO2Conc=newData1.data(:,1);
portRoverTime=newData1.textdata(2:end,1);
[date,time]=strtok(portRoverTime,'T');
time=strtok(time,'T');
portdatetime=strcat(date,',',time);
portConcTS=timeseries(portO2Conc,portdatetime);

timestring=char(time);

portTime=zeros(size(portdatetime,1),6);
for i= 1:size(portdatetime,1),
      [hr,mn,sc]=strread(char(time(i,:)),'%d%d%f','delimiter',':');
     [yr,mth,dy]=strread(char(date(i)),'%d%d%d','delimiter','-');
     [ss,ds]=strread(num2str(sc),'%d%d','delimiter','.');
     portTime(i,:)=[yr,mth,dy,hr,mn,ss];
end
[index,j]=find(abs(diff(portTime(:,5)))>5);

portAveConc=zeros(size(index));
portAveTime=zeros(size(index));
for i= 1:size(index),
    if i==1
        portAveConc=mean(portO2Conc(1:index(i)));
        portAveTime=portdatetime(index(i));       
    else
        portAveConc=[portAveConc;mean(portO2Conc(index(i-1)+1:index(i)))];
        portAveTime=[portAveTime;portdatetime(index(i))];        
    end
end
portAveConc=[portAveConc;mean(portO2Conc(index(i)+1:end))];
portAveTime=[portAveTime;portdatetime(end)];
portConcAveTS=timeseries(portAveConc,portAveTime);

portAveTimeVect=datenum(portAveTime(:,1));
EpochSecs=(portAveTimeVect-719529)*86400;
portElapsedTime=zeros(size(EpochSecs));
for i=1:size(EpochSecs,1),
        portElapsedTime(i,1)=(EpochSecs(i,1)-EpochSecs(1,1))/60/60;
end

PortLinFit=[polyfit(portElapsedTime,portAveConc,1)];
p=polyfitn(portElapsedTime,portAveConc,'constant x');
PortLinFit=[PortLinFit,datenum(portAveTime(1,1)),p.R2];

y=PortLinFit(1)*portElapsedTime + PortLinFit(2);
figure;
subplot(2,1,1);
plot(portConcTS,'rv');hold on;plot(portConcAveTS,'bx');
xlabel('Time');ylabel('O2 Conc (uM)');
legend('port','port Ave','Location','Best');

subplot(2,1,2);
plot(portElapsedTime,portAveConc,'b');hold on; plot(portElapsedTime,y,'g');
xlabel('Elapsed Time (hrs)');ylabel('O2 Conc (uM)');
legend('port Ave','lin fit','Location','Best');

s=char(date(1));
portdataname=strcat('Port','_',s);
saveas(gcf,strcat(portdataname,'.tiff'));



% Import the file
newData1 = importdata('starboard_optode.csv', DELIMITER, HEADERLINES);
%newData1 = importdata('stbd_ref_optode.csv', DELIMITER, HEADERLINES);

% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
    assignin('base', vars{i}, newData1.(vars{i}));
end

stbdO2Conc=newData1.data(:,1);
stbdTemp=newData1.data(:,3);
stbdRoverTime=newData1.textdata(2:end,1);
[date,time]=strtok(stbdRoverTime,'T');
time=strtok(time,'T');
stbddatetime=strcat(date,',',time);
stbdConcTS=timeseries(stbdO2Conc,stbddatetime);
stbdTempTS=timeseries(stbdTemp,stbddatetime);
timestring=char(time);


stbdTime=zeros(size(stbddatetime,1),6);
for i= 1:size(stbddatetime,1),
     [hr,mn,sc]=strread(char(time(i,:)),'%d%d%f','delimiter',':');
     [yr,mth,dy]=strread(char(date(i)),'%d%d%d','delimiter','-');
     [ss,ds]=strread(num2str(sc),'%d%d','delimiter','.');
     stbdTime(i,:)=[yr,mth,dy,hr,mn,ss];
end
[index,j]=find(abs(diff(stbdTime(:,5)))>5);

stbdAveTemp=zeros(size(index));
stbdAveConc=zeros(size(index));
stbdAveTime=zeros(size(index));
for i= 1:size(index),
    if i==1
        stbdAveConc=mean(stbdO2Conc(1:index(i)));
        stbdAveTemp=mean(stbdTemp(1:index(i)));
        stbdAveTime=stbddatetime(index(i));       
    else
        stbdAveConc=[stbdAveConc;mean(stbdO2Conc(index(i-1)+1:index(i)))];
        stbdAveTemp=[stbdAveTemp;mean(stbdTemp(index(i-1)+1:index(i)))];
        stbdAveTime=[stbdAveTime;stbddatetime(index(i))];        
    end
end
stbdAveConc=[stbdAveConc;mean(stbdO2Conc(index(i)+1:end))];
stbdAveTemp=[stbdAveTemp;mean(stbdTemp(index(i)+1:end))];
stbdAveTime=[stbdAveTime;stbddatetime(end)];
stbdConcAveTS=timeseries(stbdAveConc,stbdAveTime);

stbdAveTimeVect=datenum(stbdAveTime(:,1));
EpochSecs=(stbdAveTimeVect-719529)*86400;
stbdElapsedTime=zeros(size(EpochSecs));
for i=1:size(EpochSecs,1),
        stbdElapsedTime(i,1)=(EpochSecs(i,1)-EpochSecs(1,1))/60/60;
end

StbdLinFit=[polyfit(stbdElapsedTime,stbdAveConc,1)];

p=polyfitn(stbdElapsedTime,stbdAveConc,'constant x');
StbdLinFit=[StbdLinFit,datenum(stbdAveTime(1,1)),p.R2];

y=StbdLinFit(1)*stbdElapsedTime + StbdLinFit(2);
figure;
subplot(2,1,1);
plot(stbdConcTS,'rv');hold on;plot(stbdConcAveTS,'bx');
xlabel('Time');ylabel('O2 Conc (uM)');
legend('stbd','stbd Ave','Location','Best');

subplot(2,1,2);
plot(stbdElapsedTime,stbdAveConc,'b');hold on; plot(stbdElapsedTime,y,'g');
xlabel('Elapsed Time (hrs)');ylabel('O2 Conc (uM)');
legend('stbd Ave','lin fit','Location','Best');

s=char(date(1));
dataname=strcat('Stbd','_',s);
saveas(gcf,strcat(dataname,'.tiff'));

figure;subplot(2,1,1);
plot(stbdElapsedTime, stbdAveConc,'r');xlabel('Elapsed Time (hrs)');ylabel('O2 Conc (uM)');
title(strcat(['Temp data' ' ' char(date(1))]));
subplot(2,1,2);plot(stbdElapsedTime,stbdAveTemp,'b.');
xlabel('Elapsed Time (hrs)');ylabel('Temp(degC)');
v=axis; axis([v(1) v(2) 1.4 1.55]);
s=char(date(1));
dataname=strcat('Stbd Temp','_',s);
saveas(gcf,strcat(dataname,'.tiff'));

%return;
newData1 = importdata('port_ref_optode.csv', DELIMITER, HEADERLINES);

% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
    assignin('base', vars{i}, newData1.(vars{i}));
end

port_refO2Conc=newData1.data(:,1);
port_refRoverTime=newData1.textdata(2:end,1);
[date,time]=strtok(port_refRoverTime,'T');
time=strtok(time,'T');
port_refdatetime=strcat(date,',',time);
port_refConcTS=timeseries(port_refO2Conc,port_refdatetime);

timestring=char(time);

port_refTime=zeros(size(port_refdatetime,1),6);
for i= 1:size(port_refdatetime,1),
      [hr,mn,sc]=strread(char(time(i,:)),'%d%d%f','delimiter',':');
     [yr,mth,dy]=strread(char(date(i)),'%d%d%d','delimiter','-');
     [ss,ds]=strread(num2str(sc),'%d%d','delimiter','.');
     port_refTime(i,:)=[yr,mth,dy,hr,mn,ss];
end
[index,j]=find(abs(diff(port_refTime(:,5)))>5);

port_refAveConc=zeros(size(index));
port_refAveTime=zeros(size(index));
for i= 1:size(index),
    if i==1
        port_refAveConc=mean(port_refO2Conc(1:index(i)));
        port_refAveTime=port_refdatetime(index(i));       
    else
        port_refAveConc=[port_refAveConc;mean(port_refO2Conc(index(i-1)+1:index(i)))];
        port_refAveTime=[port_refAveTime;port_refdatetime(index(i))];        
    end
end
port_refAveConc=[port_refAveConc;mean(port_refO2Conc(index(i)+1:end))];
port_refAveTime=[port_refAveTime;port_refdatetime(end)];
port_refConcAveTS=timeseries(port_refAveConc,port_refAveTime);

port_refAveTimeVect=datenum(port_refAveTime(:,1));
EpochSecs=(port_refAveTimeVect-719529)*86400;
port_refElapsedTime=zeros(size(EpochSecs));
for i=1:size(EpochSecs,1),
        port_refElapsedTime(i,1)=(EpochSecs(i,1)-EpochSecs(1,1))/60/60;
end

figure;

plot(port_refElapsedTime,port_refAveConc,'b');hold on; 
plot(stbdElapsedTime,stbdAveConc,'r');
xlabel('Elapsed Time (hrs)');ylabel('O2 Conc (uM)');
legend('Port Ref ','Stbd','Location','Best');

s=char(date(1));
dataname=strcat('PortRef_Stbd','_',s);
saveas(gcf,strcat(dataname,'.tiff'));




filename=strcat(s,'.mat');
cd ..\..\MatlabData
save(filename, 'StbdLinFit' ,'PortLinFit');
close all;
cd ..





