function display_fit_results(project_name, res_data,  beta, beta_error, norm, modes, conditions)
%------------------- LineShapeKin DISPLAY_FIT_RESULTS -----------------------------
%-- version 3.1 
%
% 3/17/08
% Written by Evgenii Kovrigin, Medical College of Wisconsin
%
%   This function displays experimental data for a residue together
%   with best fit results
%
%  3/29/08 - displays indices of traces (to match with titration points)
%                - displays stacked graphs

global YES NO B_vector Model X_columns;



 
% Plot experimental data
HF=figure;
hold on; 


%--- Generate annotation for the graph
switch modes.plot_mode
    
    case 'raw+fitted'    %--- here we will plot raw and fitted data together
        
        %-- Plotting fitting results and a raw data
        graph_title=sprintf('%s_%s_fitting',res_data.name, project_name);
        set(HF,'Filename',graph_title);

        %- set figure title
        title(sprintf('%s %s :  exp. data (blue), fit results (red)',...
          res_data.name, project_name));


        %- prepare nice printout of parameters on the figure 
        outstring=sprintf('Norm: %.2e\n', norm);
        for beta_element=1:length(beta)
          newline=sprintf('[%d] %s:  %.5e  +/-  %.1e\n',  beta_element, B_vector.parameter_names{beta_element},...
                         beta( beta_element), beta_error( beta_element));
          outstring= [ outstring newline ] ;
        end
        annotation(HF,'textbox','String',...
                sprintf('Fit results:\n%s ', outstring),...
            'FitBoxToText','off','LineStyle','none',...
            'Position',[0.7589 0.5322 0.1801 0.3991]);
        
        
    case      'simulation only'  %--- other cases (simulation and raw data display)
       
    %---- Plotting simulation results
        graph_title=sprintf('%s_%s',res_data.name, project_name);
        set(HF,'Filename',graph_title);

        %- set figure title
        title('NMR line shape simulation');


        %- prepare nice printout of parameters on the figure 
        outstring='Parameters: ';
        for beta_element=1:length(beta)
          newline=sprintf('\n[%d] %s:  %.2e',  beta_element, B_vector.parameter_names{beta_element},...
                         beta( beta_element)  );
          outstring= [ outstring newline ] ;
        end
        annotation(HF,'textbox','String',...
                sprintf('Simulation:\n%s ', outstring),...
            'FitBoxToText','off','LineStyle','none',...
            'Position',[0.7589 0.5322 0.1801 0.3991]);
        
    case 'raw only'
       
    %---- Plotting simulation results
        graph_title=sprintf('%s_%s',res_data.name, project_name);
        set(HF,'Filename',graph_title);

        %- set figure title
        %- set figure title
        title(sprintf('%s %s',          res_data.name, project_name));



        
    otherwise
        error('Wrong mode.plot_mode value %s', mode.plot_mode);
        
    
end
%-- output experimental data
start_index=1;
stacking_step=0; %-- vertical shift of subsequent datasets
for dataset=1:length(res_data.indices)
    
    x=res_data.Xs{dataset};
    y=res_data.Ynormalized{dataset};
    
    if modes.stacked_plot==YES
        stacking_step=max(res_data.Ynormalized{1})*modes.percent_shift/100;
    end
    
    %-- extract composite X-data for line shape simulation
    next_start_index=start_index+res_data.lengths(dataset);
    x_composite=res_data.Xcomposite(start_index : next_start_index-1,:);
   
    y_fit=model(beta,x_composite);
    
    %-- shift up for stacked plot
    y=y+stacking_step*(dataset-1);
    y_fit=y_fit+stacking_step*(dataset-1);

    plotname=sprintf('Trace %d', res_data.indices(dataset));

    
    switch modes.plot_mode
        case 'simulation only'
                plot(x,y_fit,'k','LineWidth',1,'DisplayName',plotname);
        case 'raw+fitted'
                plot(x,y,'bo', 'LineWidth',1,'DisplayName',plotname);
                plot( x,y,'b:', 'LineWidth',1,'DisplayName',plotname);
                plot( x,y_fit,'r', 'LineWidth',1,'DisplayName',plotname);
        case 'raw only'
                plot(x,y,'bo', 'LineWidth',1,'DisplayName',plotname);
                plot( x,y,'b:', 'LineWidth',1,'DisplayName',plotname);
    end
    
    %-- plot baseline
    plot( [min(x) max(x)], [ stacking_step*(dataset-1) stacking_step*(dataset-1)], 'k:');
    
    start_index=next_start_index;
 

end

hold off;

%-- save figure
fname=fullfile(conditions.figures_path,graph_title);
saveas(HF,  fname, 'fig');
saveas(HF,  fname, 'png');

