%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Script for effecting step 2 of processing for data taken over a range of angles.
% This script takes the output from step 1, restore the correct mapping from the symmetrization
% procedure and interleave them into a single line
%
% April 9, 2015
% Author: Poh Hou Shun, Centre for Quantum Technologies, National University of Singapore
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

SEPARATION_ANGLE_MAX = 0.15;
SEPARATION_ANGLE = 0:0.02:0.24;
SEPARATION_ANGLE = [SEPARATION_ANGLE(1:8), SEPARATION_ANGLE_MAX, SEPARATION_ANGLE(9:end)];

NO_ELEMENT_PER_SET_L1 = length(SEPARATION_ANGLE); % for this set measurement taken over a range of angles

% start timing
tic;

% loop through range of angles
for i = 1:NO_ELEMENT_PER_SET_L1

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    input_file = num2str(SEPARATION_ANGLE(i));
    fid = fopen(strcat(input_file, '_output.dat'));
    
    display('file pointer opened.');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    tline = fgets(fid);

    while ischar(tline)

        % no swapping
        tline_num = str2num(tline);
        tline_num_no = tline_num;

        % a90 swap
        tline = fgets(fid);
        if ~ischar(tline)
            break;
        end;

        tline_num = str2num(tline);
        tline_num_a90 = tline_num;
        tline_num_a90(find(tline_num == 5))  = 6;
        tline_num_a90(find(tline_num == 6))  = 5;
        tline_num_a90(find(tline_num == 9))  = 10;
        tline_num_a90(find(tline_num == 10)) = 9;

        % b90 swap
        tline = fgets(fid);
        if ~ischar(tline)
            break;
        end;

        tline_num = str2num(tline);
        tline_num_b90 = tline_num;
        tline_num_b90(find(tline_num == 5))  = 9;
        tline_num_b90(find(tline_num == 6))  = 10;
        tline_num_b90(find(tline_num == 9))  = 5;
        tline_num_b90(find(tline_num == 10)) = 6;

        % ab90 swap
        tline = fgets(fid);
        if ~ischar(tline)
            break;
        end;

        tline_num = str2num(tline);
        tline_num_ab90 = tline_num;
        tline_num_ab90(find(tline_num == 5))  = 10;
        tline_num_ab90(find(tline_num == 6))  = 9;
        tline_num_ab90(find(tline_num == 9))  = 6;
        tline_num_ab90(find(tline_num == 10)) = 5;

        % evaluates the length of the shortest string from symmetrization
        min_length = min([length(tline_num_no), length(tline_num_a90), length(tline_num_b90), length(tline_num_ab90)]);

        tline_num_no = tline_num_no(1:min_length);
        tline_num_a90 = tline_num_a90(1:min_length);
        tline_num_b90 = tline_num_b90(1:min_length);
        tline_num_ab90 = tline_num_ab90(1:min_length);

        % interleaving the strings
        tline_num_combined = zeros(1, min_length*4);
        tline_num_combined(1:4:end) = tline_num_no;
        tline_num_combined(2:4:end) = tline_num_a90;
        tline_num_combined(3:4:end) = tline_num_b90;
        tline_num_combined(4:4:end) = tline_num_ab90;
        
        % writing to file
        dlmwrite(strcat(input_file, '_reordered_output.dat'), tline_num_combined, 'delimiter', ' ', '-append');

        % next line read for EOF check/next line read
        tline = fgets(fid);

        if ~ischar(tline)
            break;
        end;

    end;

    fclose(fid);

end;

display('swap completed, file pointers closed.');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% stop timing
toc;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
