Changeset 2570
- Timestamp:
- Mar 21, 2006, 3:51:39 PM (19 years ago)
- Location:
- inundation/geospatial_data
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/geospatial_data/geospatial_data.py
r2489 r2570 7 7 from utilities.numerical_tools import ensure_numeric 8 8 9 from Numeric import concatenate 9 from Numeric import concatenate, array, Float, shape 10 10 11 11 from coordinate_transforms.geo_reference import Geo_reference 12 13 from os import access, F_OK, R_OK 12 14 13 15 class Geospatial_data: 14 16 15 17 def __init__(self, 16 data_points ,18 data_points = None, 17 19 attributes = None, 18 20 geo_reference = None, 19 default_attribute_name = None): 21 default_attribute_name = None, 22 file_name = None): 23 20 24 21 25 """Create instance from data points and associated attributes … … 41 45 If None, the default is the 'first' 42 46 47 file_name: Name of input file..... 48 43 49 """ 44 50 45 self.data_points = ensure_numeric(data_points) 46 self.set_attributes(attributes) 47 self.set_geo_reference(geo_reference) 48 self.set_default_attribute_name(default_attribute_name) 49 50 51 51 if file_name is None: 52 self.file_name = None 53 self.check_data_points(data_points) 54 55 self.set_attributes(attributes) 56 self.set_geo_reference(geo_reference) 57 self.set_default_attribute_name(default_attribute_name) 58 59 60 else: 61 # print 'from init file name:', file_name 62 if access(file_name, F_OK) == 0 : 63 msg = 'File %s does not exist or is not accessable' %file_name 64 raise msg 65 else: 66 data = {} 67 # watch for case where file name and points, attributes etc are provided!! 68 # if file name then all provided info will be removed! 69 self.file_name = file_name 70 data = import_points_file(self.file_name) 71 72 # print 'data: point in init', data['pointlist'] 73 # print 'attrib: point in init', data['attributelist'] 74 # print 'geo_ref: point in init', data['geo_reference'] 75 76 data_points = data['pointlist'] 77 attributes = data['attributelist'] 78 get_reference = data['geo_reference'] 79 80 self.check_data_points(data_points) 81 self.set_attributes(attributes) 82 self.set_geo_reference(geo_reference) 83 self.set_default_attribute_name(default_attribute_name) 84 85 86 def check_data_points(self, data_points): 87 """Checks data points 88 """ 89 90 if data_points is None: 91 self.data_points = None 92 # FIXME: should throw an error if bad file name and no data! 93 print 'There is no data or files to read for data!!' 94 msg = 'file name %s does not exist in data set and the ' %self.file_name 95 # some assert 96 97 else: 98 self.data_points = ensure_numeric(data_points) 52 99 53 100 def set_attributes(self, attributes): … … 95 142 self.default_attribute_name = default_attribute_name 96 143 144 def set_file_name(self, file_name = None): 145 if file_name is None: 146 self.file_name = None 147 else: 148 if access(file_name, F_OK) == 0 : 149 msg = 'Geospatial_data object needs have either data_points \n' 150 msg += ' or a file with data points' 151 raise msg 152 else: 153 self.file_name = file_name 154 print 'file name from set', self.file_name 155 156 def set_self_from_file(self, file_name = None): 157 # check if file_name is a string and also that the file exists 158 if file_name is None: 159 self.file_name = None 160 # else: 161 # if access(self.file_name, F_OK) == 0 : 162 # msg = 'Geospatial_data object needs have either data_points \n' 163 # msg += ' or a file with data points' 164 # raise msg 165 else: 166 self.file_name = file_name 167 self = self.import_points_file(file_name) 168 # print 'data points from set self file name', self.data_points 169 # print 'attributes from set self file name', self.attributes 170 # print 'geo_ref from set self file name', self.geo_reference 171 return self 172 97 173 98 174 def get_geo_reference(self): … … 156 232 geo_ref = Geo_reference(zone1, xll, yll) 157 233 158 if zone1 == zone2: 234 if zone1 == zone2: 159 235 a_rel_points = self.get_data_points() 160 236 b_rel_points = other.get_data_points() … … 184 260 ZONE to allow addition.' 185 261 raise msg 262 186 263 264 265 ### 266 # IMPORT/EXPORT POINTS FILES 267 ### 268 ''' 269 def export_points_file(ofile, point_dict): 270 """ 271 write a points file, ofile, as a binary (.xya) or text (.pts) file 272 273 ofile is the file name, including the extension 274 275 The point_dict is defined at the top of this file. 276 """ 277 #this was done for all keys in the mesh file. 278 #if not mesh_dict.has_key('points'): 279 # mesh_dict['points'] = [] 280 if (ofile[-4:] == ".xya"): 281 _write_xya_file(ofile, point_dict) 282 elif (ofile[-4:] == ".pts"): 283 _write_pts_file(ofile, point_dict) 284 else: 285 msg = 'Unknown file type %s ' %ofile 286 raise IOError, msg 287 288 ''' 289 def import_points_file( ofile, delimiter = None, verbose = False): 290 """ load an .xya or .pts file 291 Note: will throw an IOError if it can't load the file. 292 Catch these! 293 """ 294 295 all_data = {} 296 if ofile[-4:]== ".xya": 297 try: 298 if delimiter == None: 299 try: 300 fd = open(ofile) 301 all_data = _read_xya_file(fd, ',') 302 except SyntaxError: 303 fd.close() 304 fd = open(ofile) 305 all_data = _read_xya_file(fd, ' ') 306 else: 307 fd = open(ofile) 308 all_data = _read_xya_file(fd, delimiter) 309 fd.close() 310 except (IndexError,ValueError,SyntaxError): 311 fd.close() 312 msg = 'Could not open file %s ' %ofile 313 raise IOError, msg 314 except IOError: 315 # Catch this to add an error message 316 msg = 'Could not open file %s ' %ofile 317 raise IOError, msg 318 319 elif ofile[-4:]== ".pts": 320 try: 321 # print 'hi from import_points_file' 322 all_data = _read_pts_file(ofile, verbose) 323 # print 'hi1 from import_points_file', all_data 324 except IOError, e: 325 msg = 'Could not open file %s ' %ofile 326 raise IOError, msg 327 else: 328 msg = 'Extension %s is unknown' %ofile[-4:] 329 raise IOError, msg 330 331 return all_data 332 333 def export_points_file(ofile, point_dict): 334 """ 335 write a points file, ofile, as a text (.xya) or binary (.pts) file 336 337 ofile is the file name, including the extension 338 339 The point_dict is defined at the top of this file. 340 """ 341 #this was done for all keys in the mesh file. 342 #if not mesh_dict.has_key('points'): 343 # mesh_dict['points'] = [] 344 if (ofile[-4:] == ".xya"): 345 _write_xya_file(ofile, point_dict) 346 elif (ofile[-4:] == ".pts"): 347 _write_pts_file(ofile, point_dict) 348 else: 349 msg = 'Unknown file type %s ' %ofile 350 raise IOError, msg 351 352 def _read_pts_file(file_name, verbose = False): 353 """Read .pts NetCDF file 354 355 Return a dic of array of points, and dic of array of attribute 356 eg 357 dic['points'] = [[1.0,2.0],[3.0,5.0]] 358 dic['attributelist']['elevation'] = [[7.0,5.0] 359 """ 360 #FIXME: (DSG) This format has issues. 361 # There can't be an attribute called points 362 # consider format change 363 364 # print 'hi for read points file' 365 from Scientific.IO.NetCDF import NetCDFFile 366 367 if verbose: print 'Reading ', file_name 368 369 370 # see if the file is there. Throw a QUIET IO error if it isn't 371 fd = open(file_name,'r') 372 fd.close() 373 374 #throws prints to screen if file not present 375 fid = NetCDFFile(file_name, 'r') 376 377 point_atts = {} 378 # Get the variables 379 point_atts['pointlist'] = array(fid.variables['points']) 380 keys = fid.variables.keys() 381 if verbose: print 'Got %d variables: %s' %(len(keys), keys) 382 try: 383 keys.remove('points') 384 except IOError, e: 385 fid.close() 386 msg = 'Expected keyword "points" but could not find it' 387 raise IOError, msg 388 389 attributes = {} 390 for key in keys: 391 if verbose: print "reading attribute '%s'" %key 392 393 attributes[key] = array(fid.variables[key]) 394 395 point_atts['attributelist'] = attributes 396 397 try: 398 geo_reference = Geo_reference(NetCDFObject=fid) 399 point_atts['geo_reference'] = geo_reference 400 except AttributeError, e: 401 #geo_ref not compulsory 402 point_atts['geo_reference'] = None 403 404 fid.close() 405 return point_atts 406 407 408 def _read_xya_file( fd, delimiter): 409 # print 'hello from read xya data' 410 points = [] 411 pointattributes = [] 412 title = fd.readline() 413 att_names = clean_line(title,delimiter) 414 415 att_dict = {} 416 line = fd.readline() 417 numbers = clean_line(line,delimiter) 418 while len(numbers) > 1: 419 if numbers != []: 420 try: 421 x = float(numbers[0]) 422 y = float(numbers[1]) 423 points.append([x,y]) 424 numbers.pop(0) 425 numbers.pop(0) 426 #attributes = [] 427 #print "att_names",att_names 428 #print "numbers",numbers 429 if len(att_names) != len(numbers): 430 fd.close() 431 # It might not be a problem with the title 432 #raise TitleAmountError 433 raise IOError 434 for i,num in enumerate(numbers): 435 num.strip() 436 if num != '\n' and num != '': 437 #attributes.append(float(num)) 438 att_dict.setdefault(att_names[i],[]).append(float(num)) 439 440 except ValueError: 441 raise SyntaxError 442 line = fd.readline() 443 numbers = clean_line(line,delimiter) 444 445 if line == '': 446 # end of file 447 geo_reference = None 448 else: 449 geo_reference = Geo_reference(ASCIIFile=fd,read_title=line) 450 451 xya_dict = {} 452 xya_dict['pointlist'] = array(points).astype(Float) 453 454 for key in att_dict.keys(): 455 att_dict[key] = array(att_dict[key]).astype(Float) 456 xya_dict['attributelist'] = att_dict 457 458 xya_dict['geo_reference'] = geo_reference 459 #print "xya_dict",xya_dict 460 return xya_dict 461 462 463 464 def _write_pts_file(file_name, point_atts): 465 """Write .pts NetCDF file 466 467 WARNING: This function mangles the point_atts data structure 468 """ 469 #FIXME: (DSG)This format has issues. 470 # There can't be an attribute called points 471 # consider format change 472 473 474 legal_keys = ['pointlist', 'attributelist', 'geo_reference'] 475 for key in point_atts.keys(): 476 msg = 'Key %s is illegal. Valid keys are %s' %(key, legal_keys) 477 assert key in legal_keys, msg 478 479 from Scientific.IO.NetCDF import NetCDFFile 480 _point_atts2array(point_atts) 481 # NetCDF file definition 482 outfile = NetCDFFile(file_name, 'w') 483 484 #Create new file 485 outfile.institution = 'Geoscience Australia' 486 outfile.description = 'NetCDF format for compact and portable storage ' +\ 487 'of spatial point data' 488 489 # dimension definitions 490 shape = point_atts['pointlist'].shape[0] 491 outfile.createDimension('number_of_points', shape) 492 outfile.createDimension('number_of_dimensions', 2) #This is 2d data 493 494 # variable definition 495 outfile.createVariable('points', Float, ('number_of_points', 496 'number_of_dimensions')) 497 498 #create variables 499 outfile.variables['points'][:] = point_atts['pointlist'] #.astype(Float32) 500 for key in point_atts['attributelist'].keys(): 501 outfile.createVariable(key, Float, ('number_of_points',)) 502 outfile.variables[key][:] = point_atts['attributelist'][key] #.astype(Float32) 503 504 if point_atts.has_key('geo_reference') and not point_atts['geo_reference'] == None: 505 point_atts['geo_reference'].write_NetCDF(outfile) 506 507 outfile.close() 508 509 510 511 def _write_xya_file( file_name, xya_dict, delimiter = ','): 512 """ 513 export a file, ofile, with the xya format 514 515 """ 516 points = xya_dict['pointlist'] 517 pointattributes = xya_dict['attributelist'] 518 519 fd = open(file_name,'w') 520 521 titlelist = "" 522 for title in pointattributes.keys(): 523 titlelist = titlelist + title + delimiter 524 titlelist = titlelist[0:-len(delimiter)] # remove the last delimiter 525 fd.write(titlelist+"\n") 526 #<vertex #> <x> <y> [attributes] 527 for i,vert in enumerate( points): 528 529 attlist = "," 530 for att in pointattributes.keys(): 531 attlist = attlist + str(pointattributes[att][i])+ delimiter 532 attlist = attlist[0:-len(delimiter)] # remove the last delimiter 533 attlist.strip() 534 fd.write( str(vert[0]) + delimiter 535 + str(vert[1]) 536 + attlist + "\n") 537 538 # geo_reference info 539 if xya_dict.has_key('geo_reference') and \ 540 not xya_dict['geo_reference'] is None: 541 xya_dict['geo_reference'].write_ASCII(fd) 542 fd.close() 543 544 def _point_atts2array(point_atts): 545 point_atts['pointlist'] = array(point_atts['pointlist']).astype(Float) 546 547 for key in point_atts['attributelist'].keys(): 548 point_atts['attributelist'][key]= array(point_atts['attributelist'][key]).astype(Float) 549 return point_atts 550 551 187 552 188 553 … … 205 570 206 571 207 208 209 210 211 212 572 def points_dictionary2geospatial_data(points_dictionary): 213 573 """Convert points_dictionary to geospatial data object … … 229 589 geo_reference = geo) 230 590 231 232 233 234 591 def clean_line(line,delimiter): 592 """Remove whitespace 593 """ 594 #print ">%s" %line 595 line = line.strip() 596 #print "stripped>%s" %line 597 numbers = line.split(delimiter) 598 i = len(numbers) - 1 599 while i >= 0: 600 if numbers[i] == '': 601 numbers.pop(i) 602 else: 603 numbers[i] = numbers[i].strip() 604 605 i += -1 606 #for num in numbers: 607 # print "num>%s<" %num 608 return numbers 609 610 #def add_points_files(file): 611 -
inundation/geospatial_data/test_geospatial_data.py
r2489 r2570 6 6 from Numeric import zeros, array, allclose, concatenate 7 7 from math import sqrt, pi 8 import tempfile 8 9 9 10 from geospatial_data import * … … 231 232 assert allclose(G.get_data_points(), [[2.0, 4.1], [4.0, 7.3], [5.1, 9.1], [6.1, 6.3]]) 232 233 233 def xtest_create_from_file(self): 234 """Check that object can be created from a points file 234 235 236 237 def test_create_from_xya_file(self): 238 """Check that object can be created from a points file (.pts and .xya) 235 239 """ 236 240 … … 245 249 'attributelist': {'att1': attributes, 246 250 'att2': array(attributes) + 1}} 247 251 248 252 # Create points as an xya file 249 253 FN = 'test_points.xya' … … 252 256 253 257 #Create object from file 254 G = Geospatial_data(file name = FN)255 258 G = Geospatial_data(file_name = FN) 259 256 260 assert allclose(G.get_data_points(), points) 257 261 assert allclose(G.get_attributes('att1'), attributes) 258 262 assert allclose(G.get_attributes('att2'), array(attributes) + 1) 259 263 260 261 262 264 os.remove(FN) 263 265 266 def test_loadxya(self): 267 """ 268 comma delimited 269 """ 270 fileName = tempfile.mktemp(".xya") 271 file = open(fileName,"w") 272 file.write("elevation , speed \n\ 273 1.0, 0.0, 10.0, 0.0\n\ 274 0.0, 1.0, 0.0, 10.0\n\ 275 1.0, 0.0, 10.4, 40.0\n") 276 file.close() 277 #print fileName 278 dict = import_points_file(fileName,delimiter = ',') 279 os.remove(fileName) 280 assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 281 assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) 282 assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0]) 283 284 def test_loadxya2(self): 285 """ 286 space delimited 287 """ 288 import os 289 290 fileName = tempfile.mktemp(".xya") 291 file = open(fileName,"w") 292 file.write(" elevation speed \n\ 293 1.0 0.0 10.0 0.0\n\ 294 0.0 1.0 0.0 10.0\n\ 295 1.0 0.0 10.4 40.0\n") 296 file.close() 297 #print fileName 298 # dict = import_points_file(fileName,delimiter = ' ') 299 dict = import_points_file(fileName,delimiter = ' ') 300 os.remove(fileName) 301 assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 302 assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) 303 assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0]) 304 305 def test_loadxya3(self): 306 """ 307 space delimited 308 """ 309 import os 310 311 fileName = tempfile.mktemp(".xya") 312 file = open(fileName,"w") 313 file.write(" elevation speed \n\ 314 1.0 0.0 10.0 0.0\n\ 315 0.0 1.0 0.0 10.0\n\ 316 1.0 0.0 10.4 40.0\n\ 317 #geocrap\n\ 318 56\n\ 319 56.6\n\ 320 3\n") 321 file.close() 322 #print fileName 323 dict = import_points_file(fileName,delimiter = ' ') 324 os.remove(fileName) 325 assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 326 assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) 327 assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0]) 328 329 def test_loadxy_bad_no_file_xya(self): 330 import os 331 332 fileName = tempfile.mktemp(".xya") 333 #print fileName 334 try: 335 dict = import_points_file(fileName,delimiter = ' ') 336 except IOError: 337 pass 338 else: 339 self.failUnless(0 ==1, 340 'imaginary file did not raise error!') 341 342 def Xtest_read_write_points_file_bad(self): 343 #not used yet as i'm uncertain if "tri_dict" is nesscessary to geospatial_data 344 dict = self.tri_dict.copy() 345 fileName = tempfile.mktemp(".xxx") 346 try: 347 export_points_file(fileName,dict) 348 except IOError: 349 pass 350 else: 351 self.failUnless(0 ==1, 352 'bad points file extension did not raise error!') 353 354 def Xtest_read_write_points_file_bad2(self): 355 dict = {} 356 att_dict = {} 357 dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 358 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 359 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 360 dict['attributelist'] = att_dict 361 dict['geo_reference'] = Geo_reference(56,1.9,1.9) 362 try: 363 export_points_file("_???/yeah.xya",dict) 364 except IOError: 365 pass 366 else: 367 self.failUnless(0 ==1, 368 'bad points file extension did not raise error!') 369 370 def test_loadxy_bad(self): 371 import os 372 373 fileName = tempfile.mktemp(".xya") 374 file = open(fileName,"w") 375 file.write(" elevation \n\ 376 1.0 0.0 10.0 0.0\n\ 377 0.0 1.0 0.0 10.0\n\ 378 1.0 0.0 10.4 40.0\n") 379 file.close() 380 #print fileName 381 try: 382 dict = import_points_file(fileName,delimiter = ' ') 383 except IOError: 384 pass 385 else: 386 self.failUnless(0 ==1, 387 'bad xya file did not raise error!') 388 os.remove(fileName) 389 390 def test_loadxy_bad2(self): 391 import os 392 393 fileName = tempfile.mktemp(".xya") 394 file = open(fileName,"w") 395 file.write("elevation\n\ 396 1.0 0.0 10.0 \n\ 397 0.0 1.0\n\ 398 1.0 \n") 399 file.close() 400 #print fileName 401 try: 402 dict = import_points_file(fileName,delimiter = ' ') 403 except IOError: 404 pass 405 else: 406 self.failUnless(0 ==1, 407 'bad xya file did not raise error!') 408 os.remove(fileName) 409 410 def test_loadxy_bad3(self): 411 """ 412 specifying wrong delimiter 413 """ 414 import os 415 416 fileName = tempfile.mktemp(".xya") 417 file = open(fileName,"w") 418 file.write(" elevation , speed \n\ 419 1.0, 0.0, 10.0, 0.0\n\ 420 0.0, 1.0, 0.0, 10.0\n\ 421 1.0, 0.0, 10.4, 40.0\n") 422 file.close() 423 try: 424 dict = import_points_file(fileName,delimiter = ' ') 425 except IOError: 426 pass 427 else: 428 self.failUnless(0 ==1, 429 'bad xya file did not raise error!') 430 os.remove(fileName) 431 432 def test_loadxy_bad4(self): 433 """ 434 specifying wrong delimiter 435 """ 436 import os 437 438 fileName = tempfile.mktemp(".xya") 439 file = open(fileName,"w") 440 file.write(" elevation speed \n\ 441 1.0 0.0 10.0 0.0\n\ 442 0.0 1.0 0.0 10.0\n\ 443 1.0 0.0 10.4 40.0\n\ 444 yeah") 445 file.close() 446 try: 447 dict = import_points_file(fileName,delimiter = ' ') 448 except IOError: 449 pass 450 else: 451 self.failUnless(0 ==1, 452 'bad xya file did not raise error!') 453 os.remove(fileName) 454 455 def test_loadxy_bad4(self): 456 """ 457 specifying wrong delimiter 458 """ 459 import os 460 461 fileName = tempfile.mktemp(".xya") 462 file = open(fileName,"w") 463 file.write(" elevation speed \n\ 464 1.0 0.0 10.0 0.0\n\ 465 0.0 1.0 0.0 10.0\n\ 466 1.0 0.0 10.4 40.0\n\ 467 #geocrap") 468 file.close() 469 try: 470 dict = import_points_file(fileName,delimiter = ' ') 471 except IOError: 472 pass 473 else: 474 self.failUnless(0 ==1, 475 'bad xya file did not raise error!') 476 os.remove(fileName) 477 478 def test_loadxy_bad5(self): 479 """ 480 specifying wrong delimiter 481 """ 482 import os 483 484 fileName = tempfile.mktemp(".xya") 485 file = open(fileName,"w") 486 file.write(" elevation speed \n\ 487 1.0 0.0 10.0 0.0\n\ 488 0.0 1.0 0.0 10.0\n\ 489 1.0 0.0 10.4 40.0\n\ 490 #geocrap\n\ 491 crap") 492 file.close() 493 try: 494 dict = import_points_file(fileName,delimiter = ' ') 495 except IOError: 496 pass 497 else: 498 self.failUnless(0 ==1, 499 'bad xya file did not raise error!') 500 os.remove(fileName) 501 502 def test_loadxy_bad_no_file_xya(self): 503 import os 504 505 fileName = tempfile.mktemp(".xya") 506 #print fileName 507 try: 508 dict = import_points_file(fileName,delimiter = ' ') 509 except IOError: 510 pass 511 else: 512 self.failUnless(0 ==1, 513 'imaginary file did not raise error!') 514 515 ###################### .XYA ############################## 516 517 def test_export_xya_file(self): 518 dict = {} 519 att_dict = {} 520 dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 521 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 522 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 523 dict['attributelist'] = att_dict 524 dict['geo_reference'] = Geo_reference(56,1.9,1.9) 525 526 527 fileName = tempfile.mktemp(".xya") 528 export_points_file(fileName, dict) 529 dict2 = import_points_file(fileName) 530 #print "fileName",fileName 531 os.remove(fileName) 532 #print "dict2",dict2 533 534 assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 535 assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4]) 536 answer = [10.0, 0.0, 10.4] 537 assert allclose(dict2['attributelist']['brightness'], answer) 538 #print "dict2['geo_reference']",dict2['geo_reference'] 539 self.failUnless(dict['geo_reference'] == dict2['geo_reference'], 540 'test_writepts failed. Test geo_reference') 541 542 def test_export_xya_file2(self): 543 dict = {} 544 att_dict = {} 545 dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 546 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 547 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 548 dict['attributelist'] = att_dict 549 550 551 fileName = tempfile.mktemp(".xya") 552 export_points_file(fileName, dict) 553 dict2 = import_points_file(fileName) 554 #print "fileName",fileName 555 os.remove(fileName) 556 #print "dict2",dict2 557 558 assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 559 assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4]) 560 answer = [10.0, 0.0, 10.4] 561 assert allclose(dict2['attributelist']['brightness'], answer) 562 563 564 def test_loadpts(self): 565 566 from Scientific.IO.NetCDF import NetCDFFile 567 568 fileName = tempfile.mktemp(".pts") 569 # NetCDF file definition 570 outfile = NetCDFFile(fileName, 'w') 571 572 # dimension definitions 573 outfile.createDimension('number_of_points', 3) 574 outfile.createDimension('number_of_dimensions', 2) #This is 2d data 575 576 # variable definitions 577 outfile.createVariable('points', Float, ('number_of_points', 578 'number_of_dimensions')) 579 outfile.createVariable('elevation', Float, ('number_of_points',)) 580 581 # Get handles to the variables 582 points = outfile.variables['points'] 583 elevation = outfile.variables['elevation'] 584 585 points[0, :] = [1.0,0.0] 586 elevation[0] = 10.0 587 points[1, :] = [0.0,1.0] 588 elevation[1] = 0.0 589 points[2, :] = [1.0,0.0] 590 elevation[2] = 10.4 591 592 outfile.close() 593 594 dict = import_points_file(fileName) 595 os.remove(fileName) 596 answer = [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]] 597 #print "dict['pointlist']",dict['pointlist'] 598 #print "answer",answer 599 assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 600 assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) 601 602 def test_writepts(self): 603 dict = {} 604 att_dict = {} 605 dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 606 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 607 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 608 dict['attributelist'] = att_dict 609 dict['geo_reference'] = Geo_reference(56,1.9,1.9) 610 611 612 fileName = tempfile.mktemp(".pts") 613 export_points_file(fileName, dict) 614 dict2 = import_points_file(fileName) 615 #print "fileName",fileName 616 os.remove(fileName) 617 #print "dict2",dict2 618 619 assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 620 assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4]) 621 answer = [10.0, 0.0, 10.4] 622 assert allclose(dict2['attributelist']['brightness'], answer) 623 624 #print "dict['geo_reference'] ",dict['geo_reference'] 625 #print "dict2['geo_reference']",dict2['geo_reference'] 626 627 self.failUnless(dict['geo_reference'] == dict2['geo_reference'], 628 'test_writepts failed. Test geo_reference') 629 630 ########################## BAD .PTS ########################## 631 632 def test_load_bad_no_file_pts(self): 633 import os 634 import tempfile 635 636 fileName = tempfile.mktemp(".pts") 637 #print fileName 638 try: 639 dict = import_points_file(fileName) 640 except IOError: 641 pass 642 else: 643 self.failUnless(0 ==1, 644 'imaginary file did not raise error!') 645 646 647 def test_create_from_pts_file(self): 648 649 from Scientific.IO.NetCDF import NetCDFFile 650 651 652 # fileName = tempfile.mktemp(".pts") 653 FN = 'test_points.pts' 654 # NetCDF file definition 655 outfile = NetCDFFile(FN, 'w') 656 657 # dimension definitions 658 outfile.createDimension('number_of_points', 3) 659 outfile.createDimension('number_of_dimensions', 2) #This is 2d data 660 661 # variable definitions 662 outfile.createVariable('points', Float, ('number_of_points', 663 'number_of_dimensions')) 664 outfile.createVariable('elevation', Float, ('number_of_points',)) 665 666 # Get handles to the variables 667 points = outfile.variables['points'] 668 elevation = outfile.variables['elevation'] 669 670 points[0, :] = [1.0,0.0] 671 elevation[0] = 10.0 672 points[1, :] = [0.0,1.0] 673 elevation[1] = 0.0 674 points[2, :] = [1.0,0.0] 675 elevation[2] = 10.4 676 677 outfile.close() 678 679 G = Geospatial_data(file_name = FN) 680 681 assert allclose(G.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 682 assert allclose(G.get_attributes(), [10.0, 0.0, 10.4]) 683 os.remove(FN) 264 684 265 685 if __name__ == "__main__":
Note: See TracChangeset
for help on using the changeset viewer.