import sys,os,glob pandapath=os.environ.get('PANDAPATH') sys.path.append(pandapath+'/macro/tpc/FOPI/python/argparse-1.2.1') import argparse parser=argparse.ArgumentParser(description="merge various devmap files") parser.add_argument("path",help="path to devmap files") parser.add_argument("devpat",help="devmap pattern") parser.add_argument("--repair",help="repair filenames",action="store_true") parser.add_argument("--zBin",help='merge up to z-bin',type=int,default=-1) parser.add_argument("--test",help='test only. no merging',action='store_true') parser.add_argument('--lines',help='number of lines to merge',type=int, default=-1) parser.add_argument('--nbins',help='number of z-bins to expect in one file',type=int,default=10) parser.add_argument('--rm',help='remove part files instead of moving to done',action='store_true') parser.add_argument('--nr',help='number of r-bins',type=int,default=210) args=parser.parse_args() files=glob.glob(args.path+"/*"+args.devpat+"*") dofiles=[] for f in files: if f.find("_merged.txt")!=-1: continue if f.find(args.devpat)!=-1: dofiles.append(f) dofiles.sort() if len(dofiles)==0: exit() if not args.test: outfile=open(args.path+"/"+args.devpat+"_merged.txt","w+") nz=0 nr=0 bin={} fcounter=0 wrote_header=False headline="" #if not os.system.ispath(args.path+"/done"): os.system("mkdir "+args.path+"/done") for fname in dofiles: if fcounter%100==0: print "reading files,",fcounter,"("+str(len(dofiles))+")" fcounter+=1 if fname.find('#')!=-1: os.system('rm '+fname) continue if fname.find('~')!=-1: os.system('rm '+fname) continue thefile=open(fname,'r') lcounter=-1 for line in thefile: lcounter+=1 words=line.split() if len(words)==0: continue if words[0][0]=='#': continue if lcounter==0 and not wrote_header and (fname.find("r0000z0000")!=-1 or fname.find("_tmp.txt")!=-1): nr=int(words[0]) nz=int(words[1]) print 'found header: nr={0}, nz={1}'.format(nr,nz) line = str(nr)+" "+str(nz)+" "+words[2]+" "+words[3]+" "+words[4]+" "+words[5]+" "+words[6]+"\n" headline=line if not args.test and not wrote_header: outfile.write(line) wrote_header=True else: try: zb=int(words[6]) except ValueError: continue rb=int(words[5]) if bin.get(rb,None)==None: bin[rb]={} bin[rb][zb]=words[0]+" "+words[1]+" "+words[2]+" "+words[3]+" "+words[4] if args.test and fname.find("tmp")==-1: if args.rm: os.system("rm "+fname) else: os.system("mv "+fname+" "+args.path+"/done/") thefile.close() if not wrote_header: print "header was not found! nothing will be written. please supply a header." exit() if args.test: tmpfile=open(args.path+"/"+args.devpat+"_tmp.txt","w") tmpfile.write(headline) rcounter=0 missingbins=0 errfile=open("devMapErrors"+args.devpat+".txt","w") for r in range(nr): if bin.get(r,None)!=None: missing="" errcount=0 for z in range(nz): if bin[r].get(z,None)!=None: if bin[r][z].find("0 0 0")!=-1: print r,z,bin[r][z] if not args.test: outfile.write(bin[r][z]+"\n") if args.test: tmpfile.write(bin[r][z]+" "+str(r)+" "+str(z)+"\n") else: print "For r-Bin",r,"z-Bin",z,"is missing" if errcount==0: missing+=str(z) else: missing+=","+str(z) errcount+=1 missingbins+=1 if missing!="": errfile.write("r-Bin: "+str(r)+" z-Bin: "+missing+"\n") else: print "r-Bin",r,"is missing" errfile.write("r-Bin: "+str(r)+" complete\n") missingbins+=nz print missingbins,"bins are missing in total" if not args.test: print "output written to",args.path+"/"+args.devpat+"_merged.txt" print "errors writte to devMapErrors"+args.devpat+".txt" if not args.test: outfile.close() if args.test: tmpfile.close()