function glm_specmask_bch(cfg, inplane, template)
  
% Explicitly specifies a mask of voxels for glm esimation.
% Adapted version of original glm_specmask designed for use in batch processing
%
% FORMAT glm_specmask_bch(cfg, inplane, template)
%
% cfg,inplane,template - fullpaths to the corresponding SPMcfg.mat,
% inplane image, and template image to be used in segmenting the mask..
% Creates a bigmask image from the provided inplane and template, 
% modifies an SPMcfg_file with that mask and then estimates 
% statistics from it, overriding an previously existing results files 
% in the SPMcfg_file directory.  
%                         
% This program creates a gray matter mask with all voxels to be used 
% for model estimation and estimates the model. This version is adapted for
% use in batch processing and so shouldn't require any user input at all; it
% does require, however, a precreated SPMcfg.mat file to modify and an 
% appropriate inplane anatomical image to use for the mask file.
%
% The program uses several SPM99 routines to 1) create a bigmask 
% image containing all gray matter voxels,  2) modify an SPMcfg.mat 
% file so that it refers to the mask image created in step 
% one as an explicit definition of which voxels to be included in the 
% statistics, and 3) esimate the GLM model from the modified SPMcfg.mat.
% The three steps are all performed in a single run of the program.  Note
% that once the program is run, any results in the SPMcfg file directory
% that are already there will be overridden.
% 
% Creating the bigmask image involves segmenting the gray and white
% matter from an inplane anatomical image, combining them to produce
% skull-stripped brain, then smoothing this brain with an isotropic 
% kernel of 20 mm, and then setting all voxels with values > 0.1 to 1, 
% or 0 otherwise. The resulting image can be used as a mask. If a 
% different procedure for creating a mask image is preferred, it can 
% be performed separately and used with the original glm_specmask.m 
% script.  See that code for details.
%
% --------------------------------------------------------------------
% glm_specmask_bch.m    1.0  Jeff Cooper        2002-9-17
% adapted entirely from
% @(#)glm_specmask.m    1.2  Kalina Christoff    2000-11-13


CWD = pwd;
global SWD
spm fmri


% 1. Create a mask image
% =======================================

    % segment out the gray from the inplane
	 % ---------------------------------------------------------------

		  [segWD,brain] = fileparts(inplane);  
		  cd(segWD);
		  spm_segment(inplane,template);

		  SEG = char(spm_get('files',segWD,[brain,'_seg1.img']),...
				     spm_get('files',segWD,[brain,'_seg2.img']));
		  

	 % create the bigmask - 1) extract brain by combining gray and white
    % matter, then 2) smooth brain_ image and 3) truncate at > 0.1
	 % --------------------------------
    % 1)
	 mode = 1; % save Extracted Brain only
	 warning off; 
	 spm_xbrain(SEG,mode); 
	 warning on;
	 
	 % 2) smooth brain_ & create a bigmask.img file
    P = spm_get('files',segWD,['brain_',brain,'.img']);
	 Q = 'bigmask';
	 S = [10 10 10];
	 spm_smooth(P,Q,S)
	 
	 
	 % 3) truncate
	 spm_imcalc_ui('bigmask.img','bigmask.img','i1>0.1');
	 
	 fprintf(['\n\nDone. An image "bigmask.img" was created in %s.\n', ...
				 'It can be used as a mask at the statistics stage for this ',...
				 'subject.\n\n'],segWD);
    bigmaskimg = spm_get('files',segWD,'bigmask.img');
	 cd(CWD);

   



% 2. Modify the SPMcfg.mat file, if requested
% =============================================


	   cfgWD=fileparts(cfg)
		segWD=fileparts(bigmaskimg)

		cd(cfgWD); load(cfg);
		xM.TH = ones(size(xM.TH))*(-Inf);
		vol=spm_vol([segWD,'/bigmask.img']);
 	   xM.VM=vol;
		
		save SPMcfg xM -append;
		fprintf('\n\nDone. The SPMcfg.mat file in \n%s\n',cfgWD);
		fprintf('has been modified. Estimating the model from it will\n');
		fprintf('include all voxels in the brain, as defined by the\n');
		fprintf('mask image file.\n\n');
        

		
% 3. Do the model estimation, if requested
% ========================================



  spm_spm(VY,xX,xM,F_iX0,Sess,xsDes)

  
	 








