1 | from anuga.utilities.numerical_tools import ensure_numeric |
---|
2 | from Scientific.IO.NetCDF import NetCDFFile |
---|
3 | from Numeric import asarray,transpose,sqrt,argmax,argmin,arange,Float,\ |
---|
4 | compress,zeros,fabs |
---|
5 | from anuga.utilities.polygon import inside_polygon,read_polygon |
---|
6 | from os import sep |
---|
7 | from time import localtime, strftime, gmtime |
---|
8 | def 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 |
---|
57 | from anuga.shallow_water.data_manager import urs2sts |
---|
58 | base_name='polyline' |
---|
59 | import os |
---|
60 | if os.path.exists(base_name+'.sts'): |
---|
61 | print 'sts boundary file already created.' |
---|
62 | else: |
---|
63 | urs2sts(base_name,mean_stage=0.0,verbose=False) |
---|
64 | boundary=create_sts_boundary(base_name,verbose=True) |
---|
65 | urs_boundary=boundary.tolist() |
---|
66 | |
---|
67 | #Read in large domain boundary |
---|
68 | dir='mesh_polygons' |
---|
69 | extent = 'extent.csv' |
---|
70 | bp = read_polygon(dir+sep+extent) |
---|
71 | |
---|
72 | indices=inside_polygon(urs_boundary, bp, closed=True, verbose=False) |
---|
73 | bounding_polygon=[] |
---|
74 | for i in indices[:-2]: |
---|
75 | #disregard last two entries because they are not on boundary |
---|
76 | bounding_polygon.append(urs_boundary[i]) |
---|
77 | |
---|
78 | bounding_polygon.extend(bp[2:len(bp)]) |
---|
79 | |
---|
80 | ############################### |
---|
81 | # Domain definitions |
---|
82 | ############################### |
---|
83 | |
---|
84 | # Bathymetry and topography filenames |
---|
85 | dir='data' |
---|
86 | |
---|
87 | time = strftime('%Y%m%d_%H%M%S',localtime()) #gets time for new dir |
---|
88 | poor_combined_dir_name = dir+sep+'poor_phuket_bathymetry' |
---|
89 | good_combined_dir_name = dir+sep+'good_phuket_bathymetry' |
---|
90 | meshname = 'phuket_mesh' |
---|
91 | mesh_elevname = 'phuket_mesh_elev' |
---|
92 | boundary_most_in = dir+sep+'out' |
---|
93 | boundary_most_out = dir+sep+'most_boundary_condition' |
---|
94 | |
---|
95 | dir='mesh_polygons' |
---|
96 | extent = 'extent.csv' |
---|
97 | patong_bay_polygon = read_polygon(dir+sep+'patong_bay_polygon.csv') |
---|
98 | |
---|
99 | ############################### |
---|
100 | # Interior region definitions |
---|
101 | ############################### |
---|
102 | |
---|
103 | contour20m = read_polygon(dir+sep+'20m.csv') |
---|
104 | contour20m[0][1]+=3000 |
---|
105 | contour20m[-2][0]+=3000 |
---|
106 | contour20m[-2][1]-=13000 |
---|
107 | contour20m[-1][1]+=9000 |
---|
108 | patong = read_polygon(dir+sep+'patong.csv') |
---|
109 | bay = read_polygon(dir+sep+'bay.csv') |
---|
110 | |
---|
111 | plot=False |
---|
112 | if 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 | |
---|
123 | east = 97.7 |
---|
124 | west = 96.7 |
---|
125 | north = 9.0 |
---|
126 | south = 5.9 |
---|
127 | |
---|
128 | check_ferret_extent=False |
---|
129 | if 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 | |
---|