function []= OptodeSlopesv4()
DELIMITER = ',';
HEADERLINES = 1;

%%%%%%%%%%%%%%%%%%%%%%%%% PORT  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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);
[index,j]=find(abs(diff(datenum(portTime))>0.008));

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));
%matlab conversion to epoch use datenum(1970,1,1)=719529
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');
%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)
PortLinFit=[PortLinFit,portAveConc(1,1),portAveConc(end,1),datenum(portAveTime(1,1)),datenum(portAveTime(end,1)),0,portElapsedTime(end)];


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

%%%%%%%%%%%%%%%%%%%% STBD  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

newData1 = importdata('starboard_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);


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

[index,j]=find(abs(diff(datenum(stbdTime))>0.008));

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);
% convert string to number: DATENUM(V) converts one or more date 
% vectors V into serial date numbers N, the integre number represents
% the date, the decimal portion is the time of day see ex:
% datestr(719531.5); ans = 03-Jan-1970 12:00:00
% datestr(719532.6); ans = 04-Jan-1970 14:24:00

stbdAveTimeVect=datenum(stbdAveTime(:,1));

%to calculate elapsed time in seconds convert to epoch secs
%subtract seconds since Jan 1, 1970
%datestr(719529)= 01-Jan-1970
%60 sec/min*60 min/hr*24hr/day
EpochSecs=(stbdAveTimeVect-719529)*86400;   
stbdElapsedTime=zeros(size(EpochSecs));
for i=1:size(EpochSecs,1),
  %time calculated is epoch secs * 1min/60 sec *1hr/60min    (= time in hrs)
        stbdElapsedTime(i,1)=(EpochSecs(i,1)-EpochSecs(1,1))/60/60;
end

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

%computes polyfit with R-squared
%p=polyfitn(stbdElapsedTime,stbdAveConc,'constant x');

%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)
StbdLinFit=[StbdLinFit,stbdAveConc(1,1),stbdAveConc(end,1),datenum(stbdAveTime(1,1)),datenum(stbdAveTime(end,1)),0,stbdElapsedTime(end)];



y=StbdLinFit(1)*stbdElapsedTime + StbdLinFit(2);
figure;
subplot(2,1,1);
plot(stbdConcTS,'ro');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'));


%%%%%%%%%%%%%%%%%%%% PORT REF  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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);
[index,j]=find(abs(diff(datenum(port_refTime))>0.008));

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


PortRefData=[];
PortRefData=[mean(port_refAveConc),port_refAveConc(1),datenum(port_refAveTime(1,1)),datenum(port_refAveTime(end,1)), port_refElapsedTime(end)];
filename=strcat(s,'v2.mat');

%%%%%%%%%%%%%%%%%%%% STBD REF  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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

stbd_refO2Conc=newData1.data(:,1);
stbd_refRoverTime=newData1.textdata(2:end,1);
[date,time]=strtok(stbd_refRoverTime,'T');
time=strtok(time,'T');
stbd_refdatetime=strcat(date,',',time);
stbd_refConcTS=timeseries(stbd_refO2Conc,stbd_refdatetime);

timestring=char(time);

stbd_refTime=zeros(size(stbd_refdatetime,1),6);
for i= 1:size(stbd_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','.');
     stbd_refTime(i,:)=[yr,mth,dy,hr,mn,ss];
end
%[index,j]=find(abs(diff(stbd_refTime(:,5)))>5);
[index,j]=find(abs(diff(datenum(stbd_refTime))>0.008));

stbd_refAveConc=zeros(size(index));
stbd_refAveTime=zeros(size(index));
for i= 1:size(index),
    if i==1
        stbd_refAveConc=mean(stbd_refO2Conc(1:index(i)));
        stbd_refAveTime=stbd_refdatetime(index(i));       
    else
        stbd_refAveConc=[stbd_refAveConc;mean(stbd_refO2Conc(index(i-1)+1:index(i)))];
        stbd_refAveTime=[stbd_refAveTime;stbd_refdatetime(index(i))];        
    end
end
stbd_refAveConc=[stbd_refAveConc;mean(stbd_refO2Conc(index(i)+1:end))];
stbd_refAveTime=[stbd_refAveTime;stbd_refdatetime(end)];
stbd_refConcAveTS=timeseries(stbd_refAveConc,stbd_refAveTime);

stbd_refAveTimeVect=datenum(stbd_refAveTime(:,1));
EpochSecs=(stbd_refAveTimeVect-719529)*86400;
stbd_refElapsedTime=zeros(size(EpochSecs));
for i=1:size(EpochSecs,1),
        stbd_refElapsedTime(i,1)=(EpochSecs(i,1)-EpochSecs(1,1))/60/60;
end

figure;

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

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

StbdRefData=[];
StbdRefData=[mean(stbd_refAveConc),stbd_refAveConc(1),datenum(stbd_refAveTime(1,1)),datenum(stbd_refAveTime(end,1)), stbd_refElapsedTime(end)];
filename=strcat(s,'v2.mat');

%pause(5);
cd ..\..\MatlabData
save(filename, 'StbdLinFit' ,'PortLinFit','StbdRefData','PortRefData');
close all;
cd ..
