function aggData=aggregateData(data)
%This function aggregates data over the various lanes
locations=unique(data.location);nlocations=numel(locations);
dates=unique(data.date);ndates=numel(dates);
tms=unique(data.hhmm);ntms=numel(tms);

% nperlocation=numel(dates)*numel(tms);
% nperdate=numel(tms);

%split data per location to speed up
pdata=data;
pdata=rmfield(data,{'direction','dvk', 'numlanes','hour','minute','vms'});
flds=fields(pdata);
nflds=numel(flds);
naggobs=nlocations*ndates*ntms;
for(i=1:nflds)
     eval(['aggData.' flds{i} '=repmat(data.' flds{i} '(1),[nlocations ndates ntms]);'])
end
filled=true(nlocations,ndates,ntms);
aggData.nlanes=aggData.lane;
aggData=rmfield(aggData,'lane');
j=1;
%%
for(l=1:nlocations)
    waitbar(l/nlocations)
    goodlocation=pdata.location==locations(l);
     for(i=1:nflds)
            eval(['partdata.' flds{i} '=pdata.' flds{i} '(goodlocation);'])
     end

    for(d=1:ndates)
        datej=dates(d);
        gooddate=find(partdata.date==dates(d));
       
        for(t=1:ntms)
            tmsj=tms(t);
            aggData.location(l,d,t)=locations(l);
            aggData.date(l,d,t)=datej;
            aggData.hhmm(l,d,t)=tmsj;
            
            goodtimeix=partdata.hhmm(gooddate)==tms(t);
            goodtime=gooddate(goodtimeix);

            if(isempty(goodtime))
                aggData.reliable(l,d,t)=0;
                aggData.flows(l,d,t)=NaN;
                aggData.speed(l,d,t)=NaN;
                aggData.nlanes(l,d,t)=NaN;
                
            else
                            aggData.reliable(l,d,t)=all(partdata.reliable(goodtime));
            aggData.flows(l,d,t)=sum(partdata.flows(goodtime));
            %inproduct
            aggData.speed(l,d,t)=(partdata.speed(goodtime)'*partdata.flows(goodtime))/aggData.flows(l,d,t);
            aggData.nlanes(l,d,t)=numel(goodtime);
            end
            filled(l,d,t)=true;
        end
    end
end
%%
%now remove the overlength values:
 flds=fields(pdata);
% for(i=1:nflds)
%      eval(['aggData.' flds{i} '(j:end)=[];'])
% end
[a,b,c]=find(~filled);
ix=sub2ind(size(filled),a,b,c);
for(i=1:nflds)
    if(~strcmp(flds{i},'reliable'))
        eval(['aggData.' flds{i} '(ix)=NaN;'])
    end
end
 aggData.reliable(ix)=0;
 
aggData.hhmm=double(aggData.hhmm);%otherwise, no operations are possible
aggData.decimaltime=(floor(aggData.hhmm/100))+100/60*rem(aggData.hhmm/100,1);
aggData.dateSt=num2str(dates);
aggData.datenum=datenum(aggData.dateSt,'yyyymmdd');


