-
Notifications
You must be signed in to change notification settings - Fork 4
/
makevtu.py
184 lines (145 loc) · 5.12 KB
/
makevtu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/python
import struct as st
import binascii as ba
FOUT = "vtutest.vtu"
def main():
try:
fout = open(FOUT,'wb')
except:
exit("Couldn't create file...")
writeHeader(fout,4,1)
lP = [0, 0, 0, 2, 0, 0, 0, 0, 2, 1, 1, 1]
writePoints(fout,lP)
lConn =[0, 1, 2, 3]
lOffset = [4]
lTypes = [10]
writeCells(fout,lConn,lOffset,lTypes)
lldata = []
lldata.append(["Scalars",1,2,-1,0.5])
lldata.append(["OtherSet",1.0,2,3,127.3])
writeScalarPointData(fout,lldata)
lldata = []
lldata.append(["Scalars",1.0])
lldata.append(["Scalars2",27.0])
writeScalarCellData(fout,lldata)
writeFooter(fout)
fout.close()
#nP = number of points, nC = number of cells
def writeHeader(fout,nP,nC):
fout.write("<?xml version=\"1.0\"?>\n")
fout.write("<VTKFile type=\"UnstructuredGrid\" version=\"1.0\" header_type=\"UInt64\" byte_order=\"LittleEndian\">\n")
fout.write("<UnstructuredGrid>\n")
fout.write("\t<Piece NumberOfPoints=\"" + str(nP) +"\" NumberOfCells=\"" + str(nC) + "\">\n")
def writeFooter(fout):
fout.write("\t</Piece>\n")
fout.write("</UnstructuredGrid>\n")
fout.write("</VTKFile>")
def writeRawPoints(fout,data):
fout.write("\t\t<Points>\n")
fout.write("\t\t\t<DataArray type=\"Float32\" NumberOfComponents=\"3\" format=\"binary\">\n")
fout.write(writeBin64(data))
fout.write("\t\t\t</DataArray>\n")
fout.write("\t\t</Points>\n")
#lP = list of points
#3D coordinates assumed...
def writePoints(fout,lP):
fout.write("\t\t<Points>\n")
fout.write("\t\t\t<DataArray type=\"Float32\" NumberOfComponents=\"3\" format=\"binary\">\n")
data = ""
for number in lP:
#Append number to binary datalist
data += st.pack('f',number)
fout.write(writeBin64(data))
fout.write("\t\t\t</DataArray>\n")
fout.write("\t\t</Points>\n")
#lConn = connectivity list
#lOffset = Offset list
def writeCells(fout,lConn,lOffset,lTypes):
fout.write("\t\t<Cells>\n")
fout.write("\t\t\t<DataArray type=\"UInt32\" Name=\"connectivity\" format=\"binary\">\n")
data = ""
for number in lConn:
data += st.pack('I',number)
fout.write(writeBin64(data))
fout.write("\t\t\t</DataArray>\n")
fout.write("\t\t\t<DataArray type=\"UInt32\" Name=\"offsets\" format=\"binary\">\n")
data = ""
for number in lOffset:
data += st.pack('I',number)
fout.write(writeBin64(data))
fout.write("\t\t\t</DataArray>\n")
#Data type could be UInt8 instead, but UInt32 keeps it simple for now...
fout.write("\t\t\t<DataArray type=\"UInt32\" Name=\"types\" format=\"binary\">\n")
data = ""
for number in lTypes:
data += st.pack('I',number)
fout.write(writeBin64(data))
fout.write("\t\t\t</DataArray>\n")
fout.write("\t\t</Cells>")
def writeRawCellsConn(fout,data):
fout.write("\t\t<Cells>\n")
fout.write("\t\t\t<DataArray type=\"UInt64\" Name=\"connectivity\" format=\"binary\">\n")
fout.write(writeBin64(data))
fout.write("\t\t\t</DataArray>\n")
def writeRawCellsOffset(fout,data):
fout.write("\t\t\t<DataArray type=\"UInt64\" Name=\"offsets\" format=\"binary\">\n")
fout.write(writeBin64(data))
fout.write("\t\t\t</DataArray>\n")
def writeRawCellsType(fout,data):
fout.write("\t\t\t<DataArray type=\"UInt64\" Name=\"types\" format=\"binary\">\n")
fout.write(writeBin64(data))
fout.write("\t\t\t</DataArray>\n")
fout.write("\t\t</Cells>")
# Returns Int containing data size + data
# everything in base64 encoding
def writeBin64(bindata):
datalen = st.pack('Q',len(bindata))
# Note that \n is removed from datalen
# - which btw is a ridiculous design choice
# as it makes the data size grow by 33% (4/3-1)
return ba.b2a_base64(datalen)[0:-1]+ba.b2a_base64(bindata)
##lldata = list of list of data (where 1st entry is set name)
def writeScalarPointData(fout,lldata):
fout.write("\t\t<PointData Scalars=\"scalars\">\n")
for ldata in lldata:
setname = ldata[0]
del ldata [0]
data = ""
for number in ldata:
data += st.pack('f',number)
fout.write("\t\t\t<DataArray type=\"Float32\" Name=\"" + setname + "\" format=\"binary\">\n")
fout.write(writeBin64(data))
fout.write("\t\t\t</DataArray>\n")
fout.write("\t\t</PointData>\n")
def writeRawScalarPointData(fout,ldata,setnames):
fout.write("\t\t<PointData Scalars=\"scalars\">\n")
j=-1
for data in ldata:
j=j+1
fout.write("\t\t\t<DataArray type=\"Float32\" Name=\"" + setnames[j] + "\" format=\"binary\">\n")
fout.write(writeBin64(data))
fout.write("\t\t\t</DataArray>\n")
fout.write("\t\t</PointData>\n")
def writeScalarCellData(fout,lldata):
fout.write("\t\t<CellData Scalars=\"scalars\">\n")
for ldata in lldata:
setname = ldata[0]
del ldata [0]
data = ""
for number in ldata:
data += st.pack('f',number)
fout.write("\t\t\t<DataArray type=\"Float32\" Name=\"" + setname + "\" format=\"binary\">\n")
fout.write(writeBin64(data))
fout.write("\t\t\t</DataArray>\n")
fout.write("\t\t</CellData>\n")
def writeRawScalarCellData(fout,ldata,setnames):
fout.write("\t\t<CellData Scalars=\"scalars\">\n")
j=-1
for data in ldata:
j=j+1
fout.write("\t\t\t<DataArray type=\"Float32\" Name=\"" + setnames[j] + "\" format=\"binary\">\n")
fout.write(writeBin64(data))
fout.write("\t\t\t</DataArray>\n")
fout.write("\t\t</CellData>\n")
if __name__ == "__main__":
main()