Changeset 1137 for inundation/ga
- Timestamp:
- Mar 23, 2005, 6:06:51 PM (20 years ago)
- Location:
- inundation/ga/storm_surge
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/data_manager.py
r1133 r1137 16 16 17 17 .asc: ASCII foramt of regular DEMs as output from ArcView 18 .prj: Associated ArcView file giving more meta data for asc format 19 18 20 .dem: NetCDF representation of regular DEM data 19 21 20 22 .tsh: ASCII format for storing meshes and associated boundary and region info 23 .msh: NetCDF format for storing meshes and associated boundary and region info 21 24 22 25 .nc: Native ferret NetCDF format … … 40 43 TSH -> SWW: Conversion of TSH to sww viewable using Swollen 41 44 42 TSH + Boundary SWW -> SWW: S Imluation using pyvolution45 TSH + Boundary SWW -> SWW: Simluation using pyvolution 43 46 44 47 … … 209 212 210 213 def __init__(self, domain, mode = 'w',\ 211 max_size = 2000000000,recursion=False): 214 max_size = 2000000000, 215 recursion=False): 212 216 from Scientific.IO.NetCDF import NetCDFFile 213 217 from Numeric import Int, Float, Float32 … … 978 982 outfile.yllcorner = yllcorner #Northing of lower left corner 979 983 outfile.false_easting = false_easting 980 outfile.false_northing = false_northing984 outfile.false_northing = false_northing 981 985 982 986 outfile.projection = projection … … 1019 1023 outfile.close() 1020 1024 1025 1026 def sww2asc(basename_in, basename_out = None, 1027 quantity = None, 1028 timestep = None, 1029 reduction = None, 1030 cellsize = 10, 1031 verbose = False, 1032 origin = None): 1033 """Read SWW file and convert to Digitial Elevation model format (.asc) 1034 1035 Example: 1036 1037 ncols 3121 1038 nrows 1800 1039 xllcorner 722000 1040 yllcorner 5893000 1041 cellsize 25 1042 NODATA_value -9999 1043 138.3698 137.4194 136.5062 135.5558 .......... 1044 1045 Also write accompanying file with same basename_in but extension .prj 1046 used to fix the UTM zone, datum, false northings and eastings. 1047 1048 The prj format is assumed to be as 1049 1050 Projection UTM 1051 Zone 56 1052 Datum WGS84 1053 Zunits NO 1054 Units METERS 1055 Spheroid WGS84 1056 Xshift 0.0000000000 1057 Yshift 10000000.0000000000 1058 Parameters 1059 1060 1061 if quantity is given, out values from quantity otherwise default to 1062 elevation 1063 1064 if timestep (an index) is given, output quantity at that timestep 1065 1066 if reduction is given use that to reduce quantity over all timesteps. 1067 1068 """ 1069 from Numeric import array, Float 1070 1071 #FIXME: Should be variable 1072 datum = 'WGS84' 1073 false_easting = 500000 1074 false_northing = 10000000 1075 1076 if quantity is None: 1077 quantity = 'elevation' 1078 1079 if reduction is None: 1080 reduction = max 1081 1082 if basename_out is None: 1083 basename_out = basename_in 1084 1085 swwfile = basename_in + '.sww' 1086 ascfile = basename_out + '.asc' 1087 prjfile = basename_out + '.prj' 1088 1089 1090 if verbose: print 'Reading from %s' %swwfile 1091 #Read sww file 1092 from Scientific.IO.NetCDF import NetCDFFile 1093 fid = NetCDFFile(swwfile) 1094 1095 #Get extent and reference 1096 x = fid.variables['x'][:] 1097 y = fid.variables['y'][:] 1098 1099 ymin = min(y); ymax = max(y) 1100 xmin = min(x); xmax = max(x) 1101 1102 number_of_timesteps = fid.dimensions['number_of_timesteps'] 1103 number_of_points = fid.dimensions['number_of_points'] 1104 if origin is None: 1105 xllcorner = fid.xllcorner[0] 1106 yllcorner = fid.yllcorner[0] 1107 zone = fid.zone[0] 1108 else: 1109 zone = origin[0] 1110 xllcorner = origin[1] 1111 yllcorner = origin[2] 1112 1113 1114 #Get quantity and reduce if applicable 1115 q = fid.variables[quantity] 1116 1117 if len(q.shape) == 2: 1118 q_reduced = array( number_of_points, Float ) 1119 1120 for k in range(number_of_points): 1121 q_reduced[k] = reduction( q[:,k] ) 1122 1123 q = q_reduced 1124 1125 #Now q has dimension: number_of_points 1126 1127 #Write prj file 1128 prjid = open(prjfile, 'w') 1129 prjid.write('Projection %s\n' %'UTM') 1130 prjid.write('Zone %d\n' %zone) 1131 prjid.write('Datum %s\n' %datum) 1132 prjid.write('Zunits NO\n') 1133 prjid.write('Units METERS\n') 1134 prjid.write('Spheroid %s\n' %datum) 1135 prjid.write('Xshift %f\n' %false_easting) 1136 prjid.write('Yshift %f\n' %false_northing) 1137 prjid.write('Parameters\n') 1138 prjid.close() 1139 1140 #Create grid 1141 ncols = int((xmax-xmin)/cellsize)+1 1142 nrows = int((ymax-ymin)/cellsize)+1 1143 1144 from Numeric import zeros, Float 1145 points = zeros ( (ncols*nrows, 2), Float ) 1146 1147 1148 for i in xrange(ncols): 1149 xg = i*cellsize 1150 for j in xrange(nrows): 1151 yg = j*cellsize 1152 k = i*nrows + j 1153 1154 #print k, xg, yg 1155 points[k,0] = xg 1156 points[k,1] = yg 1157 1158 #Interpolate 1159 from least_squares import Interpolation 1160 1161 1162 1163 1164 #Close 1165 fid.close() 1166 1021 1167 1022 1168 def convert_dem_from_ascii2netcdf(basename_in, basename_out = None, -
inundation/ga/storm_surge/pyvolution/interpolate_sww.py
r934 r1137 26 26 27 27 class Interpolate_sww: 28 def __init__(self, file_name, quantity_name):28 def __init__(self, file_name, quantity_name): 29 29 30 30 #It's bad to have the quantity_name passed in here. … … 62 62 self.quantity = quantity 63 63 64 def interpolate_xya(self, file_name):64 def interpolate_xya(self, file_name): 65 65 """ 66 66 Given a point file, interpolate the height w.r.t. time at the points -
inundation/ga/storm_surge/pyvolution/least_squares.py
r1104 r1137 99 99 old_title_list = mesh_dict['vertex_attribute_titles'] 100 100 101 if verbose: print "tsh file loaded"101 if verbose: print 'tsh file %s loaded' %mesh_file 102 102 103 103 # load in the .pts file … … 302 302 303 303 """ 304 304 from util import ensure_numeric 305 305 306 #Convert input to Numeric arrays 306 triangles = array(triangles).astype(Int)307 vertex_coordinates = array(vertex_coordinates).astype(Float)307 triangles = ensure_numeric(triangles, Int) 308 vertex_coordinates = ensure_numeric(vertex_coordinates, Float) 308 309 309 310 #Build underlying mesh 310 311 if verbose: print 'Building mesh' 311 312 #self.mesh = General_mesh(vertex_coordinates, triangles, 312 #FIXME: Trying the normal mesh while testing precrop 313 #FIXME: Trying the normal mesh while testing precrop, 314 # The functionality of boundary_polygon is needed for that 313 315 self.mesh = Mesh(vertex_coordinates, triangles, 314 316 origin = mesh_origin) … … 387 389 388 390 from quad import build_quadtree 391 from util import ensure_numeric 389 392 390 393 if data_origin is None: … … 393 396 394 397 #Convert input to Numeric arrays just in case. 395 point_coordinates = array(point_coordinates).astype(Float)398 point_coordinates = ensure_numeric(point_coordinates, Float) 396 399 397 400 … … 595 598 """ 596 599 597 600 from util import ensure_numeric 598 601 599 602 #Convert input to Numeric arrays 600 point_coordinates = array(point_coordinates).astype(Float)603 point_coordinates = ensure_numeric(point_coordinates, Float) 601 604 602 605 #Build n x m interpolation matrix … … 764 767 z: Single 1d vector or array of data at the point_coordinates. 765 768 """ 766 769 767 770 #Convert input to Numeric arrays 768 z = array(z).astype(Float) 769 771 from util import ensure_numeric 772 z = ensure_numeric(z, Float) 773 770 774 if len(z.shape) > 1 : 771 775 raise VectorShapeError, 'Can only deal with 1d data vector' … … 808 812 809 813 #Convert input to Numeric arrays 810 z = array(z).astype(Float) 814 from util import ensure_numeric 815 z = ensure_numeric(z, Float) 811 816 812 817 #Build n x m interpolation matrix -
inundation/ga/storm_surge/pyvolution/test_data_manager.py
r1134 r1137 365 365 366 366 367 368 367 #Check contents 369 368 #Get NetCDF … … 378 377 stage = fid.variables['stage'] 379 378 380 #Check values381 379 #Check values 382 380 Q = self.domain.quantities['stage'] … … 551 549 fid.close() 552 550 553 #Convert to NetCDF xya551 #Convert to NetCDF pts 554 552 convert_dem_from_ascii2netcdf(root) 555 553 dem2pts(root) … … 582 580 os.remove(root + '.asc') 583 581 os.remove(root + '.prj') 582 583 584 585 def test_sww2asc(self): 586 """Test that sww information can be converted correctly to asc/prj 587 format readable by e.g. ArcView 588 """ 589 590 import time, os 591 from Numeric import array, zeros, allclose, Float, concatenate 592 from Scientific.IO.NetCDF import NetCDFFile 593 594 #Setup 595 self.domain.filename = 'datatest' + str(id(self)) 596 self.domain.set_datadir('.') 597 self.domain.format = 'sww' 598 self.domain.smooth = True 599 self.domain.set_quantity('elevation', lambda x,y: -x-y) 600 601 sww = get_dataobject(self.domain) 602 sww.store_connectivity() 603 sww.store_timestep('stage') 604 605 self.domain.evolve_to_end(finaltime = 0.01) 606 sww.store_timestep('stage') 607 608 609 #Check contents 610 #Get NetCDF 611 612 fid = NetCDFFile(sww.filename, 'r') 613 614 615 # Get the variables 616 x = fid.variables['x'] 617 y = fid.variables['y'] 618 z = fid.variables['elevation'] 619 time = fid.variables['time'] 620 stage = fid.variables['stage'] 621 #print x[:] 622 #print y[:] 623 #print z[:] 624 #print stage[:] 625 626 #Export to ascii/prj files 627 sww2asc(self.domain.filename, 628 quantity = 'elevation', 629 cellsize = 0.25, 630 origin = (56,0,0)) 631 632 633 raise 'Under construction (Ole)' 634 635 #Check values 636 #Q = self.domain.quantities['stage'] 637 #Q0 = Q.vertex_values[:,0] 638 #Q1 = Q.vertex_values[:,1] 639 #Q2 = Q.vertex_values[:,2] 640 # 641 #A = stage[1,:] 642 #assert allclose(A[0], min(Q2[0], Q1[1])) 643 #assert allclose(A[1], min(Q0[1], Q1[3], Q2[2])) 644 #assert allclose(A[2], Q0[3]) 645 #assert allclose(A[3], min(Q0[0], Q1[5], Q2[4])) 646 # 647 ##Center point 648 #assert allclose(A[4], min(Q1[0], Q2[1], Q0[2],\ 649 # Q0[5], Q2[6], Q1[7])) 650 651 652 fid.close() 653 654 #Cleanup 655 os.remove(sww.filename) 584 656 585 657 -
inundation/ga/storm_surge/pyvolution/test_util.py
r1093 r1137 195 195 196 196 197 197 def test_ensure_numeric(self): 198 from util import ensure_numeric 199 from Numeric import ArrayType, Float, array 200 201 A = [1,2,3,4] 202 B = ensure_numeric(A) 203 assert type(B) == ArrayType 204 assert B.typecode() == 'l' 205 assert B[0] == 1 and B[1] == 2 and B[2] == 3 and B[3] == 4 206 207 208 A = [1,2,3.14,4] 209 B = ensure_numeric(A) 210 assert type(B) == ArrayType 211 assert B.typecode() == 'd' 212 assert B[0] == 1 and B[1] == 2 and B[2] == 3.14 and B[3] == 4 213 214 215 A = [1,2,3,4] 216 B = ensure_numeric(A, Float) 217 assert type(B) == ArrayType 218 assert B.typecode() == 'd' 219 assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0 220 221 222 A = [1,2,3,4] 223 B = ensure_numeric(A, Float) 224 assert type(B) == ArrayType 225 assert B.typecode() == 'd' 226 assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0 227 228 229 A = array([1,2,3,4]) 230 B = ensure_numeric(A) 231 assert type(B) == ArrayType 232 assert B.typecode() == 'l' 233 assert A == B 234 assert A is B #Same object 235 236 237 A = array([1,2,3,4]) 238 B = ensure_numeric(A, Float) 239 assert type(B) == ArrayType 240 assert B.typecode() == 'd' 241 assert A == B 242 assert A is not B #Not the same object 243 244 245 246 198 247 199 248 def test_spatio_temporal_file_function_time(self): -
inundation/ga/storm_surge/pyvolution/util.py
r1104 r1137 94 94 return False 95 95 96 def ensure_numeric(A, typecode = None): 97 """Ensure that sequence is a Numeric array. 98 Inputs: 99 A: Sequance. If A is already a Numeric array it will be returned 100 unaltered 101 If not, an attempt is made to convert it to a Numeric 102 array 103 typecode: Numeric type. If specified, use this in the conversion. 104 If not, let Numeric decide 105 106 This function is necessary as array(A) can cause memory overflow. 107 """ 108 109 from Numeric import ArrayType, array 110 111 if typecode is None: 112 if type(A) == ArrayType: 113 return A 114 else: 115 return array(A) 116 else: 117 if type(A) == ArrayType: 118 if A.typecode == typecode: 119 return array(A) 120 else: 121 return A.astype(typecode) 122 else: 123 return array(A).astype(typecode) 124 125 96 126 97 127 def file_function(filename, domain=None, quantities = None, interpolation_points = None, verbose = False): … … 253 283 254 284 try: 255 P = array(interpolation_points)285 P = ensure_numeric(interpolation_points) 256 286 except: 257 287 msg = 'Interpolation points must be an N x 2 Numeric array or a list of points\n' … … 498 528 values.append(float(value)) 499 529 500 q = array(values)530 q = ensure_numeric(values) 501 531 502 532 msg = 'ERROR: File must contain at least one independent value' … … 724 754 values.append(float(value)) 725 755 726 q = array(values)756 q = ensure_numeric(values) 727 757 728 758 msg = 'ERROR: File must contain at least one independent value' … … 932 962 if verbose: print 'Checking input to inside_polygon' 933 963 try: 934 points = array(points).astype(Float)964 points = ensure_numeric(points, Float) 935 965 except: 936 966 msg = 'Points could not be converted to Numeric array' … … 938 968 939 969 try: 940 polygon = array(polygon).astype(Float)970 polygon = ensure_numeric(polygon, Float) 941 971 except: 942 972 msg = 'Polygon could not be converted to Numeric array' … … 967 997 if verbose: print 'Checking input to outside_polygon' 968 998 try: 969 points = array(points).astype(Float)999 points = ensure_numeric(points, Float) 970 1000 except: 971 1001 msg = 'Points could not be converted to Numeric array' … … 973 1003 974 1004 try: 975 polygon = array(polygon).astype(Float)1005 polygon = ensure_numeric(polygon, Float) 976 1006 except: 977 1007 msg = 'Polygon could not be converted to Numeric array' … … 1041 1071 #Input checks 1042 1072 try: 1043 points = array(points).astype(Float)1073 points = ensure_numeric(points, Float) 1044 1074 except: 1045 1075 msg = 'Points could not be converted to Numeric array' … … 1047 1077 1048 1078 try: 1049 polygon = array(polygon).astype(Float)1079 polygon = ensure_numeric(polygon, Float) 1050 1080 except: 1051 1081 msg = 'Polygon could not be converted to Numeric array' … … 1134 1164 #Input checks 1135 1165 try: 1136 #FIXME: This is where it can die due to lack of memeory 1137 #Maybe comment out and test for Numeric type 1138 points = array(points).astype(Float) 1166 points = ensure_numeric(points, Float) 1139 1167 except: 1140 1168 msg = 'Points could not be converted to Numeric array' … … 1143 1171 #if verbose: print 'Checking input to separate_points_by_polygon 2' 1144 1172 try: 1145 #FIXME: SAme thing 1146 polygon = array(polygon).astype(Float) 1173 polygon = ensure_numeric(polygon, Float) 1147 1174 except: 1148 1175 msg = 'Polygon could not be converted to Numeric array' -
inundation/ga/storm_surge/pyvolution/util_ext.h
r907 r1137 125 125 126 126 //Convert to consecutive array 127 A = (PyArrayObject*) PyArray_ContiguousFromObject((PyObject*) B, B -> descr -> type, 0, 0); 127 A = (PyArrayObject*) PyArray_ContiguousFromObject((PyObject*) B, 128 B -> descr -> type, 0, 0); 128 129 129 130 Py_DECREF(B); //FIXME: Is this really needed?? -
inundation/ga/storm_surge/wiki/issues.txt
r652 r1137 2 2 OPEN ISSUES: 3 3 ------------ 4 5 6 Issue: Pmesh do not write in msh format 7 Importance: Mid-High 8 Action: See under pmesh/documentation 9 10 Issue: geo_reference is not passed into domain and further on to sww file. Hence we can't export results properly to e.g GIS 11 Importance: High 12 Action: Make it happen! 13 4 14 5 15
Note: See TracChangeset
for help on using the changeset viewer.