def compute_river_discharge(drain, stream, string, **kwargs): """ Given a stream network and drainage map it computes a rester with the the given resolution with the area in km2 of the upper basin for each pixel (bas_area) and a statistic on the bas_area for another series of raster if q_spec string=sum if piedmont case kwargs-> a=a, dtm=dtm and string=mean """ msgr = get_msgr() info = gcore.parse_command("r.info", flags="g", map=stream) raster_out = {} for name, value in kwargs.items(): raster_out[name] = raster2numpy(stream) bas_area = raster2numpy(stream) river_comp = raster2compressM(stream).tocoo() count = 0 for i, j in zip(river_comp.row, river_comp.col): count = count + 1 msgr.message("\n %i \n" % count) p_x, p_y = get_coo(stream, i, j) # pdb.set_trace() coo = "%f, %f" % (p_x, p_y) gcore.run_command( "r.water.outlet", overwrite=True, input=drain, output="area_temp", coordinates=coo, ) for name, value in kwargs.items(): formula = "temp = if(not(area_temp),0, %s)" % (value) # pdb.set_trace() mapcalc(formula, overwrite=True) # pdb.set_trace() info = gcore.parse_command("r.univar", map="temp", flags="g") # pdb.set_trace() raster_out[name][i][j] = float(info[string]) bas_area[i][j] = area_of_watershed("area_temp") # pdb.set_trace() return raster_out, bas_area
def build_network(stream, dtm, basins_tot): """ Build the network of streams with the ID of all the basins in order to know the dependencies among basins """ pid = os.getpid() tmp_neighbors = "tmprgreen_%i_neighbors" % pid tmp_closure = "tmprgreen_%i_closure" % pid tmp_down = "tmprgreen_%i_down" % pid river = raster2numpy(stream) river_comp = raster2compressM(stream).tocoo() gcore.run_command( "r.neighbors", input=stream, output=tmp_neighbors, method="minimum", size="5", quantile="0.5", ) formula = "%s = %s-%s" % (tmp_closure, tmp_neighbors, stream) mapcalc(formula) # del the map down, it should be not necessary gcore.run_command("r.stats.zonal", base=stream, cover=tmp_closure, output=tmp_down, method="min") # pdb.set_trace() dtm_n = raster2numpy(dtm) clos = raster2numpy(tmp_closure) ID_down = raster2numpy(tmp_down) # pdb.set_trace() for i, j, v in zip(river_comp.row, river_comp.col, river_comp.data): up = clos[i][j] if up < 0: ID = river[i, j] basins_tot[(ID + int(ID_down[i, j]))].up.add(ID) basins_tot[ID].h_closure = dtm_n[i, j]