source: anuga_work/development/Rudy_vandrie_2007/create_hydro_example.py @ 6409

Last change on this file since 6409 was 4440, checked in by ole, 18 years ago

Examples prepared for Rudy

File size: 3.4 KB
Line 
1"""Create mesh and time boundary for Hydrograph example
2"""
3
4
5from Numeric import array, zeros, Float, allclose
6
7from anuga.pmesh.mesh import *
8from anuga.pmesh.mesh_interface import create_mesh_from_regions
9from anuga.coordinate_transforms.geo_reference import Geo_reference
10from anuga.geospatial_data import Geospatial_data
11
12import project
13
14
15def prepare_hydrograph(filename, hydrograph, field):
16    """Convert .out file to NetCDF tms file.
17    This is a 'throw-away' code taylor made for this type of file
18
19    hydrograph is an identifier e.g.
20    HYDROGRAPHS_SUB17       
21
22    field is one of the column headers.
23
24    Time will always be read and converted into seconds.
25
26    The file has contents like
27
28    #####START_HYDROGRAPHS_SUB17       
29     Time    Rain  Rainperv    Qtop      Qbot      Qper      Qimp    Qinto_OS   Qout_OS  Stage
30      0.0    0.00    0.00     0.000     0.000     0.000     0.000     0.000     0.000     0.000
31      5.0    2.65    0.00     0.000     0.000     0.000     0.004     0.004     0.004     0.000
32     10.0    2.65    0.00     0.000     0.000     0.000     0.004     0.004     0.004     0.000
33     15.0    2.65    0.00     0.000     0.000     0.000     0.004     0.004     0.004     0.000
34
35   
36    """
37
38    from Scientific.IO.NetCDF import NetCDFFile
39    from Numeric import array
40
41    assert filename[-4:] == '.tms'
42   
43    outfilename = filename
44    infilename = filename[:-4] + '.out'
45
46    print 'Creating', outfilename
47
48    # Read the ascii (.txt) version of this file
49    fid = open(infilename)
50
51    # Read all lines and search for selected hydrograph
52    lines = fid.readlines()
53    fid.close()
54
55    found = False
56    search_string = 'START_'+hydrograph
57    print 'looking for', search_string
58    for i, line in enumerate(lines):
59        if line.startswith('####'):
60            if line.find(search_string) >= 0:
61                print 'Found', line
62                found = True
63                break
64
65    if found is False:
66        msg = 'Did not find', hydrograph
67        raise Exception, msg
68
69    # Select data column
70    headers = lines[i+1]
71    fields = headers.split()
72    index = fields.index(field)
73    print 'Found header #%d: "%s"' %(index, fields[index])
74
75
76    # Read data
77    search_string = 'END_'+hydrograph
78    time = []
79    data = []   
80    for line in lines[i+2:]:
81        if line.startswith('####'):
82            if line.find(search_string) >= 0:
83                break       
84       
85        fields = line.split()
86        time.append(float(fields[0]))
87        data.append(float(fields[index]))
88
89
90    # Convert to NetCDF
91    N = len(time)
92    T = array(time, Float)*60  # Time (seconds)
93    Q = array(data, Float)     # Values (m^3/s)
94
95    # At this point, the hydrograph is stored in arrays T (time) and Q
96   
97
98    # Create tms NetCDF file
99    fid = NetCDFFile(outfilename, 'w')
100    fid.institution = 'Geoscience Australia'
101    fid.description = 'Hydrograph example'
102    fid.starttime = 0.0
103    fid.createDimension('number_of_timesteps', len(T))
104    fid.createVariable('time', Float, ('number_of_timesteps',))
105    fid.variables['time'][:] = T
106
107    fid.createVariable('hydrograph', Float, ('number_of_timesteps',))
108    fid.variables['hydrograph'][:] = Q
109
110    fid.close()
111
112
113#-------------------------------------------------------------
114if __name__ == "__main__":
115
116
117    # Prepare hydrograph
118    prepare_hydrograph(filename=project.boundary_filename,
119                       hydrograph='HYDROGRAPHS_SUB1',
120                       field='Qinto_OS')
121
122
Note: See TracBrowser for help on using the repository browser.