close all, clear all

path = uigetdir();

leggenda = 1;

%% Setup tempo
global startTime dt

%startTime = datenum('May 25, 2007  14:25:00.0000'); % anno mese giorno ora minuti secondi
%dt = datenum('Jan 0, 0000  0:00:30.0000'); % tempo di campionamento

old = pwd;
cd(path)
impostazioni()
cd(old)

datestr(startTime,'HH:MM:SS:FFF dd/mm')
datestr(dt,'HH:MM:SS:FFF dd/mm')

%% load csv files

files = dir((fullfile(path,'*.dat')));

start = 1;

for j = 1:length(files)
    data = load(fullfile(path,files(j).name));
    
    id = data(start:end,1);
    temperatureR = data(start:end,2);
    humidityR = data(start:end,3);
    totalRadiationR = data(start:end,4);
    activeRadiationR = data(start:end,5);
    internalVoltageR = data(start:end,6);

    log(j).id = id;
    log(j).temperature = -39.60 + 0.01*temperatureR;                                    % [°C]
    log(j).humidity = -4 + 0.0405*humidityR + (-2.8 * 10^-6)*(humidityR.^2);            % [%]
    log(j).humidity_true = (log(j).temperature - 25) .* (0.01 + 0.00008*humidityR) + log(j).humidity; % [%]
    log(j).activeRadiation = 0.625 * 1e6 * (activeRadiationR/100000) * 1000;            % [lux]
    log(j).totalRadiation = 0.769 * 1e5 * (totalRadiationR/100000) * 1000;              % [lux]
    log(j).internalVoltage = internalVoltageR/4096 * 3;                                 % [V]
    log(j).name = files(j).name;
end
m = inf;
for j = 1:length(files)
    if ( length( log(j).temperature ) < m )
        m = length( log(j).temperature );
    end
end
for j = 1:length(files)
    log(j).id = log(j).id(1:m);
    log(j).temperature = log(j).temperature(1:m);
    log(j).humidity = log(j).humidity(1:m);
    log(j).humidity_true = log(j).humidity_true(1:m);
    log(j).activeRadiation = log(j).activeRadiation(1:m);
    log(j).totalRadiation = log(j).totalRadiation(1:m);
    log(j).internalVoltage = log(j).internalVoltage(1:m);                            % [V]
end



%% plots

if leggenda
    for j = 1:length(files)
        leg2 = log(j).name;
        leg(j,:) = leg2(1:end-4);
    end
end

t = (0:m-1);

figure(1)
plot(t,[log.temperature])
xlabel('n')
ylabel('°C')
title('Temperature')
if leggenda 
    legend(leg)
end
axis tight
dcm_obj = datacursormode(gcf);
set(dcm_obj,'UpdateFcn',@myfunction)

figure(2)
plot(t,[log.humidity])
title('Humidity concentration')
xlabel('n')
ylabel('%')
if leggenda 
    legend(leg)
end
axis tight
dcm_obj = datacursormode(gcf);
set(dcm_obj,'UpdateFcn',@myfunction)

figure(3)
plot(t,[log.humidity_true])
title('Humidity concentration (true)')
xlabel('n')
ylabel('%')
if leggenda 
    legend(leg)
end
axis tight
dcm_obj = datacursormode(gcf);
set(dcm_obj,'UpdateFcn',@myfunction)

figure(4)
plot(t, [log.activeRadiation])
title('Active radiation')
xlabel('n')
ylabel('lux')
if leggenda 
    legend(leg)
end
axis tight
dcm_obj = datacursormode(gcf);
set(dcm_obj,'UpdateFcn',@myfunction)

figure(5)
plot(t, [log.totalRadiation])
title('Total solar radiation')
xlabel('n')
ylabel('lux')
if leggenda 
    legend(leg)
end
axis tight
dcm_obj = datacursormode(gcf);
set(dcm_obj,'UpdateFcn',@myfunction)

figure(6)
plot(t(1:end-1), diff([log.id]))
axis tight
dcm_obj = datacursormode(gcf);
set(dcm_obj,'UpdateFcn',@myfunction)

