%This function inserts new time intervals in betweem two demand peaks.
%There will be one new interval for every 2h. Vectors timstart, duration
%and flows must have the same length

function [ timesvec,flowsvec ] = getTimeIntervals(timestart, duration , flows,  finaltime )


% One extra phase every 1h
interval=1;
timesvec=[0];
for i=1:length(timestart)-1
    timesvec=[timesvec timestart(i)  timestart(i)+duration(i)];
   
    
    deltatime=(timestart(i+1)-timesvec(end));
    inbetweenPhases=ceil(deltatime/(interval*3600));
    inbetweentimes=linspace(timesvec(end),timestart(i+1),inbetweenPhases);
    timesvec=[timesvec inbetweentimes(2:end-1)  ];
    
      
       
end

timesvec=[timesvec timestart(end)  timestart(end)+duration(end)];
deltatime=(finaltime-timesvec(end));
inbetweenPhases=ceil(deltatime/(interval*3600));
inbetweentimes=linspace(timesvec(end),finaltime,inbetweenPhases);
timesvec=[timesvec inbetweentimes(2:end)];

flowsvec=zeros(length(timesvec)-1,1);


for i=1:length(flowsvec)
    
    if ismember(timesvec(i),timestart)
    
    [~, array_position] = min(abs(timesvec(i) -timestart ));
    flowsvec(i)=flows(array_position);
    end
   
end


end

