function xrayrun(stage,numxs,dark,core,slice,LOthreshold,plotting,bfrac,dfrac,all,stret,fn,rim)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% xray.m 
% xray(stage,numxs,dark,core,slice)
% current version: 3.5
% recommended values to begin with: stage=1 stage of data processing
%                                   numxs=5 number of horizontal cross
%                                   sections across core plane.
%                                   dark=0 analyze dark vesicles if = 1
%                                          analyze bright vesicles if =0
%				    core='05017-12-2' : is the sample no.
%				    slice='0793' : 4-digit slice no.
%                                         (ignored if stage > 1)
%
% Goes through a suit of codes for optimal binarization of xray CT data.
%
% USAGE: 
%
% Start by clearing the matlab memory of any stored variables and figures:
%	-type 'putz'
%
% Stage 1: Load a single image slice and launches ImprocMULT2.m
%	-compares "seismic" result with a Sobel edge detector 
%
% Stage 2: Stage 2 applies binarization to 6 randomly selected images. 
% Check if OK. Launches ImprocMULT2.m
%
% Stage 3: Applies the binarization code to all images. Launches
% ImprocMULT2.m (caution! This takes time!)
%
% Sanjoy Som 2009
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure, close %because matlab is crazy
warning off all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INITIALIZATION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic
disp(['Flow number: ' num2str(fn)])

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if stage==1
% Stage 1:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    filename=[core '_rec' slice '.bmp'];
    %filename=[core(1:end-2) '_rec' slice '.bmp'];	
    I=imread([filename]);
    if all == 1 %plots everthing
    	improcMULT(I,numxs,stage,dark,LOthreshold,plotting,bfrac,dfrac,stret,rim);
    else %plots only overview images
	stage=2;
	improcMULT2(I,numxs,stage,dark,LOthreshold,plotting,bfrac,dfrac,filename,stret,rim);
   end
	
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
elseif stage==2
% Stage 2:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %randomly select n slices 
    n = 6;
    files=dir('images/*.bmp');
    nofiles = length(files);
    r = ceil(nofiles.*rand(n,1));	

    for j=1:6
        filename = files(r(j)).name;
	I=imread([filename]);
        improcMULT2(I,numxs,stage,dark,LOthreshold,plotting,bfrac,dfrac,filename,stret,rim);
        title(['image number: ' filename(15:end-4)])
    end %for j=1:n
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
elseif stage==3
% Stage 3: batch processing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    files=dir('images/*.bmp');
    for j=1:length(files)
        I=imread(files(j).name);
	filename=files(j).name;
        Ibin=improcMULT2(I,numxs,stage,dark,LOthreshold,plotting,bfrac,dfrac,filename,stret,rim);
        imwrite(Ibin,['binaries/' filename(1:end-4) '.tif'],'tif');
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Write log
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%log.stage=stage;
%log.cross_sections=numxs;
%log.dark=dark;
%log.core_number=core;
%log.slice_number=slice;
%filename=[core '_log.txt'];
%save(sprintf('%s',filename),'log')
toc
