% Code to match data based on commensurate time
% Programmed by Rob
% Last revision: 3/30/2007
clear all;
clc;
close(gcf);
% Load the time_image and temperature_time raw data as well as filenamelist
load time_image.txt;
load temperature_time_DAQ.txt;
load filenamelist.mat;
% Round the temperature_time data to the nearest second
[num_pics,num_char] = size(filenamelist);
rounded_time_temperature_data = [round(temperature_time_DAQ(:,1)),temperature_time_DAQ(:,2)];
[time_temperature_rows,time_temperature_columns] = size(rounded_time_temperature_data);
seconds_to_find = time_image(:,2)-time_image(1,2);
% Use the image times to get a corresponding rounded temperature value for each image
w = waitbar(0,'Overall data matching progress');
matched_temperature = zeros(time_temperature_rows,num_pics);
for m = 1:num_pics;
waitbar(m/num_pics);
for n = 1:time_temperature_rows;
if rounded_time_temperature_data(n,1) == seconds_to_find(m);
matched_temperature(n,m) = rounded_time_temperature_data(n,2);
end;
end;
warning off MATLAB:divideByZero;
mean_matched_temperature(m) = sum(matched_temperature(:,m)/length(find(matched_temperature(:,m) > 0)));
end;
% Clear any infinite values by replacing NaN with the average of one value before and one after the NaN (only should be relevant if DAQ rate is less than 1 point per second)
for p = 2:(length(mean_matched_temperature)-1);
if mean_matched_temperature(p) == NaN;
mean_matched_temperature(p) = (mean_matched_temperature(p-1)+mean_matched_temperature(p+1))/2;
end;
end;
close(w);
% Plot and arrange the matched data and then save it
ltt = figure;
AH = axes('fontsize',18,'fontweight','bold');
hold on;
set(ltt,'Position',[150,50,800,600]); % [left bottom width height]
plot(seconds_to_find/3600,mean_matched_temperature,'og');
xlabel('Time [hrs]');
ylabel('Temperature [\circC]');
box on;
temperature_image = [(1:num_pics)',(mean_matched_temperature)'];
save temperature_image.txt temperature_image -ascii -tabs;
saveas(ltt,'temperature_time.fig');
print -dtiff -r300 temperature_time;