1 | """ This script takes a River2D msh file of format: |
---|
2 | |
---|
3 | node no.,x,y,z,roughness |
---|
4 | no more nodes. |
---|
5 | Vert1,Vert2,Vert3,Tag,TagValue |
---|
6 | no more boundary segments. |
---|
7 | |
---|
8 | |
---|
9 | and converts it to a format where it can be used to creat a domain from Points and Vertices. |
---|
10 | |
---|
11 | All you need to do is specify the data file name and output file name |
---|
12 | |
---|
13 | |
---|
14 | """ |
---|
15 | import string |
---|
16 | |
---|
17 | infid = open('Riv2dMesh.msh') # input file |
---|
18 | outfid1 = open('ANUGA_MeshPts.csv', 'w') # output filename |
---|
19 | outfid2 = open('ANUGA_MeshVertices.csv', 'w') # output filename |
---|
20 | print "\nFILES are open ready for conversion....\n" |
---|
21 | |
---|
22 | reading_points = True |
---|
23 | reading_vertices = False |
---|
24 | last_point_index = 0 |
---|
25 | for line in infid.readlines(): |
---|
26 | print 'line', line.strip() |
---|
27 | |
---|
28 | if line.strip() == '': |
---|
29 | continue |
---|
30 | |
---|
31 | if line.lower().startswith('no more nodes'): |
---|
32 | reading_vertices = True |
---|
33 | reading_points = False |
---|
34 | continue |
---|
35 | |
---|
36 | if line.lower().startswith('no more boundary segments'): |
---|
37 | break |
---|
38 | |
---|
39 | if reading_points is True: # Read Points |
---|
40 | |
---|
41 | # Check that point indices are consecutive |
---|
42 | s = line[:5] |
---|
43 | index = int(s.strip()) |
---|
44 | assert index-1 == last_point_index |
---|
45 | last_point_index = index |
---|
46 | |
---|
47 | # Get points |
---|
48 | s = line[9:38] |
---|
49 | fields = s.split(' ') |
---|
50 | x=float(fields[0]) |
---|
51 | y=float(fields[1]) |
---|
52 | #z=float(fields[3]) # TODO: Get elevation data a separate list |
---|
53 | outfid1.write('%.3f, %.3f\n' % (x, y)) |
---|
54 | |
---|
55 | elif reading_vertices is True: # Read Vertices |
---|
56 | fields = line.split(' ') |
---|
57 | |
---|
58 | # Create indices and make them start from 0 |
---|
59 | pt1=int(line[1:6]) - 1 |
---|
60 | pt2=int(line[10:16]) - 1 |
---|
61 | pt3=int(line[20:26]) - 1 |
---|
62 | outfid2.write('%d, %d, %d\n' % (pt1, pt2,pt3)) |
---|
63 | |
---|
64 | |
---|
65 | outfid1.close() |
---|
66 | outfid2.close() |
---|
67 | print "\nConversion complete.\n" |
---|
68 | |
---|