source: anuga_work/development/boxingday08/project.py @ 5402

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

Project file and get_gauges to support boxing day validation scripts committed in changeset:5401

File size: 5.6 KB
Line 
1from anuga.utilities.numerical_tools import ensure_numeric
2from Scientific.IO.NetCDF import NetCDFFile
3from Numeric import asarray,transpose,sqrt,argmax,argmin,arange,Float,\
4     compress,zeros,fabs
5from anuga.utilities.polygon import inside_polygon,read_polygon
6from os import sep
7from time import localtime, strftime, gmtime
8def create_sts_boundary(filename,verbose=False):
9    fid = NetCDFFile(filename+'.sts', 'r')    #Open existing file for read
10    x = fid.variables['x'][:]+fid.xllcorner   #x-coordinates of vertices
11    y = fid.variables['y'][:]+fid.yllcorner   #y-coordinates of vertices
12    coordinates=transpose(asarray([x.tolist(),y.tolist()]))
13   
14    #find indices of the southernmost gauges
15    #if more than one find gauges furthest left
16    #will fail if domain crosses 180 degrees
17    #i.e. if left most most point is not west i.e x=1 and other points 170<x<180
18    #or even in utm just a diffenet zone
19    south=min(y)
20    south_indices=compress(y-south<1e-6,arange(len(y)))
21    west=x[south_indices[0]]
22    west_index=south_indices[0]
23    if south_indices>1:
24         #find most western gauge
25         for i,south_index in enumerate(south_indices[1:]):
26              if x[south_index]<west:
27                   west_index=south_index
28   
29    n=len(x)
30    boundary=zeros((n,2),Float)
31    indices=arange(n).tolist()
32    boundary[0][0]=x[west_index]
33    boundary[0][1]=y[west_index]
34    indices.remove(west_index)
35    for i in range(n-1):
36         min_dist=1e12
37         for j,k in enumerate(indices):
38              dist=sqrt((boundary[i][0]-x[k])**2+(boundary[i][1]-y[k])**2)
39              if dist<min_dist:
40                   min_dist=dist
41                   index=k
42              elif fabs(dist-min_dist)<1e-6:
43                   msg='Two or more points are equal distance apart. Please resolve'
44                   raise msg
45         indices.remove(index)
46         boundary[i+1][0]=x[index]
47         boundary[i+1][1]=y[index]
48    #ensure boundary is a polygon i.e no intesections
49    #e.g.
50    #msg='Boundary is not a regular polygon. Intersections found'
51    #assert boundary.is_polygon, msg
52             
53    #So far i cannot tell if the wrong point is closer.
54    return boundary
55
56#read in polyline boundary
57from anuga.shallow_water.data_manager import urs2sts
58base_name='polyline'
59import os
60if os.path.exists(base_name+'.sts'):
61        print 'sts boundary file already created.'
62else:
63    urs2sts(base_name,mean_stage=0.0,verbose=False)
64boundary=create_sts_boundary(base_name,verbose=True)
65urs_boundary=boundary.tolist()
66
67#Read in large domain boundary
68dir='mesh_polygons'
69extent = 'extent.csv'
70bp = read_polygon(dir+sep+extent)
71
72indices=inside_polygon(urs_boundary, bp, closed=True, verbose=False)
73bounding_polygon=[]
74for i in indices[:-2]:
75    #disregard last two entries because they are not on boundary
76    bounding_polygon.append(urs_boundary[i])
77
78bounding_polygon.extend(bp[2:len(bp)])
79
80###############################
81# Domain definitions
82###############################
83
84# Bathymetry and topography filenames
85dir='data'
86
87time = strftime('%Y%m%d_%H%M%S',localtime()) #gets time for new dir
88poor_combined_dir_name = dir+sep+'poor_phuket_bathymetry'
89good_combined_dir_name = dir+sep+'good_phuket_bathymetry'
90meshname = 'phuket_mesh'
91mesh_elevname = 'phuket_mesh_elev'
92boundary_most_in = dir+sep+'out'
93boundary_most_out = dir+sep+'most_boundary_condition'
94
95dir='mesh_polygons'
96extent = 'extent.csv'
97patong_bay_polygon = read_polygon(dir+sep+'patong_bay_polygon.csv')
98
99###############################
100# Interior region definitions
101###############################
102
103contour20m = read_polygon(dir+sep+'20m.csv')
104contour20m[0][1]+=3000
105contour20m[-2][0]+=3000
106contour20m[-2][1]-=13000
107contour20m[-1][1]+=9000
108patong = read_polygon(dir+sep+'patong.csv')
109bay = read_polygon(dir+sep+'bay.csv')
110
111plot=False
112if plot:
113    from pylab import plot,show,hold
114    bounding_polygon=ensure_numeric(bounding_polygon)#for plotting only
115    plot(bounding_polygon[:,0],bounding_polygon[:,1],'o')
116    plot(bounding_polygon[:,0],bounding_polygon[:,1])
117    contour20m = ensure_numeric(contour20m)
118    plot(contour20m[:,0],contour20m[:,1])
119    patong=ensure_numeric(patong)
120    plot(patong[:,0],patong[:,1])
121    show()
122
123east  = 97.7
124west  = 96.7
125north = 9.0
126south = 5.9
127
128check_ferret_extent=False
129if check_ferret_extent:
130    east  = east
131    west  = west
132    north = north
133    south = south
134   
135    from anuga.coordinate_transforms.redfearn import redfearn
136    zone0, easting0, northing0 = redfearn(north,east)
137    print zone0,easting0,northing0
138    zone1, easting1, northing1 = redfearn(south,east)
139    print zone1,easting1,northing1
140    zone2, easting2, northing2 = redfearn(south,west)
141    print zone2,easting2,northing2
142    zone3, easting3, northing3 = redfearn(north,west)
143    print zone3,easting3,northing3
144   
145    assert zone0==zone1==zone2==zone3
146   
147    ferret_polygon=[[easting0, northing0],[easting1, northing1],[easting2, northing2],[easting3, northing3]]
148    points_outside=[]
149    from anuga.utilities.polygon import is_inside_polygon
150    for i,point in enumerate(bounding_polygon):
151        if not is_inside_polygon(point, ferret_polygon, closed=True, verbose=False):
152            print 'point %d = (%f,%f) not inside boundary'%(i,point[0],point[1])
153            points_outside.append(point)
154
155    from pylab import plot,show
156    ferret_polygon=ensure_numeric(ferret_polygon)
157    points_outside=ensure_numeric(points_outside)
158    plot(ferret_polygon[:,0],ferret_polygon[:,1])
159    plot(points_outside[:,0],points_outside[:,1],'o')
160    show()
161    print ferret_polygon[0]
162    print ferret_polygon[1]
163    print ferret_polygon[2]
164    print ferret_polygon[3]
165    print points_outside
166   
Note: See TracBrowser for help on using the repository browser.