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('Riv2dMesh2.cdg') # input file is a River 2D run 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 | lines = infid.readlines() |
---|
23 | |
---|
24 | # Ignore all metadata prior to node information |
---|
25 | for i, line in enumerate(lines): |
---|
26 | if line.strip().startswith('Node #'): |
---|
27 | # Found node information |
---|
28 | break |
---|
29 | |
---|
30 | |
---|
31 | reading_points = True |
---|
32 | reading_vertices = False |
---|
33 | last_point_index = 0 |
---|
34 | |
---|
35 | for L in lines[i+1:]: |
---|
36 | line = L.strip() |
---|
37 | |
---|
38 | print line |
---|
39 | |
---|
40 | if line == '': |
---|
41 | continue |
---|
42 | |
---|
43 | if line.startswith('Element Information'): |
---|
44 | continue |
---|
45 | |
---|
46 | if line.startswith('Element #'): |
---|
47 | reading_vertices = True |
---|
48 | reading_points = False |
---|
49 | continue |
---|
50 | |
---|
51 | if line.startswith('Boundary Element #'): |
---|
52 | print 'Reached boundary info, not processing yet' |
---|
53 | break |
---|
54 | |
---|
55 | if reading_points is True: # Read Points |
---|
56 | fields = line.split() |
---|
57 | |
---|
58 | #print fields, len(fields) |
---|
59 | |
---|
60 | if len(fields) == 9: |
---|
61 | # No single character flag |
---|
62 | x_index = 1 |
---|
63 | elif len(fields) == 10: |
---|
64 | # Single character flag used in in second column |
---|
65 | x_index = 2 |
---|
66 | else: |
---|
67 | print fields, len(fields) |
---|
68 | raise Exception, 'Bad file format' |
---|
69 | |
---|
70 | # Check that point indices are consecutive |
---|
71 | s = fields[0] |
---|
72 | index = int(s) |
---|
73 | assert index-1 == last_point_index |
---|
74 | last_point_index = index |
---|
75 | |
---|
76 | # Get points |
---|
77 | x=float(fields[x_index]) |
---|
78 | y=float(fields[x_index + 1]) |
---|
79 | z=float(fields[x_index + 2]) # Elevation |
---|
80 | n=float(fields[x_index + 3]) # Mannings roughness |
---|
81 | outfid1.write('%.3f, %.3f\n' % (x, y)) |
---|
82 | |
---|
83 | elif reading_vertices is True: # Read Elements |
---|
84 | fields = line.split() |
---|
85 | |
---|
86 | # Create indices and make them start from 0 |
---|
87 | pt1 = int(fields[3]) - 1 |
---|
88 | pt2 = int(fields[4]) - 1 |
---|
89 | pt3 = int(fields[5]) - 1 |
---|
90 | outfid2.write('%d, %d, %d\n' % (pt1, pt2,pt3)) |
---|
91 | |
---|
92 | |
---|
93 | outfid1.close() |
---|
94 | outfid2.close() |
---|
95 | print '\nConversion complete.' |
---|
96 | raw_input('Press ENTER to exit.') |
---|