Changeset 2643
- Timestamp:
- Mar 31, 2006, 4:18:50 PM (19 years ago)
- Location:
- inundation/geospatial_data
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/geospatial_data/geospatial_data.py
r2624 r2643 23 23 absolute = True): 24 24 25 """Create instance from data points and associated attributes 25 26 """ 27 Create instance from data points and associated attributes 26 28 27 29 … … 45 47 If None, the default is the 'first' 46 48 47 file_name: Name of input file..... 48 49 absolute: is the data point absolute or relative to a xll and yll 50 in a zone 51 52 """ 49 file_name: Name of input netCDF file or xya file. netCDF file must 50 have dimensions "points" etc. 51 xya file is a CVS file with lats(x), longs(y) and elevation(a). 52 first line must be attribute name eg elevation 53 54 absolute: Return data points that are absolute or relative to 55 to a xll and yll in a zone 56 57 """ 58 53 59 54 60 if file_name is None: … … 60 66 self.set_default_attribute_name(default_attribute_name) 61 67 self.set_absolute(absolute) 62 63 68 64 69 else: … … 71 76 # if file name then all provided info will be removed! 72 77 self.file_name = file_name 73 data = import_points_file(self.file_name)78 self.new_import_points_file(file_name) 74 79 75 # print 'data: point in init', data['pointlist'] 76 # print 'attrib: point in init', data['attributelist'] 77 # print 'geo_ref: point in init', data['geo_reference'] 78 79 data_points = data['pointlist'] 80 attributes = data['attributelist'] 81 geo_reference = data['geo_reference'] 82 83 self.check_data_points(data_points) 84 self.set_attributes(attributes) 85 self.set_geo_reference(geo_reference) 80 # print'data: point in init',self.data_points 81 # print'attrib: point in init',self.attributes 82 # print'geo_ref: point in init',self.geo_reference 83 self.check_data_points(self.data_points) 84 self.set_attributes(self.attributes) 85 self.set_geo_reference(self.geo_reference) 86 86 self.set_default_attribute_name(default_attribute_name) 87 87 … … 366 366 """ 367 367 368 a ll_data= {}368 attributes = {} 369 369 if ofile[-4:]== ".xya": 370 370 try: … … 372 372 try: 373 373 fd = open(ofile) 374 all_data = _read_xya_file(fd, ',') 374 # all_data = _read_xya_file(fd, ',') 375 data_points, attributes, geo_reference = _new_read_xya_file(fd, ',') 375 376 except SyntaxError: 376 377 fd.close() 377 378 fd = open(ofile) 378 all_data = _read_xya_file(fd, ' ') 379 # all_data = _read_xya_file(fd, ' ') 380 data_points, attributes, geo_reference = _new_read_xya_file(fd, ' ') 379 381 else: 380 382 fd = open(ofile) 381 all_data = _read_xya_file(fd, delimiter) 383 # all_data = _read_xya_file(fd, delimiter) 384 data_points, attributes, geo_reference = _new_read_xya_file(fd, delimiter) 382 385 fd.close() 383 386 except (IndexError,ValueError,SyntaxError): … … 393 396 try: 394 397 # print 'hi from import_points_file' 395 all_data = _read_pts_file(ofile, verbose) 398 # all_data = _read_pts_file(ofile, verbose) 399 data_points, attributes, geo_reference = _new_read_pts_file(ofile, verbose) 396 400 # print 'hi1 from import_points_file', all_data 397 401 except IOError, e: … … 401 405 msg = 'Extension %s is unknown' %ofile[-4:] 402 406 raise IOError, msg 403 404 return all_data 407 408 # print'in import data_points', data_points 409 # print'in import attributes', attributes 410 # print'in import data_points', geo_reference 411 self.data_points = data_points 412 self.attributes = attributes 413 self.geo_reference = geo_reference 414 415 # return all_data 405 416 406 417 def new_export_points_file(self, ofile): … … 491 502 raise IOError, msg 492 503 ''' 493 def _read_pts_file(file_name, verbose = False): 504 505 506 def _new_read_pts_file(file_name, verbose = False): 494 507 """Read .pts NetCDF file 495 508 … … 516 529 fid = NetCDFFile(file_name, 'r') 517 530 518 point_atts = {}531 # point_atts = {} 519 532 # Get the variables 520 point_atts['pointlist'] = array(fid.variables['points']) 533 # point_atts['pointlist'] = array(fid.variables['points']) 534 pointlist = array(fid.variables['points']) 521 535 keys = fid.variables.keys() 522 536 if verbose: print 'Got %d variables: %s' %(len(keys), keys) … … 534 548 attributes[key] = array(fid.variables[key]) 535 549 536 point_atts['attributelist'] = attributes550 # point_atts['attributelist'] = attributes 537 551 538 552 try: 539 553 geo_reference = Geo_reference(NetCDFObject=fid) 540 point_atts['geo_reference'] = geo_reference554 # point_atts['geo_reference'] = geo_reference 541 555 except AttributeError, e: 542 556 #geo_ref not compulsory 543 point_atts['geo_reference'] = None 557 # point_atts['geo_reference'] = None 558 geo_reference = None 544 559 545 560 fid.close() 546 return point_atts 547 548 549 def _read_xya_file( fd, delimiter): 561 562 return pointlist, attributes, geo_reference 563 564 565 def _new_read_xya_file( fd, delimiter): 550 566 # print 'hello from read xya data' 551 567 points = [] … … 591 607 geo_reference = Geo_reference(ASCIIFile=fd,read_title=line) 592 608 609 # xya_dict = {} 610 # xya_dict['pointlist'] = array(points).astype(Float) 611 pointlist = array(points).astype(Float) 612 613 for key in att_dict.keys(): 614 att_dict[key] = array(att_dict[key]).astype(Float) 615 # xya_dict['attributelist'] = att_dict 616 617 # xya_dict['geo_reference'] = geo_reference 618 #print "xya_dict",xya_dict 619 return pointlist, att_dict, geo_reference 620 621 622 def _read_pts_file(file_name, verbose = False): 623 """Read .pts NetCDF file 624 625 Return a dic of array of points, and dic of array of attribute 626 eg 627 dic['points'] = [[1.0,2.0],[3.0,5.0]] 628 dic['attributelist']['elevation'] = [[7.0,5.0] 629 """ 630 #FIXME: (DSG) This format has issues. 631 # There can't be an attribute called points 632 # consider format change 633 634 # print 'hi for read points file' 635 from Scientific.IO.NetCDF import NetCDFFile 636 637 if verbose: print 'Reading ', file_name 638 639 640 # see if the file is there. Throw a QUIET IO error if it isn't 641 fd = open(file_name,'r') 642 fd.close() 643 644 #throws prints to screen if file not present 645 fid = NetCDFFile(file_name, 'r') 646 647 point_atts = {} 648 # Get the variables 649 point_atts['pointlist'] = array(fid.variables['points']) 650 keys = fid.variables.keys() 651 if verbose: print 'Got %d variables: %s' %(len(keys), keys) 652 try: 653 keys.remove('points') 654 except IOError, e: 655 fid.close() 656 msg = 'Expected keyword "points" but could not find it' 657 raise IOError, msg 658 659 attributes = {} 660 for key in keys: 661 if verbose: print "reading attribute '%s'" %key 662 663 attributes[key] = array(fid.variables[key]) 664 665 point_atts['attributelist'] = attributes 666 667 try: 668 geo_reference = Geo_reference(NetCDFObject=fid) 669 point_atts['geo_reference'] = geo_reference 670 except AttributeError, e: 671 #geo_ref not compulsory 672 point_atts['geo_reference'] = None 673 674 fid.close() 675 return point_atts 676 677 678 def _read_xya_file( fd, delimiter): 679 # print 'hello from read xya data' 680 points = [] 681 pointattributes = [] 682 title = fd.readline() 683 att_names = clean_line(title,delimiter) 684 685 att_dict = {} 686 line = fd.readline() 687 numbers = clean_line(line,delimiter) 688 #print 'read xya numbers', numbers 689 while len(numbers) > 1: 690 if numbers != []: 691 try: 692 x = float(numbers[0]) 693 y = float(numbers[1]) 694 points.append([x,y]) 695 numbers.pop(0) 696 numbers.pop(0) 697 #attributes = [] 698 #print "att_names",att_names 699 #print "numbers",numbers 700 if len(att_names) != len(numbers): 701 fd.close() 702 # It might not be a problem with the title 703 #raise TitleAmountError 704 raise IOError 705 for i,num in enumerate(numbers): 706 num.strip() 707 if num != '\n' and num != '': 708 #attributes.append(float(num)) 709 att_dict.setdefault(att_names[i],[]).append(float(num)) 710 711 except ValueError: 712 raise SyntaxError 713 line = fd.readline() 714 numbers = clean_line(line,delimiter) 715 716 if line == '': 717 # end of file 718 geo_reference = None 719 else: 720 geo_reference = Geo_reference(ASCIIFile=fd,read_title=line) 721 593 722 xya_dict = {} 594 723 xya_dict['pointlist'] = array(points).astype(Float) -
inundation/geospatial_data/test_geospatial_data.py
r2624 r2643 337 337 file.close() 338 338 #print fileName 339 dict = import_points_file(fileName,delimiter = ',') 339 340 dict = import_points_file(fileName,delimiter = ',') 340 341 os.remove(fileName)
Note: See TracChangeset
for help on using the changeset viewer.