Changeset 6428 for branches/numpy/anuga/geospatial_data/geospatial_data.py
- Timestamp:
- Feb 27, 2009, 11:54:09 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/numpy/anuga/geospatial_data/geospatial_data.py
r6410 r6428 1 """Class Geospatial_data - Manipulation of locations on the planet and2 associated attributes. 3 1 """Class Geospatial_data 2 3 Manipulation of locations on the planet and associated attributes. 4 4 """ 5 5 … … 11 11 from RandomArray import randint, seed, get_seed 12 12 from copy import deepcopy 13 13 14 from Scientific.IO.NetCDF import NetCDFFile 14 15 15 import numpy as num 16 16 … … 25 25 from anuga.config import netcdf_mode_r, netcdf_mode_w, netcdf_mode_a 26 26 from anuga.config import netcdf_float 27 27 28 28 29 DEFAULT_ATTRIBUTE = 'elevation' … … 139 140 140 141 self.set_verbose(verbose) 141 self.geo_reference = None # create the attribute142 self.geo_reference = None 142 143 self.file_name = file_name 143 144 … … 148 149 149 150 if file_name is None: 150 if latitudes is not None \ 151 or longitudes is not None \ 152 or points_are_lats_longs: 151 if (latitudes is not None or longitudes is not None 152 or points_are_lats_longs): 153 153 data_points, geo_reference = \ 154 154 _set_using_lat_long(latitudes=latitudes, … … 156 156 geo_reference=geo_reference, 157 157 data_points=data_points, 158 points_are_lats_longs= \159 points_are_lats_longs)158 points_are_lats_longs= 159 points_are_lats_longs) 160 160 self.check_data_points(data_points) 161 161 self.set_attributes(attributes) … … 213 213 msg = 'There is no data or file provided!' 214 214 raise ValueError, msg 215 216 215 else: 217 216 self.data_points = ensure_numeric(data_points) … … 257 256 """ 258 257 259 from anuga.coordinate_transforms.geo_reference import Geo_reference260 261 258 if geo_reference is None: 262 259 # Use default - points are in absolute coordinates … … 269 266 # FIXME (Ole): This exception will be raised even 270 267 # if geo_reference is None. Is that the intent Duncan? 271 msg = 'Argument geo_reference must be a valid Geo_reference '272 msg += 'object or None.'268 msg = ('Argument geo_reference must be a valid Geo_reference ' 269 'object or None.') 273 270 raise Expection, msg 274 271 … … 378 375 # @param isSouthHemisphere If True, return lat/lon points in S.Hemi. 379 376 # @return A set of data points, in appropriate form. 380 def get_data_points(self, absolute=True, geo_reference=None, 381 as_lat_long=False, isSouthHemisphere=True): 377 def get_data_points(self, 378 absolute=True, 379 geo_reference=None, 380 as_lat_long=False, 381 isSouthHemisphere=True): 382 382 """Get coordinates for all data points as an Nx2 array 383 383 … … 460 460 # @return The new geospatial object. 461 461 def __add__(self, other): 462 """Returns the addition of 2 geospati cal objects,462 """Returns the addition of 2 geospatial objects, 463 463 objects are concatencated to the end of each other 464 464 … … 488 488 if self.attributes is None: 489 489 if other.attributes is not None: 490 msg = 'Geospatial data must have the same '491 msg += 'attributes to allow addition.'490 msg = ('Geospatial data must have the same ' 491 'attributes to allow addition.') 492 492 raise Exception, msg 493 493 … … 499 499 attrib1 = self.attributes[x] 500 500 attrib2 = other.attributes[x] 501 new_attributes[x] = num.concatenate((attrib1, attrib2)) 501 new_attributes[x] = num.concatenate((attrib1, attrib2), 502 axis=0) #??default# 502 503 else: 503 msg = 'Geospatial data must have the same \n'504 msg += 'attributes to allow addition.'504 msg = ('Geospatial data must have the same ' 505 'attributes to allow addition.') 505 506 raise Exception, msg 506 507 else: … … 523 524 return self + other 524 525 525 526 527 526 ################################################################################ 527 # IMPORT/EXPORT POINTS FILES 528 ################################################################################ 528 529 529 530 ## … … 560 561 except IOError, e: 561 562 # This should only be if a file is not found 562 msg = 'Could not open file %s. ' % file_name563 msg += 'Check the file location.'563 msg = ('Could not open file %s. Check the file location.' 564 % file_name) 564 565 raise IOError, msg 565 566 except SyntaxError, e: 566 567 # This should only be if there is a format error 567 msg = 'Problem with format of file %s. \n' %file_name568 msg += Error_message['IOError']568 msg = ('Problem with format of file %s.\n%s' 569 % (file_name, Error_message['IOError'])) 569 570 raise SyntaxError, msg 570 571 else: 571 msg = 'Extension %s is unknown' % file_name[-4:]572 msg = 'Extension %s is unknown' % file_name[-4:] 572 573 raise IOError, msg 573 574 … … 614 615 self.get_all_attributes(), 615 616 self.get_geo_reference()) 616 617 617 elif file_name[-4:] == ".txt" or file_name[-4:] == ".csv": 618 618 msg = "ERROR: trying to write a .txt file with relative data." … … 621 621 self.get_data_points(absolute=True, 622 622 as_lat_long=as_lat_long, 623 isSouthHemisphere=isSouthHemisphere),623 isSouthHemisphere=isSouthHemisphere), 624 624 self.get_all_attributes(), 625 625 as_lat_long=as_lat_long) 626 627 626 elif file_name[-4:] == ".urs" : 628 627 msg = "ERROR: Can not write a .urs file as a relative file." … … 630 629 _write_urs_file(file_name, 631 630 self.get_data_points(as_lat_long=True, 632 isSouthHemisphere=isSouthHemisphere)) 633 631 isSouthHemisphere=isSouthHemisphere)) 634 632 else: 635 633 msg = 'Unknown file type %s ' %file_name … … 694 692 random_list = [] 695 693 remainder_list = [] 696 new_size = round(factor *self_size)694 new_size = round(factor * self_size) 697 695 698 696 # Find unique random numbers … … 713 711 # Set seed if provided, mainly important for unit test! 714 712 # plus recalcule seed when no seed provided. 715 if seed_num !=None:713 if seed_num is not None: 716 714 seed(seed_num, seed_num) 717 715 else: … … 734 732 735 733 # pops array index (random_num) from remainder_list 736 # (which starts as the 737 # total_list and appends to random_list 734 # (which starts as the total_list and appends to random_list) 738 735 random_num_len = len(random_num) 739 736 for i in random_num: … … 746 743 747 744 # FIXME: move to tests, it might take a long time 748 # then create an array of random leng htbetween 500 and 1000,745 # then create an array of random length between 500 and 1000, 749 746 # and use a random factor between 0 and 1 750 747 # setup for assertion … … 752 749 test_total.extend(remainder_list) 753 750 test_total.sort() 754 msg = 'The two random lists made from the original list when added ' \755 'together DO NOT equal the original list'751 msg = ('The two random lists made from the original list when added ' 752 'together DO NOT equal the original list') 756 753 assert total_list == test_total, msg 757 754 … … 770 767 file pointer position 771 768 """ 772 from Scientific.IO.NetCDF import NetCDFFile 769 773 770 # FIXME - what to do if the file isn't there 774 771 … … 799 796 self.number_of_blocks = self.number_of_points/self.max_read_lines 800 797 # This computes the number of full blocks. The last block may be 801 # smaller and won't be i rcluded in this estimate.798 # smaller and won't be included in this estimate. 802 799 803 800 if self.verbose is True: 804 print 'Reading %d points (in ~%d blocks) from file %s. ' \ 805 % (self.number_of_points, 806 self.number_of_blocks, 807 self.file_name), 808 print 'Each block consists of %d data points' \ 809 % self.max_read_lines 810 801 print ('Reading %d points (in ~%d blocks) from file %s. ' 802 % (self.number_of_points, self.number_of_blocks, 803 self.file_name)), 804 print ('Each block consists of %d data points' 805 % self.max_read_lines) 811 806 else: 812 807 # Assume the file is a csv file … … 839 834 840 835 if self.verbose is True: 841 if self.show_verbose >= self.start_row \ 842 and self.show_verbose < fin_row: 843 print 'Reading block %d (points %d to %d) out of %d'\ 844 %(self.block_number, 845 self.start_row, 846 fin_row, 847 self.number_of_blocks) 836 if (self.show_verbose >= self.start_row 837 and self.show_verbose < fin_row): 838 print ('Reading block %d (points %d to %d) out of %d' 839 % (self.block_number, self.start_row, 840 fin_row, self.number_of_blocks)) 848 841 849 842 self.show_verbose += max(self.max_read_lines, 850 843 self.verbose_block_size) 851 852 844 853 845 # Read next block … … 861 853 862 854 self.block_number += 1 863 864 855 else: 865 856 # Assume the file is a csv file … … 868 859 att_dict, 869 860 geo_ref, 870 self.file_pointer) = \871 _read_csv_file_blocking(self.file_pointer,872 self.header[:],873 max_read_lines=\874 self.max_read_lines,875 verbose=self.verbose)861 self.file_pointer) = _read_csv_file_blocking(self.file_pointer, 862 self.header[:], 863 max_read_lines= \ 864 self.max_read_lines, 865 verbose= \ 866 self.verbose) 876 867 877 868 # Check that the zones haven't changed. … … 880 871 self.blocking_georef = geo_ref 881 872 elif self.blocking_georef is not None: 882 msg = 'Geo reference given, then not given.'883 msg += ' This should not happen.'873 msg = ('Geo reference given, then not given.' 874 ' This should not happen.') 884 875 raise ValueError, msg 885 876 geo = Geospatial_data(pointlist, att_dict, geo_ref) … … 899 890 del self.file_pointer 900 891 # This should only be if there is a format error 901 msg = 'Could not open file %s. \n' % self.file_name902 msg += Error_message['IOError']892 msg = ('Could not open file %s.\n%s' 893 % (self.file_name, Error_message['IOError'])) 903 894 raise SyntaxError, msg 904 895 return geo 905 906 896 907 897 ##################### Error messages ########### 908 898 Error_message = {} 909 899 Em = Error_message 910 Em['IOError'] = "NOTE: The format for a comma separated .txt/.csv file is:\n"911 Em['IOError'] += " 1st line: [column names]\n" 912 Em['IOError'] += " other lines: [x value], [y value], [attributes]\n" 913 Em['IOError'] += "\n" 914 Em['IOError'] += " for example:\n" 915 Em['IOError'] += " x, y, elevation, friction\n" 916 Em['IOError'] += " 0.6, 0.7, 4.9, 0.3\n" 917 Em['IOError'] += " 1.9, 2.8, 5, 0.3\n" 918 Em['IOError'] += " 2.7, 2.4, 5.2, 0.3\n" 919 Em['IOError'] += "\n" 920 Em['IOError'] += "The first two columns are assumed to be x, y coordinates.\n" 921 Em['IOError'] += "The attribute values must be numeric.\n" 900 Em['IOError'] = ('NOTE: The format for a comma separated .txt/.csv file is:\n' 901 ' 1st line: [column names]\n' 902 ' other lines: [x value], [y value], [attributes]\n' 903 '\n' 904 ' for example:\n' 905 ' x, y, elevation, friction\n' 906 ' 0.6, 0.7, 4.9, 0.3\n' 907 ' 1.9, 2.8, 5, 0.3\n' 908 ' 2.7, 2.4, 5.2, 0.3\n' 909 '\n' 910 'The first two columns are assumed to be x, y coordinates.\n' 911 'The attribute values must be numeric.\n') 922 912 923 913 ## … … 936 926 937 927 if geo_reference is not None: 938 msg = "A georeference is specified yet latitude and longitude " \939 "are also specified!"928 msg = ('A georeference is specified yet latitude and longitude ' 929 'are also specified!') 940 930 raise ValueError, msg 941 931 942 932 if data_points is not None and not points_are_lats_longs: 943 msg = "Data points are specified yet latitude and longitude are " \944 "also specified."933 msg = ('Data points are specified yet latitude and longitude are ' 934 'also specified.') 945 935 raise ValueError, msg 946 936 … … 983 973 dic['attributelist']['elevation'] = [[7.0,5.0]] 984 974 """ 985 986 from Scientific.IO.NetCDF import NetCDFFile987 975 988 976 if verbose: print 'Reading ', file_name … … 1127 1115 if len(header) != len(numbers): 1128 1116 file_pointer.close() 1129 msg = "File load error. " \1130 "There might be a problem with the file header."1117 msg = ('File load error. ' 1118 'There might be a problem with the file header.') 1131 1119 raise SyntaxError, msg 1132 1120 for i,n in enumerate(numbers): 1133 1121 n.strip() 1134 1122 if n != '\n' and n != '': 1135 #attributes.append(float(n))1136 1123 att_dict.setdefault(header[i],[]).append(float(n)) 1137 1124 except ValueError: … … 1174 1161 # @note Will throw IOError and AttributeError exceptions. 1175 1162 def _read_pts_file_header(fid, verbose=False): 1176 """Read the geo_reference and number_of_points from a .pts file"""1163 '''Read the geo_reference and number_of_points from a .pts file''' 1177 1164 1178 1165 keys = fid.variables.keys() … … 1202 1189 # @return Tuple of (pointlist, attributes). 1203 1190 def _read_pts_file_blocking(fid, start_row, fin_row, keys): 1204 """Read the body of a .csv file."""1191 '''Read the body of a .csv file.''' 1205 1192 1206 1193 pointlist = num.array(fid.variables['points'][start_row:fin_row]) … … 1240 1227 """ 1241 1228 1242 from Scientific.IO.NetCDF import NetCDFFile1243 1244 1229 # NetCDF file definition 1245 1230 outfile = NetCDFFile(file_name, netcdf_mode_w) … … 1256 1241 1257 1242 # Variable definition 1258 outfile.createVariable('points', netcdf_float, ('number_of_points',1259 1243 outfile.createVariable('points', netcdf_float, 1244 ('number_of_points', 'number_of_dimensions')) 1260 1245 1261 1246 # create variables … … 1385 1370 """Convert points_dictionary to geospatial data object""" 1386 1371 1387 msg = 'Points dictionary must have key pointlist'1372 msg = "Points dictionary must have key 'pointlist'" 1388 1373 assert points_dictionary.has_key('pointlist'), msg 1389 1374 1390 msg = 'Points dictionary must have key attributelist'1375 msg = "Points dictionary must have key 'attributelist'" 1391 1376 assert points_dictionary.has_key('attributelist'), msg 1392 1377 … … 1398 1383 return Geospatial_data(points_dictionary['pointlist'], 1399 1384 points_dictionary['attributelist'], 1400 geo_reference =geo)1385 geo_reference=geo) 1401 1386 1402 1387 … … 1436 1421 # Sort of geo_reference and convert points 1437 1422 if geo_reference is None: 1438 geo = None # Geo_reference()1423 geo = None # Geo_reference() 1439 1424 else: 1440 1425 if isinstance(geo_reference, Geo_reference): … … 1572 1557 from anuga.fit_interpolate.benchmark_least_squares import mem_usage 1573 1558 1574 1575 1559 attribute_smoothed = 'elevation' 1576 1560 … … 1579 1563 mesh_file = 'temp.msh' 1580 1564 1581 if north_boundary is None or south_boundary is None \1582 or east_boundary is None or west_boundary is None:1565 if (north_boundary is None or south_boundary is None 1566 or east_boundary is None or west_boundary is None): 1583 1567 no_boundary = True 1584 1568 else: … … 1589 1573 raise Expection, msg 1590 1574 1591 poly_topo = [[east_boundary, south_boundary],1592 [east_boundary, north_boundary],1593 [west_boundary, north_boundary],1594 [west_boundary, south_boundary]]1575 poly_topo = [[east_boundary, south_boundary], 1576 [east_boundary, north_boundary], 1577 [west_boundary, north_boundary], 1578 [west_boundary, south_boundary]] 1595 1579 1596 1580 create_mesh_from_regions(poly_topo, … … 1622 1606 if alpha_list == None: 1623 1607 alphas = [0.001,0.01,100] 1624 #alphas = [0.000001, 0.00001, 0.0001, 0.001, 0.01, \1625 #0.1, 1.0, 10.0, 100.0,1000.0,10000.0]1608 #alphas = [0.000001, 0.00001, 0.0001, 0.001, 0.01, 1609 # 0.1, 1.0, 10.0, 100.0,1000.0,10000.0] 1626 1610 else: 1627 1611 alphas = alpha_list … … 1647 1631 # add G_other data to domains with different alphas 1648 1632 if verbose: 1649 print '\n Calculating domain and mesh for Alpha =', alpha, '\n'1633 print '\nCalculating domain and mesh for Alpha =', alpha, '\n' 1650 1634 domain = Domain(mesh_file, use_cache=cache, verbose=verbose) 1651 1635 if verbose: print domain.statistics() … … 1671 1655 sample_cov = cov(elevation_sample) 1672 1656 ele_cov = cov(elevation_sample - elevation_predicted) 1673 normal_cov[i,:] = [alpha, ele_cov / sample_cov]1657 normal_cov[i,:] = [alpha, ele_cov / sample_cov] 1674 1658 1675 1659 if verbose: … … 1694 1678 print 'Final results:' 1695 1679 for i, alpha in enumerate(alphas): 1696 print 'covariance for alpha %s = %s ' \1697 % (normal_cov[i][0], normal_cov[i][1])1698 print '\n Optimal alpha is: %s ' \1699 % normal_cov_new[(num.argmin(normal_cov_new, axis=0))[1], 0]1680 print ('covariance for alpha %s = %s ' 1681 % (normal_cov[i][0], normal_cov[i][1])) 1682 print ('\nOptimal alpha is: %s ' 1683 % normal_cov_new[(num.argmin(normal_cov_new, axis=0))[1], 0]) 1700 1684 1701 1685 # covariance and optimal alpha … … 1779 1763 from anuga.fit_interpolate.benchmark_least_squares import mem_usage 1780 1764 1781 1782 1765 attribute_smoothed = 'elevation' 1783 1766 … … 1785 1768 mesh_file = 'temp.msh' 1786 1769 1787 if north_boundary is None or south_boundary is None \1788 or east_boundary is None or west_boundary is None:1770 if (north_boundary is None or south_boundary is None 1771 or east_boundary is None or west_boundary is None): 1789 1772 no_boundary = True 1790 1773 else: … … 1795 1778 raise Expection, msg 1796 1779 1797 poly_topo = [[east_boundary, south_boundary],1798 [east_boundary, north_boundary],1799 [west_boundary, north_boundary],1800 [west_boundary, south_boundary]]1780 poly_topo = [[east_boundary, south_boundary], 1781 [east_boundary, north_boundary], 1782 [west_boundary, north_boundary], 1783 [west_boundary, south_boundary]] 1801 1784 1802 1785 create_mesh_from_regions(poly_topo, … … 1822 1805 points = G_small.get_data_points() 1823 1806 1824 # FIXME: Remove points outside boundary polygon1825 # print 'new point',len(points)1826 #1827 # new_points=[]1828 # new_points=array([],dtype=float)1829 # new_points=resize(new_points,(len(points),2))1830 # print "BOUNDARY", boundary_poly1831 # for i,point in enumerate(points):1832 # if is_inside_polygon(point,boundary_poly, verbose=True):1833 # new_points[i] = point1834 # print"WOW",i,new_points[i]1835 # points = new_points1836 1837 1807 if verbose: print "Number of points in sample to compare: ", len(points) 1838 1808 1839 1809 if alpha_list == None: 1840 1810 alphas = [0.001,0.01,100] 1841 #alphas = [0.000001, 0.00001, 0.0001, 0.001, 0.01, \1842 #0.1, 1.0, 10.0, 100.0,1000.0,10000.0]1811 #alphas = [0.000001, 0.00001, 0.0001, 0.001, 0.01, 1812 # 0.1, 1.0, 10.0, 100.0,1000.0,10000.0] 1843 1813 else: 1844 1814 alphas = alpha_list … … 1851 1821 # add G_other data to domains with different alphas 1852 1822 if verbose: 1853 print '\n Calculating domain and mesh for Alpha =', alpha, '\n'1823 print '\nCalculating domain and mesh for Alpha =', alpha, '\n' 1854 1824 domain = Domain(mesh_file, use_cache=cache, verbose=verbose) 1855 1825 if verbose: print domain.statistics() … … 1878 1848 if verbose: 1879 1849 print 'Determine difference between predicted results and actual data' 1850 1880 1851 for i, alpha in enumerate(domains): 1881 1852 if verbose: print'Alpha =', alpha
Note: See TracChangeset
for help on using the changeset viewer.