%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%           SETUP ALL VARIABLES AND DIR            %%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%       start timer    %%%%%%%%%%%%%%%%%%%%%%%%%%

[work.y, work.m, work.d, work.h, work.mn, work.s] = datevec(datestr(clock,0));

work.yShort         = rem(work.y, 100);

ticID               = tic;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

checkSetupUserVariables

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

indices             = [];
count               = [];
out                 = [];
work.numberOfFig    = 1;

work.tauCscale      =   (user.temp1 / user.temp2) * 10^(247.8 / (user.temp2 - 140)) / (10^ (247.8 / (user.temp1 - 140)));
work.constBiPol     =    -(2.675^ 4 * 6.626^ 2 / (2 * pi)^ 2 * user.tauC * work.tauCscale * user.Ssquare);

%constants for the dipolar cross relaxation rates

% % work.gammaH         =    2.675E8; %Beat
work.gammaH         =    2.6752E8; %Julien

work.gammaC         =    6.73E7;
work.gammaN         =    -2.71E7;
work.gammaD         =    4.1066E7;

if strcmp(user.noesyType,'1H')
    user.gamma      = work.gammaH;
elseif strcmp(user.noesyType, '13C')
    user.gamma      = work.gammaC;
end

% % % work.hbar           = 1.054571726 * 10^-34; %Beat
work.hbar           = 1.0545726691251e-34; %Julien


work.mu0            = 4 * pi * 10^-7;
work.omega0         = 2 * pi * user.B0field * 10^6;
work.delta          = 1/10*(work.mu0^2*user.gamma^4*work.hbar^2)/(4*pi)^2;
work.delta00        = (work.mu0^2*user.gamma^4*work.hbar^2)/(4*pi)^2;

%%%%%%%%%%%%%%%%%%%%%%           conditions          %%%%%%%%%%%%%%%%%%%%%%    
addpath('./eNOEprogram/read_in/numcode/');
atomNumGrpNum

%%%%%%%%%%%%%%%%%%%%          create directories       %%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%         and output pathnames      %%%%%%%%%%%%%%%%%%%%


work.path   = sprintf('%d%02d%02d_%02d.%02d_%s_%s',work.yShort, work.m, work.d, work.h, work.mn, user.projectname, user.heightVolume);
mkdir(user.outputDirectory, work.path)
user.timeStamp = sprintf('%d%02d%02d_%02d.%02d',work.yShort, work.m, work.d, work.h, work.mn);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

work.finalCollected             = sprintf('%s/%s/final', user.outputDirectory, work.path);
mkdir(work.finalCollected);

%%%%%%%%%%%%%%%%%%%%%%%%%    setup diary    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

diary(sprintf('./%s/%s_logFile.txt', work.finalCollected, user.projectname))
diary off

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

work.buildUpDataDir             = sprintf('%s/%s/buildup', user.outputDirectory, work.path);
mkdir(work.buildUpDataDir);
addpath(work.buildUpDataDir);

work.buildUpPlotDir             = sprintf('%s/%s/buildupPlots', user.outputDirectory, work.path);
mkdir(work.buildUpPlotDir);
addpath(work.buildUpPlotDir);

work.decayPlotDir               = sprintf('%s/%s/diagDecayPlots', user.outputDirectory, work.path);
mkdir(work.decayPlotDir);
addpath(work.decayPlotDir);

work.spindiffDataDir            = sprintf('%s/%s/spinDiff', user.outputDirectory, work.path);
mkdir(work.spindiffDataDir);
addpath(work.spindiffDataDir);

work.workSpaceVarDir            = sprintf('%s/%s/wSpace', user.outputDirectory, work.path);
mkdir(work.workSpaceVarDir);

work.sigmaPlotDir               = sprintf('%s/%s/final', user.outputDirectory, work.path);

work.distancePlotDir            = sprintf('%s/%s/final', user.outputDirectory, work.path);

%%%%%%%%%%%%%%%%%%%%%%%%  assignment check  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

work.doubleOutFileName          = sprintf('%s/doubleAssigned.txt',work.finalCollected);
work.rPdbGreaterUplOutFileName  = sprintf('%s/greaterThanUpl.txt',work.finalCollected);

%%%%%%%%%%%%%%%%%%%%%%%%  Buildup outfiles  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

work.RhoOutFileName             = sprintf('%s/RhoOut.txt',work.buildUpDataDir); 
work.NORhoOutNumCodeFileName    = sprintf('%s/NORhoOutNumCode.txt',work.finalCollected); 
work.NORhoOutLetterCodeFileName = sprintf('%s/NORhoOutLetterCode.txt',work.finalCollected); 

work.SigmaOutFileName           = sprintf('%s/SigmaOut.txt',work.buildUpDataDir);

%%%%%%%%%%%%%%%%%%%%%  spindifusion outfiles   %%%%%%%%%%%%%%%%%%%%%%%%%%%%

work.rhosimOutFileName          = sprintf('%s/rhosimOut',work.spindiffDataDir);
work.PathwayOutFileName         = sprintf('%s/PathwayOut',work.spindiffDataDir);
work.DistanceOutFileName        = sprintf('%s/DistancesOut',work.spindiffDataDir);
work.sIsoRelativeError          = sprintf('./%s/sIsoRelErr', work.finalCollected);
work.sAnIsoRelativeError        = sprintf('./%s/sAnIsoRelErr', work.finalCollected);

%%%%%%%%%%%%%%%%%%%%%%%   plotData outfiles  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

work.sIsoExp_sTrueSave              = sprintf('./%s/sIsExp_sTrue', work.sigmaPlotDir);
work.sAnIsoExp_sTrueSave            = sprintf('./%s/sAnExp_sTrue', work.sigmaPlotDir);
    
work.sIsoExp_sIsoSimAllSave         = sprintf('./%s/sIsoExp_sSimAll', work.sigmaPlotDir);
work.sAnisoExp_sAnisoSimAllSave     = sprintf('./%s/sAnisoExp_sSimAll', work.sigmaPlotDir);
work.sIsoExp_sIsoSimBackboneSave    = sprintf('./%s/sIsoExp_sSimBackbone', work.sigmaPlotDir);
work.sAnisoExp_sAnisoSimBackboneSave= sprintf('./%s/sAnisoExp_sSimBackbone', work.sigmaPlotDir);

work.rIsoExp_rPdbSave               = sprintf('./%s/rIsoExp_rPdb', work.distancePlotDir);
work.rAnisoExp_rPdbSave             = sprintf('./%s/rAnisoExp_rPdb', work.distancePlotDir);

%%%%%%%%%%%%%%%%%%%%%%%%% saveData outfiles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

work.workSpAllSave                  = sprintf('./%s/wsp_all.mat', work.workSpaceVarDir);

work.methMethSigmaDataSave          = sprintf('./%s/mmsData.mat', work.workSpaceVarDir);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%       add path variables      %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

addpath('./eNOEprogram/handTools/');
addpath('./eNOEprogram/shared/');
addpath('./eNOEprogram/lib/');
addpath('./eNOEprogram/read_in/');
addpath('./eNOEprogram/read_in/numcode/');
addpath('./eNOEprogram/read_in/sigmaData/');
addpath('./eNOEprogram/read_in/nmrDrawTab/');
addpath('./eNOEprogram/read_in/intensityFiles/');
addpath('./eNOEprogram/read_out/');
addpath('./eNOEprogram/setupAndMaster/');
addpath('./eNOEprogram/buildups/');
addpath('./eNOEprogram/spinDiffusion/');
addpath('./eNOEprogram/spinDiffusion/3spin/');
addpath('./eNOEprogram/spinDiffusion/shared/');
addpath('./eNOEprogram/spinDiffusion/fullMatrix/');
addpath('./eNOEprogram/sigmaCorrDist/');
addpath('./eNOEprogram/plots/');
addpath('./eNOEprogram/plots/auto/');
addpath('./eNOEprogram/plots/basicFun/');
addpath('./eNOEprogram/plots/interactive/');
addpath('./eNOEprogram/rasMol/');
addpath('./input_files/');
addpath('./output_files/');
addpath(user.inputDirectory);
addpath(user.outputDirectory);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%