overwrite=True) else: r.in_gdal(input=Settings.flow_weights, output=flow_weights, overwrite=True) r.mapcalc(flow + ' = ' + cellArea_meters2 * flow_weights, overwrite=True) # Hydrologic correction r.hydrodem(input=DEM_original_import, output=DEM, flags='a', overwrite=True) # No offmap flow r.watershed(elevation=DEM, flow=flow, accumulation=accumulation, flags='s', overwrite=True) r.mapcalc(accumulation_onmap + ' = if(' + accumulation + '>0,' + accumulation + ',null())', overwrite=True) r.mapcalc('tmp' + ' = if(isnull(' + accumulation_onmap + '),null(),' + DEM + ')', overwrite=True) g.rename(raster=('tmp', DEM), overwrite=True) # Ensure that null cells are shared -- should be unnecessary! r.mapcalc(accumulation_onmap + ' = if(isnull(' + DEM + '),null(),' + accumulation_onmap + ')', overwrite=True) # Repeat is sometimes needed r.mapcalc(DEM + ' = if(isnull(' + accumulation_onmap + '),null(),' + DEM +
# Much of this will depend on experiment DEMs = gscript.parse_command('g.list', type='raster', pattern='*__DEM__*').keys() DEMs = sorted(DEMs) for DEM in DEMs: r.patch(input='boundaries,'+DEM, output='tmp', overwrite=True) drainarray.read('tmp') scanName = DEM.split('__DEM__')[0] mainThalweg = scanName + '__main_thalweg__' tribThalweg = scanName + '__trib_thalweg__' # Main channel #start_x = margin_left/1000. #start_y = _y[:,1][drainarray[:,1] == np.min(drainarray[:,1])] flowIn = garray.array() flowIn[:,2][drainarray[:,2] < (np.min(drainarray[:,2])+.01)] = 1 flowIn.write('tmpFlowIn', overwrite=True) r.watershed(elevation='tmp', flow='tmpFlowIn', threshold=np.sum(flowIn), stream='tmpStream', accumulation='tmpAccum', flags='s', overwrite=True) r.mapcalc('tmpStreamZ = (tmpStream * 0 + 1) * tmp', overwrite=True) r.to_vect(input='tmpStreamZ', output='tmpStreamLine', type='line', overwrite=True) r.to_vect(input='tmpStreamZ', output='tmpStreamPoints', type='point', column='z', overwrite=True) v.db_addcolumn(map='tmpStreamPoints', columns='x double precision, y double precision') v.to_db(map='tmpStreamPoints', option='coor', columns='x,y') """ # Tributary channel start_x = _x[drainarray[-2,:] == np.min(drainarray[-2,:])] # CHECK INDEXING (TOP/BOTTOM) if len(start_x) > 0: start_x = start_x[0] # ARBITRARY, SHOULD FIX SOMETIME, PROBABLY NOT IMPORTANT THOUGH. startpoint = str(start_x)+','+str(margin_bottom)/1000. r.drain(input='tmp', drain=tribThalweg, start_coordinates=startpoint) """
slope='tmp', format='percent', overwrite=True) r.mapcalc('slope = tmp/100.', overwrite=True) # Assuming you work in meters. ASSUMING! # Have to include output name or write to a temporary file, FOR ALL! r.mapcalc('cellArea_meters2 = ' + str(reg.nsres) + ' * ' + str(reg.ewres), overwrite=True) r.mapcalc("cellArea_km2 = cellArea_meters2 / 10^6", overwrite=True) print "Running r.watershed" r.watershed(elevation=elevation, flow='cellArea_meters2', accumulation='drainageArea_m2', drainage='drainageDirection', stream='streams', threshold=thresh, flags='s', overwrite=True) # for irregular outlines on topo r.mapcalc("drainageArea_m2 = drainageArea_m2 + 0*" + elevation, overwrite=True) #r.watershed(elevation=elevation, flow='cellArea_km2', accumulation='drainageArea_km2', drainage='drainageDirection', stream='streams', threshold=thresh, flags='s', overwrite=True) # Remove areas of negative (offmap) accumulation #r.mapcalc('drainageArea_km2 = drainageArea_km2 * (drainageArea_km2 > 0)', overwrite=True) #r.null(map='drainageArea_km2', setnull=0) #r.mapcalc(elevation+" = "+elevation+"*drainageArea_km2*0", overwrite=True) # Get watershed print "Building drainage network" r.stream_extract(elevation=elevation, accumulation='drainageArea_m2',
for DEM in DEMs: print DEM r.patch(input='boundaries,' + DEM, output='tmp', overwrite=True) drainarray.read('tmp') # Main channel start_x = margin_left / 1000. + .1 start_y = _y[:, 1][drainarray[:, 1] == np.min(drainarray[:, 1])] flowIn = garray.array() flowIn[:] = ((_x <= (margin_left / 1000. + .01)) * (_x > _x[0, 1])) * (_y >= 1.28) * (_y <= 1.30) flowIn.write('tmpFlowIn', overwrite=True) # Must fix here: some cells on wall at boundary r.watershed(elevation='tmp', flow='tmpFlowIn', threshold=np.sum(flowIn), stream='tmpStream', accumulation='tmpAccum', flags='s', quiet=True, overwrite=True) r.mapcalc('tmpStreamZ = (tmpStream * 0 + 1) * tmp', quiet=True, overwrite=True) r.to_vect(input='tmpStreamZ', output='channel_centerline_' + DEM.split('_')[-1], type='line', quiet=True, overwrite=True) """ for DEM in DEMs: print DEM g.rename(vector=['Line__'+DEM,'channel_centerline_'+DEM.split('_')[-1]])
if DEM_input != '': # Import DEM and set region r.in_gdal(input=DEM_input, output=DEM_original_import) g.region(raster=DEM_original_import) # Build flow accumulation with only fully on-map flow # Cell areas r.cell_area(output=cellArea_meters2, units='m2', overwrite=True) # Hydrologic correction r.hydrodem(input=DEM_original_import, output=DEM, flags='a', overwrite=True) # No offmap flow r.watershed(elevation=DEM, flow=cellArea_meters2, accumulation=accumulation, flags='m', overwrite=True) r.mapcalc(accumulation_onmap + ' = ' + accumulation + ' * (' + accumulation + ' > 0)', overwrite=True) r.null(map=accumulation_onmap, setnull=0) r.mapcalc(DEM + ' = if(isnull(' + accumulation_onmap + '),null(),' + DEM + ')', overwrite=True) # Ensure that null cells are shared r.mapcalc(accumulation_onmap + ' = if(isnull(' + DEM + '),null(),' + accumulation_onmap + ')', overwrite=True) # Repeat is sometimes needed r.mapcalc(DEM + ' = if(isnull(' + accumulation_onmap + '),null(),' + DEM +