Changeset 1854
 Timestamp:
 Sep 28, 2005, 10:29:52 AM (17 years ago)
 Location:
 inundation/pyvolution
 Files:

 3 edited
Legend:
 Unmodified
 Added
 Removed

inundation/pyvolution/data_manager.py
r1835 r1854 221 221 def __init__(self, domain, mode = 'w',\ 222 222 max_size = 2000000000, 223 recursion =False):223 recursion = False): 224 224 from Scientific.IO.NetCDF import NetCDFFile 225 225 from Numeric import Int, Float, Float32 … … 1631 1631 from Scientific.IO.NetCDF import NetCDFFile 1632 1632 from Numeric import Float, Int, Int32, searchsorted, zeros, array 1633 from Numeric import allclose, around 1634 1633 1635 precision = Float 1634 1636 … … 1651 1653 1652 1654 1653 e_lat = file_e.variables['LAT'][:] 1654 1655 1656 #Precision used by most for lat/lon is 4 or 5 decimals 1657 e_lat = around(file_e.variables['LAT'][:], 5) 1658 e_lon = around(file_e.variables['LON'][:], 5) 1655 1659 1656 1660 #Check that files are compatible 1657 from Numeric import allclose1658 1661 assert allclose(latitudes, file_u.variables['LAT']) 1659 assert allclose(latitudes, file_v.variables['LAT']) 1660 assert allclose(latitudes, file_e.variables['LAT'])1662 assert allclose(latitudes, file_v.variables['LAT']) 1663 assert allclose(latitudes, e_lat) 1661 1664 1662 1665 assert allclose(longitudes, file_u.variables['LON']) 1663 1666 assert allclose(longitudes, file_v.variables['LON']) 1664 assert allclose(longitudes, file_e.variables['LON'])1667 assert allclose(longitudes, e_lon) 1665 1668 1666 1669 
inundation/pyvolution/test_util.py
r1835 r1854 1112 1112 1113 1113 1114 def test_populate_polygon_with_exclude(self): 1115 1116 1117 polygon = [[0,0], [1,0], [1,1], [0,1]] 1118 ex_poly = [[0,0], [0.5,0], [0.5, 0.5], [0,0.5]] #SW quarter 1119 points = populate_polygon(polygon, 5, exclude = [ex_poly]) 1120 1121 assert len(points) == 5 1122 for point in points: 1123 assert inside_polygon(point, polygon) 1124 assert not inside_polygon(point, ex_poly) 1125 1126 1127 #overlap 1128 polygon = [[0,0], [1,0], [1,1], [0,1]] 1129 ex_poly = [[1,1], [0.5,0], [0.5, 0.5], [1,0.5]] 1130 points = populate_polygon(polygon, 5, exclude = [ex_poly]) 1131 1132 assert len(points) == 5 1133 for point in points: 1134 assert inside_polygon(point, polygon) 1135 assert not inside_polygon(point, ex_poly) 1136 1137 #Multiple 1138 polygon = [[0,0], [1,0], [1,1], [0,1]] 1139 ex_poly1 = [[0,0], [0.5,0], [0.5, 0.5], [0,0.5]] #SW quarter 1140 ex_poly2 = [[0.5,0.5], [0.5,1], [1, 1], [1,0.5]] #NE quarter 1141 1142 points = populate_polygon(polygon, 20, exclude = [ex_poly1, ex_poly2]) 1143 1144 assert len(points) == 20 1145 for point in points: 1146 assert inside_polygon(point, polygon) 1147 assert not inside_polygon(point, ex_poly1) 1148 assert not inside_polygon(point, ex_poly2) 1149 1150 1151 #Very convoluted polygon 1152 polygon = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,10]] 1153 ex_poly = [[1,1], [5,0], [5, 5], [1,5]] 1154 points = populate_polygon(polygon, 20, exclude = [ex_poly]) 1155 1156 assert len(points) == 20 1157 for point in points: 1158 assert inside_polygon(point, polygon) 1159 assert not inside_polygon(point, ex_poly), '%s' %str(point) 1160 1161 1114 1162 1115 1163 # 
inundation/pyvolution/util.py
r1835 r1854 777 777 return polygon 778 778 779 def populate_polygon(polygon, number_of_points, seed = None ):779 def populate_polygon(polygon, number_of_points, seed = None, exclude = None): 780 780 """Populate given polygon with uniformly distributed points. 781 781 … … 784 784 number_of_points  (optional) number of points 785 785 seed  seed for random number generator (default=None) 786 exclude  list of polygons (inside main polygon) from where points should be excluded 786 787 787 788 Output: … … 815 816 y = uniform(min_y, max_y) 816 817 818 append = False 817 819 if inside_polygon( [x,y], polygon ): 818 points.append([x,y]) 820 821 append = True 822 823 #Check exclusions 824 if exclude is not None: 825 for ex_poly in exclude: 826 if inside_polygon( [x,y], ex_poly ): 827 append = False 828 829 830 if append is True: 831 points.append([x,y]) 819 832 820 833 return points 834 835 821 836 822 837 def tsh2sww(filename, verbose=True):
Note: See TracChangeset
for help on using the changeset viewer.