%-----------------------------------------------------------------------
%Design specific parameters
%-----------------------------------------------------------------------

clear
spm_defaults;
%subject directory names
subs = {'CBU050019','CBU050020'};

nsubs = length(subs);
%number of blocks
nsess = 2;
ntot = nsubs*nsess;
%condition names
cnames{1} = {'CN' 'CN1' 'CTrue' 'edWC' 'idWC' 'SH' 'WFalse' 'WTrue'};
cnames{2} = {'CN' 'CN1' 'CTrue' 'edWC' 'idWC' 'SH' 'WFalse' 'WTrue'};
ncond = length(cnames{1});
TR = 2;
%where the subject directories are
dataroot = '/imaging/ah01/datafromnewscanner';
statsdir = fullfile(dataroot,'stats');
hpf = 60;
incmoves = 0;
modeldur = 0;
imgfilt = '^s.*\.nii$';
movefilt = '^rp_.*\.txt$';
mnames = {'x_trans' 'y_trans' 'z_trans' 'x_rot' 'y_rot' 'z_rot'};
debug=0;

evorder = {'CN' 'CN1' 'CTrue' 'edWC' 'idWC' 'SH' 'WFalse' 'WTrue'};

%-----------------------------------------------------------------------
%Design setup
%-----------------------------------------------------------------------

% basis functions and timing parameters
%---------------------------------------------------------------------------
% OPTIONS:'hrf'
%         'hrf (with time derivative)'
%         'hrf (with time and dispersion derivatives)'
%         'Fourier set'
%         'Fourier set (Hanning)'
%         'Gamma functions'
%         'Finite Impulse Response'
%---------------------------------------------------------------------------

%MD No slice timing, therefore using temporal-derivates to shift HRF forward and back in time

xBF.name       = 'hrf';
xBF.length     = 32.2;              % length in seconds
xBF.order      = 1;                 % order of basis set
xBF.T          = 16;                % number of time bins per scan
xBF.T0         = 1;                 % first time bin (see slice timing) - middle of TA
xBF.UNITS      = 'scans';           % OPTIONS: 'scans'|'secs' for onsets
xBF.Volterra   = 1;                 % OPTIONS: 1|2 = order of convolution




for sub = 1:nsubs
    
    clear SPM
    disp(subs{sub})
    SPM.xY.RT = TR;
    SPM.xGX.iGXcalc = 'None';
    SPM.xVi.form = 'AR(1)';
    SPM.xBF = xBF;
    csub = subs{sub};
    subdata = fullfile(dataroot, csub);
    anadir = fullfile(subdata,'switchresults');
    if exist(anadir)~=7; mkdir(subdata,'switchresults');end
        cd(anadir);
        evdir = fullfile(subdata,subs,'/',{sub} );

        tc = 0;
        allfiles='';
    
    for sess = 1:nsess
        tc = tc+1;
        

        sessdata = fullfile(subdata, num2str(sess +4));%+4 to get past other atsk        

    
        
        files = spm_select('List', sessdata, imgfilt);
        if incmoves==1
            mfname = spm_select ('List', sessdata, movefilt);
            moves = load(fullfile(sessdata,mfname));
        end
        SPM.nscan(tc) = size(files,1);
        
        %if it crashes here then your filter is probably set up wrong, or
        %you may be looking in the wrong place for the files
	ffiles = '';
        for f =1:size(files,1)
            ffiles(f,:) = fullfile(sessdata,files(f,:));
        end
        allfiles = strvcat(allfiles,ffiles);
        
        for c = 1:ncond
            %use this for variable durations
            efile = [evdir 'on_' num2str(sess) '_' num2str(evorder{c}) '.txt']
            ons = load(efile)
            efile2 = [evdir 'dur_' num2str(sess) '_' num2str(evorder{c}) '.txt']
            durs = load(efile2)
            % or just put in dur as 0 for onsets only
            %if modeldur==1
            %    durs = ons(cidx,3)
            %else
           %     durs = zeros(length(ons),1)
           % end
                        
            SPM.Sess(tc).U(c) = struct(...
                'ons',ons,...
                'dur',durs,...
                'name',{cnames{sess}(c)},...
                'P',struct('name','none'))
        end
        
        SPM.xX.K(tc).HParam = hpf;

        
        if incmoves==1
            SPM.Sess(tc).C.C    = moves;     % [n x c double] covariates
            SPM.Sess(tc).C.name = mnames; % [1 x c cell]   names
        else
            SPM.Sess(tc).C.C = [];
            SPM.Sess(tc).C.name = {};
        end
    end
    
    cd (anadir)
    
    if debug==0
        SPM.xY.P = allfiles;
        SPMdes = spm_fmri_spm_ui(SPM);
        spm_unlink(fullfile('.', 'mask.img')); % avoid overwrite dialog
        SPMest = spm_spm(SPMdes);
        
        % set up the contrasts
        cons{1} = [0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 0 0];
        cons{2} = [0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0];
        cons{3} = [0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0];
        cons{4} = [0 0 1 0 0 0 -1 1 0 0 1 0 0 0 -1 1 0 0];
        cons{5} = [0 0 1 0 0 0 -1 1 0 0 0 0 0 0 0 0 0 0];
        cons{6} = [0 0 0 0 0 0 0 0 0 0 1 0 0 0 -1 1 0 0];
        cons{7} = [0 0 0 1 -1 0 0 0 0 0 0 1 -1 0 0 0 0 0];
        cons{8} = [0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0];
        cons{9} = [0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0];
        cons{10} = [-1 0 0 0 1 0 0 0 -1 0 0 0 1 0 0 0 0 0];
        cons{11} = [-1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0];
        cons{12} = [0 0 0 0 0 0 0 0 -1 0 0 0 1 0 0 0 0 0];
        cons{13} = [-1 0 0 0 0 1 0 0 -1 0 0 0 0 1 0 0 0 0];
        cons{14} = [-1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0];
        cons{15} = [0 0 0 0 0 0 0 0 -1 0 0 0 0 1 0 0 0 0];
                

        
        
        cname{1} = 'working vs knowing';
        cname{2} = 'working vs knowing 1';
        cname{3} = 'working vs knowing 2';
        cname{4} = 'true - false all';
        cname{5} = 'true - false 1';
        cname{6} = 'true - false 2';
        cname{7} = 'ED - ID';
        cname{8} = 'ED - ID 1';
        cname{9} = 'ED - ID 2';
        cname{10} = 'ID - CN';
        cname{11} = 'ID - CN 1';
        cname{12} = 'ID - CN 2';
        cname{13} = 'sh - cn';
        cname{14} = 'sh - cn 1';
        cname{15} = 'sh - cn 2';
        
        
       % use this to make the con images
        for i = 1:size(cons,2)
            if length(SPMest.xCon)==0
                SPMest.xCon = spm_FcUtil('Set',cname{i},'T','c',cons{i}',SPMest.xX.xKXs);
            else
                SPMest.xCon(end+1) = spm_FcUtil('Set',cname{i},'T','c',cons{i}',SPMest.xX.xKXs);
            end
        end
        spm_contrasts(SPMest);
    end
end
