I am a beginner so sorry if this has been covered before,but I can't seem to find exactly what I need to solve my problem. I am trying to write an AWK "script" that can convert an MDF(Mesh Definition File) as input into a (VALID) VTK file as output.
I have a sample MDF file that looks like this :
TITLE "1"
NMESHPOINTS 4
NNODES 4
NELEMENTS_TRIANG1 2
TIMESTEP 0.00001
NINTERNAL_TIMESTEPS 1000
NEXTERNAL_TIMESTEPS 100
DAMPING_FACTOR 0.01
MESHPOINT_COORDINATES
1 0.0 0.0 0.0
2 1.0 0.0 0.0
3 1.0 1.0 0.0
4 0.0 1.0 0.0
NODES_TRIANG1
1 1 2 3
2 1 3 4
And I want to make a valid VTK file from this input. Here is how the output should look like:
# vtk DataFile Version 1.0
2D Unstructured Grid
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 4 float
0.0 0.0 0.0
1.0 0.0 0.0
1.0 1.0 0.0
0.0 1.0 0.0
CELLS 2 8
3 0 1 2
3 0 2 3
CELL_TYPES 2
5
5
I tried to make a picture how the mappings works I hope it explains some of them. To make it a bit easier for this specific example let's say we only want to work with triangles.
Sadly I dont have the same file as VTK and MDF too, I tried to manualy write one. Is there any way to do this with AWK? Any help will be much appreciated!!
Excellent diagram showing the input -> output mapping! Made it extremely easy to write this:
$ cat tst.awk
$1 ~ /^[[:alpha:]]/ { f[$1] = $2 }
!NF { block = "" }
$1 == "MESHPOINT_COORDINATES" {
block = $1
print "# vtk DataFile Version 1.0"
print "2D Unstructured Grid"
print "ASCII"
print ""
print "DATASET UNSTRUCTURED_GRID"
printf "POINTS %d float\n", f["NMESHPOINTS"]
next
}
block == "MESHPOINT_COORDINATES" {
$1 = ""
sub(/^[[:space:]]+/,"")
print
}
$1 == "NODES_TRIANG1" {
block = $1
printf "\nCELLS %d %d\n", f["NELEMENTS_TRIANG1"], f["NELEMENTS_TRIANG1"] * 4
next
}
block == "NODES_TRIANG1" {
printf "%s", 3
for (i=2; i<=NF; i++) {
printf " %s", $i - 1
}
print ""
nlines++
}
END {
printf "\nCELL_TYPES %d\n", nlines
for (i=1; i<=nlines; i++) {
print 5
}
}
.
$ awk -f tst.awk file.mdf
# vtk DataFile Version 1.0
2D Unstructured Grid
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 4 float
0.0 0.0 0.0
1.0 0.0 0.0
1.0 1.0 0.0
0.0 1.0 0.0
CELLS 2 8
3 0 1 2
3 0 2 3
CELL_TYPES 2
5
5
Normally we only answer questions where the poster has attempted to solve it themselves first but you put enough effort into creating the example and describing the mapping that IMHO you deserve help with a solution so - see the above, try to figure out how it's working yourself (add "prints", check the man page, etc.) and then post a new question if you have any specific questions about it.