Peano
CSVConvert.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 pi=np.pi
9 def file_len(fname):
10  with open(fname) as f:
11  for i, l in enumerate(f):
12  pass
13  return i + 1
14 
15 lis=os.listdir("./")
16 for item in lis:
17  if item.endswith(".pvd") or item.endswith(".vtp"): os.remove(os.path.join("./",item))
18 
19 parser = argparse.ArgumentParser(description='Tracer_conversion')
20 parser.add_argument("-if", "--input-file", dest="file_name", required="True", type=str, help="input csv file name (and directory)" )
21 parser.add_argument("-dt", "--delta-time", dest="dt", default=-1, type=float, help="time interval between two snapshot, default is to print every timestep" )
22 parser.add_argument("-of", "--output-file", dest="output_name", default=" ", type=str, help="output file name, extension and number will be add automatically" )
23 args = parser.parse_args()
24 
25 
26 dt=args.dt;
27 file_name=args.file_name
28 if args.output_name==" ":
29  output_name="TracerData"
30 else:
31  output_name=args.output_name
32 
33 
35 f=open(file_name)
36 print("Read in datafile: "+file_name)
37 
38 #skip the description line
39 dat=f.readlines()[1:]
40 
41 #first find out the number of timesteps
42 tstep=0
43 told=1e6
44 for line in dat:
45  tem=list(map(float,line.split(', ')))
46  tnew=tem[0]
47  if (told-tnew)<0:
48  break
49  else:
50  tstep+=1
51  told=tnew
52 
53 number_of_data=len(tem)-6
54 
55 print("number of data entries: "+str(number_of_data))
56 print("total timesteps: "+str(tstep))
57 
58 #second, find out how many tracer recorded here.
59 N_tracer=0
60 ID1=-100; ID2=-100
61 coors=[]
62 for line in dat:
63  tem=list(map(float,line.split(', ')))
64  if (not tem[1]==ID1) or (not tem[2]==ID2) and (not tem[6]==0):
65  ID1=tem[1]; ID2=tem[2]
66  N_tracer+=1
67  coor=[tem[3],tem[4],tem[5]]
68  coors.append(coor)
69 
70 coors=np.array(coors)
71 print("valid tracers: "+str(len(coors)))
72 
73 #create the real data set
74 data_set=np.zeros((N_tracer,tstep,4+number_of_data))
75 
76 ID1=list(map(float,dat[0].split(', ')))[1]; ID2=list(map(float,dat[0].split(', ')))[2]
77 t_count=0 #counting the tsteps
78 N_count=0 #counting the tracer
79 for line in dat:
80  tem=list(map(float,line.split(', ')))
81  if (tem[1]==ID1) and (tem[2]==ID2):
82  data_set[N_count][t_count][0]=tem[0]; #timestep
83  data_set[N_count][t_count][1]=tem[3]; data_set[N_count][t_count][2]=tem[4]; data_set[N_count][t_count][3]=tem[5] #coordinates
84  for i in range(number_of_data):
85  data_set[N_count][t_count][4+i]=tem[6+i];
86  t_count+=1
87  else:
88  ID1=tem[1]; ID2=tem[2]
89  N_count+=1
90  t_count=0
91  data_set[N_count][t_count][0]=tem[0]; #timestep
92  data_set[N_count][t_count][1]=tem[3]; data_set[N_count][t_count][2]=tem[4]; data_set[N_count][t_count][3]=tem[5] #coordinates
93  for i in range(number_of_data):
94  data_set[N_count][t_count][4+i]=tem[6+i];
95  t_count+=1
96 
97 f.close()
98 #print(data_set[0,:,:])
99 
100 print("----------------------------------------------------------------")
101 
102 snapshot_count=0
103 snapshot_ids=[]
104 t_old=-1e10; t_new=0
105 for t_id in range(tstep-1,-1,-1):
106  t_new=data_set[0][t_id][0]
107  if (t_new-t_old)<dt:
108  continue
109  t_old=t_new
110  print("writing vtp file "+output_name+"-"+str(snapshot_count)+".vtp"+", t= "+str(t_new))
111  f=open(output_name+"-"+str(snapshot_count)+".vtp","w")
112  f.write("<!-- Generated from CSV file of Tracer, Peano4 ExaHyPE 2-->\n\n")
113  f.write("<VTKFile type=\"PolyData\">\n")
114  f.write("<PolyData>\n")
115  f.write("<Piece NumberOfPoints=\""+str(N_tracer)+"\">\n")
116  f.write("<Points><DataArray type=\"Float64\" NumberOfComponents=\"3\" format=\"ascii\">\n")
117  for tra_index in range(N_tracer):
118  f.write(str(data_set[tra_index][t_id][1])+" "+str(data_set[tra_index][t_id][2])+" "+str(data_set[tra_index][t_id][3])+"\n")
119  f.write("</DataArray></Points>\n")
120 
121  f.write("<PointData>\n")
122  f.write("<DataArray type=\"Float64\" Name=\"data\" format=\"ascii\" NumberOfComponents=\""+str(number_of_data)+"\" >")
123  for tra_index in range(N_tracer):
124  for data_index in range(4,4+number_of_data):
125  f.write(str(data_set[tra_index][t_id][data_index])+" ")
126  f.write("\n")
127  f.write("</DataArray>\n")
128  f.write("</PointData>\n")
129 
130  f.write("</Piece>\n</PolyData>\n</VTKFile>")
131  f.close()
132  snapshot_ids.append(t_id)
133  snapshot_count+=1
134 
135 
136 f=open(output_name+".pvd","w")
137 f.write("<VTKFile type=\"Collection\">\n")
138 f.write("<Collection>\n")
139 count=0
140 for t_id in snapshot_ids:
141  f.write("<DataSet timestep=\""+str(data_set[0][t_id][0])+"\" file=\""+output_name+"-"+str(count)+".vtp\" />\n")
142  count+=1
143 f.write("</Collection>\n")
144 f.write("</VTKFile>")
145 f.close()
146 print("pvd file "+output_name+".pvd created. Total snapshot: "+str(snapshot_count))
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
def file_len(fname)
Definition: CSVConvert.py:9