%NOTE - FOR MORE HELP ABOUT THE SYNTAX FOR ANY COMMAND, TYPE IN MATLAB:
%>> help routine_name
%
%e.g.
%
%>> help gen_sqw
%
% prints in the Matlab command window the help for the gen_sqw routine
%

%============= Correcting for sample misalignment ==============

%Make a series of hk-slices at different l, in order to work out what Bragg
%positions we have. Step sizes and energy integration should be customised for your data
%Step sizes should be as small as possible, and energy integration tight.
alignment_slice1=cut_sqw(sqw_file,proj2,[-5,0.03,8],[-5,0.03,8],[-0.05,0.05],[-10,10],'-nopix');
alignment_slice2=cut_sqw(sqw_file,proj2,[0.95,1.05],[-5,0.03,8],[-3,0.03,3],[-10,10],'-nopix');
alignment_slice3=cut_sqw(sqw_file,proj2,[-5,0.03,8],[-0.05,0.05],[-3,0.03,3],[-10,10],'-nopix');

%Look at the 3 orthogonal slices to figure out what bragg peaks are visible
plot(compact(alignment_slice1)); keep_figure;
plot(compact(alignment_slice2)); keep_figure;
plot(compact(alignment_slice3)); keep_figure;

%Our notional Bragg peaks
bp=[4,0,0; 2,0,0; 1,1,0; 4,4,0; 1,0,1];%list of accessible Bragg peaks (in data they may be off from these notional positions)


%Get true Bragg peak positions.
%This routine takes radial and transverse cuts around the Bragg peaks listed above. See the help for further information
%about how the routine works - you will in general have to adjust some of the inputs here, especially the energy window
[rlu0,width,wcut,wpeak]=bragg_positions(sqw_file, bp, 1.5, 0.06, 0.4,...
                                     1.5, 0.06, 0.4, 20, 'gauss','bin_ab');

%Check how well the function did (note the command line prompts to allow you to scan through the cuts made above)
bragg_positions_view(wcut,wpeak)


%Determine corrections to lattice and orientation (in this example we choose to keep the lattice angles fixed,
%but allow the lattice parameters to be refined, keeping a cubic structure by keeping ratios of lattice pars to be same):
[rlu_corr,alatt,angdeg] = refine_crystal(rlu0, alatt, angdeg, bp,'fix_angdeg','fix_alatt_ratio');                                                        


%Apply changes to sqw file
change_crystal_horace(sqw_file, rlu_corr);


%Check the outcome: Get Bragg peak positions and look at output: should be much better
[rlu0,width,wcut,wpeak]=bragg_positions(sqw_file, bp, 1.5, 0.06, 0.4,...
                                     1.5, 0.06, 0.4, 20, 'gauss','bin_ab');
bragg_positions_view(wcut,wpeak)

%=========
%Generally you only want to figure out the misorientation once, then apply some correction to subsequent data.
% You can do this by finding the values of the notional goniometers gl, gs, dpsi that are used in gen_sqw:

[alatt, angdeg, dpsi_deg, gl_deg, gs_deg] = crystal_pars_correct (u, v, alatt0, angdeg0, omega0_deg, dpsi0_deg, gl0_deg, gs0_deg, rlu_corr);
%u and v are the notional scattering plane, alatt0, angdeg0, etc are the original values for those parameters you used in gen_sqw
%rlu_corr is the misalignment correction matrix determined above. The routine outputs the corrected lattic parameters (if these were
%refined) and the values of dpsi, gl and gs to use in future regenerations of the sqw file
