% Batch mode scripts for running spm2 in TRC
% Created by Ze Wang, 08-05-2004
% zewang@mail.med.upenn.edu

clear PAR
PAR=[];
global PAR
warning off;
PAR.SPM_path=spm('Dir');
addpath(PAR.SPM_path);

PAR.batchcode_which= mfilename('fullpath');
PAR.batchcode_which=fileparts(PAR.batchcode_which);
addpath(PAR.batchcode_which);
old_pwd=pwd;
cd(PAR.batchcode_which);
cd ../
data_root=pwd;
cd(old_pwd);

PAR.root=data_root;

% This file sets up various things specific to this 
% analysis, and stores them in the global variable PAR,
% which is used by the other batch files.
% You don't have to do it this way of course, I just
% found it easier



%%%%%%%%%%%%%%%%%%%%%%
 %                   %
 %   GENERAL PREFS   %
 %                   %
 %%%%%%%%%%%%%%%%%%%%%%
% Where the subjects' data directories are stored
%PAR.root = '';   % you can explicitly specify the data path here
PAR.anatname='Anat';    % Anatomical directory name
%prefixes for structural 3D image, supposed to be the same for every subj.
PAR.structprefs = 'mprage';

if ~exist('groupnum','var') groupnum=1; end
 PAR.sess= {''};
 PAR.funcfolder={'func'};  % folder names for fMRI images
 
 PAR.fmriprefix={'rest'};
 % Subjects' directories
 PAR.groupname={''}; 
 PAR.sublistfile='sublist.m';
 fid=fopen(PAR.sublistfile,'rt');
nsubs=0;
while 1
    text=fgets(fid);
    while (feof(fid)==0) && (isempty(text))
        text=fgets(fid);
    end
    
    if feof(fid), break;end;
    text=deblank(text);
    if isempty(text), continue; end
    if text(1)=='#', continue; end
    if isempty(text)==0
        nsubs=nsubs+1;
        oPAR.subjects{nsubs}=text;
    end
end
fclose(fid);
nstart=1;
nend=nsubs;
for i=nstart:nend
    PAR.subjects{i-nstart+1}=oPAR.subjects{i};
end
nsubs=length(PAR.subjects);

PAR.nsubs = length(PAR.subjects);
for i=1:PAR.nsubs
  PAR.nsess(i)=1;
  PAR.subs(i).craving=NaN;
  PAR.subs(i).drugdependence=NaN;
  PAR.subs(i).bart=NaN;
  for t=1:PAR.nsess(i)
      tmp=dir([PAR.root filesep PAR.groupname{groupnum}  filesep PAR.subjects{i} filesep  PAR.sess{t} filesep ['*' PAR.anatname] ]);
    if size(tmp,1)==0
        fprintf('Can not find the anatomical directory for subject\n')
        fprintf('%s: %s \n', PAR.subjects{i}, PAR.sess{t});
        PAR.structdir{i,t}='';
%         return;
    end
    if size(tmp,1)>1
        fprintf('More than 1 anatomical directories are found here!\n')
        return;
    end
    PAR.structdir{i,t}=fullfile(PAR.root, PAR.groupname{groupnum} , PAR.subjects{i}, PAR.sess{t}, spm_str_manip(char(tmp.name),'d'));

    end
end

% The condition names are assumed same for different sessions
for i=1:PAR.nsubs
  for t=1:PAR.nsess(i)
    for j=1:length(PAR.funcfolder)
            tmp=dir(fullfile(PAR.root, PAR.groupname{groupnum},  PAR.subjects{i}, PAR.sess{t},   ['*' PAR.funcfolder{j} '*']));
            if size(tmp,1)>1
                fprintf('More than 1 condition directories are found here!\n')
                return;
            end
            if size(tmp,1)<1
                fprintf('no imgs ----%s  %s %s \n', PAR.subjects{i}, PAR.sess{t},PAR.funcfolder{j} )
                PAR.condirs{i,t,j}='';
            end
            if size(tmp,1)==1
                PAR.condirs{i,t,j}=fullfile(PAR.root, PAR.groupname{groupnum}, PAR.subjects{i}, PAR.sess{t}, spm_str_manip(char(tmp.name),'d'));
            end
    end
  end
end


PAR.fMRI='';
PAR.coiltype='';
PAR.nsubs = length(PAR.subjects);
PAR.nsesses = length(PAR.sess);
PAR.localtemplate = 'Loctmp';
ncond=length(PAR.funcfolder);
PAR.ncond=ncond;
ntot = PAR.nsubs * PAR.nsesses;

PAR.TRs = ones(1,PAR.nsubs)*2;
PAR.mp='no';
PAR.objmaskfs=zeros(1,PAR.nsubs);

% Smoothing for normalized images
PAR.FWHM = [4.5];
PAR.FWHMben = [10];
% Model stuff
% Condition namesfiles
PAR.Filter='fltwht*.img';
PAR.Filter='flt_*.img';
PAR.rbimdir = 'RBIMmaps';
PAR.ana_dir = ['RBIM_ana'];
PAR.groupdir = ['RBIM_RESULTS']; 
PAR.netdir = 'NETWORK';
clear nslices
global nslices
if exist('nslices','var')&& ~isempty(nslices)
    PAR.num_slices=nslices;
else
    PAR.num_slices=32;
end
PAR.ref_slice=1;
PAR.slicetime = PAR.TRs(1)/PAR.num_slices; 
if mod(PAR.num_slices,2)
    PAR.sliceorder=[2:2:PAR.num_slices 1:2:PAR.num_slices];
else
    PAR.sliceorder=[1:2:PAR.num_slices 2:2:PAR.num_slices];
end
PAR.slice_gap = PAR.slicetime;  % since the last slice is acquired in the end, the time between last slices and next volume is MYV.slicetime 

% for SampleEn
PAR.dim=3;
PAR.r=0.2;

clear i sb ss;
