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



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);
StbdO2Sat=newData1.data(:,2);
StbdTemperature=newData1.data(:,3);
StbdRoverTime=newData1.textdata(2:end,1);
[date,time]=strtok(StbdRoverTime,'T');
time=strtok(time,'T');
Stbddatetime=strcat(date,',',time);
StbdTS=timeseries(StbdO2Sat,Stbddatetime);
StbdConcTS=timeseries(StbdO2Conc,Stbddatetime);
StbdTemp=timeseries(StbdTemperature,Stbddatetime);

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

RefO2Conc=newData1.data(:,1);
RefO2Sat=newData1.data(:,2);
RefTemperature=newData1.data(:,3);
RefRoverTime=newData1.textdata(2:end,1);
[date,time]=strtok(RefRoverTime,'T');
time=strtok(time,'T');
Refdatetime=strcat(date,',',time);
RefTS=timeseries(RefO2Sat,Refdatetime);
RefConcTS=timeseries(RefO2Conc,Refdatetime);
RefTemp=timeseries(RefTemperature,Refdatetime);

figure;
plot(PortConcTS,'rx');hold on;plot(StbdConcTS,'gx');plot(RefConcTS,'bx')
legend('Port','Stbd','Ref');
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)]);

pwd

s=input('enter the experiment date:','s');
filename=strcat(s,'.mat');
save(filename,'PortTS','RefTS','StbdTS','PortConcTS','RefConcTS','StbdConcTS');
dataname=strcat('Optode Results','_',s);
title(dataname);
saveas(gcf,strcat(dataname,'Conc.fig'),'fig');
saveas(gcf,strcat(dataname,'Conc.tiff'));

figure; plot(RefTemp,'mx');hold on; plot(StbdTemp,'kx');plot(PortTemp,'cx');
legend('Ref Temp','Stbd Temp','Port Temp');



figure;
plot(datenum(Portdatetime),PortO2Conc,'r');hold on;plot(datenum(Stbddatetime),StbdO2Conc,'g');plot(datenum(Refdatetime),RefO2Conc,'b')
legend('Port','Stbd','Ref');
xlabel('Time');ylabel('O2 Conc (uM)');
datetick('x','dd HH:MM','keepticks');

figure;
plot(datenum(Portdatetime),PortTemperature,'r');hold on;plot(datenum(Stbddatetime),StbdTemperature,'g');plot(datenum(Refdatetime),RefTemperature,'b')
legend('Port','Stbd','Ref');
xlabel('Time');ylabel('Temp (deg C)');
axis tight;
datetick('x', 'dd HH:MM','keepticks');

