Y0da Y0da - 3 months ago 104
Python Question

Convert Tecplot ascii to numpy

I want to convert a Tecplot file into an array but I don't know how to do it.
Here is an extract of the file:

TITLE = "Test"
VARIABLES = "x" "y"
ZONE I=18, F=BLOCK
0.1294538E-01 0.1299554E-01 0.1303974E-01 0.1311453E-01 0.1313446E-01 0.1319080E-01
0.1322709E-01 0.1323904E-01 0.1331753E-01 0.1335821E-01 0.1340850E-01 0.1347061E-01
0.1350522E-01 0.1358302E-01 0.1359585E-01 0.1363086E-01 0.1368307E-01 0.1370017E-01
0.1377368E-01 0.1381353E-01 0.1386420E-01 0.1391916E-01 0.1395847E-01 0.1400548E-01
0.1405659E-01 0.1410006E-01 0.1417611E-01 0.1419149E-01 0.1420015E-01 0.1428019E-01
0.1434745E-01 0.1436735E-01 0.1439856E-01 0.1445430E-01 0.1448778E-01 0.1454278E-01


I want to retrieve
x
and
y
as array. So
x
should contain:

0.1294538E-01 0.1299554E-01 0.1303974E-01 0.1311453E-01 0.1313446E-01 0.1319080E-01
0.1322709E-01 0.1323904E-01 0.1331753E-01 0.1335821E-01 0.1340850E-01 0.1347061E-01
0.1350522E-01 0.1358302E-01 0.1359585E-01 0.1363086E-01 0.1368307E-01 0.1370017E-01


And
y
should contain:

0.1377368E-01 0.1381353E-01 0.1386420E-01 0.1391916E-01 0.1395847E-01 0.1400548E-01
0.1405659E-01 0.1410006E-01 0.1417611E-01 0.1419149E-01 0.1420015E-01 0.1428019E-01
0.1434745E-01 0.1436735E-01 0.1439856E-01 0.1445430E-01 0.1448778E-01 0.1454278E-01


I have seen
np.loadtxt('./file.dat', skiprows=3)
but I can't find the right options to say read all numbers and separate every 18 figures.

Also, I started something like this with no luck:

with open(file, 'r') as a:
for line in a.readlines():
A = re.match(r'TITLE = (.*$)', line, re.M | re.I)
B = re.match(r'VARIABLES = (.*$)', line, re.M | re.I)
C = re.match(r'ZONE (.*$)', line, re.M | re.I)
if A or B or C:
continue
else:
D = re.match(r'(.*$)', line, re.M | re.I)
value = "{:.16}".format(D.group(1))
y.append(float(value))
j = j+1
if j == 18:
j = 0


Thank you for your help!

Answer

Solved it with the last option:

arrays = []
with open(file, 'r') as a:
    for line in a.readlines():
        A = re.match(r'TITLE = (.*$)', line, re.M | re.I)
        B = re.match(r'VARIABLES = (.*$)', line, re.M | re.I)
        C = re.match(r'ZONE (.*$)', line, re.M | re.I)
        if A or B or C:
                continue
        else:
            arrays.append([float(s) for s in line.split()])
arrays = np.concatenate(arrays)

len_var = len(arrays)
x = arrays[0:len_var/2-1]
y = arrays[len_var/2:len_var]

This answer was of great help for the creation of the array: http://stackoverflow.com/a/4289557/6522112 and also this one for traveling the array: http://stackoverflow.com/a/952952/6522112. But in the end using np.concatenate seems better.

For the record, I created this function in order to read any file:

def tecplot_reader(file, nb_var):
    """Tecplot reader."""
    arrays = []
    with open(file, 'r') as a:
        for line in a.readlines():
            A = re.match(r'TITLE = (.*$)', line, re.M | re.I)
            B = re.match(r'VARIABLES = (.*$)', line, re.M | re.I)
            C = re.match(r'ZONE (.*$)', line, re.M | re.I)
            if A or B or C:
                    continue
            else:
                arrays.append([float(s) for s in line.split()])
    arrays = np.concatenate(arrays)
    output = np.split(arrays, nb_var)

    return output

So just do: x, y, z = tecplot_reader('./file', 3)

Comments