%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Script for effecting step 3 of processing for data taken over a range of angles.
% This script takes the output from step 2, and ascii representation of binary bit for singles 
% and coincidence event for Alice and Bob for setting a0b0, a0b1, a1b0, and a1b1.
%
% 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 measurment taken over a range of angles

% start timing
tic;

for i = 1:NO_ELEMENT_PER_SET_L1

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

    input_file = num2str(SEPARATION_ANGLE(i));
    fid = fopen(strcat(input_file, '_reordered_output.dat'));

    % defining file pointers for writing
    fid_a0b0_alice = fopen(strcat(input_file, '_a0b0_alice.dat'), 'w');
    fid_a0b1_alice = fopen(strcat(input_file, '_a0b1_alice.dat'), 'w');
    fid_a1b0_alice = fopen(strcat(input_file, '_a1b0_alice.dat'), 'w');
    fid_a1b1_alice = fopen(strcat(input_file, '_a1b1_alice.dat'), 'w');

    fid_a0b0_bob = fopen(strcat(input_file, '_a0b0_bob.dat'), 'w');
    fid_a0b1_bob = fopen(strcat(input_file, '_a0b1_bob.dat'), 'w');
    fid_a1b0_bob = fopen(strcat(input_file, '_a1b0_bob.dat'), 'w');
    fid_a1b1_bob = fopen(strcat(input_file, '_a1b1_bob.dat'), 'w');

    fid_a0b0_alice_bob = fopen(strcat(input_file, '_a0b0_alice_bob.dat'), 'w');
    fid_a0b1_alice_bob = fopen(strcat(input_file, '_a0b1_alice_bob.dat'), 'w');
    fid_a1b0_alice_bob = fopen(strcat(input_file, '_a1b0_alice_bob.dat'), 'w');
    fid_a1b1_alice_bob = fopen(strcat(input_file, '_a1b1_alice_bob.dat'), 'w');
    
    display('file pointer opened.');

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

    line_processed = 0;
    j = 0;

    tline = fgets(fid);

    while j <= 4 && ischar(tline)

        j = j + 1;

        tline_num = str2num(tline);

        line_processed = line_processed + length(tline_num);
        
        % retrieving the 2nd and third bit
        coin_pat_bin_alice_temp = bitget(tline_num, 2);
        coin_pat_bin_bob_temp = bitget(tline_num, 3);

        % interleaving alice and bob string
        coin_pat_bin_alice_bob_temp = zeros(1, 2*length(coin_pat_bin_alice_temp));
        coin_pat_bin_alice_bob_temp(1:2:end) = coin_pat_bin_alice_temp;
        coin_pat_bin_alice_bob_temp(2:2:end) = coin_pat_bin_bob_temp;

        % convert to strings
        coin_pat_bin_alice = num2str(coin_pat_bin_alice_temp);
        coin_pat_bin_bob = num2str(coin_pat_bin_bob_temp);
        coin_pat_bin_alice_bob = num2str(coin_pat_bin_alice_bob_temp);

        % remove white spaces
        coin_pat_bin_alice = coin_pat_bin_alice(find(~isspace(coin_pat_bin_alice)));
        coin_pat_bin_bob = coin_pat_bin_bob(find(~isspace(coin_pat_bin_bob)));
        coin_pat_bin_alice_bob = coin_pat_bin_alice_bob(find(~isspace(coin_pat_bin_alice_bob)));

        % writing to the relevant file for each of the four settings
        if j == 1

            fprintf(fid_a0b0_alice, '%s', coin_pat_bin_alice);
            fprintf(fid_a0b0_bob, '%s', coin_pat_bin_bob);
            fprintf(fid_a0b0_alice_bob, '%s', coin_pat_bin_alice_bob);

        elseif j == 2

            fprintf(fid_a0b1_alice, '%s', coin_pat_bin_alice);
            fprintf(fid_a0b1_bob, '%s', coin_pat_bin_bob);
            fprintf(fid_a0b1_alice_bob, '%s', coin_pat_bin_alice_bob);

        elseif j == 3

            fprintf(fid_a1b0_alice, '%s', coin_pat_bin_alice);
            fprintf(fid_a1b0_bob, '%s', coin_pat_bin_bob);
            fprintf(fid_a1b0_alice_bob, '%s', coin_pat_bin_alice_bob);

        else

            fprintf(fid_a1b1_alice, '%s', coin_pat_bin_alice);
            fprintf(fid_a1b1_bob, '%s', coin_pat_bin_bob);
            fprintf(fid_a1b1_alice_bob, '%s', coin_pat_bin_alice_bob);

        end;

        if j == 4
            j = 0;
        end;

        tline = fgets(fid);

        if ~ischar(tline)
            break;
        end;

    end;

    % closing all file pointers
    fclose(fid);

    fclose(fid_a0b0_alice);
    fclose(fid_a0b1_alice);
    fclose(fid_a1b0_alice);
    fclose(fid_a1b1_alice);

    fclose(fid_a0b0_bob);
    fclose(fid_a0b1_bob);
    fclose(fid_a1b0_bob);
    fclose(fid_a1b1_bob);

    fclose(fid_a0b0_alice_bob);
    fclose(fid_a0b1_alice_bob);
    fclose(fid_a1b0_alice_bob);
    fclose(fid_a1b1_alice_bob);

    display('completed, file pointers closed.');
    display(strcat('total no of events processed: ', num2str(line_processed)));

end;

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

% stop timing
toc;

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

end;
