#! /usr/bin/env python
# -*- coding:utf-8 -*-

import Manyo.Utsusemi as mu
from os import path, environ
from glob import glob

def MakeT0file(runNo):
    tt = mu.T0TreatToolsNeunet()
    datapath=environ["UTSUSEMI_DATA_DIR"]
    blname=environ["UTSUSEMI_INST_CODE"]
    edbdir=glob(path.join(datapath,blname,blname)+runNo+"_*/")[0]
    #edbfile=[]
    #t0bfile=[]

    footer = ".edb"
    files=glob(edbdir+blname+runNo+"*"+footer)
    #basefile = ((files[0].split("/"))[-1].split(footer))[0]
    #basefiles = files=glob(path.join(dirs[0],basefile)+"*.edb")
    #basefiles = glob(path.join(dirs[0],basefile)+"*.edb")
    
    for f in files:
        edbfile = f
        t0bfile = path.join(edbdir)+"/"+f.split("/")[-1].replace("edb","t0b")
        #print (">>>>", edbfile, t0bfile, path.exists(edbfile), path.exists(t0bfile))
        tt.readOrgEvent(edbfile)
        tt.saveT0IndexEvent(t0bfile)
        print (">>>>", edbfile, t0bfile, path.exists(edbfile), path.exists(t0bfile))
        #tt.readT0IndexEvent(t0bfile[i])


if __name__ == '__main__':
    runno = input('input RunNo>')
    print (type(runno), str("%06d" %int(runno)))

    MakeT0file(str("%06d" %int(runno)))
