source: anuga_work/development/river2d_integration/River2Dcdg_ANUGAMesh.py @ 6367

Last change on this file since 6367 was 6097, checked in by ole, 16 years ago

Work on River2D integration

File size: 2.6 KB
RevLine 
[6083]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 
9and converts it to a format where it can be used to creat a domain from Points and Vertices.
10
11All you need to do is specify the data file name and output file name
12
13
14"""
15import string
16
17infid = open('Riv2dMesh2.cdg') # input file is a River 2D run file
18outfid1 = open('ANUGA_MeshPts.csv', 'w') # output filename
19outfid2 = open('ANUGA_MeshVertices.csv', 'w') # output filename
20print "\nFILES are open ready for conversion....\n"
21
[6085]22lines = infid.readlines()
[6083]23
[6085]24# Ignore all metadata prior to node information
25for i, line in enumerate(lines):
26    if line.strip().startswith('Node #'):
27        # Found node information
28        break
29
30
[6083]31reading_points = True
32reading_vertices = False
33last_point_index = 0
34
[6085]35for L in lines[i+1:]:
36    line = L.strip()
[6083]37
[6085]38    print line
39   
40    if line == '':
[6083]41        continue
42
[6085]43    if line.startswith('Element Information'):
[6083]44        continue
[6085]45       
46    if line.startswith('Element #'):
[6083]47        reading_vertices = True
48        reading_points = False
49        continue
50
[6085]51    if line.startswith('Boundary Element #'):
52        print 'Reached boundary info, not processing yet'
[6083]53        break
54
55    if reading_points is True:   # Read Points
[6085]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           
[6083]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
[6085]77        x=float(fields[x_index])
78        y=float(fields[x_index + 1])
[6097]79        z=float(fields[x_index + 2]) # Elevation
80        n=float(fields[x_index + 3]) # Mannings roughness
[6083]81        outfid1.write('%.3f, %.3f\n' % (x, y))
82
[6097]83    elif reading_vertices is True:    # Read Elements
[6085]84        fields = line.split()
[6083]85
86        # Create indices and make them start from 0
[6097]87        pt1 = int(fields[3]) - 1
88        pt2 = int(fields[4]) - 1
89        pt3 = int(fields[5]) - 1
[6083]90        outfid2.write('%d, %d, %d\n' % (pt1, pt2,pt3)) 
91
92   
93outfid1.close()
94outfid2.close()
[6097]95print '\nConversion complete.'
96raw_input('Press ENTER to exit.')
Note: See TracBrowser for help on using the repository browser.