Пример #1
0
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
Пример #2
0
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]
Пример #3
0
def main(options, flags):
    #############################################################
    # inizialitation
    #############################################################
    pid = os.getpid()
    DEBUG = flags["d"]
    atexit.register(cleanup, pattern=("tmprgreen_%i*" % pid), debug=DEBUG)
    # TOD_add the possibilities to have q_points
    # required
    discharge = options["discharge"]
    dtm = options["elevation"]
    # basin potential
    basins = options["basins"]
    stream = options["stream"]  # raster
    rivers = options["rivers"]  # vec
    lakes = options["lakes"]  # vec
    E = options["output"]
    threshold = options["threshold"]

    # existing plants
    #    segments = options['segments']
    #    output_segm = options['output_segm']
    #    output_point = options['output_point']
    #    hydro = options['hydro']
    #    hydro_layer = options['hydro_layer']
    #    hydro_kind_intake = options['hydro_kind_intake']
    #    hydro_kind_turbine = options['hydro_kind_turbine']
    #    other = options['other']
    #    other_kind_intake = options['other_kind_intake']
    #    other_kind_turbine = options['other_kind_turbine']

    # optional

    msgr = get_msgr()
    # info = gcore.parse_command('g.region', flags='m')
    # print info
    #############################################################
    # check temporary raster

    if rivers:
        # cp the vector in the current mapset in order to clean it
        tmp_river = "tmprgreen_%i_river" % pid
        to_copy = "%s,%s" % (rivers, tmp_river)
        gcore.run_command("g.copy", vector=to_copy)
        rivers = tmp_river
        gcore.run_command("v.build", map=rivers)
        tmp_dtm_corr = "tmprgreen_%i_dtm_corr" % pid
        dtm_corr(dtm, rivers, tmp_dtm_corr, lakes)
        basins, stream = basin.check_compute_basin_stream(
            basins, stream, tmp_dtm_corr, threshold)
    else:
        basins, stream = basin.check_compute_basin_stream(
            basins, stream, dtm, threshold)

    perc_overlay = check_overlay_rr(discharge, stream)
    # pdb.set_trace()
    try:
        p = float(perc_overlay)
        if p < 90:
            warn = ("Discharge map doesn't overlay all the stream map."
                    "It covers only the %s %% of rivers") % (perc_overlay)
            msgr.warning(warn)
    except ValueError:
        msgr.error("Could not convert data to a float")
    except:
        msgr.error("Unexpected error")

    msgr.message("\Init basins\n")
    # pdb.set_trace()
    #############################################################
    # core
    #############################################################
    basins_tot, inputs = basin.init_basins(basins)

    msgr.message("\nBuild the basin network\n")
    #############################################################
    # build_network(stream, dtm, basins_tot) build relationship among basins
    # Identify the closure point with the r.neigbours module
    # if the difference betwwen the stream and the neighbours
    # is negative it means a new subsanin starts
    #############################################################
    basin.build_network(stream, dtm, basins_tot)
    stream_n = raster2numpy(stream)
    discharge_n = raster2numpy(discharge)
    basin.fill_basins(inputs, basins_tot, basins, dtm, discharge_n, stream_n)

    ###################################################################
    # check if lakes and delate stream in lakes optional
    ###################################################################

    if lakes:
        remove_pixel_from_raster(lakes, stream)

    ####################################################################
    # write results
    ####################################################################
    # if not rivers or debug I write the result in the new vector stream
    msgr.message("\nWrite results\n")

    basin.write_results2newvec(stream, E, basins_tot, inputs)
    power2energy(E, "Etot_kW", 8760)