Changeset 826
- Timestamp:
- Feb 1, 2005, 5:18:44 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/data_manager.py
r815 r826 792 792 793 793 794 def dem2xya(filename, verbose=False): 795 """Read Digitial Elevation model from the following ASCII format (.asc) 796 797 Example: 798 799 ncols 3121 800 nrows 1800 801 xllcorner 722000 802 yllcorner 5893000 803 cellsize 25 804 NODATA_value -9999 805 138.3698 137.4194 136.5062 135.5558 .......... 806 807 Convert to NetCDF xyz format (.xya) 808 """ 809 810 import os 811 from Scientific.IO.NetCDF import NetCDFFile 812 from Numeric import Float, arrayrange, concatenate 813 814 root, ext = os.path.splitext(filename) 815 fid = open(filename) 816 817 if verbose: print 'Reading DEM from %s' %filename 818 lines = fid.readlines() 819 fid.close() 820 821 if verbose: print 'Got', len(lines), ' lines' 822 823 ncols = int(lines[0].split()[1].strip()) 824 nrows = int(lines[1].split()[1].strip()) 825 xllcorner = float(lines[2].split()[1].strip()) 826 yllcorner = float(lines[3].split()[1].strip()) 827 cellsize = float(lines[4].split()[1].strip()) 828 NODATA_value = int(lines[5].split()[1].strip()) 829 830 assert len(lines) == nrows + 6 831 832 netcdfname = root + '.xya' 833 if verbose: print 'Store to NetCDF file %s' %netcdfname 834 # NetCDF file definition 835 fid = NetCDFFile(netcdfname, 'w') 836 837 #Create new file 838 fid.institution = 'Geoscience Australia' 839 fid.description = 'NetCDF xya format for compact and portable storage ' +\ 840 'of spatial point data' 841 842 fid.ncols = ncols 843 fid.nrows = nrows 844 845 846 # dimension definitions 847 fid.createDimension('number_of_points', nrows*ncols) 848 fid.createDimension('number_of_attributes', 1) #Always 1 with the dem fmt 849 fid.createDimension('number_of_dimensions', 2) #This is 2d data 850 851 852 # variable definitions 853 fid.createVariable('points', Float, ('number_of_points', 854 'number_of_dimensions')) 855 fid.createVariable('attributes', Float, ('number_of_points', 856 'number_of_attributes')) 857 858 # Get handles to the variables 859 points = fid.variables['points'] 860 attributes = fid.variables['attributes'] 861 862 #Store data 863 #FIXME: Could be faster using array operations 864 #FIXME: I think I swapped x and y here - 865 # but this function is probably obsolete anyway 866 for i, line in enumerate(lines[6:]): 867 fields = line.split() 868 if verbose: print 'Processing row %d of %d' %(i, nrows) 869 870 x = i*cellsize 871 for j, field in enumerate(fields): 872 index = i*ncols + j 873 874 y = j*cellsize 875 points[index, :] = [x,y] 876 877 z = float(field) 878 attributes[index, 0] = z 879 880 fid.close() 881 882 883 884 def dem2netcdf(filename, verbose=False): 885 """Read Digitial Elevation model from the following ASCII format (.asc) 886 887 Example: 888 889 ncols 3121 890 nrows 1800 891 xllcorner 722000 892 yllcorner 5893000 893 cellsize 25 894 NODATA_value -9999 895 138.3698 137.4194 136.5062 135.5558 .......... 896 897 Convert to NetCDF format (.dem) mimcing the ASCII format closely. 898 """ 899 900 import os 901 from Scientific.IO.NetCDF import NetCDFFile 902 from Numeric import Float, array 903 904 root, ext = os.path.splitext(filename) 905 fid = open(filename) 906 907 if verbose: print 'Reading DEM from %s' %filename 908 lines = fid.readlines() 909 fid.close() 910 911 if verbose: print 'Got', len(lines), ' lines' 912 913 ncols = int(lines[0].split()[1].strip()) 914 nrows = int(lines[1].split()[1].strip()) 915 xllcorner = float(lines[2].split()[1].strip()) 916 yllcorner = float(lines[3].split()[1].strip()) 917 cellsize = float(lines[4].split()[1].strip()) 918 NODATA_value = int(lines[5].split()[1].strip()) 919 920 assert len(lines) == nrows + 6 921 922 netcdfname = root + '.dem' 923 if verbose: print 'Store to NetCDF file %s' %netcdfname 924 # NetCDF file definition 925 fid = NetCDFFile(netcdfname, 'w') 926 927 #Create new file 928 fid.institution = 'Geoscience Australia' 929 fid.description = 'NetCDF DEM format for compact and portable storage ' +\ 930 'of spatial point data' 931 932 fid.ncols = ncols 933 fid.nrows = nrows 934 fid.xllcorner = xllcorner 935 fid.yllcorner = yllcorner 936 fid.cellsize = cellsize 937 fid.NODATA_value = NODATA_value 938 939 # dimension definitions 940 fid.createDimension('number_of_rows', nrows) 941 fid.createDimension('number_of_columns', ncols) 942 943 # variable definitions 944 fid.createVariable('elevation', Float, ('number_of_rows', 945 'number_of_columns')) 946 947 # Get handles to the variables 948 elevation = fid.variables['elevation'] 949 950 #Store data 951 for i, line in enumerate(lines[6:]): 952 fields = line.split() 953 if verbose: print 'Processing row %d of %d' %(i, nrows) 954 955 elevation[i, :] = array([float(x) for x in fields]) 956 957 fid.close() 958 959 960 961 794 962 795 963 #OBSOLETE STUFF -
inundation/ga/storm_surge/pyvolution/least_squares.py
r820 r826 114 114 point_coordinates, 115 115 point_attributes, 116 alpha = DEFAULT_ALPHA): 116 alpha = DEFAULT_ALPHA, 117 verbose = False): 117 118 """ 118 119 Fit a smooth surface to a trianglulation, … … 138 139 triangles, 139 140 point_coordinates, 140 alpha = alpha) 141 142 vertex_attributes = interp.fit_points(point_attributes) 141 alpha = alpha, 142 verbose = verbose) 143 144 vertex_attributes = interp.fit_points(point_attributes, verbose = verbose) 143 145 return vertex_attributes 144 146 145 147 146 148 147 def xya2rectangular(xya_name, M, N, alpha = DEFAULT_ALPHA): 149 def xya2rectangular(xya_name, M, N, alpha = DEFAULT_ALPHA, 150 verbose = False, reduction = 1): 148 151 """Fits attributes from xya file to MxN rectangular mesh 149 152 … … 156 159 157 160 import util, mesh_factory 158 159 points, attributes = util.read_xya(xya_name) 160 161 162 if verbose: print 'Read xya' 163 points, attributes = util.read_xya(xya_name) 164 165 #Reduce number of points a bit 166 points = points[::reduction] 167 attributes = attributes[::reduction] 168 169 if verbose: print 'Got %d data points' %len(points) 170 171 if verbose: print 'Create mesh' 161 172 #Find extent 162 173 max_x = min_x = points[0][0] … … 179 190 triangles, 180 191 points, 181 attributes, alpha=alpha )192 attributes, alpha=alpha, verbose=verbose) 182 193 183 194 … … 193 204 triangles, 194 205 point_coordinates = None, 195 alpha = DEFAULT_ALPHA): 206 alpha = DEFAULT_ALPHA, 207 verbose = False): 196 208 197 209 … … 216 228 """ 217 229 218 219 230 #Convert input to Numeric arrays 220 231 vertex_coordinates = array(vertex_coordinates).astype(Float) … … 222 233 223 234 #Build underlying mesh 235 if verbose: print 'Building mesh' 224 236 self.mesh = General_mesh(vertex_coordinates, triangles) 225 237 … … 228 240 229 241 #Build coefficient matrices 230 self.build_coefficient_matrix_B(point_coordinates) 242 self.build_coefficient_matrix_B(point_coordinates, verbose = verbose) 243 231 244 232 245 def set_point_coordinates(self, point_coordinates): … … 236 249 self.build_coefficient_matrix_B(point_coordinates) 237 250 238 def build_coefficient_matrix_B(self, point_coordinates=None): 251 def build_coefficient_matrix_B(self, point_coordinates=None, 252 verbose = False): 239 253 """Build final coefficient matrix""" 240 254 241 255 242 256 if self.alpha <> 0: 257 if verbose: print 'Building smoothing matrix' 243 258 self.build_smoothing_matrix_D() 244 259 245 260 if point_coordinates: 246 261 if verbose: print 'Building interpolation matrix' 247 262 self.build_interpolation_matrix_A(point_coordinates) 248 263 … … 357 372 #print "PDSG - xi1", xi1 358 373 #print "PDSG - xi2", xi2 359 #print "PDSG element %i verts((%f, %f),(%f, %f),(%f, %f))"\374 #print "PDSG element %i verts((%f, %f),(%f, %f),(%f, %f))"\ 360 375 # % (k, xi0[0], xi0[1], xi1[0], xi1[1], xi2[0], xi2[1]) 361 376 … … 366 381 367 382 368 369 383 #Compute interpolation 370 384 sigma2 = dot((x-xi0), n2)/dot((xi2-xi0), n2) … … 603 617 604 618 605 def fit_points(self, z ):619 def fit_points(self, z, verbose=False): 606 620 """Like fit, but more robust when each point has two or more attributes 607 621 FIXME (Ole): The name fit_points doesn't carry any meaning … … 610 624 611 625 try: 626 if verbose: print 'Solving penalised least_squares problem' 612 627 return self.fit(z) 613 628 except VectorShapeError, e: -
inundation/ga/storm_surge/pyvolution/quantity.py
r773 r826 308 308 """ 309 309 310 from Numeric import array, Float, Int 310 from Numeric import array, Float, Int, allclose 311 311 312 312 values = array(values).astype(Float) … … 347 347 348 348 elif location == 'unique vertices': 349 assert len(values.shape) == 1, 'Values array must be 1d' 350 self.set_vertex_values(values, indexes = indexes) 349 assert len(values.shape) == 1 or allclose(values.shape[1:], 1),\ 350 'Values array must be 1d' 351 352 self.set_vertex_values(values.flat, indexes = indexes) 351 353 else: 352 354 if len(values.shape) == 1: … … 395 397 A = array(A, Float) 396 398 399 #print 'SHAPE A', A.shape 397 400 assert len(A.shape) == 1 398 401 -
inundation/ga/storm_surge/pyvolution/test_data_manager.py
r773 r826 495 495 #Cleanup 496 496 os.remove(sww.filename) 497 498 499 500 def test_dem2xya(self): 501 """Test conversion from dem in ascii format to native NetCDF xya format 502 """ 503 504 import time, os 505 from Numeric import array, zeros, allclose, Float, concatenate 506 from Scientific.IO.NetCDF import NetCDFFile 507 508 #Write test dem file 509 root = 'demtest'+str(time.time()) 510 511 filename = root+'.asc' 512 fid = open(filename, 'w') 513 fid.write("""ncols 5 514 nrows 6 515 xllcorner 2000.5 516 yllcorner 3000.5 517 cellsize 25 518 NODATA_value -9999 519 """) 520 #Create linear function 521 522 ref_points = [] 523 ref_attributes = [] 524 for i in range(6): 525 x = i*25.0 526 for j in range(5): 527 y = j*25.0 528 z = x+2*y 529 530 ref_points.append( [x,y] ) 531 ref_attributes.append([z]) 532 fid.write('%f ' %z) 533 fid.write('\n') 534 535 fid.close() 536 537 #Convert to NetCDF xya 538 dem2xya(filename) 539 540 #Check contents 541 #Get NetCDF 542 fid = NetCDFFile(root+'.xya.', 'r') 543 544 # Get the variables 545 points = fid.variables['points'] 546 attributes = fid.variables['attributes'] 547 548 #Check values 549 550 #print points[:] 551 #print ref_points 552 assert allclose(points, ref_points) 553 554 #print attributes[:] 555 #print ref_attributes 556 assert allclose(attributes, ref_attributes) 557 558 #Cleanup 559 fid.close() 560 561 os.remove(filename) 562 os.remove(root + '.xya') 563 564 497 565 498 566 #------------------------------------------------------------- -
inundation/ga/storm_surge/pyvolution/util.py
r820 r826 1 1 """This modul contains various auxiliary function used by pyvolution. 2 3 It is also a clearing house for function tat may later earn a module 4 of their own. 5 """ 2 6 3 7 def angle(v): … … 286 290 return res 287 291 288 def read_xya(file _name):292 def read_xya(filename): 289 293 """Read simple xya file, no header, just 290 294 x y [attributes] 291 295 separated by whitespace 296 297 If xya is a NetCDF file it will be read otherwise attemot to read as ASCII 292 298 293 299 Return list of points and list of attributes 294 300 """ 295 296 fid = open(file_name) 297 lines = fid.readlines() 298 299 points = [] 300 attributes = [] 301 for line in lines: 302 fields = line.strip().split() 303 304 points.append( (float(fields[0]), float(fields[1])) ) 305 attributes.append( [float(z) for z in fields[2:] ] ) 306 307 return points, attributes 301 302 303 from Scientific.IO.NetCDF import NetCDFFile 304 305 306 try: 307 #Get NetCDF 308 fid = NetCDFFile(filename, 'r') 309 310 # Get the variables 311 points = fid.variables['points'] 312 attributes = fid.variables['attributes'] 313 ncols = fid.ncols 314 nrows = fid.nrows 315 #fid.close() #Don't close - arrays are needed outside this function 316 except: 317 #Read as ASCII 318 fid = open(filename) 319 lines = fid.readlines() 320 321 points = [] 322 attributes = [] 323 for line in lines: 324 fields = line.strip().split() 325 points.append( (float(fields[0]), float(fields[1])) ) 326 attributes.append( [float(z) for z in fields[2:] ] ) 327 nrows = ncols = None #FIXME: HACK 328 329 return points, attributes #, nrows[0], ncols[0] 308 330 309 331 310 332 ##################################### 311 #POLY FON STUFF333 #POLYGON STUFF 312 334 # 313 335 #FIXME: ALl these should be put into new module polygon.py
Note: See TracChangeset
for help on using the changeset viewer.