function  [res_data presets]= load_raw_residue_data(Sparky_data_path,cur_resname, ...
    conditions)
% Loading spectral data for a  residue
% Returns
% res_data - a structure with the data for this residue
%           name - residue name
%           indices - array of indices of titration points corresponding 
%                       to ligand concentrations in main array
%                       (we may have some titration points dropped and then using index
 %                          we can extract corresponding concentrations)
%           Xs       -  array of X arrays, each one is a column (sequential order as read from the data folder.
%                          NOT the sequential titration order! That one is set by indices.)
%           Ys        - array of Y arrays 
%           
%           lengths - lengths of individual X arrays
%
% presets - a structure containing what it finds in 'manual_R2.txt' and
% 'manual_w0.txt' files. 
%           R2s - array of preset R2s or empty to enable estimates by
%           fitting
%           w0s - array of preset w0 or empty to enable estimates by fitting
%           scale_factor- a divider to compensate for incomplete peak
%           envelope
%
% 3/11/08
% Written by Evgenii Kovrigin, Medical College of Wisconsin



%% ======== USER INPUT =============





%======= END OF USER INPUT ============



disp(sprintf('\nFitting residue: %s', cur_resname));

%-- save residue name
res_data.name=cur_resname;


%%-- read list of frequencies
fname=fullfile(Sparky_data_path,cur_resname, conditions.titration_points_list_fname);
res_data.indices=load(fname);


% count points whose indices are given
Npoints=length(res_data.indices);

%-- allocate memory for X and Y datasets (may be different length for
%     different titration points)
res_data.Xs=cell(1,Npoints);
res_data.Ys=cell(1,Npoints);
res_data.lengths=[];


% - go through list of indices and collect only those titration 
for count=1:1:Npoints
    %% -- fit data of one residue-frequency dataset
    cur_point=res_data.indices(count);

    %-- get data 
    fname=fullfile(Sparky_data_path,cur_resname,sprintf('%d.txt',cur_point));
    raw_data=load(fname);

    %disp(sprintf('Getting %d point', cur_point));
   
    %-- extract X and Y
    X=raw_data(:,1);
    Y=raw_data(:,2);
    
    %-- adjust size to have odd number of points for proper Simpson's
    %     integratoin
    Xlen=length(X);
    if mod(Xlen,2)==0
        Xlen=Xlen-1;
    end
    
    %-- add to the data structure
    res_data.Xs{count}=X(1:Xlen);
    res_data.Ys{count}=Y(1:Xlen);
    N  = Xlen;
    res_data.lengths=[ res_data.lengths N];
    
end

%% Read control files for parameters R2 and w0
fname=fullfile(Sparky_data_path,cur_resname, conditions.manual_R2_fname);
presets.R2s=load(fname');
fname=fullfile(Sparky_data_path,cur_resname, conditions.manual_w0_fname);
presets.w0s=load(fname);
%-- read scaling factor for intensities
fname=fullfile(Sparky_data_path,cur_resname, conditions.scale_factor_fname);
presets.scale_factor=load(fname);
disp(presets)



 