
% Template MATLAB code for visualizing data using the STEM
% function.

% Prior to running this MATLAB code template, assign the channel ID to read
% data from to the 'readChannelID' variable. Also, assign the field ID
% within the channel that you want to read data from to plot.

% TODO - Replace the [] with channel ID to read data from:
readChannelID = YOUR_CHANNELID_HERE;
% TODO - Replace the [] with the Field ID to read data from:
fieldID = 2;

% Channel Read API Key 
% If your channel is private, then enter the read API
% Key between the '' below: 
readAPIKey = 'YOUR_READ_API_KEY_HERE';

%% Read Data %%
[data, time] = thingSpeakRead(readChannelID, 'Field', fieldID, 'NumPoints', 8000, 'ReadKey', readAPIKey);
%% Change time zone to Helsinki
time = time + 0.08333333;

%%Initialize two arrows, where cleaned up comulative datas will be stored to.
cdata=[];
ctime=[];

%%Firt we check, how many rows we have in the raw data, where is 'NaN' values
rows = size(time);

for i = 1:rows
    %%Test, if we have 'NaN' or not    
    if isnan(data(i)) | data(i) < 0
           
    else
       %% If good valid values, add it to the arrows
       cdata = [cdata; data(i)];
       %% Time format change
       ctime = [ctime; datestr(time(i), 'dd.mm.yyyy')];
    end
end

%%How many valid rows we have finally
crows = size(cdata);
startData = cdata(1);
startTime = ctime(1,1:10);
%% Initialize two arrows, where daily consumption will be stored to
pvdata=[];
pvtime=[];

for i = 1:crows
    if startTime == ctime(i,1:10)
       %% Calculate the daily consumption: The highest daily consumption - the lowest daily consumption 
       pv = cdata(i)-startData;
       %% The current day
       ctime(i,1:10);
    else
    %% The day changed. Store the previous day's final value
    
       
    pvdata = [pvdata; pv];
    paiva=ctime((i-1),1:10);
    

    %%Change format to be suitable for plotting
    str = datetime(paiva,'InputFormat','dd.MM.yyyy');
    pvtime = [pvtime; str];
    %% Move to the next day before we go back to the if-loop again
    startData = cdata(i);
    startTime = ctime(i,1:10);    
    end
end


%% The ongoing day values have to still add to the daily arrows
pvdata = [pvdata; pv];
    paiva=ctime((i),1:10);
    str = datetime(paiva,'InputFormat','dd.MM.yyyy');
pvtime = [pvtime; str];
    
%% Now the daily based results are ready in the daily arrows
%% Next we clarify how many day bars is needed
k=size(pvdata);


%% Because the amount of the result points is limeted, the oldest stored day is more or less uncomplete
%% Remove the oldest day bar and replace it with the average of the other days
summa = 0;
for k = 2:(k)
    summa = summa + pvdata(k);   
end

ka = summa/(k-1);
%%round to integer:
ka = round(ka);
pvdata(1) = ka;



%% plot
b = bar(pvtime,pvdata);
b.FaceColor = 'flat';
b.CData(1,:) = [.1 50 .1];
b.FaceColor = 'flat';
   

%% visible also the numeric values
for k = 1:(k)
    m=text(k-1,pvdata(k)+5,num2cell(pvdata(k)),'FontSize',10);
    set(m, 'Rotation',90)
end
h=text(0,10,'   ka','FontSize',10);
set(h,'Rotation',90)

xtickformat('eee,dd.MM')


xtickangle(90)
