Changeset 832
- Timestamp:
- Feb 2, 2005, 5:53:35 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
r826 r832 793 793 794 794 def dem2xya(filename, verbose=False): 795 """Read Digitial Elevation model from the following ASCIIformat (.asc)795 """Read Digitial Elevation model from the following NetCDF format (.asc) 796 796 797 797 Example: … … 862 862 #Store data 863 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 anyway864 #FIXME: Perhaps the y dimension needs to be reversed 865 #FIXME: x and y definitely needs to be swapped 866 866 for i, line in enumerate(lines[6:]): 867 867 fields = line.split() … … 881 881 882 882 883 884 def dem2netcdf(filename, verbose=False): 883 def asciidem2netcdf(filename, verbose=False): 885 884 """Read Digitial Elevation model from the following ASCII format (.asc) 886 885 -
inundation/ga/storm_surge/pyvolution/least_squares.py
r826 r832 161 161 162 162 if verbose: print 'Read xya' 163 points, attributes = util.read_xya(xya_name)163 points, attributes, _ = util.read_xya(xya_name) 164 164 165 165 #Reduce number of points a bit -
inundation/ga/storm_surge/pyvolution/test_data_manager.py
r826 r832 536 536 537 537 #Convert to NetCDF xya 538 asciidem2netcdf(filename) 538 539 dem2xya(filename) 539 540 540 541 #Check contents 541 542 #Get NetCDF 542 fid = NetCDFFile(root+'.xya .', 'r')543 fid = NetCDFFile(root+'.xya', 'r') 543 544 544 545 # Get the variables -
inundation/ga/storm_surge/pyvolution/test_util.py
r820 r832 337 337 338 338 339 def test_xya (self):339 def test_xya_ascii(self): 340 340 import time, os 341 341 FN = 'xyatest' + str(time.time()) + '.xya' … … 346 346 fid.close() 347 347 348 points, attributes = read_xya(FN)348 points, attributes, _ = read_xya(FN) 349 349 350 350 assert allclose(points, [ [0,1], [1,0], [1,1] ]) … … 354 354 os.remove(FN) 355 355 356 def test_xya_ascii_w_names(self): 357 import time, os 358 FN = 'xyatest' + str(time.time()) + '.xya' 359 fid = open(FN, 'w') 360 fid.write(' %s %s %s\n' %('a1', 'a2', 'a3') ) 361 fid.write('%f %f %f %f %f\n' %(0,1,10,20,30) ) 362 fid.write('%f %f %f %f %f\n' %(1,0,30,20,10) ) 363 fid.write('%f %f %f %f %f\n' %(1,1,40.2,40.3,40.4) ) 364 fid.close() 365 366 points, attributes, attribute_names = read_xya(FN) 367 368 assert attribute_names[0] == 'a1' 369 assert attribute_names[1] == 'a2' 370 assert attribute_names[2] == 'a3' 371 assert allclose(points, [ [0,1], [1,0], [1,1] ]) 372 assert allclose(attributes, [ [10,20,30], [30,20,10], 373 [40.2,40.3,40.4] ]) 374 375 os.remove(FN) 356 376 357 377 def test_polygon_function_constants(self): -
inundation/ga/storm_surge/pyvolution/util.py
r826 r832 51 51 52 52 53 54 #def point_on_line(point, line):55 53 def point_on_line(x, y, x0, y0, x1, y1): 56 54 """Determine whether a point is on a line segment … … 61 59 62 60 """ 63 64 #FIXME: Could do with some C-optimisation65 61 66 62 from Numeric import array, dot, allclose … … 291 287 292 288 def read_xya(filename): 293 """Read simple xya file, no header, just289 """Read simple xya file, possibly with a header in the first line, just 294 290 x y [attributes] 295 291 separated by whitespace … … 297 293 If xya is a NetCDF file it will be read otherwise attemot to read as ASCII 298 294 299 Return list of points and list of attributes300 """301 302 295 Return list of points, list of attributes and 296 attribute names if present otherwise None 297 """ 298 303 299 from Scientific.IO.NetCDF import NetCDFFile 304 305 306 300 try: 307 #Get NetCDF 308 fid = NetCDFFile(filename, 'r') 301 #Get NetCDF 302 303 #FIXME: How can we suppress error message if this fails? 304 fid = NetCDFFile(filename, 'r') 309 305 310 306 # Get the variables 311 307 points = fid.variables['points'] 312 308 attributes = fid.variables['attributes'] 313 ncols = fid.ncols314 nrows = fid.nrows315 # fid.close() #Don't close - arrays are needed outside this function309 attribute_names = fid.variables['attribute_names'] 310 #Don't close - arrays are needed outside this function, 311 #alternatively take a copy here 316 312 except: 317 #Read as ASCII 313 #Read as ASCII file assuming that it is separated by whitespace 318 314 fid = open(filename) 319 315 lines = fid.readlines() 316 fid.close() 317 318 #Check if there is a header line 319 fields = lines[0].strip().split() 320 try: 321 float(fields[0]) 322 except: 323 #This must be a header line 324 attribute_names = fields 325 lines = lines[1:] 326 else: 327 attribute_names = None 320 328 321 329 points = [] … … 323 331 for line in lines: 324 332 fields = line.strip().split() 325 points.append( (float(fields[0]), 333 points.append( (float(fields[0]), float(fields[1])) ) 326 334 attributes.append( [float(z) for z in fields[2:] ] ) 327 nrows = ncols = None #FIXME: HACK 328 329 return points, attributes #, nrows[0], ncols[0] 335 336 return points, attributes, attribute_names 330 337 331 338 … … 407 414 py = polygon[:,1] 408 415 409 410 416 #Begin algorithm 411 417 indices = [] … … 420 426 421 427 #Check for case where point is contained in line segment 422 ##if point_on_line( (x,y), [ [px[i], py[i]], [px[j], py[j]] ]):423 428 if point_on_line(x, y, px[i], py[i], px[j], py[j]): 424 429 if closed:
Note: See TracChangeset
for help on using the changeset viewer.