Changeset 4470
- Timestamp:
- May 21, 2007, 4:38:46 PM (18 years ago)
- Location:
- anuga_core/source/anuga
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/coordinate_transforms/geo_reference.py
r4451 r4470 287 287 other.zone = self.zone 288 288 else: 289 msg = ' Both geospatial_data objectsmust be in the same '+\289 msg = 'Geospatial data must be in the same '+\ 290 290 'ZONE to allow reconciliation. I got zone %d and %d'\ 291 291 %(self.zone, other.zone) -
anuga_core/source/anuga/geospatial_data/geospatial_data.py
r4452 r4470 463 463 if self.attributes is None: 464 464 if other.attributes is not None: 465 msg = ' Both geospatial_data objectsmust have the same \n'465 msg = 'Geospatial data must have the same \n' 466 466 msg += 'attributes to allow addition.' 467 467 raise Exception, msg … … 478 478 479 479 else: 480 msg = ' Both geospatial_data objectsmust have the same \n'480 msg = 'Geospatial data must have the same \n' 481 481 msg += 'attributes to allow addition.' 482 482 raise Exception, msg … … 492 492 493 493 def import_points_file(self, file_name, delimiter=None, verbose=False): 494 """ load an . xya or .pts file494 """ load an .txt, .csv or .xya or .pts file 495 495 Note: will throw an IOError if it can't load the file. 496 496 Catch these! … … 526 526 fd.close() 527 527 msg = 'Could not open file %s ' %file_name 528 msg += 'Check the file location.' 528 529 raise IOError, msg 529 530 except IOError, e: … … 543 544 544 545 elif file_name[-4:]== ".txt" or file_name[-4:]== ".csv": 545 #let's do ticket#116 stuff546 #547 546 try: 548 547 data_points, attributes, geo_reference =\ 549 548 _read_csv_file(file_name, verbose) 550 except IOError, e: 551 msg = 'Could not open file %s ' %file_name 552 raise IOError, msg 549 except IOError, e: 550 # This should only be if a file is not found 551 msg = 'Could not open file %s. ' %file_name 552 msg += 'Check the file location.' 553 raise IOError, msg 554 except SyntaxError, e: 555 # This should only be if there is a format error 556 msg = 'Could not open file %s. \n' %file_name 557 msg += Error_message['IOError'] 558 #print "msg", msg 559 raise SyntaxError, msg 553 560 else: 554 561 msg = 'Extension %s is unknown' %file_name[-4:] … … 823 830 del self.file_pointer 824 831 raise 832 except SyntaxError: 833 self.file_pointer.close() 834 del self.header 835 del self.file_pointer 836 # This should only be if there is a format error 837 msg = 'Could not open file %s. \n' %self.file_name 838 msg += Error_message['IOError'] 839 raise SyntaxError, msg 825 840 return geo 841 ##################### Error messages ########### 842 Error_message = {} 843 Em = Error_message 844 Em['IOError'] = "NOTE: The format for a comma seperated .txt/.csv file is:\n" 845 Em['IOError'] += " 1st line: [column names]\n" 846 Em['IOError'] += " other lines: x, y, [attributes]\n" 847 Em['IOError'] += "\n" 848 Em['IOError'] += " for example:\n" 849 Em['IOError'] += " x, y, elevation, friction\n" 850 Em['IOError'] += " 0.6, 0.7, 4.9, 0.3\n" 851 Em['IOError'] += " 1.9, 2.8, 5, 0.3\n" 852 Em['IOError'] += " 2.7, 2.4, 5.2, 0.3\n" 853 Em['IOError'] += "\n" 854 Em['IOError'] += "The first two columns are assumed to be x, y coordinates.\n" 855 Em['IOError'] += "The attributes must be numeric.\n" 826 856 827 857 def _set_using_lat_long(latitudes, … … 924 954 dic['attributelist']['elevation'] = [[7.0,5.0] 925 955 """ 926 927 #from anuga.shallow_water.data_manager import Exposure_csv928 #csv =Exposure_csv(file_name)929 956 930 957 file_pointer = open(file_name) … … 970 997 #This is to remove the x and y headers. 971 998 header = header[:] 972 x_header = header.pop(0) 973 y_header = header.pop(0) 999 try: 1000 x_header = header.pop(0) 1001 y_header = header.pop(0) 1002 except IndexError: 1003 # if there are not two columns this will occur. 1004 # eg if it is a space seperated file 1005 raise SyntaxError 974 1006 975 1007 read_lines = 0 … … 983 1015 continue 984 1016 read_lines += 1 985 if True: # remove.. #if numbers != []: 986 try: 987 x = float(numbers[0]) 988 y = float(numbers[1]) 989 points.append([x,y]) 990 numbers.pop(0) 991 numbers.pop(0) 992 if len(header) != len(numbers): 993 994 file_pointer.close() 995 # It might not be a problem with the header 996 #raise TitleAmountError 997 msg = "File load error. There might be a problem with the file header" 998 raise IOError, msg 999 for i,num in enumerate(numbers): 1000 num.strip() 1001 if num != '\n' and num != '': 1002 #attributes.append(float(num)) 1003 att_dict.setdefault(header[i],[]).append(float(num)) 1004 #except IOError: 1005 except ValueError: 1006 raise SyntaxError 1017 try: 1018 x = float(numbers[0]) 1019 y = float(numbers[1]) 1020 points.append([x,y]) 1021 numbers.pop(0) 1022 numbers.pop(0) 1023 if len(header) != len(numbers): 1024 file_pointer.close() 1025 msg = "File load error. There might be a problem with the file header" 1026 raise SyntaxError, msg 1027 for i,num in enumerate(numbers): 1028 num.strip() 1029 if num != '\n' and num != '': 1030 #attributes.append(float(num)) 1031 att_dict.setdefault(header[i],[]).append(float(num)) 1032 #except IOError: 1033 except ValueError: 1034 raise SyntaxError 1007 1035 if points == []: 1008 1036 raise StopIteration -
anuga_core/source/anuga/geospatial_data/test_geospatial_data.py
r4456 r4470 978 978 979 979 def test_load_csv_lat_long_bad_blocking(self): 980 980 """ 981 test_load_csv_lat_long_bad_blocking(self): 982 Different zones in Geo references 983 """ 981 984 fileName = tempfile.mktemp(".csv") 982 985 file = open(fileName,"w") … … 989 992 load_file_now=False) 990 993 994 #for i in results: 995 # pass 991 996 try: 992 997 for i in results: … … 1036 1041 def test_load_csv_bad(self): 1037 1042 """ test_load_csv_bad(self): 1038 space delimited 1043 header column, body column missmatch 1044 (Format error) 1039 1045 """ 1040 1046 import os … … 1053 1059 # Blocking 1054 1060 geo_list = [] 1061 #for i in results: 1062 # geo_list.append(i) 1055 1063 try: 1056 1064 for i in results: 1057 1065 geo_list.append(i) 1058 except IOError:1066 except SyntaxError: 1059 1067 pass 1060 1068 else: … … 1063 1071 os.remove(fileName) 1064 1072 1065 1073 def test_load_csv_badII(self): 1074 """ test_load_csv_bad(self): 1075 header column, body column missmatch 1076 (Format error) 1077 """ 1078 import os 1079 1080 fileName = tempfile.mktemp(".txt") 1081 file = open(fileName,"w") 1082 file.write(" x,y,elevation , speed \n\ 1083 1.0, 0.0, 10.0, 0.0\n\ 1084 0.0, 1.0, 0.0, 10\n\ 1085 1.0, 0.0 ,10.4 yeah\n") 1086 file.close() 1087 1088 results = Geospatial_data(fileName, max_read_lines=2, 1089 load_file_now=False) 1090 1091 # Blocking 1092 geo_list = [] 1093 #for i in results: 1094 # geo_list.append(i) 1095 try: 1096 for i in results: 1097 geo_list.append(i) 1098 except SyntaxError: 1099 pass 1100 else: 1101 msg = 'bad file did not raise error!' 1102 raise msg 1103 os.remove(fileName) 1104 1105 def test_load_csv_badIII(self): 1106 """ test_load_csv_bad(self): 1107 space delimited 1108 """ 1109 import os 1110 1111 fileName = tempfile.mktemp(".txt") 1112 file = open(fileName,"w") 1113 file.write(" x y elevation speed \n\ 1114 1. 0.0 10.0 0.0\n\ 1115 0.0 1.0 0.0 10.0\n\ 1116 1.0 0.0 10.4 40.0\n") 1117 file.close() 1118 1119 try: 1120 results = Geospatial_data(fileName, max_read_lines=2, 1121 load_file_now=True) 1122 except SyntaxError: 1123 pass 1124 else: 1125 msg = 'bad file did not raise error!' 1126 raise msg 1127 os.remove(fileName) 1128 1129 def test_load_csv_badIV(self): 1130 """ test_load_csv_bad(self): 1131 header column, body column missmatch 1132 (Format error) 1133 """ 1134 import os 1135 1136 fileName = tempfile.mktemp(".txt") 1137 file = open(fileName,"w") 1138 file.write(" x,y,elevation , speed \n\ 1139 1.0, 0.0, 10.0, wow\n\ 1140 0.0, 1.0, 0.0, ha\n\ 1141 1.0, 0.0 ,10.4, yeah\n") 1142 file.close() 1143 1144 results = Geospatial_data(fileName, max_read_lines=2, 1145 load_file_now=False) 1146 1147 # Blocking 1148 geo_list = [] 1149 #for i in results: 1150 # geo_list.append(i) 1151 try: 1152 for i in results: 1153 geo_list.append(i) 1154 except SyntaxError: 1155 pass 1156 else: 1157 msg = 'bad file did not raise error!' 1158 raise msg 1159 os.remove(fileName) 1160 1066 1161 def test_load_pts_blocking(self): 1067 1162 #This is pts!
Note: See TracChangeset
for help on using the changeset viewer.