sys.path.insert(0, '/home/jagust/cindeem/CODE/PetProcessing/pvc')
import metzler
import logging, logging.config
from time import asctime

if __name__ == '__main__':
    """Uses specified subject dir
    finds dvr dir
    find aparc_aseg in pet space
    fins pons normed fdg
    """
    # start wx gui app
    app = wx.App()

    roifile = bg.FileDialog(prompt='Choose fsroi csv file',
                            indir='/home/jagust/cindeem/CODE/PetProcessing')

    arda = '/home/jagust/arda/lblid'
    root = bg.SimpleDirDialog(prompt='Choose FDG data dir',
                              indir='/home/jagust')

    cleantime = asctime().replace(' ', '-').replace(':', '-')
    logfile = os.path.join(root, 'logs', '%s_%s.log' % (__file__, cleantime))

    log_settings = pp.get_logging_configdict(logfile)
    logging.config.dictConfig(log_settings)

    tracer = 'FDG'
    user = os.environ['USER']
    logging.info('###START %s %s :::' % (tracer, __file__))
    logging.info('###TRACER  %s  :::' % (tracer))
    roifile = bg.FilesDialog(
        prompt='Choose roifile',
        indir='/home/jagust/cindeem/CODE/PetProcessing/pib')
    nifti = bg.FilesDialog(prompt='Choose realigned ', indir='%s/' % root)
    try:
        m = pp.re.search('B[0-9]{2}-[0-9]{3}', nifti[0])
        subid = m.group()
    except:
        logging.error('cant find ID in %s' % (nifti[0]))
        raise IOError('cant find ID in %s' % (nifti[0]))

    realigned_dir, _ = os.path.split(nifti[0])
    pth, _ = os.path.split(realigned_dir)
    # get ref and brainmask
    rbrainmask = bg.FileDialog(prompt='Choose rbrainmask', indir=pth)
    if rbrainmask is None:
        logging.error('%s missing, skipping' % (globstr))
        raise IOError('%s missing, skipping' % (globstr))
    rcere = bg.FileDialog(prompt='Choose refregion', indir=pth)
    if rcere is None:
        logging.error('%s missing, skipping' % (globstr))

    # get timing file
    sub, _ = os.path.split(pth)
    timingf = bg.FileDialog(prompt='Select Timing File', indir=pth)
    if len(timingf) < 1:
        logging.error('%s missing, skipping' % (globstr))
        raise IOError('%s missing, skipping' % (globstr))
    timingf = timingf
    ft = ft.read_frametimes(timingf)