function [BlockOrganize] = DistributeIntoBlocks(Block, StartTimeBlocks, EndTimeBlocks, TimeInterval)
% [BlockOrganize] = DistributeIntoBlocks(Block, StartTimeBlocks,
% EndTimeBlocks, TimeInterval)
% To organize a raw secondly data "Block", pick up data within each time
% block, boundaried by StartTimeBlocks and EndTimeBlocks
% and to obtain the new "BlockOrganize".
% TimeInterval is the interval in seconds between StartTimeBlocks and EndTimeBlocks.

for k = 1:length(StartTimeBlocks)
    temp_start = StartTimeBlocks(k);
    temp_end = EndTimeBlocks(k);
    for i = 1:length(fieldnames(Block))
        temp_j = length(Block.(['D',num2str(i)]));
        for j = 1:temp_j
            temp_t = Block.(['D',num2str(i)])(j).t.raw;
            temp_index = intersect(find(temp_t >= temp_start),find(temp_t <= temp_end));
            %since the interval is 29 minutes 59 seconds, both side included yields to 30 minutes
            if length(temp_index)>= TimeInterval
                BlockOrganize.(['D',num2str(i)])(k).t = temp_t(temp_index);
                BlockOrganize.(['D',num2str(i)])(k).n_unit = 'counts_per_second';
                BlockOrganize.(['D',num2str(i)])(k).InstrumentID = Block.(['D',num2str(i)])(j).AdditionalMetadata.InstrumentID;
                for q = 1:8
                    temp_d = Block.(['D',num2str(i)])(j).(['n',num2str(q)]).raw(temp_index);
                    BlockOrganize.(['D',num2str(i)])(k).(['n',num2str(q)]) = temp_d;
                end    
            end

        end
    end
end
display('%--- function DistributeIntoBlocks() complete ----%');
end