Changeset 1867
- Timestamp:
- Oct 5, 2005, 5:37:19 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/pyvolution/test_data_manager.py
r1865 r1867 17 17 import time 18 18 from mesh_factory import rectangular 19 20 19 21 20 #Create basic mesh … … 875 874 os.remove(ascfile) 876 875 os.remove(swwfile) 876 877 878 879 def test_sww2dem_larger(self): 880 """Test that sww information can be converted correctly to asc/prj 881 format readable by e.g. ArcView. Here: 882 883 ncols 11 884 nrows 11 885 xllcorner 308500 886 yllcorner 6189000 887 cellsize 10.000000 888 NODATA_value -9999 889 -100 -110 -120 -130 -140 -150 -160 -170 -180 -190 -200 890 -90 -100 -110 -120 -130 -140 -150 -160 -170 -180 -190 891 -80 -90 -100 -110 -120 -130 -140 -150 -160 -170 -180 892 -70 -80 -90 -100 -110 -120 -130 -140 -150 -160 -170 893 -60 -70 -80 -90 -100 -110 -120 -130 -140 -150 -160 894 -50 -60 -70 -80 -90 -100 -110 -120 -130 -140 -150 895 -40 -50 -60 -70 -80 -90 -100 -110 -120 -130 -140 896 -30 -40 -50 -60 -70 -80 -90 -100 -110 -120 -130 897 -20 -30 -40 -50 -60 -70 -80 -90 -100 -110 -120 898 -10 -20 -30 -40 -50 -60 -70 -80 -90 -100 -110 899 0 -10 -20 -30 -40 -50 -60 -70 -80 -90 -100 900 901 """ 902 903 import time, os 904 from Numeric import array, zeros, allclose, Float, concatenate 905 from Scientific.IO.NetCDF import NetCDFFile 906 907 #Setup 908 909 from mesh_factory import rectangular 910 911 #Create basic mesh (100m x 100m) 912 points, vertices, boundary = rectangular(2, 2, 100, 100) 913 914 #Create shallow water domain 915 domain = Domain(points, vertices, boundary) 916 domain.default_order = 2 917 918 domain.filename = 'datatest' 919 920 prjfile = domain.filename + '_elevation.prj' 921 ascfile = domain.filename + '_elevation.asc' 922 swwfile = domain.filename + '.sww' 923 924 domain.set_datadir('.') 925 domain.format = 'sww' 926 domain.smooth = True 927 domain.geo_reference = Geo_reference(56, 308500, 6189000) 928 929 # 930 domain.set_quantity('elevation', lambda x,y: -x-y) 931 domain.set_quantity('stage', 0) 932 933 B = Transmissive_boundary(domain) 934 domain.set_boundary( {'left': B, 'right': B, 'top': B, 'bottom': B}) 935 936 937 # 938 sww = get_dataobject(domain) 939 sww.store_connectivity() 940 sww.store_timestep('stage') 941 942 domain.evolve_to_end(finaltime = 0.01) 943 sww.store_timestep('stage') 944 945 cellsize = 10 #10m grid 946 947 948 #Check contents 949 #Get NetCDF 950 951 fid = NetCDFFile(sww.filename, 'r') 952 953 # Get the variables 954 x = fid.variables['x'][:] 955 y = fid.variables['y'][:] 956 z = fid.variables['elevation'][:] 957 time = fid.variables['time'][:] 958 stage = fid.variables['stage'][:] 959 960 961 #Export to ascii/prj files 962 sww2dem(domain.filename, 963 quantity = 'elevation', 964 cellsize = cellsize, 965 verbose = False, 966 format = 'asc') 967 #dem2pts(root, easting_min=2010.0, easting_max=2110.0, 968 # northing_min=3035.0, northing_max=3125.5) 969 970 971 972 #Check prj (meta data) 973 prjid = open(prjfile) 974 lines = prjid.readlines() 975 prjid.close() 976 977 L = lines[0].strip().split() 978 assert L[0].strip().lower() == 'projection' 979 assert L[1].strip().lower() == 'utm' 980 981 L = lines[1].strip().split() 982 assert L[0].strip().lower() == 'zone' 983 assert L[1].strip().lower() == '56' 984 985 L = lines[2].strip().split() 986 assert L[0].strip().lower() == 'datum' 987 assert L[1].strip().lower() == 'wgs84' 988 989 L = lines[3].strip().split() 990 assert L[0].strip().lower() == 'zunits' 991 assert L[1].strip().lower() == 'no' 992 993 L = lines[4].strip().split() 994 assert L[0].strip().lower() == 'units' 995 assert L[1].strip().lower() == 'meters' 996 997 L = lines[5].strip().split() 998 assert L[0].strip().lower() == 'spheroid' 999 assert L[1].strip().lower() == 'wgs84' 1000 1001 L = lines[6].strip().split() 1002 assert L[0].strip().lower() == 'xshift' 1003 assert L[1].strip().lower() == '500000' 1004 1005 L = lines[7].strip().split() 1006 assert L[0].strip().lower() == 'yshift' 1007 assert L[1].strip().lower() == '10000000' 1008 1009 L = lines[8].strip().split() 1010 assert L[0].strip().lower() == 'parameters' 1011 1012 1013 #Check asc file 1014 ascid = open(ascfile) 1015 lines = ascid.readlines() 1016 ascid.close() 1017 1018 L = lines[0].strip().split() 1019 assert L[0].strip().lower() == 'ncols' 1020 assert L[1].strip().lower() == '11' 1021 1022 L = lines[1].strip().split() 1023 assert L[0].strip().lower() == 'nrows' 1024 assert L[1].strip().lower() == '11' 1025 1026 L = lines[2].strip().split() 1027 assert L[0].strip().lower() == 'xllcorner' 1028 assert allclose(float(L[1].strip().lower()), 308500) 1029 1030 L = lines[3].strip().split() 1031 assert L[0].strip().lower() == 'yllcorner' 1032 assert allclose(float(L[1].strip().lower()), 6189000) 1033 1034 L = lines[4].strip().split() 1035 assert L[0].strip().lower() == 'cellsize' 1036 assert allclose(float(L[1].strip().lower()), cellsize) 1037 1038 L = lines[5].strip().split() 1039 assert L[0].strip() == 'NODATA_value' 1040 assert L[1].strip().lower() == '-9999' 1041 1042 #Check grid values (FIXME: Use same strategy for other sww2dem tests) 1043 for i, line in enumerate(lines[6:]): 1044 for j, value in enumerate( line.split() ): 1045 #assert allclose(float(value), -(10-i+j)*cellsize) 1046 assert float(value) == -(10-i+j)*cellsize 1047 1048 1049 fid.close() 1050 1051 #Cleanup 1052 os.remove(prjfile) 1053 os.remove(ascfile) 1054 os.remove(swwfile) 1055 1056 1057 1058 #FIXME: TODO 1059 def xtest_sww2dem_boundingbox(self): 1060 """Test that sww information can be converted correctly to asc/prj 1061 format readable by e.g. ArcView. 1062 This will test that mesh can be restricted by bounding box 1063 1064 Original extent is 100m x 100m: 1065 1066 Eastings: 308500 - 308600 1067 Northings: 6189000 - 6189100 1068 1069 Bounding box changes this to the 50m x 50m square defined by 1070 1071 Eastings: 308530 - 308570 1072 Northings: 6189050 - 6189100 1073 1074 The cropped values should be 1075 1076 -130 -140 -150 -160 -170 1077 -120 -130 -140 -150 -160 1078 -110 -120 -130 -140 -150 1079 -100 -110 -120 -130 -140 1080 -90 -100 -110 -120 -130 1081 -80 -90 -100 -110 -120 1082 1083 and the new lower reference point should be 1084 Eastings: 308530 1085 Northings: 6189050 1086 1087 Original dataset is the same as in test_sww2dem_larger() 1088 1089 """ 1090 1091 import time, os 1092 from Numeric import array, zeros, allclose, Float, concatenate 1093 from Scientific.IO.NetCDF import NetCDFFile 1094 1095 #Setup 1096 1097 from mesh_factory import rectangular 1098 1099 #Create basic mesh (100m x 100m) 1100 points, vertices, boundary = rectangular(2, 2, 100, 100) 1101 1102 #Create shallow water domain 1103 domain = Domain(points, vertices, boundary) 1104 domain.default_order = 2 1105 1106 domain.filename = 'datatest' 1107 1108 prjfile = domain.filename + '_elevation.prj' 1109 ascfile = domain.filename + '_elevation.asc' 1110 swwfile = domain.filename + '.sww' 1111 1112 domain.set_datadir('.') 1113 domain.format = 'sww' 1114 domain.smooth = True 1115 domain.geo_reference = Geo_reference(56, 308500, 6189000) 1116 1117 # 1118 domain.set_quantity('elevation', lambda x,y: -x-y) 1119 domain.set_quantity('stage', 0) 1120 1121 B = Transmissive_boundary(domain) 1122 domain.set_boundary( {'left': B, 'right': B, 'top': B, 'bottom': B}) 1123 1124 1125 # 1126 sww = get_dataobject(domain) 1127 sww.store_connectivity() 1128 sww.store_timestep('stage') 1129 1130 domain.evolve_to_end(finaltime = 0.01) 1131 sww.store_timestep('stage') 1132 1133 cellsize = 10 #10m grid 1134 1135 1136 #Check contents 1137 #Get NetCDF 1138 1139 fid = NetCDFFile(sww.filename, 'r') 1140 1141 # Get the variables 1142 x = fid.variables['x'][:] 1143 y = fid.variables['y'][:] 1144 z = fid.variables['elevation'][:] 1145 time = fid.variables['time'][:] 1146 stage = fid.variables['stage'][:] 1147 1148 1149 #Export to ascii/prj files 1150 sww2dem(domain.filename, 1151 quantity = 'elevation', 1152 cellsize = cellsize, 1153 easting_min = 308530, 1154 easting_max = 308570, 1155 northing_min = 6189050, 1156 northing_max = 6189100, 1157 verbose = False, 1158 format = 'asc') 1159 1160 1161 #Check prj (meta data) 1162 prjid = open(prjfile) 1163 lines = prjid.readlines() 1164 prjid.close() 1165 1166 L = lines[0].strip().split() 1167 assert L[0].strip().lower() == 'projection' 1168 assert L[1].strip().lower() == 'utm' 1169 1170 L = lines[1].strip().split() 1171 assert L[0].strip().lower() == 'zone' 1172 assert L[1].strip().lower() == '56' 1173 1174 L = lines[2].strip().split() 1175 assert L[0].strip().lower() == 'datum' 1176 assert L[1].strip().lower() == 'wgs84' 1177 1178 L = lines[3].strip().split() 1179 assert L[0].strip().lower() == 'zunits' 1180 assert L[1].strip().lower() == 'no' 1181 1182 L = lines[4].strip().split() 1183 assert L[0].strip().lower() == 'units' 1184 assert L[1].strip().lower() == 'meters' 1185 1186 L = lines[5].strip().split() 1187 assert L[0].strip().lower() == 'spheroid' 1188 assert L[1].strip().lower() == 'wgs84' 1189 1190 L = lines[6].strip().split() 1191 assert L[0].strip().lower() == 'xshift' 1192 assert L[1].strip().lower() == '500000' 1193 1194 L = lines[7].strip().split() 1195 assert L[0].strip().lower() == 'yshift' 1196 assert L[1].strip().lower() == '10000000' 1197 1198 L = lines[8].strip().split() 1199 assert L[0].strip().lower() == 'parameters' 1200 1201 1202 #Check asc file 1203 ascid = open(ascfile) 1204 lines = ascid.readlines() 1205 ascid.close() 1206 1207 L = lines[0].strip().split() 1208 assert L[0].strip().lower() == 'ncols' 1209 assert L[1].strip().lower() == '5' 1210 1211 L = lines[1].strip().split() 1212 assert L[0].strip().lower() == 'nrows' 1213 assert L[1].strip().lower() == '6' 1214 1215 L = lines[2].strip().split() 1216 assert L[0].strip().lower() == 'xllcorner' 1217 assert allclose(float(L[1].strip().lower()), 308530) 1218 1219 L = lines[3].strip().split() 1220 assert L[0].strip().lower() == 'yllcorner' 1221 assert allclose(float(L[1].strip().lower()), 6189050) 1222 1223 L = lines[4].strip().split() 1224 assert L[0].strip().lower() == 'cellsize' 1225 assert allclose(float(L[1].strip().lower()), cellsize) 1226 1227 L = lines[5].strip().split() 1228 assert L[0].strip() == 'NODATA_value' 1229 assert L[1].strip().lower() == '-9999' 1230 1231 #Check grid values 1232 for i, line in enumerate(lines[6:]): 1233 for j, value in enumerate( line.split() ): 1234 #assert float(value) == -(10-i+j)*cellsize 1235 assert float(value) == -(10-i+j+3)*cellsize 1236 1237 1238 fid.close() 1239 1240 #Cleanup 1241 os.remove(prjfile) 1242 os.remove(ascfile) 1243 os.remove(swwfile) 1244 877 1245 878 1246
Note: See TracChangeset
for help on using the changeset viewer.