Changeset 4175
- Timestamp:
- Jan 15, 2007, 12:10:20 PM (18 years ago)
- Location:
- anuga_core/source/anuga
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/fit_interpolate/fit.py
r4174 r4175 320 320 321 321 """ 322 323 322 # use blocking to load in the point info 324 323 if type(point_coordinates_or_filename) == types.StringType: … … 328 327 for i,geo_block in enumerate(Geospatial_data(filename, 329 328 max_read_lines=max_read_lines, 330 load_file_now=False)): 329 load_file_now=False, 330 verbose=verbose)): 331 331 if verbose is True and 0 == i%200: # round every 5 minutes 332 332 print 'Block %i' %i -
anuga_core/source/anuga/fit_interpolate/test_fit.py
r4174 r4175 241 241 os.remove(fileName) 242 242 243 def test_fit_to_mesh(self):244 245 a = [-1.0, 0.0]246 b = [3.0, 4.0]247 c = [4.0,1.0]248 d = [-3.0, 2.0] #3249 e = [-1.0,-2.0]250 f = [1.0, -2.0] #5251 252 vertices = [a, b, c, d,e,f]253 triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc254 255 256 fileName = tempfile.mktemp(".ddd")257 file = open(fileName,"w")258 file.write(" x,y, elevation \n\259 -2.0, 2.0, 0.\n\260 -1.0, 1.0, 0.\n\261 0.0, 2.0 , 2.\n\262 1.0, 1.0 , 2.\n\263 2.0, 1.0 ,3. \n\264 0.0, 0.0 , 0.\n\265 1.0, 0.0 , 1.\n\266 0.0, -1.0, -1.\n\267 -0.2, -0.5, -0.7\n\268 -0.9, -1.5, -2.4\n\269 0.5, -1.9, -1.4\n\270 3.0, 1.0 , 4.\n")271 file.close()272 273 f = fit_to_mesh(vertices, triangles,fileName,274 alpha=0.0, max_read_lines=2)275 #use_cache=True, verbose=True)276 answer = linear_function(vertices)277 #print "f\n",f278 #print "answer\n",answer279 assert allclose(f, answer)280 281 os.remove(fileName)282 243 283 244 def test_fit_to_mesh_pts(self): … … 314 275 geo.export_points_file(fileName_pts) 315 276 f = fit_to_mesh(vertices, triangles,fileName_pts, 316 alpha=0.0, max_read_lines=2) 277 alpha=0.0, max_read_lines=2) #, 317 278 #use_cache=True, verbose=True) 318 279 answer = linear_function(vertices) -
anuga_core/source/anuga/geospatial_data/geospatial_data.py
r4174 r4175 13 13 #from MA import tolist 14 14 15 from Scientific.IO.NetCDF import NetCDFFile 16 15 17 from anuga.utilities.numerical_tools import ensure_numeric 16 18 from anuga.coordinate_transforms.geo_reference import Geo_reference, TitleError … … 586 588 msg += 'Text file format is moving to comma seperated .txt files.' 587 589 warn(msg, DeprecationWarning) 588 error(msg, DeprecationWarning)589 590 590 591 if (file_name[-4:] == ".xya"): … … 699 700 700 701 def __iter__(self): 701 # read in the header and save the file pointer position 702 702 """ 703 read in the header and save the file pointer position 704 """ 705 706 from Scientific.IO.NetCDF import NetCDFFile 707 703 708 #FIXME - what to do if the file isn't there 704 709 705 #FIXME - give warning if the file format is .xya 706 if self.file_name[-4:]== ".xya" or self.file_name[-4:]== ".pts": 710 if self.file_name[-4:]== ".xya": 707 711 #let's just read it all 708 712 pass 713 elif self.file_name[-4:]== ".pts": 714 715 # see if the file is there. Throw a QUIET IO error if it isn't 716 fd = open(self.file_name,'r') 717 fd.close() 718 719 #throws prints to screen if file not present 720 self.fid = NetCDFFile(self.file_name, 'r') 721 722 self.blocking_georef, self.blocking_keys, self.last_row = \ 723 _read_pts_file_header(self.fid, self.verbose) 724 self.start_row=0 709 725 else: 710 726 file_pointer = open(self.file_name) … … 712 728 _read_csv_file_header(file_pointer) 713 729 714 715 730 if self.max_read_lines is None: 731 self.max_read_lines = MAX_READ_LINES 716 732 return self 717 733 718 734 def next(self): 719 735 # read a block, instanciate a new geospatial and return it 720 if self.file_name[-4:]== ".xya" or self.file_name[-4:]== ".pts":736 if self.file_name[-4:]== ".xya" : 721 737 if not hasattr(self,'finished_reading') or \ 722 738 self.finished_reading is False: … … 728 744 self.finished_reading = False 729 745 746 elif self.file_name[-4:]== ".pts": 747 if self.start_row == self.last_row: 748 # read the end of the file last iteration 749 # FIXME clean up, remove blocking atts eg 750 #self.max_read_lines 751 #self.blocking_georef,self.last_row 752 #self.start_row 753 ### self.blocking_keys 754 self.fid.close() 755 raise StopIteration 756 fin_row = self.start_row + self.max_read_lines 757 if fin_row > self.last_row: 758 fin_row = self.last_row 759 760 #call stuff 761 pointlist, att_dict, = \ 762 _read_pts_file_blocking( self.fid, 763 self.start_row, 764 fin_row, 765 self.blocking_keys 766 ) 767 geo = Geospatial_data(pointlist, att_dict, self.blocking_georef) 768 self.start_row = fin_row 769 730 770 else: 731 771 try: 732 772 pointlist, att_dict, self.file_pointer = \ 733 773 _read_csv_file_blocking( self.file_pointer, 734 self.header[:], 735 max_read_lines=self.max_read_lines) 774 self.header[:], 775 max_read_lines=self.max_read_lines, 776 verbose=self.verbose) 736 777 geo = Geospatial_data(pointlist, att_dict) 737 778 except StopIteration: … … 761 802 fid = NetCDFFile(file_name, 'r') 762 803 763 # point_atts = {}764 # Get the variables765 # point_atts['pointlist'] = array(fid.variables['points'])766 804 pointlist = array(fid.variables['points']) 767 805 keys = fid.variables.keys() … … 780 818 attributes[key] = array(fid.variables[key]) 781 819 782 # point_atts['attributelist'] = attributes783 820 784 821 try: 785 822 geo_reference = Geo_reference(NetCDFObject=fid) 786 # point_atts['geo_reference'] = geo_reference787 823 except AttributeError, e: 788 #geo_ref not compulsory789 # point_atts['geo_reference'] = None790 824 geo_reference = None 791 825 … … 895 929 return pointlist, att_dict,file_pointer 896 930 931 def _read_pts_file_header(fid, verbose=False): 932 933 """ 934 Read the geo_reference of a .pts file 935 """ 936 937 keys = fid.variables.keys() 938 try: 939 keys.remove('points') 940 except IOError, e: 941 fid.close() 942 msg = 'Expected keyword "points" but could not find it' 943 raise IOError, msg 944 if verbose: print 'Got %d variables: %s' %(len(keys), keys) 945 946 try: 947 geo_reference = Geo_reference(NetCDFObject=fid) 948 except AttributeError, e: 949 geo_reference = None 950 951 return geo_reference, keys, fid.dimensions['number_of_points'] 952 953 def _read_pts_file_blocking(fid, start_row, fin_row, keys): 954 #verbose=False): 955 956 957 """ 958 Read the body of a .csv file. 959 header: The list header of the csv file, with the x and y labels. 960 """ 961 962 pointlist = array(fid.variables['points'][start_row:fin_row]) 963 964 attributes = {} 965 for key in keys: 966 attributes[key] = array(fid.variables[key][start_row:fin_row]) 967 968 return pointlist, attributes 969 970 897 971 def _read_xya_file(fd, delimiter): 898 972 points = [] -
anuga_core/source/anuga/geospatial_data/test_geospatial_data.py
r4165 r4175 16 16 warnings.filterwarnings(action = 'ignore', 17 17 message='.xya format is deprecated. Please use .txt.', 18 category=DeprecationWarning) 19 20 warnings.filterwarnings(action = 'ignore', 21 message='The text file values must be ab', 18 22 category=DeprecationWarning) 19 23 … … 974 978 [10.4]) 975 979 976 os.remove(fileName) 980 os.remove(fileName) 977 981 978 982 def test_load_csv_bad(self): … … 1004 1008 raise msg 1005 1009 os.remove(fileName) 1006 1007 def depreciated_test_export_xya_file(self): 1010 1011 1012 def test_load_pts_blocking(self): 1013 """ test_load_csv(self): 1014 space delimited 1015 """ 1016 import os 1017 1018 fileName = tempfile.mktemp(".txt") 1019 file = open(fileName,"w") 1020 file.write(" x,y, elevation , speed \n\ 1021 1.0, 0.0, 10.0, 0.0\n\ 1022 0.0, 1.0, 0.0, 10.0\n\ 1023 1.0, 0.0 ,10.4, 40.0\n") 1024 file.close() 1025 1026 pts_file = tempfile.mktemp(".pts") 1027 1028 convert = Geospatial_data(fileName) 1029 convert.export_points_file(pts_file) 1030 results = Geospatial_data(pts_file, max_read_lines=2) 1031 1032 assert allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0], 1033 [1.0, 0.0]]) 1034 assert allclose(results.get_attributes(attribute_name='elevation'), 1035 [10.0, 0.0, 10.4]) 1036 assert allclose(results.get_attributes(attribute_name='speed'), 1037 [0.0, 10.0, 40.0]) 1038 1039 # Blocking 1040 geo_list = [] 1041 for i in results: 1042 geo_list.append(i) 1043 assert allclose(geo_list[0].get_data_points(), 1044 [[1.0, 0.0],[0.0, 1.0]]) 1045 assert allclose(geo_list[0].get_attributes(attribute_name='elevation'), 1046 [10.0, 0.0]) 1047 assert allclose(geo_list[1].get_data_points(), 1048 [[1.0, 0.0]]) 1049 assert allclose(geo_list[1].get_attributes(attribute_name='elevation'), 1050 [10.4]) 1051 1052 os.remove(fileName) 1053 os.remove(pts_file) 1054 1055 1056 def test_export_xya_file(self): 1057 # depreciated since it's testing using geo refs in a text file 1008 1058 # dict = {} 1009 1059 att_dict = {}
Note: See TracChangeset
for help on using the changeset viewer.