def RunStatsOnVols(subject,Regressor):
    mainDir,subID=os.path.split(subject)

        
    for meth in Methods:
          
        WrkFile=subject
          
        if os.path.exists(WrkFile):
              
            trash='.exe'
            FileBase=subID
            while trash:
                FileBase,trash=os.path.splitext(FileBase)
            subID=FileBase
          
            if meth=='Uncorrected':
                target=subject
            else:
                
                target=os.path.join(mainDir,'{}_{}.nii.gz'.format(subID,meth))
                print target
                if not os.path.exists(target):
                    if meth=='FS':
                        Runfs.main(WrkFile,1./tr,itl)
                    elif meth=='FSL':
                        Runfsl.main(WrkFile,1./tr,itl)
                    elif meth=='SPM':
                        Runspm.main(WrkFile,1./tr,itl)
                    
                    time.sleep(20)                        
                else:
                    print 'found {} for {}'.format(meth,subID)
                
            CheckOutput=os.path.join(mainDir,'tPython',meth,con,'Beta.nii.gz')
            print CheckOutput
            if not os.path.exists(CheckOutput):
                
                pr=sp.Popen('fslmaths {} -s 5 {}/Smooth{}_{}'.format(target,mainDir,subID,meth),shell=True)
                print 'Blurring:\nfslmaths {} -s 5 Smooth{}_{}'.format(target,subID,meth)
                pr.wait()
                target=os.path.join(mainDir,'Smooth{}_{}.nii.gz'.format(subID,meth))  
                Runglm.main(target,con,Regressor,meth)
            else:
          
                print 'Ran for {} {}, skipping'.format(subject,meth)
                  
        else:
            print ('No Nii File For {}'.format(subID))
def main(WrkFile,tr,itl):


    Files=[]
    MethOrder=[]
    base,subID=os.path.split(WrkFile)
    print subID
    subID=re.search('(.*)(.nii.*)',subID).group(1)    


    
    print 'subID: {}'.format(subID)
    
    
    ########################### FS #############################
    
    target=os.path.join(base,'{}_FS.nii.gz'.format(subID))
    print target
    if not os.path.exists(target):
        Runfs.main(WrkFile,1./tr,itl)
        Files.append(target)
        MethOrder.append('FS')
    else:
        print 'found FS for {}'.format(subID)
        Files.append(target)
        MethOrder.append('FS')
        
        
     ########################### SPM #############################
       
    target=os.path.join(base,'{}_SPM.nii.gz'.format(subID))
    if not os.path.exists(target):
        Runspm.main(WrkFile,1./tr,itl)
        Files.append(target)
        MethOrder.append('SPM')
    else:
        print 'found SPM for {}'.format(subID)
        Files.append(target)
        MethOrder.append('SPM')
        
        
    ########################### FSL #############################
        
    target=os.path.join(base,'{}_FSL.nii.gz'.format(subID))
    if not os.path.exists(target):
        Runfsl.main(WrkFile,1./tr,itl)
        Files.append(target)
        MethOrder.append('FSL')
    else:
        print 'found FSL for {}'.format(subID)
        Files.append(target)
        MethOrder.append('FSL')
        
        
    ########################### Filt #############################
        
    target=os.path.join(base,'{}_Filt.nii.gz'.format(subID))
    if not os.path.exists(target):
        RunFilt.main(WrkFile,1./tr,itl)
        Files.append(target)
        MethOrder.append('Filt')
    else:
        print 'found Filt for {}'.format(subID)
        Files.append(target)
        MethOrder.append('Filt')
        
        
    ########################### Uncorrected #############################
    
    target=os.path.join(base,'{}_Uncorrected.nii.gz'.format(subID))
    if not os.path.exists(target):
        cmd='cp {} {}/{}_Uncorrected.nii.gz'.format(WrkFile,base,subID)
        pr=sp.Popen(cmd,shell=True)
        pr.wait()
        
        Files.append(target)
        MethOrder.append('Uncorrected')
    else:
        print 'found Uncorrected for {}'.format(subID)
        Files.append(target)
        MethOrder.append('Uncorrected')    
        
    
    return Files,MethOrder


#                 
# if __name__ == '__main__':
#     Subjects=glob.glob(sys.argv[1])
#     print sys.argv[1]
#     print len(Subjects)
#     for sub in Subjects:
#         RunStatsOnVols(sub)
            
            
            
            
            
            
            
            
            
                trash = ".exe"
                FileBase = WrkFile
                while trash:
                    FileBase, trash = os.path.splitext(FileBase)
                subID = FileBase

                target = os.path.join(subject, "{}_FS.nii.gz".format(subID))
                print target
                if not os.path.exists(target):
                    Runfs.main(WrkFile, 1.0 / tr, itl)
                else:
                    print "found FS for {}".format(subID)

                target = os.path.join(subject, "{}_SPM.nii.gz".format(subID))
                if not os.path.exists(target):
                    Runspm.main(WrkFile, 1.0 / tr, itl)
                else:
                    print "found SPM for {}".format(subID)

                target = os.path.join(subject, "{}_FSL.nii.gz".format(subID))
                if not os.path.exists(target):
                    Runfsl.main(WrkFile, 1.0 / tr, itl)
                else:
                    print "found FSL for {}".format(subID)

                InputNii = os.path.join(subject, "{}_{}.nii.gz".format(subID, meth))
                InputNii = WrkFile
                Regressor = os.path.join(subject, "SuperiorFrontalL.txt")

                CheckOutput = os.path.join(subject, "tPython", meth, con, "Beta.nii.gz")
                if not os.path.exists(CheckOutput):