[5576] | 1 | import os |
---|
| 2 | from os import sep |
---|
| 3 | import project |
---|
| 4 | try: |
---|
| 5 | from pylab import plot,show,xlabel,ylabel,legend,title,savefig,hold |
---|
| 6 | except: |
---|
| 7 | print 'Cannot import pylab plotting will not work. Csv files are still created' |
---|
| 8 | from anuga.utilities.polygon import inside_polygon |
---|
| 9 | from anuga.utilities.numerical_tools import ensure_numeric |
---|
| 10 | from Scientific.IO.NetCDF import NetCDFFile |
---|
| 11 | |
---|
[5607] | 12 | def get_sts_gauge_data(filename,verbose=False): |
---|
[5576] | 13 | from Numeric import asarray,transpose,sqrt,argmax,argmin,arange,Float,\ |
---|
| 14 | compress,zeros,fabs,take |
---|
| 15 | fid = NetCDFFile(filename+'.sts', 'r') #Open existing file for read |
---|
| 16 | x = fid.variables['x'][:]+fid.xllcorner #x-coordinates of vertices |
---|
| 17 | y = fid.variables['y'][:]+fid.yllcorner #y-coordinates of vertices |
---|
| 18 | points=transpose(asarray([x.tolist(),y.tolist()])) |
---|
[5607] | 19 | time=fid.variables['time'][:]+fid.starttime |
---|
[5576] | 20 | elevation=fid.variables['elevation'][:] |
---|
[5607] | 21 | |
---|
[5580] | 22 | basename='sts_gauge' |
---|
[5576] | 23 | quantity_names=['stage','xmomentum','ymomentum'] |
---|
| 24 | quantities = {} |
---|
| 25 | for i, name in enumerate(quantity_names): |
---|
| 26 | quantities[name] = fid.variables[name][:] |
---|
[5607] | 27 | |
---|
| 28 | maxname = 'max_sts_stage.csv' |
---|
| 29 | fid_max = open(project.boundaries_dir+sep+maxname,'w') |
---|
| 30 | s = 'x, y, max_stage \n' |
---|
| 31 | fid_max.write(s) |
---|
| 32 | for j in range(len(x)): |
---|
[5581] | 33 | locx=int(x[j]) |
---|
| 34 | locy=int(y[j]) |
---|
[5607] | 35 | stage = quantities['stage'][:,j] |
---|
| 36 | xmomentum = quantities['xmomentum'][:,j] |
---|
| 37 | ymomentum = quantities['ymomentum'][:,j] |
---|
| 38 | |
---|
| 39 | s = '%.6f, %.6f, %.6f\n' %(x[j], y[j], max(stage)) |
---|
| 40 | fid_max.write(s) |
---|
| 41 | |
---|
[5581] | 42 | fid_sts = open(project.boundaries_dir+sep+basename+'_'+str(locx)+'_'+str(locy)+'.csv', 'w') |
---|
| 43 | s = 'time, stage, xmomentum, ymomentum \n' |
---|
| 44 | fid_sts.write(s) |
---|
[5607] | 45 | |
---|
[5581] | 46 | for k in range(len(time)-1): |
---|
[5607] | 47 | s = '%.6f, %.6f, %.6f, %.6f\n' %(time[k], stage[k], xmomentum[k], ymomentum[k]) |
---|
[5581] | 48 | fid_sts.write(s) |
---|
| 49 | |
---|
| 50 | fid_sts.close() |
---|
| 51 | |
---|
[5576] | 52 | fid.close() |
---|
[5581] | 53 | |
---|
[5607] | 54 | return quantities,elevation,time |
---|
[5576] | 55 | |
---|
[5607] | 56 | quantities,elevation,time=get_sts_gauge_data(os.path.join(project.boundaries_dir,project.scenario_name),verbose=False) |
---|
| 57 | |
---|
| 58 | print len(elevation), len(quantities['stage'][0,:]) |
---|