% Parsing program of huge Crystfel data file to extract only h k l
% and intensity up to a certain h k l, ignoring negative intensities.
% Note: this program will produced a parsed file with a size in the range
% of 100s of MB!! It takes a couple of minutes to generate on my 2018
% macbook pro, too!

clear; close all;
Str = fileread('all.dat');
CStr = strsplit(Str, newline);
N = size(CStr,2);

fileID = fopen('exptable.dat','w');
fileChunk=0;
printToFile = 0;
for i = 1:N
    % Find start of data chunk
    lineId = contains(CStr(i), 'h    k    l'); % Logical identifier of lines that start new data chunk
    if (lineId == logical(1)) % i.e. YES! this line contains 'h k l'!!
        fileChunk = fileChunk+1;
        %CStr(i)
        fprintf(fileID,'Chunk %i',fileChunk);
        printToFile = 1; % Allows next line to be printed to file
    end
    % Find end of data chunk
    lineId2 = contains(CStr(i), 'End of reflections'); % Line below the end of latest data chunk
    if (lineId2 == logical(1)) % i.e. YES! this line contains 'End of reflections'!!
        %CStr(i-1)
        printToFile = 0; % Stops next line to be printed to file
    end
    % Writing to file part of loop
    if (printToFile == 1)
        thisLine = cell2mat(CStr(i));
        fprintf(fileID,'%28.28s \n',thisLine);
    end
end
fprintf(fileID,'%i \n',fileChunk);fileChunk % Number of diffraction patterns
fclose(fileID);

