%% Running an experiment

%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
%

%=================Generating an sqw file===============

data_path='/home/your_federal_id/RBXXXXXXX/';%directory where data files are

%par_file=[data_path,'4to1_102.par'];
par_file='';
sqw_file=[data_path,'my_real_file.sqw'];
efix=400;%Ei
psi=[0:2:90];%values of sample orientation psi for each run
runno=[15052:15097];%array of run numbers - must be array same size as psi
emode=1;%direct geometry spectrometer
alatt=[2.87,2.87,2.87];%lattice parameters
angdeg=[90,90,90];%lattice angles
u=[1,0,0]; v=[0,1,0];%scattering plane (u//ki when psi=0, v perp ki)
omega=0; dpsi=0; gl=0; gs=0;

for i=1:numel(psi)
    spefile{i}=[data_path,'map',num2str(runno(i)),'_ei400.nxspe'];%construct cell array of nxspe file names
end

gen_sqw (spefile, par_file, sqw_file, efix, emode, alatt, angdeg,...
    u, v, psi, omega, dpsi, gl, gs);%make the sqw file

%A comment on accumulate_sqw
%accumulate_sqw (spe_file, par_file, sqw_file, efix, emode, alatt, angdeg,...
%                                                u, v, psi, omega, dpsi, gl, gs)
 
%Or
%accumulate_sqw (spe_file, par_file, sqw_file, efix, emode, alatt, angdeg,...
%                                                u, v, psi, omega, dpsi, gl, gs,'clean')
 
%This is a way of appending newly processed spe files to an existing
%dataset. The key point is that the psi and spe_file arrays contain a list
%of PLANNED files and run-numbers - only those that actually exist will be
%included in the file.

%Can run this periodically overnight, say.

%=================== Making cuts and slices =================

%Define viewing (projection) axes for plots. Need not be related to sample orientation!
proj.u=[1,1,0]; proj.v=[-1,1,0]; proj.uoffset=[0,0,0,0]; proj.type='rrr';
proj2.u=[1,0,0]; proj2.v=[0,1,0]; proj2.uoffset=[0,0,0,0]; proj2.type='rrr';
proj3.u=[1,1,1]; proj3.v=[-1,1,0]; proj3.uoffset=[0,0,0,0]; proj3.type='rrr';


%3d volume plots
my_vol=cut_sqw(sqw_file,proj,[-3,0.05,3],[-3,0.05,3],[-0.1,0.1],[0,4,360],'-nopix');
plot(my_vol);

my_vol2=cut_sqw(sqw_file,proj2,[-3,0.05,3],[-3,0.05,3],[-0.1,0.1],[0,4,360],'-nopix');
plot(my_vol2);

my_vol3=cut_sqw(sqw_file,proj3,[-3,0.05,3],[-3,0.05,3],[-0.1,0.1],[0,4,360],'-nopix');
plot(my_vol3);

%2d slice
my_slice=cut_sqw(sqw_file,proj,[-3,0.05,3],[-1.1,-0.9],[-0.1,0.1],[0,4,280]);
plot(my_slice);

%1d cut
tic
my_cut1=cut_sqw(sqw_file,proj,[-3,0.05,3],[-1.1,-0.9],[-0.1,0.1],[130,150]);
toc

plot(my_cut1);

%Illustration of cut from a cut:
tic
my_cut2=cut(my_slice,[],[130,150]);%[] is to keep 1st axes as it is, [130,150] is integration range for 2nd axis
%note because the object we are cutting from is 2d, we only need 2 binning arguments, rather than the 4 that are
%needed when taking a cut form the 4-dimensional dataset in the file
toc

plot(my_cut2);%this is identical to my_cut1, but was much faster to create.
%Imagine if you were running a script to take many cuts from the data - it
%is probably quicker to take them from existing data objects, where
%possible!

%============== Basic customisation of plots =================

%Make axes tight:
plot(compact(my_slice));

%Smoothing:
plot(smooth(my_slice));%this gives an error - think about why!

plot(smooth(d2d(my_slice)));%d2d command (for 2d objects) converts from sqw type data, with detector pixel retained
%to d2d / dnd object that is smaller in memory and without detector pixel info

%Smoothing options:
plot(smooth(d2d(my_slice),[2,2],'gaussian'));


%Set colour scale and other axes scales in script:
lz 0 0.5
ly 50 250
lx -1.5 1.5

%Reset a limit
lx

%Retain a figure, so it is not replaced next time you make a plot (of the
%same dimensionality)
keep_figure;
plot(my_slice);

%Cursor to find a particular data point value
plot(my_cut2);
xycursor

