function [StructOrg] = DistributeIntoQBlocks(Struct, StartTimeBlocks, EndTimeBlocks)
% [Struct_New] = DistributeIntoQBlocks(Struct, StartTimeBlocks, EndTimeBlocks)
% Input the OPC-ID-ordered "Struct" and given StartTime Blocks and Endtime
% Blocks, and output the redistributed measurement within each of the
% blocks

OPC_ID = {'9284','9290','9281','9278','9287','9286'};

length_k = length(StartTimeBlocks);
for k = 1:length_k
    temp_start = StartTimeBlocks(k);
    temp_end = EndTimeBlocks(k);
    for i = 1:length(fieldnames(Struct))
        temp_j = length(Struct.(['OPC',OPC_ID{i}]));
        for j = 1:temp_j
            temp_t = Struct.(['OPC',OPC_ID{i}])(j).t.raw;
            temp_index = intersect(find(temp_t >= temp_start),find(temp_t < temp_end));
            %since the interval is 30 minutes, only left bounday is included
            if ~isempty(temp_index)
                break  %as long as not empty data, break following loop
            end
        end
        StructOrg.(['OPC',OPC_ID{i}])(k).t = temp_t(temp_index); 
        for q = 1:8
            temp_d = Struct.(['OPC',OPC_ID{i}])(j).(['n',num2str(q)]).raw(temp_index);
            StructOrg.(['OPC',OPC_ID{i}])(k).(['n',num2str(q)]) = double(temp_d);             
        end 
        StructOrg.(['OPC',OPC_ID{i}])(k).bin_unit = 'counts_per_second';  
        StructOrg.(['OPC',OPC_ID{i}])(k).z = Struct.(['OPC',OPC_ID{i}])(j).z;     
    end
    display(['The ',num2str(k),' th (of ', num2str(length_k),') time blocks complete']);
end
display('%--- function DistributeIntoQBlocks() complete ----%');

end
