source: branches/anuga_1_1/anuga_work/production/bunbury_storm_surge_2009/sts2csv.py @ 7729

Last change on this file since 7729 was 7729, checked in by sexton, 14 years ago

function to read in sts file and convert to csv file for analysis purposes

File size: 2.7 KB
Line 
1"""
2Function to read in sts file and convert to csv file for analysis purposes.
3Adapted from build_urs_boundary.py from the tsunami modelling space.
4May 2010.
5"""
6
7import os
8import os.path
9from time import localtime, strftime, gmtime
10
11import project
12
13from Scientific.IO.NetCDF import NetCDFFile
14#import Numeric as num
15
16home = os.path.join(os.getenv(ENV_INUNDATIONHOME), 'data') # Absolute path for data folder
17anuga_folder = os.path.join(home, state, scenario_folder, 'anuga')
18boundaries_folder = os.path.join(anuga_folder, 'boundaries')
19event_folder = os.path.join(boundaries_folder, 'gcom_60min')
20
21filename = join(event_folder,'gcom_60_min_boundary.sts')
22
23#-------------------------------------------------------------------------------
24# Get gauges (timeseries of index points)
25#-------------------------------------------------------------------------------
26def get_sts_gauge_data(filename, verbose=False):
27    print 'get_sts_gauge_data: filename=%s' % filename
28    fid = NetCDFFile(filename+'.sts', 'r')      #Open existing file for read
29    #permutation = fid.variables['permutation'][:]
30    x = fid.variables['x'][:] + fid.xllcorner   #x-coordinates of vertices
31    y = fid.variables['y'][:] + fid.yllcorner   #y-coordinates of vertices
32    #points = num.transpose(num.asarray([x.tolist(), y.tolist()]))
33    time = fid.variables['time'][:] #+ fid.starttime
34    elevation = fid.variables['elevation'][:]
35       
36    basename = 'sts_gauge'
37    quantity_names = ['stage', 'xmomentum', 'ymomentum']
38    quantities = {}
39    for i, name in enumerate(quantity_names):
40        quantities[name] = fid.variables[name][:]
41
42    #---------------------------------------------------------------------------
43    # Get timeseries values for wave height and components of momentum
44    #---------------------------------------------------------------------------
45
46    for j in range(len(x)):
47        index = j # permutation[j]
48        stage = quantities['stage'][:,j]
49        xmomentum = quantities['xmomentum'][:,j]
50        ymomentum = quantities['ymomentum'][:,j]
51
52        out_file = os.path.join(project.event_folder,
53                                basename+'_'+str(index)+'.csv')
54        fid_sts = open(out_file, 'w')
55        fid_sts.write('time, stage, xmomentum, ymomentum \n')
56
57        #-----------------------------------------------------------------------
58        # End of the get gauges
59        #-----------------------------------------------------------------------
60        for k in range(len(time)-1):
61            fid_sts.write('%.6f, %.6f, %.6f, %.6f\n'
62                          % (time[k], stage[k], xmomentum[k], ymomentum[k]))
63
64        fid_sts.close()     
65
66    fid.close()
67
68    return quantities,elevation,time
Note: See TracBrowser for help on using the repository browser.