% ------------------------------
% Filename      : PlotLog.m
% Author        : Sven Maerivoet
% Last modified : 14/11/2015
% Target        : MATLAB 7.5
% 
% All rights reserved.
% ------------------------------

%% ========
% LOAD DATA
% =========
filename = 'LOGGER17.CSV';
kBlockSize = 60 * 15; % aggregate measurements over 15 minutes
kMinTemperature = 15;
kMaxTemperature = 25;
kHourIncrement = 6;
kTemperatureOffset = -2;

%% ========
% LOAD DATA
% =========
disp('Loading data...');
fieldSpecs = ...
	['%d ' ...
	 '%d %q ' ...
	 '%f %d %d'];
[tMs, ...
    tDoW, tDateTimeDesc, ...
	tTemperature, tLightLevel, tVcc] = textread(filename,fieldSpecs,'headerlines',1,'delimiter',',');

% offset date-timestamps by representing them as an hour
tDateTimeVec = datevec(tDateTimeDesc,'yyyy/mm/dd HH:MM:SS');
t0 = ((tDateTimeVec(1,4) * 3600) + (tDateTimeVec(1,5) * 60) + tDateTimeVec(1,6)) / 3600;

%% ===================
% AVERAGE MEASUREMENTS
% ====================
disp('Averaging data...');
nrOfMeasurements = length(tMs);
nrOfBlocks = floor(nrOfMeasurements / kBlockSize);

tMsAvg = zeros(nrOfBlocks,1);
tDoWAvg = zeros(nrOfBlocks,1);
tDateTimeVecAvg = zeros(nrOfBlocks,size(tDateTimeVec,2));
tTemperatureAvg = zeros(nrOfBlocks,1);
tLightLevelAvg = zeros(nrOfBlocks,1);
tVccAvg = zeros(nrOfBlocks,1);
for blockIndex = 1:nrOfBlocks
    measurementIndex = ((blockIndex - 1) * kBlockSize) + 1;
    tMsAvg(blockIndex) = mean(tMs(measurementIndex:measurementIndex + kBlockSize - 1));
    tDoWAvg(blockIndex) = mean(tDoW(measurementIndex:measurementIndex + kBlockSize - 1));
    tDateTimeVecAvg(blockIndex,:) = mean(tDateTimeVec(measurementIndex:measurementIndex + kBlockSize - 1,:));
    tTemperatureAvg(blockIndex) = mean(tTemperature(measurementIndex:measurementIndex + kBlockSize - 1));
    tLightLevelAvg(blockIndex) = mean(tLightLevel(measurementIndex:measurementIndex + kBlockSize - 1));
    tVccAvg(blockIndex) = mean(tVcc(measurementIndex:measurementIndex + kBlockSize - 1));
end

%% ================
% PLOT MEASUREMENTS
% =================
disp('Plotting data...');

figure
hold on;
[hAxes, hTemperatureGraph, hLightLevelGraph] = plotyy(...
	tMsAvg,tTemperatureAvg + kTemperatureOffset, ...
    tMsAvg,(tLightLevelAvg / max(tLightLevelAvg)) * 100);

hTemperatureGraph.LineStyle = '-';
hTemperatureGraph.LineWidth = 5;
hTemperatureGraph.Color = 'r';
hAxes(1).YColor = 'r';
hLightLevelGraph.LineStyle = '-';
hLightLevelGraph.LineWidth = 2;
hLightLevelGraph.Color = 'k';
hAxes(2).YColor = 'k';

box on;
grid on;
axis tight;

hAxes(1).YLim = [kMinTemperature,kMaxTemperature];
hAxes(1).YTick = kMinTemperature:kMaxTemperature;
yTicks = hAxes(1).YTick;
for i = 1:length(yTicks);
    yTickLabels{i} = sprintf('%d °C',yTicks(i));
end
hAxes(1).YTickLabel = yTickLabels;

hAxes(2).YLim = [0,100];
hAxes(2).YTick = 0:10:100;
yTicks = hAxes(2).YTick;
for i = 1:length(yTicks);
    yTickLabels{i} = sprintf('%d%%',yTicks(i));
end
hAxes(2).YTickLabel = yTickLabels;

tBegin = min(tMs);
tEnd = max(tMs);
tRange = tEnd - tBegin;
tickIncrement = 1000 * 3600 * kHourIncrement;
nrOfTicks = round(tRange / tickIncrement);
set(gca,'XTick',tBegin:tickIncrement:tEnd);
tickRange = round(t0 + (tBegin:tickIncrement:tEnd) / 1000 / 3600);
tickLabels = zeros(nrOfTicks,1);
set(gca,'XTickLabel',mod(tickRange,24));

xlabel('Time of day [h]');
ylabel(hAxes(1),'Temperature [°C]');
ylabel(hAxes(2),'Light level [%]');
legend('Temperature','Light level','Location','NorthEast');
