Changeset 2570 for inundation/geospatial_data/test_geospatial_data.py
- Timestamp:
- Mar 21, 2006, 3:51:39 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.