Changeset 2009 for inundation/pyvolution/data_manager.py
- Timestamp:
- Nov 7, 2005, 2:21:12 PM (19 years ago)
- File:
-
- 1 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
Note: See TracChangeset
for help on using the changeset viewer.