def main(InputNii,con,Regressor,meth,TR,itl,Fs,WrkDir=''):
	
	txt=np.loadtxt(Regressor)
	if not all([i==0 for i in txt]):
		
		RootDir,FileName=os.path.split(InputNii)
	
		trash='.exe'
		FileBase=FileName
		while trash:
			FileBase,trash=os.path.splitext(FileBase)
	
		   
		OutputPath=os.path.join(RootDir,'tPython',meth,con)
		
		if not WrkDir:
			WrkDir=os.path.join(RootDir,'{}_Work'.format(FileBase))
			
		GLMFiles=os.path.join(WrkDir,'WorkFiles','GLM',meth,con)
		ClusterJobDir=os.path.join(WrkDir,'ShFiles','GLM',meth,con)
		ErrorOutput=os.path.join(WrkDir,'ClusterError','GLM',meth,con)
		ClusterOutput=os.path.join(WrkDir,'ClusterOutput','GLM',meth,con)
		
		ReportFile=os.path.join(GLMFiles,'Report.txt')
		if os.path.exists(ReportFile):
			cmd='rm {}'.format(ReportFile)
			print cmd
			os.system(cmd)
		BaseDir,fname=os.path.split(InputNii)
		
		while not fname.find('.')==-1:
			fname,trash=os.path.splitext(fname)
			
		
		if not os.path.exists(WrkDir):
			os.makedirs(WrkDir)
	
		if not os.path.exists(ClusterJobDir):
			os.makedirs(ClusterJobDir)
		else:
			cmd='rm {}/*.sh'.format(ClusterJobDir)
			Report(ReportFile,cmd)
			os.system(cmd)
		
		if not os.path.exists(GLMFiles):
			os.makedirs(GLMFiles)
		else:
			cmd='rm {}/*'.format(GLMFiles)
			Report(ReportFile,cmd)
			os.system(cmd)
			
		if not os.path.exists(ErrorOutput):
			os.makedirs(ErrorOutput)
		else:
			cmd='rm {}/*'.format(ErrorOutput)
			Report(ReportFile,cmd)
			os.system(cmd)
			
			
		if not os.path.exists(ClusterOutput):
			os.makedirs(ClusterOutput)
		else:
			cmd='rm {}/*'.format(ClusterOutput)
			Report(ReportFile,cmd)
			os.system(cmd)
			
			
		if not os.path.exists(OutputPath):
			os.makedirs(OutputPath)
		print InputNii
		img=nb.load(InputNii)
		nslice=img.get_shape()[2]
		

		nb.loadsave.save(img,'{}/FullNii.nii.gz'.format(GLMFiles))
		# cmd='cp {} {}/FullNii.nii'.format(InputNii,GLMFiles)
		# print cmd
		# process=sp.Popen(cmd,shell=True)
		# process.wait()
		
		cmd='fslsplit {0}/FullNii.nii {0}/RefVol -t'.format(GLMFiles)
		Report(ReportFile,cmd)
		process=sp.Popen(cmd,shell=True)
		process.wait()
		
		RVs=glob.glob(os.path.join(GLMFiles,'RefVol*'))
		
		for r in RVs[1:]:
			cmd='rm {}'.format(r)
			Report(ReportFile,cmd)
			process=sp.Popen(cmd,shell=True)
			process.wait()
		
		RV=RVs[0]
		cmd='fslsplit {} {}/RefZ -z'.format(RV,GLMFiles)
		Report(ReportFile,cmd)
		process=sp.Popen(cmd,shell=True)
		process.wait()    
		RefVol=RV
		
		
		RVs=glob.glob(os.path.join(GLMFiles,'RefZ*'))
		for r in RVs[1:]:
			cmd='rm {}'.format(r)
			Report(ReportFile,cmd)
			process=sp.Popen(cmd,shell=True)
			process.wait()
		RV=RVs[0]
		
		cmd='fslsplit {0}/FullNii.nii {0}/vol -z'.format(GLMFiles)
		Report(ReportFile,cmd)
		process=sp.Popen(cmd,shell=True)
		process.wait()
		
		vols=sorted(glob.glob(os.path.join(GLMFiles,'vol*.nii.gz')))
		nVols=len(vols)        
		
		SlicesOrder,SliceTiming=MakeT(itl,0,nslice,TR)
		
		Regressor=np.loadtxt(Regressor)
		if not len(Regressor) >= img.shape[-1]*np.round(Fs*TR):
			print 'Interpolating'
			t1=np.array(range(len(Regressor)))*2
			t20=np.arange(t1[0],t1[-1],1./Fs)
			f=tpl.interp1d(t1,Regressor,kind='cubic')
			Regressor=f(t20)
			
		
		for k in xrange(img.shape[2]):
			RegressorDownsampled=np.zeros((img.shape[-1]))
			
			if len(Regressor[np.round(SliceTiming[k]*Fs)::(np.round(Fs*TR))])<img.shape[3]:
				RegressorDownsampled = list(Regressor[np.round(SliceTiming[k]*Fs)::np.round(Fs*TR)]).append(0)       
			else:   
				RegressorDownsampled = Regressor[np.round((SliceTiming[k])*Fs)::np.round(Fs*TR)]
				print 'slice {} starting at {}*{}={}::{}'.format(k,SliceTiming[k],Fs,np.round(SliceTiming[k]*Fs),np.round(Fs*TR))
				

			print len(RegressorDownsampled)
			np.savetxt(os.path.join(GLMFiles,'SliceReg{0:04d}.txt'.format(int(k))),RegressorDownsampled,fmt='%1.16f')
		
		regs=sorted(glob.glob(os.path.join(GLMFiles,'SliceReg*.txt')))
		nRegs=len(regs)
		
		if not nRegs==nVols:
			print ('Number of slices does not equal number of regs')
			return
		
		Report(ReportFile,'Number of Vols: {}'.format(nVols))
		for v,r in zip(vols,regs):
			trash,volID=os.path.split(v)
	
			volID,trash=os.path.splitext(volID)
	
			while trash:
				volID,trash=os.path.splitext(volID)
	
				
			rpt='Making SH for Volume {}'.format(volID)
			Report(ReportFile,rpt)
			shPath=os.path.join(ClusterJobDir,volID+'.sh')
			GLMsh.main(v,r,RV,shPath,ErrorOutput,ClusterOutput)
		
	
		SubmitFiles=glob.glob(ClusterJobDir+'/*.sh')
		p=Pool(20)
		p.map(SH.main,SubmitFiles)
		p.close()
		p.join()
		
		Min=0
		Nvols=len(glob.glob(os.path.join(GLMFiles,'vol*')))
		NBetas=len(glob.glob(os.path.join(GLMFiles,'Beta*')))
		
		while not Nvols==NBetas and Min<120:
			time.sleep(60)
			Min=Min+1
			Nvols=len(glob.glob(os.path.join(GLMFiles,'vol*')))
			NBetas=len(glob.glob(os.path.join(GLMFiles,'Beta*')))
			
			if Min==120:
				print ('Timeout Error For {}'.format(InputNii))
				Report(ReportFile,'Timeout Error For {}'.format(InputNii))
			print 'waiting'
			
		Betas=sorted(glob.glob(os.path.join(GLMFiles,'Beta*')))
		#Report(ReportFile,cmd)
		files=''
		for b in Betas:
			files=files+b+' '
		cmd='fslmerge -z {} {}'.format(os.path.join(OutputPath,'Beta'),files)
		Report(ReportFile,cmd)
		process=sp.Popen(cmd,shell=True)
		process.wait()
		
		cmd='fslcpgeom {} {}'.format(RefVol,os.path.join(OutputPath,'Beta'))
		Report(ReportFile,cmd)
		process=sp.Popen(cmd,shell=True)
		process.wait()        
		
		Betas=sorted(glob.glob(os.path.join(GLMFiles,'tStat*')))
		#print Betas
		files=''
		for b in Betas:
			files=files+b+' '
		cmd='fslmerge -z {} {}'.format(os.path.join(OutputPath,'tStat'),files)
		Report(ReportFile,cmd)
		process=sp.Popen(cmd,shell=True)
		process.wait()
		
		cmd='fslcpgeom {} {}'.format(RefVol,os.path.join(OutputPath,'tStat'))
		Report(ReportFile,cmd)
		process=sp.Popen(cmd,shell=True)
		process.wait()
		
		
		Betas=sorted(glob.glob(os.path.join(GLMFiles,'pVal*')))
		#print Betas
		files='' 
		for b in Betas:
			files=files+b+' '
		cmd='fslmerge -z {} {}'.format(os.path.join(OutputPath,'pVal'),files)
		Report(ReportFile,cmd)
		process=sp.Popen(cmd,shell=True)
		process.wait()
		
		cmd='fslcpgeom {} {}'.format(RefVol,os.path.join(OutputPath,'pVal'))
		Report(ReportFile,cmd)
		process=sp.Popen(cmd,shell=True)
		process.wait()
		
	else:
		print ('All Zero Regressor.  Skipping.')
def main(InputNii,con,Regressor,meth,WrkDir='',OutputDir=''):
    
    txt=np.loadtxt(Regressor)
    if not all([i==0 for i in txt]):
        

            
        
        RootDir,FileName=os.path.split(InputNii)
    
        trash='.exe'
        FileBase=FileName
        while trash:
            FileBase,trash=os.path.splitext(FileBase)
    
        if OutputDir=='': 
            OutputPath=os.path.join(RootDir,'tPython',meth,con)
        else:
            OutputPath=os.path.join(OutputDir,'tPython',meth,con)
        
        if not WrkDir:
            WrkDir=os.path.join(RootDir,'{}_Work'.format(FileBase))
            
        GLMFiles=os.path.join(WrkDir,'WorkFiles','GLM',meth,con)
        ClusterJobDir=os.path.join(WrkDir,'ShFiles','GLM',meth,con)
        ErrorOutput=os.path.join(WrkDir,'ClusterError','GLM',meth,con)
        ClusterOutput=os.path.join(WrkDir,'ClusterOutput','GLM',meth,con)
        
        ReportFile=os.path.join(GLMFiles,'Report.txt')
        if os.path.exists(ReportFile):
            cmd='rm {}'.format(ReportFile)
            print cmd
            os.system(cmd)
        BaseDir,fname=os.path.split(InputNii)
        
        while not fname.find('.')==-1:
            fname,trash=os.path.splitext(fname)
            
        
        if not os.path.exists(WrkDir):
            os.makedirs(WrkDir)
    
        if not os.path.exists(ClusterJobDir):
            os.makedirs(ClusterJobDir)
        else:
            cmd='rm {}/*.sh'.format(ClusterJobDir)
            Report(ReportFile,cmd)
            os.system(cmd)
        
        if not os.path.exists(GLMFiles):
            os.makedirs(GLMFiles)
        else:
            cmd='rm {}/*'.format(GLMFiles)
            Report(ReportFile,cmd)
            os.system(cmd)
            
        if not os.path.exists(ErrorOutput):
            os.makedirs(ErrorOutput)
        else:
            cmd='rm {}/*'.format(ErrorOutput)
            Report(ReportFile,cmd)
            os.system(cmd)
            
            
        if not os.path.exists(ClusterOutput):
            os.makedirs(ClusterOutput)
        else:
            cmd='rm {}/*'.format(ClusterOutput)
            Report(ReportFile,cmd)
            os.system(cmd)
            
            
        if not os.path.exists(OutputPath):
            os.makedirs(OutputPath)
        print InputNii
        img=nb.load(InputNii)
        nb.loadsave.save(img,'{}/FullNii.nii.gz'.format(GLMFiles))
        # cmd='cp {} {}/FullNii.nii'.format(InputNii,GLMFiles)
        # print cmd
        # process=sp.Popen(cmd,shell=True)
        # process.wait()
        
        cmd='fslsplit {0}/FullNii.nii {0}/RefVol -t'.format(GLMFiles)
        Report(ReportFile,cmd)
        process=sp.Popen(cmd,shell=True)
        process.wait()
        
        RVs=glob.glob(os.path.join(GLMFiles,'RefVol*'))
        
        for r in RVs[1:]:
            cmd='rm {}'.format(r)
            Report(ReportFile,cmd)
            process=sp.Popen(cmd,shell=True)
            process.wait()
        
        RV=RVs[0]
        cmd='fslsplit {} {}/RefZ -z'.format(RV,GLMFiles)
        Report(ReportFile,cmd)
        process=sp.Popen(cmd,shell=True)
        process.wait()    
        RefVol=RV
        
        
        RVs=glob.glob(os.path.join(GLMFiles,'RefZ*'))
        for r in RVs[1:]:
            cmd='rm {}'.format(r)
            Report(ReportFile,cmd)
            process=sp.Popen(cmd,shell=True)
            process.wait()
        RV=RVs[0]
        
        cmd='fslsplit {0}/FullNii.nii {0}/vol -z'.format(GLMFiles)
        Report(ReportFile,cmd)
        process=sp.Popen(cmd,shell=True)
        process.wait()
        
        vols=glob.glob(os.path.join(GLMFiles,'vol*.nii.gz'))
        nVols=len(vols)
        Report(ReportFile,'Number of Vols: {}'.format(nVols))
        for v in vols:
            trash,volID=os.path.split(v)
    
            volID,trash=os.path.splitext(volID)
    
            while trash:
                volID,trash=os.path.splitext(volID)
    
                
            rpt='Making SH for Volume {}'.format(volID)
            Report(ReportFile,rpt)
            shPath=os.path.join(ClusterJobDir,volID+'.sh')
            GLMsh.main(v,Regressor,RV,shPath,ErrorOutput,ClusterOutput)
        
        cmd='chmod 777 {}/*.sh'.format(ClusterJobDir)
        pr=sp.Popen(cmd,shell=True)
        pr.wait()
        
        
        SubmitFiles=glob.glob(ClusterJobDir+'/*.sh')
        p=Pool(20)
        p.map(SH.main,SubmitFiles)
        p.close()
        p.join()
        
        Min=0
        Nvols=len(glob.glob(os.path.join(GLMFiles,'vol*')))
        NBetas=len(glob.glob(os.path.join(GLMFiles,'Beta*')))
        
        while not Nvols==NBetas and Min<120:
            time.sleep(60)
            Min=Min+1
            Nvols=len(glob.glob(os.path.join(GLMFiles,'vol*')))
            NBetas=len(glob.glob(os.path.join(GLMFiles,'Beta*')))
            
            if Min==120:
                print ('Timeout Error For {}'.format(InputNii))
                Report(ReportFile,'Timeout Error For {}'.format(InputNii))
            print 'waiting'
            
            
        Betas=sorted(glob.glob(os.path.join(GLMFiles,'Beta*')))
        #Report(ReportFile,cmd)
        files=''
        for b in Betas:
            files=files+b+' '
        cmd='fslmerge -z {} {}'.format(os.path.join(OutputPath,'Beta'),files)
        Report(ReportFile,cmd)
        process=sp.Popen(cmd,shell=True)
        process.wait()
        
        cmd='fslcpgeom {} {}'.format(RefVol,os.path.join(OutputPath,'Beta'))
        Report(ReportFile,cmd)
        process=sp.Popen(cmd,shell=True)
        process.wait()        
        
        Betas=sorted(glob.glob(os.path.join(GLMFiles,'tStat*')))
        #print Betas
        files=''
        for b in Betas:
            files=files+b+' '
        cmd='fslmerge -z {} {}'.format(os.path.join(OutputPath,'tStat'),files)
        Report(ReportFile,cmd)
        process=sp.Popen(cmd,shell=True)
        process.wait()
        
        cmd='fslcpgeom {} {}'.format(RefVol,os.path.join(OutputPath,'tStat'))
        Report(ReportFile,cmd)
        process=sp.Popen(cmd,shell=True)
        process.wait()
        
        
        Betas=sorted(glob.glob(os.path.join(GLMFiles,'pVal*')))
        #print Betas
        files='' 
        for b in Betas:
            files=files+b+' '
        cmd='fslmerge -z {} {}'.format(os.path.join(OutputPath,'pVal'),files)
        Report(ReportFile,cmd)
        process=sp.Popen(cmd,shell=True)
        process.wait()
        
        cmd='fslcpgeom {} {}'.format(RefVol,os.path.join(OutputPath,'pVal'))
        Report(ReportFile,cmd)
        process=sp.Popen(cmd,shell=True)
        process.wait()
        
    else:
        print ('All Zero Regressor.  Skipping.')