Changeset 2009
- Timestamp:
- Nov 7, 2005, 2:21:12 PM (19 years ago)
- Location:
- inundation/pyvolution
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/pyvolution/data_manager.py
r2007 r2009 1203 1203 1204 1204 infile.close() 1205 outfile.close() 1206 1207 1208 1209 def _read_hecras_cross_sections(lines): 1210 """Return block of surface lines for each cross section 1211 Starts with SURFACE LINE, 1212 Ends with END CROSS-SECTION 1213 """ 1214 1215 points = [] 1216 1217 reading_surface = False 1218 for i, line in enumerate(lines): 1219 1220 if len(line.strip()) == 0: #Ignore blanks 1221 continue 1222 1223 if lines[i].strip().startswith('SURFACE LINE'): 1224 reading_surface = True 1225 continue 1226 1227 if lines[i].strip().startswith('END') and reading_surface: 1228 yield points 1229 reading_surface = False 1230 points = [] 1231 1232 if reading_surface: 1233 fields = line.strip().split(',') 1234 easting = float(fields[0]) 1235 northing = float(fields[1]) 1236 elevation = float(fields[2]) 1237 points.append([easting, northing, elevation]) 1238 1239 1240 1241 1242 def hecras_cross_sections2pts(basename_in, 1243 basename_out=None, 1244 verbose=False): 1245 """Read HEC-RAS Elevation datal from the following ASCII format (.sdf) 1246 1247 Example: 1248 1249 1250 # RAS export file created on Mon 15Aug2005 11:42 1251 # by HEC-RAS Version 3.1.1 1252 1253 BEGIN HEADER: 1254 UNITS: METRIC 1255 DTM TYPE: TIN 1256 DTM: v:\1\cit\perth_topo\river_tin 1257 STREAM LAYER: c:\local\hecras\21_02_03\up_canning_cent3d.shp 1258 CROSS-SECTION LAYER: c:\local\hecras\21_02_03\up_can_xs3d.shp 1259 MAP PROJECTION: UTM 1260 PROJECTION ZONE: 50 1261 DATUM: AGD66 1262 VERTICAL DATUM: 1263 NUMBER OF REACHES: 19 1264 NUMBER OF CROSS-SECTIONS: 14206 1265 END HEADER: 1266 1267 1268 Only the SURFACE LINE data of the following form will be utilised 1269 1270 CROSS-SECTION: 1271 STREAM ID:Southern-Wungong 1272 REACH ID:Southern-Wungong 1273 STATION:19040.* 1274 CUT LINE: 1275 405548.671603161 , 6438142.7594925 1276 405734.536092045 , 6438326.10404912 1277 405745.130459356 , 6438331.48627354 1278 405813.89633823 , 6438368.6272789 1279 SURFACE LINE: 1280 405548.67, 6438142.76, 35.37 1281 405552.24, 6438146.28, 35.41 1282 405554.78, 6438148.78, 35.44 1283 405555.80, 6438149.79, 35.44 1284 405559.37, 6438153.31, 35.45 1285 405560.88, 6438154.81, 35.44 1286 405562.93, 6438156.83, 35.42 1287 405566.50, 6438160.35, 35.38 1288 405566.99, 6438160.83, 35.37 1289 ... 1290 END CROSS-SECTION 1291 1292 Convert to NetCDF pts format which is 1293 1294 points: (Nx2) Float array 1295 elevation: N Float array 1296 """ 1297 1298 #FIXME: Can this be written feasibly using write_pts? 1299 1300 import os 1301 from Scientific.IO.NetCDF import NetCDFFile 1302 from Numeric import Float, zeros, reshape 1303 1304 root = basename_in 1305 1306 #Get ASCII file 1307 infile = open(root + '.sdf', 'r') #Open SDF file for read 1308 1309 if verbose: print 'Reading DEM from %s' %(root + '.sdf') 1310 1311 lines = infile.readlines() 1312 infile.close() 1313 1314 if verbose: print 'Converting to pts format' 1315 1316 i = 0 1317 while lines[i].strip() == '' or lines[i].strip().startswith('#'): 1318 i += 1 1319 1320 assert lines[i].strip().upper() == 'BEGIN HEADER:' 1321 i += 1 1322 1323 assert lines[i].strip().upper().startswith('UNITS:') 1324 units = lines[i].strip().split()[1] 1325 i += 1 1326 1327 assert lines[i].strip().upper().startswith('DTM TYPE:') 1328 i += 1 1329 1330 assert lines[i].strip().upper().startswith('DTM:') 1331 i += 1 1332 1333 assert lines[i].strip().upper().startswith('STREAM') 1334 i += 1 1335 1336 assert lines[i].strip().upper().startswith('CROSS') 1337 i += 1 1338 1339 assert lines[i].strip().upper().startswith('MAP PROJECTION:') 1340 projection = lines[i].strip().split(':')[1] 1341 i += 1 1342 1343 assert lines[i].strip().upper().startswith('PROJECTION ZONE:') 1344 zone = int(lines[i].strip().split(':')[1]) 1345 i += 1 1346 1347 assert lines[i].strip().upper().startswith('DATUM:') 1348 datum = lines[i].strip().split(':')[1] 1349 i += 1 1350 1351 assert lines[i].strip().upper().startswith('VERTICAL DATUM:') 1352 i += 1 1353 1354 assert lines[i].strip().upper().startswith('NUMBER OF REACHES:') 1355 i += 1 1356 1357 assert lines[i].strip().upper().startswith('NUMBER OF CROSS-SECTIONS:') 1358 number_of_cross_sections = int(lines[i].strip().split(':')[1]) 1359 i += 1 1360 1361 1362 #Now read all points 1363 points = [] 1364 elevation = [] 1365 for j, entries in enumerate(_read_hecras_cross_sections(lines[i:])): 1366 for k, entry in enumerate(entries): 1367 points.append(entry[:2]) 1368 elevation.append(entry[2]) 1369 1370 1371 msg = 'Actual #number_of_cross_sections == %d, Reported as %d'\ 1372 %(j+1, number_of_cross_sections) 1373 assert j+1 == number_of_cross_sections, msg 1374 1375 #Get output file 1376 if basename_out == None: 1377 ptsname = root + '.pts' 1378 else: 1379 ptsname = basename_out + '.pts' 1380 1381 if verbose: print 'Store to NetCDF file %s' %ptsname 1382 # NetCDF file definition 1383 outfile = NetCDFFile(ptsname, 'w') 1384 1385 #Create new file 1386 outfile.institution = 'Geoscience Australia' 1387 outfile.description = 'NetCDF pts format for compact and portable ' +\ 1388 'storage of spatial point data derived from HEC-RAS' 1389 1390 #Georeferencing 1391 outfile.zone = zone 1392 outfile.xllcorner = 0.0 1393 outfile.yllcorner = 0.0 1394 outfile.false_easting = 500000 #FIXME: Use defaults from e.g. config.py 1395 outfile.false_northing = 1000000 1396 1397 outfile.projection = projection 1398 outfile.datum = datum 1399 outfile.units = units 1400 1401 1402 outfile.createDimension('number_of_points', len(points)) 1403 outfile.createDimension('number_of_dimensions', 2) #This is 2d data 1404 1405 # variable definitions 1406 outfile.createVariable('points', Float, ('number_of_points', 1407 'number_of_dimensions')) 1408 outfile.createVariable('elevation', Float, ('number_of_points',)) 1409 1410 # Get handles to the variables 1411 outfile.variables['points'][:] = points 1412 outfile.variables['elevation'][:] = elevation 1413 1205 1414 outfile.close() 1206 1415 -
inundation/pyvolution/test_data_manager.py
r2008 r2009 739 739 os.remove(root + '.asc') 740 740 os.remove(root + '.prj') 741 742 743 744 def test_hecras_cross_sections2pts(self): 745 """Test conversion from HECRAS cross sections in ascii format 746 to native NetCDF pts format 747 """ 748 749 import time, os 750 from Numeric import array, zeros, allclose, Float, concatenate 751 from Scientific.IO.NetCDF import NetCDFFile 752 753 #Write test asc file 754 root = 'hecrastest' 755 756 filename = root+'.sdf' 757 fid = open(filename, 'w') 758 fid.write(""" 759 # RAS export file created on Mon 15Aug2005 11:42 760 # by HEC-RAS Version 3.1.1 761 762 BEGIN HEADER: 763 UNITS: METRIC 764 DTM TYPE: TIN 765 DTM: v:\1\cit\perth_topo\river_tin 766 STREAM LAYER: c:\\x_local\hecras\21_02_03\up_canning_cent3d.shp 767 CROSS-SECTION LAYER: c:\\x_local\hecras\21_02_03\up_can_xs3d.shp 768 MAP PROJECTION: UTM 769 PROJECTION ZONE: 50 770 DATUM: AGD66 771 VERTICAL DATUM: 772 NUMBER OF REACHES: 19 773 NUMBER OF CROSS-SECTIONS: 2 774 END HEADER: 775 776 777 BEGIN CROSS-SECTIONS: 778 779 CROSS-SECTION: 780 STREAM ID:Southern-Wungong 781 REACH ID:Southern-Wungong 782 STATION:21410 783 CUT LINE: 784 407546.08 , 6437277.542 785 407329.32 , 6437489.482 786 407283.11 , 6437541.232 787 SURFACE LINE: 788 407546.08, 6437277.54, 52.14 789 407538.88, 6437284.58, 51.07 790 407531.68, 6437291.62, 50.56 791 407524.48, 6437298.66, 49.58 792 407517.28, 6437305.70, 49.09 793 407510.08, 6437312.74, 48.76 794 END: 795 796 CROSS-SECTION: 797 STREAM ID:Swan River 798 REACH ID:Swan Mouth 799 STATION:840.* 800 CUT LINE: 801 381178.0855 , 6452559.0685 802 380485.4755 , 6453169.272 803 SURFACE LINE: 804 381178.09, 6452559.07, 4.17 805 381169.49, 6452566.64, 4.26 806 381157.78, 6452576.96, 4.34 807 381155.97, 6452578.56, 4.35 808 381143.72, 6452589.35, 4.43 809 381136.69, 6452595.54, 4.58 810 381114.74, 6452614.88, 4.41 811 381075.53, 6452649.43, 4.17 812 381071.47, 6452653.00, 3.99 813 381063.46, 6452660.06, 3.67 814 381054.41, 6452668.03, 3.67 815 END: 816 END CROSS-SECTIONS: 817 """) 818 819 fid.close() 820 821 822 #Convert to NetCDF pts 823 hecras_cross_sections2pts(root) 824 825 #Check contents 826 #Get NetCDF 827 fid = NetCDFFile(root+'.pts', 'r') 828 829 # Get the variables 830 #print fid.variables.keys() 831 points = fid.variables['points'] 832 elevation = fid.variables['elevation'] 833 834 #Check values 835 ref_points = [[407546.08, 6437277.54], 836 [407538.88, 6437284.58], 837 [407531.68, 6437291.62], 838 [407524.48, 6437298.66], 839 [407517.28, 6437305.70], 840 [407510.08, 6437312.74]] 841 842 ref_points += [[381178.09, 6452559.07], 843 [381169.49, 6452566.64], 844 [381157.78, 6452576.96], 845 [381155.97, 6452578.56], 846 [381143.72, 6452589.35], 847 [381136.69, 6452595.54], 848 [381114.74, 6452614.88], 849 [381075.53, 6452649.43], 850 [381071.47, 6452653.00], 851 [381063.46, 6452660.06], 852 [381054.41, 6452668.03]] 853 854 855 ref_elevation = [52.14, 51.07, 50.56, 49.58, 49.09, 48.76] 856 ref_elevation += [4.17, 4.26, 4.34, 4.35, 4.43, 4.58, 4.41, 4.17, 3.99, 3.67, 3.67] 857 858 #print points[:] 859 #print ref_points 860 assert allclose(points, ref_points) 861 862 #print attributes[:] 863 #print ref_elevation 864 assert allclose(elevation, ref_elevation) 865 866 #Cleanup 867 fid.close() 868 869 870 os.remove(root + '.sdf') 871 os.remove(root + '.pts') 741 872 742 873
Note: See TracChangeset
for help on using the changeset viewer.