1 | """ |
---|
2 | Function to read in sts file and convert to csv file for analysis purposes. |
---|
3 | Adapted from build_urs_boundary.py from the tsunami modelling space. |
---|
4 | May 2010. |
---|
5 | """ |
---|
6 | |
---|
7 | import os |
---|
8 | import os.path |
---|
9 | from time import localtime, strftime, gmtime |
---|
10 | |
---|
11 | import project |
---|
12 | |
---|
13 | from Scientific.IO.NetCDF import NetCDFFile |
---|
14 | #import Numeric as num |
---|
15 | |
---|
16 | home = os.path.join(os.getenv(ENV_INUNDATIONHOME), 'data') # Absolute path for data folder |
---|
17 | anuga_folder = os.path.join(home, state, scenario_folder, 'anuga') |
---|
18 | boundaries_folder = os.path.join(anuga_folder, 'boundaries') |
---|
19 | event_folder = os.path.join(boundaries_folder, 'gcom_60min') |
---|
20 | |
---|
21 | filename = join(event_folder,'gcom_60_min_boundary.sts') |
---|
22 | |
---|
23 | #------------------------------------------------------------------------------- |
---|
24 | # Get gauges (timeseries of index points) |
---|
25 | #------------------------------------------------------------------------------- |
---|
26 | def 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 |
---|