%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% Script for effecting step 1 of processing for data taken over a range of angles.
% This script splits the data file into individual data file for each angle to the nearest 
% symmetrization set
%
% April 9, 2015
% Author: Poh Hou Shun, Centre for Quantum Technologies, National University of Singapore
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

SEPARATION_ANGLE_MAX = 0.15; % inserting angle for maximum violation
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 = 4; % 4 measurement for symmetrization
NO_ELEMENT_PER_SET_L2 = 4; % 4 combinations of settings for complete inequality
NO_ELEMENT_PER_SET_L3 = length(SEPARATION_ANGLE); % for this set measurement taken over a range of angles

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

% start timing
tic;

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

input_file = 'data';

fid = fopen(strcat(input_file, '.dat'));

% counting the number of lines in the file and determining the number of lines corresponding 
% to the nearest complete set to process
STOP_LINE = floor(linecount(fid)/NO_ELEMENT_PER_SET_L1/NO_ELEMENT_PER_SET_L2/NO_ELEMENT_PER_SET_L3)...
*NO_ELEMENT_PER_SET_L1*NO_ELEMENT_PER_SET_L2*NO_ELEMENT_PER_SET_L3;

fclose(fid);

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

fid = fopen(strcat(input_file, '.dat'));

display('file pointer opened.');

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

% define terminate process flag
terminate_process = 0;

% line counter for file
line_num = 0;

% continuously loop through the file
while 1
   
    % loop through range of angles
    for k = 1:NO_ELEMENT_PER_SET_L3

        % loop through set of settings
        for j = 1:NO_ELEMENT_PER_SET_L2

            line_num = line_num + NO_ELEMENT_PER_SET_L1;

            if line_num > STOP_LINE
            	terminate_process = 1;
                break;
            end;

            % loop through symmetrization set
            for i = 1:NO_ELEMENT_PER_SET_L1

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

                tline_num = str2num(tline);
                
                % appending to output file
                dlmwrite(strcat(num2str(SEPARATION_ANGLE(k)), '_output.dat'), tline_num, 'delimiter', ' ', '-append');

            end;

        end;
        
        % stop when nearest complete set is processed
        if	terminate_process == 1
            break;
        end;

    end;

    % stop when nearest complete set is processed
    if	terminate_process == 1
        break;
	end;

end;

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

fclose(fid);

display('file range split completed, file pointers closed.');

% stop timing
toc;

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