Peano
CSVMerge.py
Go to the documentation of this file.
1 import numpy as np
2 import matplotlib.pyplot as plt
3 from scipy import io
4 import os
5 import time
6 import argparse
7 
8 parser = argparse.ArgumentParser(description='csv_merger')
9 parser.add_argument("-iname", "--input-name", dest="input_name", required="True", type=str, help="input csv file prefix" )
10 parser.add_argument("-path", "--input-path", dest="path", default=".", type=str, help="input csv file directory" )
11 parser.add_argument("-of", "--output-file", dest="output_name", default=" ", type=str, help="output file name" )
12 args = parser.parse_args()
13 
14 
16 
17 path=args.path
18 input_name=args.input_name
19 lis_file=[]
20 lis=os.listdir(path)
21 for item in lis:
22  if item.startswith(input_name): lis_file.append(item)
23 
24 #print(lis_file)
25 particle_collection={}
26 ID1=-999; ID2=-999
27 for filename in lis_file:
28  f=open(path+"/"+filename)
29  print("Read in datafile: "+path+"/"+filename)
30  dat=f.readlines()[1:]
31  for line in dat:
32  tem=list(map(float,line.split(', ')))
33  if (not (tem[0],tem[1]) in particle_collection) and (not all(data==0 for data in tem[6:])):
34  particle_collection[(tem[0],tem[1])]=[[]]
35  particle_collection[(tem[0],tem[1])][0]=tem[2:]
36  elif (not tem[2] in np.array(particle_collection[(tem[0],tem[1])])[:,0] ) and (not all(data==0 for data in tem[6:])):
37  particle_collection[(tem[0],tem[1])].append(tem[2:])
38 
39  f.close()
40 
41 for items in particle_collection.values():
42  items.sort(reverse=True)
43 
44 
45 print("number of valid tracers: "+str(len(particle_collection.keys())) )
46 
47 if args.output_name==" ":
48  output_name=path+"/merged_"+input_name+".csv"
49 else:
50  output_name=path+"/"+args.output_name+".csv"
51 
52 f=open(output_name,"w")
53 f.write("t, number(0), number(1), x(0), x(1), x(2), data \n")
54 
55 for number, data in particle_collection.items():
56  for snapshot in data:
57  f.write( str(snapshot[0])+", "+str(int(number[0]))+", "+str(int(number[1])) )
58  for variable in snapshot[1:]:
59  f.write(", "+str(variable))
60  f.write("\n")
61 
62 f.close()
63 
64 
65 
66 
67 
68 
69 
70