Peano
Loading...
Searching...
No Matches
CSVConvert.py
Go to the documentation of this file.
1import numpy as np
2import matplotlib.pyplot as plt
3from scipy import io
4import os
5import time
6import argparse
7
8pi=np.pi
9def file_len(fname):
10 with open(fname) as f:
11 for i, l in enumerate(f):
12 pass
13 return i + 1
14
15lis=os.listdir("./")
16for item in lis:
17 if item.endswith(".pvd") or item.endswith(".vtp"): os.remove(os.path.join("./",item))
18
19parser = argparse.ArgumentParser(description='Tracer_conversion')
20parser.add_argument("-if", "--input-file", dest="file_name", required="True", type=str, help="input csv file name (and directory)" )
21parser.add_argument("-dt", "--delta-time", dest="dt", default=-1, type=float, help="time interval between two snapshot, default is to print every timestep" )
22parser.add_argument("-of", "--output-file", dest="output_name", default=" ", type=str, help="output file name, extension and number will be add automatically" )
23args = parser.parse_args()
24
25
26dt=args.dt;
27file_name=args.file_name
28if args.output_name==" ":
29 output_name="TracerData"
30else:
31 output_name=args.output_name
32
33
35f=open(file_name)
36print("Read in datafile: "+file_name)
37
38#skip the description line
39dat=f.readlines()[1:]
40
41#first find out the number of timesteps
42tstep=0
43told=1e6
44for 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
53number_of_data=len(tem)-6
54
55print("number of data entries: "+str(number_of_data))
56print("total timesteps: "+str(tstep))
57
58#second, find out how many tracer recorded here.
59N_tracer=0
60ID1=-100; ID2=-100
61coors=[]
62for 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
70coors=np.array(coors)
71print("valid tracers: "+str(len(coors)))
72
73#create the real data set
74data_set=np.zeros((N_tracer,tstep,4+number_of_data))
75
76ID1=list(map(float,dat[0].split(', ')))[1]; ID2=list(map(float,dat[0].split(', ')))[2]
77t_count=0 #counting the tsteps
78N_count=0 #counting the tracer
79for 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
97f.close()
98#print(data_set[0,:,:])
99
100print("----------------------------------------------------------------")
101
102snapshot_count=0
103snapshot_ids=[]
104t_old=-1e10; t_new=0
105for 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
136f=open(output_name+".pvd","w")
137f.write("<VTKFile type=\"Collection\">\n")
138f.write("<Collection>\n")
139count=0
140for 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
143f.write("</Collection>\n")
144f.write("</VTKFile>")
145f.close()
146print("pvd file "+output_name+".pvd created. Total snapshot: "+str(snapshot_count))
147
148
149
150
151
152
153
154
155
156
157
158
159
file_len(fname)
Definition CSVConvert.py:9