function xop = gtLoadReflectionsFromFile(file)
% GTLOADREFLECTIONSFROMFILE
%     Specifiy the file from which to load
%     Loads the list of reflections (hkl, intensity, 
%     multiplicity, 2theta, d-spacing) from text files saved from Diamond 
%     (.csv) or XOP (.dat).
%
%     xop = gtLoadReflectionsFromFile(filename)
%     --------------------------------
%     Reads from the specified file
%     If the file is not the right type, it gives instructions for how to 
%     get the right thing.

%     Diamond -> .csv parameters file
%         XOP -> .dat parameters file
%     INPUT: 
%       filename (including path)
%
%     OUTPUT :
%       xop     = structure with fields from the text file
%

isreflections = true;
if (~isempty(strfind(file,'.csv')))
    % .csv file
    delim = ';';
    pattern = '%d8 %f32 %f32 %f32 %f32 %d8 %d8 %d8 %d8';
    columns = 9;
elseif (~isempty(strfind(file,'.dat')) && ~strcmpi(file,'spacegroups.dat'))
    % .dat file
    delim = 'space';
    pattern = '%d8 %d8 %d8 %f32 %f32 %f32 %f32 %d8 %f32 %f32';
    columns = 10;
else
    % give the info message
    disp('The file selected is not the right kind (.csv or .dat) ')
    disp(' ')
    disp('If you have a .cif file for the current material, open it with Diamond')
    disp('  and let the program create the structure picture. Check the option ''Create packaing diagram''')
    disp('  and leave unchecked the option ''Connect atoms''.')
    disp('  When finished, show the powder pattern (top right corner) and select a wavelength')
    disp('  value in Angstrom, 2theta minimum and maximum in degrees.')
    disp('  Press enter when finished to calculate the reflection parameters.')
    disp('  Right-Click on the table header and save it as .csv file in the local directory. Close Diamond.')
    disp(' ')
    disp('If you don''t have a .cif file for the current material, open xop shell program.')
    disp('  On the top menu choose ''Optics'', ''Crystals'' and ''XPOWDER''.')
    disp('  In the tab ''Unit Cell'' insert the lattice parameters')
    disp('  and the atoms positions in the unit cell. Refer to the International Tables of Crystallography.')
    disp('  When done, click on tab ''Reflections'' and ''Set Parameters''.')
    disp('  Set the wavelength and the maximum value for 2-theta and click on ''Accept''.')
    disp('  Finally save the table in a .dat file in the local directory and use the function ''gtLoadReflections'' again.')
    disp(' ')
    disp('If you are really depressed, use this link to find the atoms positions:')
    disp('    http://it.iucr.org/Ab/ch7o1v0001/')
    
    isreflections = false;
end

if isreflections
    disp(['Opening file ' char(file) '...'])
    fid = fopen(file, 'r');
    if (fid == -1)
        disp('Error opening file...')
        return
    end
    if strcmpi(delim, 'space')
        testo = textscan(fid, '%s', columns);
        C = textscan(fid, pattern);
    else
        testo = textscan(fid, '%s', columns, 'delimiter', delim);
        C = textscan(fid, pattern, 'delimiter', delim);
    end
    fclose(fid);

    testo = char(testo{1});
    for ii = 1:columns
        title = testo(ii, :);
        title = strtrim(title);
        title = lower(title);
        title = strrep(title, '.', '');
        title = regexprep(title, '[^a-zA-Z]', '');
        title = regexprep(title, 'theta', 'twotheta');
        if (length(title) == 1)
            title = regexprep(title, 'm', 'mult');
            title = regexprep(title, '^f', 'formfactor');
        end
        title = strtrim(title);
        title = strrep(title, 'dspc', 'dspacing');
        if ~strcmpi(title, 'no')
            reflections.(title) = C{ii};
        end
    end

    reflections.hkl = double([reflections.h reflections.k reflections.l]);
    reflections = rmfield(reflections, {'h','k','l'});

    xop = reflections;
    [xop.xop_dir, filename, extension] = fileparts(file);
    xop.filename = [filename, extension];
end

end % end of function

