Ejemplo n.º 1
0
def main(options, flags):
    pid = os.getpid()
    pat = "tmprgreen_%i_*" % pid
    DEBUG = False
    atexit.register(cleanup, pattern=pat, debug=DEBUG)
    rain = options["rain"]
    q_spec = options["q_spec"]
    a_river = options["a_river"]
    q_river = options["q_river"]
    dtm = options["elevation"]
    river = options["river"]
    lakes = options["lakes"]
    threshold = options["threshold"]
    DEBUG = flags["d"]
    rf = flags["f"]  # ra flow map
    rp = flags["p"]  # rain map, piedmont formula
    k_mat = options["k_matrix"]  # raster
    m_mat = options["m_matrix"]  # raster
    a_mat = options["a_matrix"]
    new_stream = options["streams"]
    env_area = options["env_area"]
    corr_fact = options["corr_fact"]

    if not a_river:
        a_river = "tmprgreen_%i_a_river" % pid
    msgr = get_msgr()

    msgr.warning("set region to elevation raster")
    gcore.run_command("g.region", raster=dtm)

    # compute temporary DTM
    if river:
        tmp_dtm_corr = "tmprgreen_%i_dtm_corr" % pid
        dtm_corr(dtm, river, tmp_dtm_corr, lakes)
        dtm_old = dtm
        dtm = tmp_dtm_corr
    # compute the area for each cell
    tmp_acc = "tmprgreen_%i_acc" % pid
    compute_a(threshold, dtm, new_stream, a_river, tmp_acc)
    # compute q_river
    if rf:
        compute_q(threshold, q_spec, q_river, dtm)
        q_spec = "tmpgreen_%i_q_spec" % pid
        command = "%s=if(%s,%s/%s)" % (q_spec, a_river, q_river, a_river)
        mapcalc(command, overwrite=True)
    elif rp:
        if not (q_spec):
            # compute the mean elevation of the basin
            h_cum = "tmprgreen_%i_h_cum" % pid
            h_mean = "tmprgreen_%i_h_mean" % pid
            gcore.run_command(
                "r.watershed",
                elevation=dtm,
                flow=dtm_old,
                threshold=threshold,
                accumulation=h_cum,
                memory=3000,
            )
            command = "%s = %s/%s" % (h_mean, h_cum, tmp_acc)
            mapcalc(command)
            # TODO: compute the mean rain
            command = "q_spec =0.0086*%s+0.03416*%s-24.5694" % (rain, h_mean)
            q_spec = "q_spec"
            mapcalc(command)
            command = "%s = %s * %s/1000.0" % (q_river, q_spec, a_river)
            mapcalc(command)
    # compute MVF with Regione Veneto Formula
    min_flow = options["mfd"]
    if options["k_b"]:
        msgr.warning("Regione Veneto plan")
        k_b = options["k_b"]  # raster
        k_n = options["k_n"]  # raster
        regione_veneto(a_river, q_spec, k_b, k_n, min_flow)
    elif k_mat:
        msgr.warning("Piedmont Plan")
        command = "%s=%s*%s*%s*%s*%s/1000.0" % (
            min_flow,
            k_mat,
            a_river,
            q_spec,
            m_mat,
            a_mat,
        )
        mapcalc(command, overwrite=True)
    else:
        msgr.warning("No formula for the MVF")
    if corr_fact:
        # if corr_fact coumpute the environemtal flow else the minimum flow"
        command = "%s=if(not(%s), %s, %s*%s)" % (
            env_area,
            min_flow,
            corr_fact * min_flow,
        )
Ejemplo n.º 2
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)
def main(options, flags):
    pid = os.getpid()
    pat = "tmprgreen_%i_*" % pid
    DEBUG = False
    atexit.register(cleanup, pattern=pat, debug=DEBUG)
    rain = options['rain']
    q_spec = options['q_spec']
    a_river = options['a_river']
    q_river = options['q_river']
    dtm = options['elevation']
    river = options['river']
    lakes = options['lakes']
    threshold = options['threshold']
    DEBUG = flags['d']
    rf = flags['f']  # ra flow map
    rp = flags['p']  # rain map, piedmont formula
    k_mat = options['k_matrix']  # raster
    m_mat = options['m_matrix']  # raster
    a_mat = options['a_matrix']
    new_stream = options['streams']
    env_area = options['env_area']
    corr_fact = options['corr_fact']

    if not a_river:
        a_river = "tmprgreen_%i_a_river" % pid
    msgr = get_msgr()

    msgr.warning("set region to elevation raster")
    gcore.run_command('g.region', raster=dtm)

    # compute temporary DTM
    if river:
        tmp_dtm_corr = "tmprgreen_%i_dtm_corr" % pid
        dtm_corr(dtm, river, tmp_dtm_corr, lakes)
        dtm_old = dtm
        dtm = tmp_dtm_corr
    # compute the area for each cell
    tmp_acc = 'tmprgreen_%i_acc' % pid
    compute_a(threshold, dtm, new_stream, a_river, tmp_acc)
    # compute q_river
    if rf:
        compute_q(threshold, q_spec, q_river, dtm)
        q_spec = "tmpgreen_%i_q_spec" % pid
        command = "%s=if(%s,%s/%s)" % (q_spec, a_river, q_river, a_river)
        mapcalc(command, overwrite=True)
    elif rp:
        if not (q_spec):
            # compute the mean elevation of the basin
            h_cum = 'tmprgreen_%i_h_cum' % pid
            h_mean = 'tmprgreen_%i_h_mean' % pid
            gcore.run_command('r.watershed',
                              elevation=dtm,
                              flow=dtm_old,
                              threshold=threshold,
                              accumulation=h_cum,
                              memory=3000)
            command = '%s = %s/%s' % (h_mean, h_cum, tmp_acc)
            mapcalc(command)
            #TODO: compute the mean rain
            command = 'q_spec =0.0086*%s+0.03416*%s-24.5694' % (rain, h_mean)
            q_spec = 'q_spec'
            mapcalc(command)
            command = '%s = %s * %s/1000.0' % (q_river, q_spec, a_river)
            mapcalc(command)
    # compute MVF with Regione Veneto Formula
    min_flow = options['mfd']
    if options['k_b']:
        msgr.warning("Regione Veneto plan")
        k_b = options['k_b']  # raster
        k_n = options['k_n']  # raster
        regione_veneto(a_river, q_spec, k_b, k_n, min_flow)
    elif k_mat:
        msgr.warning("Piedmont Plan")
        command = '%s=%s*%s*%s*%s*%s/1000.0' % (min_flow, k_mat, a_river,
                                                q_spec, m_mat, a_mat)
        mapcalc(command, overwrite=True)
    else:
        msgr.warning("No formula for the MVF")
    if corr_fact:
        # if corr_fact coumpute the environemtal flow else the minimum flow"
        command = '%s=if(not(%s), %s, %s*%s)' % (env_area, min_flow,
                                                 corr_fact * min_flow)