close all;
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);
PortO2Sat=newData1.data(:,2);
PortTemperature=newData1.data(:,2);
PortRoverTime=newData1.textdata(2:end,1);
[date,time]=strtok(PortRoverTime,'T');
Portdatetime=strcat(date,',',time);
PortTS=timeseries(PortO2Sat,Portdatetime);
PortConcTS=timeseries(PortO2Conc,Portdatetime);

time=strtok(time,'T');

timestring=char(time);
hour=[];
min=[];
sec=[];
for i= 1:size(timestring,1),
    [hr,mn,sc]=strread(timestring(i,:),'%d%d%f','delimiter',':');
    hour=[hour;hr];
    min=[min;mn];
    sec=[sec;sc];
end
[index,j]=find(abs(diff(min))>2);

PortAveConc=[];
PortAveTime=[];
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
PortConcAveTS=timeseries(PortAveConc,PortAveTime);
figure;
plot(PortConcTS,'r');hold on;plot(PortConcAveTS,'b')
legend('Port','Port Ave');
xlabel('Time');ylabel('O2 Conc (uM)');
% set(gca,'XTick',[0 0.5 1.0]);
% set(gca,'XTickLabel',[Refdatetime(1);Refdatetime(floor(size(Refdatetime,1)/2));Refdatetime(end)]);
% saveas(gcf,strcat(dataname,'Conc.fig'),'fig');
% saveas(gcf,strcat(dataname,'Conc.tiff'));
% 
 






