% this configure file is to set all necessary parameter for BaSiC
clear; close all; clc;
%% movie information
TTTDIR = 'S:\group\Timelapsedata\Timelapsedata\'; % patch to you TTT image folder
movieID = '170221SH14'; % it will look for movieID folder in the TTTDIR
FluorWL_group = {'w02.png','w03.png','w04.png'}; %which wavelength to process, multiple wavelengths can be used as {'w01.png', 'w02.png','w03.png'};
inputDirectory = sprintf('%s/%s/',TTTDIR,movieID); % inputDiectory where the images are stored
outputDirectory = sprintf('%s/%s/BaSiC/',TTTDIR,movieID); % outputDirectory where the estimated shading profiles are stored
mkdir(outputDirectory);
existSegmentation = false; % bool, if true then provide segmentation directory
segmentationDirectory = sprintf('%s/%s/Analysis/Segmentation_170227/',TTTDIR,movieID);

%% Step I: Generation of flatfield_ap (a base flatfield for all positions)
% output: flatfield_ap in outputDirectory; if exist, please set runStepI to
% false
runStepI = false; % bool, if true then run SLIC to generate new flatfield_ap and please specify the following parameters
runAllPositionsI = false; %bool, if true use all positions in the movie folder. If false, specity positionVector
positionVectorI = [7 10 11 26 31 32 33]; % or in the format of 1:36
lambdaFlatfield_allposition = 2.0; % Range 0-5.0, default 2.0; check the ouptut flatfield_allposition, if not smooth, please increase lambdaFlatfield_all if oversmooth decrease it

%% step II: Generation of position-wise flatfield and darkfield 
% output: flatfield and darkfield in position subdirectory under
% outputDirectory as well as a overview csv file
runStepII = true; % bool, if true then run SLIC to generate new position-wise flatfield & darkfield and please specify the following parameters
runAllPositionsII = false; %bool, if true run across all positions in the movie folder. If false, specity positionVector
temporalRangeII = []; %[minTimepoint maxTimepoint]; % leave empty if considering all timepoints;
useFlatfield_allposition = false; % default true, turn to false if you would like to estimate flatfield and darkfield per position;
positionVectorII = 1:64; %[7 10 11 26 31 32 33]; % or in the format of 1:36
lambdaFlatfield = []; %4.0*ones(length(positionVectorII),1); % leave empty if using automatic parameter setting; Otherwise set to the same length of positionVectorII;
lambdaDarkfield = []; %4.0*ones(length(positionVectorII),1); % leave empty if using automatic parameter setting; Otherwise set to the same length of positionVectorII; 
useSegmentation = true; % bool, default false, set to be true if you would like to consider segmentation in computing flatfield and darkfield;


%% step III: Generation of baseline fluorescence to correct background bleaching
% output: basefluor in position subdirectory under
% outputDirectory; corrected images if runCorrection is on
runStepIII = true; %bool, if true then run SLIC to generate new basefluor and please specify the following parameters
runAllPositionsIII = false; %bool, if true run across all positions in the movie folder. If false, specity positionVector
positionVectorIII = 1:64; %[7 10 11 26 31 32 33]; % or in the format of 1:36
temporalRangeIII = []; %[minTimepoint maxTimepoint]; % leave empty if considering all timepoints;
runCorrection = true; %bool, default false, if true then run SLIC to generate corrected images
adjustFlatfield = false; %bool, default false, if true then use ratioflat in correction
floatOutput = false; %bool, default true,output float tif image otherwise output 8-bit png image (negative values will be set to 0)
                    % ATTENTION: temporary hack by LS, if false 16-bit png
                    % image that can be read in qTfy


%% save the specified configuration
save([outputDirectory '/BaSiCconfig.mat']);

%% run BaSiC_main
BaSiC_main([outputDirectory '/BaSiCconfig.mat']);

