Example #1
0
def PreProcess():
    # Here are the different parameters and their default value fr this script
    default_param = AGPD.get_common_default_param()
    default_param["breach"] = False
    default_param["fill"] = False
    default_param["min_slope"] = 0.0001
    default_param["save_path"] = "./"

    default_param = AGPD.ingest_param(default_param, sys.argv)

    if (default_param["help"] or len(sys.argv) == 1):
        print("""
			This command-line tool provides basic Preprocessing functions for a raster.
			Breaching is achieved running https://doi.org/10.1002/hyp.10648 algorithm (Lindsay2016), with an implementation from RICHDEM
			Filling is from Wang and liu 2006. I think MDH implemented the code.
			It also "clean" the raster and tidy the nodata in it.
			To use, run the script with relevant options, for example:
			-> filling and breaching
				lsdtt-depressions file=myraster.tif fill breach 0.00001 

			option available:
				file: name of the raster (file=name.tif)
				path: path to the file (default = current folder)
				breach: want to breach?
				fill: want to fill?
				min_slope: imply a min slope for filling

			""")
        quit()

    sevadir = default_param["save_path"]

    print("Welcome to the command-line tool to .")
    print("Let me first load the raster ...")
    mydem = LSDDEM(file_name=default_param["file"],
                   path=default_param["path"],
                   already_preprocessed=False,
                   verbose=False)
    print("Got it. Now dealing with the depressions ...")
    mydem.PreProcessing(filling=default_param["fill"],
                        carving=default_param["breach"],
                        minimum_slope_for_filling=float(
                            default_param["min_slope"])
                        )  # Unecessary if already preprocessed of course.

    mydem.save_dir

    print("Saving the raster! same name basically, but with _PP at the end")
    rl.save_raster(mydem.cppdem.get_PP_raster(),
                   mydem.x_min,
                   mydem.x_max,
                   mydem.y_max,
                   mydem.y_min,
                   mydem.resolution,
                   mydem.crs,
                   mydem.path + sevadir + mydem.prefix + "_PP.tif",
                   fmt='GTIFF')
Example #2
0
def extract_single_river_from_source():
    # Here are the different parameters and their default value fr this script
    default_param = AGPD.get_common_default_param()
    default_param["already_preprocessed"] = False
    default_param["X"] = 0
    default_param["Y"] = 0
    default_param["output_name"] = "Single_River"
    default_param = AGPD.ingest_param(default_param, sys.argv)

    if (default_param["help"] or len(sys.argv) == 1):
        print("""
			This command-line tool preprocess your raster to the right format and remove elevation below a threshold, typically the sea.

			Quick example: lsdtt-extract-single-river file=test_raster.tif X=592149.7 Y=4103817.2

			Option available:
				file: name of the raster (file=name.tif)
				path: path to the file (default = current folder)
				already_preprocessed (default = False): Add the keyword if your raster is already preprocessed for flow analysis, False if it needs preprocessing. To get more control on the preprocessing you can use lsdtt-depressions command.
				X: Easting/X coordinate of the source
				Y: Easting/Y coordinate of the source
				output_name (default = Single_River): prefix of the ouput(s) files.

			Future improvements:
				- Automatic plotting of base statistic
				- Adding Chi/ksn/gradient options
			""")
        quit()

    print(
        "This command-line tool extract a single river from a DEM from the XY coordinates of the source"
    )
    print("Loading the dem")
    mydem = LSDDEM(file_name=default_param["file"],
                   path=default_param["path"],
                   already_preprocessed=bool(
                       default_param["already_preprocessed"]),
                   verbose=True)
    if (bool(default_param["already_preprocessed"]) == False):
        print(
            "I am preprocessing your dem (carving + filling), if you have already done that, you can save time by adding the option already_preprocessed=True to the command line (it saves time)"
        )
        mydem.PreProcessing()

    river = pd.DataFrame(
        mydem.ExtractSingleRiverFromSource(float(default_param["X"]),
                                           float(default_param["Y"])))
    river.to_csv("%s.csv" % (default_param["output_name"]), index=False)
Example #3
0
def hypsometry_tools_general():
    # Here are the different parameters and their default value fr this script
    default_param = AGPD.get_common_default_param()
    default_param["hypsometry"] = False
    default_param["absolute_elevation"] = False
    default_param["normalise_to_outlets"] = False
    default_param["X"] = None
    default_param["Y"] = None

    default_param = AGPD.ingest_param(default_param, sys.argv)

    # Checking the param
    if (isinstance(default_param["X"], str)):
        X = [float(default_param["X"])]
        Y = [float(default_param["Y"])]
    else:
        X = [float(i) for i in default_param["X"]]
        Y = [float(i) for i in default_param["Y"]]

    if (default_param["help"] or len(sys.argv) == 1):
        print("""
			This command-line tool run concavity analysis tools from LSDTopoTools.
			Description of the algorithms in Mudd et al., 2018 -> https://www.earth-surf-dynam.net/6/505/2018/
			To use, run the script with relevant options, for example:
				lsdtt-concavity-tools.py file=myraster.tif quick_movern X=5432 Y=78546

			option available:
				file: name of the raster (file=name.tif)
				path: path to the file (default = current folder)
				hypsometry: run hypsometric calculations
				absolute_elevation: hypsometric elevation will be the absolute value rather than the relavie/normalised one
				X: X coordinate of the outlet (So far only single basin is supported as this is an alpha tool)
				Y: Y coordinate of the outlet (So far only single basin is supported as this is an alpha tool)
				help: if written, diplay this message. Documentation soon to be written.
			""")
        quit()

    print(
        "Welcome to the command-line tool to plot basin-wide hypsometry stuff!"
    )
    print("Let me first load the raster ...")
    mydem = LSDDEM(file_name=default_param["file"],
                   path=default_param["path"],
                   already_preprocessed=False,
                   verbose=False)
    print("Got it. Now dealing with the depressions ...")
    mydem.PreProcessing(filling=True,
                        carving=True,
                        minimum_slope_for_filling=0.0001
                        )  # Unecessary if already preprocessed of course.
    print("Done! Extracting the river network")
    mydem.ExtractRiverNetwork(method="area_threshold", area_threshold_min=1000)
    print("I have some rivers for you! Defining the watershed of interest...")

    mydem.DefineCatchment(method="from_XY",
                          X_coords=X,
                          Y_coords=Y,
                          test_edges=False,
                          coord_search_radius_nodes=25,
                          coord_threshold_stream_order=1)
    print("I should have it now.")
    print("I got all the common info, now I am running what you want!")
    mydem.GenerateChi()

    print(
        "Producing a figure of your catchement just for you to check if their location is OK!"
    )
    qp.plot_check_catchments(mydem)

    if default_param["hypsometry"]:
        print(
            "I am going to plot relative hypsometric curves for your different basins"
        )
        print(
            "This is quite quickly coded (for Jorien) and will probably evolve in the future!"
        )

        fig, ax = plt.subplots()

        # Basin Array
        BAS = mydem.cppdem.get_chi_basin()
        TOPT = mydem.cppdem.get_PP_raster()
        DAD = mydem.cppdem.get_DA_raster()

        for bas in np.unique(BAS):
            if (bas != -9999):
                # getting hte topo raster ONLY for current basin
                this_topo = TOPT[BAS == bas].ravel()
                # Now getting the DA
                this_DA = DAD[BAS == bas].ravel()
                A = np.nanmax(this_DA)  # maximum area of that basin

                # Normalising the topo
                if (default_param["absolute_elevation"] == False):
                    this_topo = (this_topo - np.nanmin(this_topo))
                    this_topo = this_topo / np.nanmax(this_topo)
                elif (default_param["normalise_to_outlets"]):
                    this_topo = (this_topo - np.nanmin(this_topo))

                # Y is h/H, X is a/A
                Y = []
                X = []
                for i in range(101):
                    if (default_param["absolute_elevation"] == False):
                        Y.append(i * 0.01)
                    else:
                        Y.append(np.percentile(this_topo, i))

                    X.append(np.nanmax(this_DA[this_topo >= Y[-1]]) / A)
                ax.plot(X, Y, lw=1, label=bas, alpha=0.8)

        ax.set_xlabel(r"$\frac{a}{A}$")
        if (default_param["absolute_elevation"] == False):
            ax.set_ylabel(r"$\frac{h}{H}$")
        else:
            ax.set_ylabel("Elevation (m)")

        ax.legend()

        if (default_param["absolute_elevation"] == False):
            suffix = "_relative_hypsometry"
        else:
            suffix = "_absolute_hypsometry"
            if (default_param["normalise_to_outlets"]):
                suffix += "_norm2outlet"

        plt.savefig(mydem.save_dir + mydem.prefix + suffix + "." + "png",
                    dpi=500)

        plt.clf()

    print("Finished!")
def chi_mapping_tools():
    # Here are the different parameters and their default value fr this script
    default_param = AGPD.get_common_default_param()
    default_param["map"] = False
    default_param["theta_ref"] = 0.45
    default_param["A_0"] = 1
    default_param["area_threshold"] = 500

    default_param["X"] = None
    default_param["Y"] = None

    default_param = AGPD.ingest_param(default_param, sys.argv)

    # Checking the param
    if (isinstance(default_param["X"], str)):
        X = [float(default_param["X"])]
        Y = [float(default_param["Y"])]
    else:
        X = [float(i) for i in default_param["X"]]
        Y = [float(i) for i in default_param["Y"]]

    if (default_param["help"] or len(sys.argv) == 1):
        print("""
			This command-line tool run chi analysis tools from LSDTopoTools. This tool is focused on extracting chi, ksn or things like that.
			Chi -> DOI: 10.1002/esp.3302 An integral approach to bedrock river profile analysis, Perron and Royden 2013
			ksn -> (calculated from a robust statistical method based on chi): https://agupubs.onlinelibrary.wiley.com/doi/epdf/10.1002/2013JF002981, Mudd et al., 2014

			
			To use, run the script with relevant options, for example:
				lsdtt-chi-tools file=myraster.tif map X=531586 Y=6189787 theta_ref=0.55 A_0=10

			option available:
				file: name of the raster (file=name.tif)
				path: path to the file (default = current folder)
				map: generate map of chi
				theta_ref: theta ref on the main equation
				A_0: A_0 on the chi equation
				area_threshold: the threshold of pixel to initiate the river network
				X: X coordinate of the outlet (So far only single basin is supported as this is an alpha tool)
				Y: Y coordinate of the outlet (So far only single basin is supported as this is an alpha tool)
				help: if written, diplay this message. Documentation soon to be written.

			""")

        quit()

    print(
        "Welcome to the command-line tool to plot basin-wide hypsometry stuff!"
    )
    print("Let me first load the raster ...")
    mydem = LSDDEM(file_name=default_param["file"],
                   path=default_param["path"],
                   already_preprocessed=False,
                   verbose=False)
    print("Got it. Now dealing with the depressions ...")
    mydem.PreProcessing(filling=True,
                        carving=True,
                        minimum_slope_for_filling=0.0001
                        )  # Unecessary if already preprocessed of course.
    print("Done! Extracting the river network")
    mydem.ExtractRiverNetwork(method="area_threshold", area_threshold_min=1000)
    print("I have some rivers for you! Defining the watershed of interest...")

    mydem.DefineCatchment(method="from_XY",
                          X_coords=X,
                          Y_coords=Y,
                          test_edges=False,
                          coord_search_radius_nodes=25,
                          coord_threshold_stream_order=1)
    print("I should have it now.")
    print("I got all the common info, now I am running what you want!")
    mydem.GenerateChi(theta=float(default_param["theta_ref"]),
                      A_0=float(default_param["A_0"]))

    print(
        "Producing a figure of your catchement just for you to check if their location is OK!"
    )
    qp.plot_check_catchments(mydem)

    if default_param["map"]:
        print("I am going to plot chi maps")
        fig, ax = qp.plot_nice_topography(mydem,
                                          figure_width=4,
                                          figure_width_units="inches",
                                          cmap="gist_earth",
                                          hillshade=True,
                                          alpha_hillshade=1,
                                          color_min=None,
                                          color_max=None,
                                          dpi=300,
                                          output="return",
                                          format_figure="png",
                                          fontsize_ticks=6,
                                          fontsize_label=8,
                                          hillshade_cmin=0,
                                          hillshade_cmax=250,
                                          colorbar=False,
                                          colorbar_label=None,
                                          colorbar_ax=None)

        normalize = matplotlib.colors.Normalize(
            vmin=mydem.df_base_river["drainage_area"].min(),
            vmax=mydem.df_base_river["drainage_area"].max())
        mydem.df_base_river = mydem.df_base_river[
            mydem.df_base_river["chi"] >= 0]
        cb = ax.scatter(mydem.df_base_river["x"],
                        mydem.df_base_river["y"],
                        s=4 *
                        normalize(mydem.df_base_river["drainage_area"].values),
                        c=mydem.df_base_river["chi"],
                        vmin=mydem.df_base_river["chi"].quantile(0.1),
                        vmax=mydem.df_base_river["chi"].quantile(0.9),
                        zorder=5,
                        lw=0)
        plt.colorbar(cb)
        plt.savefig(mydem.save_dir + mydem.prefix + "_chi_map" + "." + "png",
                    dpi=500)
        plt.clf()

    print("Finished!")
Example #5
0
# print("Downloading a test dataset: ")
# file = wget.download("https://github.com/LSDtopotools/LSDTT_workshop_data/raw/master/WAWater.bil")
# wget.download("https://github.com/LSDtopotools/LSDTT_workshop_data/raw/master/WAWater.hdr")
# print("Done")
##################################################################################################

my_raster_path = "./"  # I am telling saving my path to a variable. In this case, I assume the rasters is in the same folder than my script
file_name = "WAWater.bil"  # The name of your raster with extension. RasterIO then takes care internally of decoding it. Good guy rasterio!

# I am now telling lsdtopytools where is my raster, and What do I want to do with it. No worries It will deal with the detail internally
mydem = LSDDEM(
    path=my_raster_path, file_name=file_name
)  # If your dem is already preprocessed: filled or carved basically, add: , is_preprocessed = True
## Loaded in the system, now preprocessing: I want to carve it and imposing a minimal slope on remaining flat surfaces: 0.0001
mydem.PreProcessing(filling=True,
                    carving=True,
                    minimum_slope_for_filling=0.0001
                    )  # Unecessary if already preprocessed of course.
## First plotting a nice topography
quickplot.plot_nice_topography(
    mydem
)  # many available options are available to custumize, see documentation.

## Now plotting the pre-processing differential raster to check the results and potential artifacts
quickplot.plot_preprocessing_diff(
    mydem
)  # many available options are available to custumize, see documentation.

# Now I am testing a fill-only preprocessing to check the difference
mydem.PreProcessing(filling=True,
                    carving=False,
                    minimum_slope_for_filling=0.0001
Example #6
0
def concavity_FFS_down_to_top():
    # Here are the different parameters and their default value fr this script
    default_param = AGPD.get_common_default_param()
    default_param["already_preprocessed"] = False
    default_param["save_XY_array"] = False
    default_param["X_source"] = None
    default_param["Y_source"] = None
    default_param["min_elevation"] = None
    default_param["area_threshold"] = None
    default_param["flow_distance_step"] = None
    default_param["min_DA"] = None
    default_param["prefix"] = ""
    default_param = AGPD.ingest_param(default_param, sys.argv)

    if (default_param["help"] or len(sys.argv) == 1 or "help" in sys.argv):
        print("""
			This command-line tool run concavity analysis tools from LSDTopoTools.
			Description of the algorithms in Mudd et al., 2018 -> https://www.earth-surf-dynam.net/6/505/2018/
			To use, run the script with relevant options, for example:
				lsdtt-concavity-down-to-top file=myraster.tif ---

			option available:
				file: name of the raster (file=name.tif)
				path: path to the file (default = current folder)
				todo
				help: if written, diplay this message. Documentation soon to be written.
			""")
        quit()

    try:
        default_param["X_source"] = float(default_param["X_source"])
        default_param["Y_source"] = float(default_param["Y_source"])
        default_param["min_elevation"] = float(default_param["min_elevation"])
        default_param["area_threshold"] = float(
            default_param["area_threshold"])
        default_param["flow_distance_step"] = float(
            default_param["flow_distance_step"])
        default_param["min_DA"] = float(default_param["min_DA"])
    except:
        print(default_param)
        print(
            "I struggle to understand your input parameters: make sure I can convert them to number"
        )
        raise SystemExit("I need to quit now")

    mydem = LSDDEM(file_name=default_param["file"],
                   path=default_param["path"],
                   already_preprocessed=default_param["already_preprocessed"],
                   remove_seas=True,
                   sea_level=default_param["min_elevation"])

    if (default_param["already_preprocessed"] == False):
        print(
            "I am preprocessing your raster with default options, see lsdtt-depressions for extended options."
        )
        print(
            "Command line tools can load already processed rasters with the keyword already_preprocessed"
        )
        mydem.PreProcessing()

    # flwo routines
    print("Processing flow routines with d8 algorithm")
    mydem.CommonFlowRoutines()

    print("Extracting the river")
    river = mydem.ExtractSingleRiverFromSource(default_param["X_source"],
                                               default_param["Y_source"])
    river = pd.DataFrame(river)
    print("Saving teh river to csv file: river_for_concavity.csv")
    river.to_csv("%sriver_for_concavity.csv" % (default_param["prefix"]),
                 index=False)

    print("Initialising the concavity analysis")
    river.sort_values("elevation", inplace=True)
    river.reset_index(drop=True, inplace=True)

    # OUtputs
    global_results = {}
    outlet_info = {
        "X": [],
        "Y": [],
        "elevation": [],
        "flow_distance": [],
        "drainage_area": []
    }

    # potential saving of basins
    XY_basins = {}

    flow_distance_this = river["flow_distance"].min()
    index_this = river["flow_distance"].idxmin()
    while (flow_distance_this < river["flow_distance"].max()
           and river["drainage_area"][index_this] > default_param["min_DA"]):
        print("processing  flow distance =", flow_distance_this)
        del mydem
        mydem = LSDDEM(file_name="%sdem_for_concavity.tif" %
                       (default_param["prefix"]),
                       already_preprocessed=True,
                       remove_seas=True,
                       sea_level=river["elevation"][index_this] - 5)
        mydem.CommonFlowRoutines()
        # mydem.save_array_to_raster_extent(mydem.cppdem.get_DA_raster(), name = "DA", save_directory = "./")

        mydem.ExtractRiverNetwork(
            area_threshold_min=default_param["area_threshold"])
        # print(river.iloc[index_this])
        mydem.DefineCatchment(method="from_XY",
                              X_coords=[river["X"][index_this]],
                              Y_coords=[river["Y"][index_this]],
                              coord_search_radius_nodes=0)
        mydem.GenerateChi()
        print(mydem.df_base_river["source_key"].unique())

        mydem.cppdem.calculate_movern_disorder(0.05, 0.05, 19, 1,
                                               default_param["area_threshold"])
        all_disorder = mydem.cppdem.get_best_fits_movern_per_BK()
        global_results[str(round(
            river["flow_distance"][index_this], 2))] = np.array(
                all_disorder[0]
            )  # [0] means basin 0, which is normal as I only have one
        outlet_info["X"].append(river["X"][index_this])
        outlet_info["Y"].append(river["Y"][index_this])
        outlet_info["elevation"].append(river["elevation"][index_this])
        outlet_info["flow_distance"].append(
            round(river["flow_distance"][index_this], 2))
        outlet_info["drainage_area"].append(river["drainage_area"][index_this])

        # New index
        flow_distance_this += default_param["flow_distance_step"]
        index_this = river.index[
            river["flow_distance"] >= flow_distance_this].values[0]

        np.savez("%sdown_to_top_conc.npz" % (default_param["prefix"]),
                 **global_results)
        pd.DataFrame(outlet_info).to_csv("%sdown_to_top_conc_basin_info.csv" %
                                         (default_param["prefix"]),
                                         index=False)

        if (default_param["save_XY_array"]):
            this_basin = mydem.cppdem.query_xy_for_each_basin()[
                0]  # [0] because I want a single basin there
            print(this_basin)
            XY_basins[str(round(river["flow_distance"][index_this],
                                2))] = this_basin
            np.savez(default_param["prefix"] + "down_to_top_basins_XY.npz",
                     **XY_basins)
Example #7
0
def spawn_XY_outlet_subbasins():

    default_param = AGPD.get_common_default_param()
    default_param["already_preprocessed"] = False
    default_param["X"] = 0
    default_param["Y"] = 0
    default_param["area_threshold"] = 5000
    default_param["min_DA"] = 1e6
    default_param["max_DA"] = 1e9
    default_param["min_elevation"] = 0
    default_param["prefix"] = ""
    default_param = AGPD.ingest_param(default_param, sys.argv)

    if (default_param["help"] or len(sys.argv) == 1 or "help" in sys.argv):
        print("""
Command-line tool to extract basin information about all the subbasins within a main one. Outputs a file with outlet coordinates readable from other command-line tools and a basin perimeter csv readable by GISs to if the basins corresponds to your needs.
Takes several arguments (the values after = are example values to adapt): 
file=NameOfFile.tif -> The code NEEDS the neame of the raster to process.
already_preprocessed -> OPTIONAL Tell the code your raster does not need preprocessing, otherwise carve the DEM (see lsdtt-depressions for more options)
prefix=test -> OPTIONAL Add a prefix to each outputted file (handy for automation)
min_elevation=45 -> DEFAULT 0. Ignore any basin bellow that elevation
area_threshold=3500 -> DEFAULT 5000. River network area threshold in number of pixels (part of the basin selection is based on river junctions HIGHLY sensitive to that variable).
min_DA=1e7 -> minimum drainage area to extract a subbasin
max_DA=1e9 -> maximum drainage area for a subbasin
X=234 -> X Coordinate (in map unit) of the outlet (needs to be the exact pixel at the moment, will add a snapping option later) 
Y=234 -> Y Coordinate (in map unit) of the outlet (needs to be the exact pixel at the moment, will add a snapping option later) 

Example:
lsdtt-concFFS-spawn-outlets file=DEM.tif already_preprocessed min_DA=1e7 max_DA=1e9 area_threshold=3500
			""")
        return 0

    area_threshold = int(default_param["area_threshold"])
    X = float(default_param["X"])
    min_DA = float(default_param["min_DA"])
    Y = float(default_param["Y"])
    max_DA = float(default_param["max_DA"])
    min_elevation = float(default_param["min_elevation"])

    mydem = LSDDEM(file_name=default_param["file"],
                   path=default_param["path"],
                   already_preprocessed=default_param["already_preprocessed"],
                   remove_seas=True,
                   sea_level=min_elevation)
    if (default_param["already_preprocessed"] == False):
        mydem.PreProcessing()
    # Extracting basins
    mydem.CommonFlowRoutines()
    mydem.ExtractRiverNetwork(method="area_threshold",
                              area_threshold_min=area_threshold)
    # df_outlet = mydem.DefineCatchment(  method = default_param["method"], min_area = min_DA, max_area = max_DA, test_edges = default_param["test_edges"])#, X_coords = [X_coordinates_outlets[7]], Y_coords = [Y_coordinates_outlets[7]])
    df_outlet = mydem.cppdem.calculate_outlets_min_max_draining_to_baselevel(
        X, Y, min_DA, max_DA, 500)
    mydem.check_catchment_defined = True

    for key, val in df_outlet.items():
        df_outlet[key] = np.array(df_outlet[key])
    mydem.GenerateChi(theta=0.4, A_0=1)
    mydem.df_base_river.to_csv(default_param["prefix"] + "rivers.csv",
                               index=False)
    df_outlet["area_threshold"] = np.full(df_outlet["X"].shape[0],
                                          area_threshold)
    df_outlet = pd.DataFrame(df_outlet)
    df_outlet.to_csv(default_param["prefix"] + "outlets.csv", index=False)
    df_outlet["ID"] = np.array(list(range(df_outlet.shape[0])))

    this = mydem.cppdem.extract_perimeter_of_basins()
    df_perimeter = {"X": [], "Y": [], "Z": [], "IDs": []}
    for key, val in this.items():
        df_perimeter["X"].append(np.array(val["X"]))
        df_perimeter["Y"].append(np.array(val["Y"]))
        df_perimeter["Z"].append(np.array(val["Z"]))
        df_perimeter["IDs"].append(np.full(np.array(val["Z"]).shape[0], key))

    ## Log from the analysis
    for key, val in df_perimeter.items():
        df_perimeter[key] = np.concatenate(val)
    pd.DataFrame(df_perimeter).to_csv(default_param["prefix"] +
                                      "perimeters.csv",
                                      index=False)
Example #8
0
def spawn_XY_outlet():
    """
Command-line tool to prechoose the basins used for other analysis. Outputs a file with outlet coordinates readable from other command-line tools and a basin perimeter csv readable by GISs to if the basins corresponds to your needs.
Takes several arguments (the values after = are example values to adapt): 
file=NameOfFile.tif -> The code NEEDS the neame of the raster to process.
already_preprocessed -> OPTIONAL Tell the code your raster does not need preprocessing, otherwise carve the DEM (see lsdtt-depressions for more options)
test_edges -> OPTIONAL will test if the basin extracted are potentially influenced by nodata and threfore uncomplete. WARNING, will take out ANY basin potentially cut, if you know what you are doing, you can turn off.
prefix=test -> OPTIONAL Add a prefix to each outputted file (handy for automation)
method=from_range -> DEFAULT from_range: determine the method to select basin. Can be
		from_range -> select largest basins bigger than min_DA but smaller than max_DA (in m^2)
		min_area -> select largest basins bigger than min_DA
		main_basin -> select the largest basin
		Other methods to come.
min_elevation=45 -> DEFAULT 0. Ignore any basin bellow that elevation
area_threshold=3500 -> DEFAULT 5000. River network area threshold in number of pixels (part of the basin selection is based on river junctions HIGHLY sensitive to that variable).

Example:
lsdtt-concFFS-spawn-outlets file=DEM.tif already_preprocessed min_DA=1e7 max_DA=1e9 area_threshold=3500
			"""

    default_param = AGPD.get_common_default_param()
    default_param["already_preprocessed"] = False
    default_param["test_edges"] = False
    default_param["area_threshold"] = 5000
    default_param["method"] = "from_range"
    default_param["min_DA"] = 1e6
    default_param["max_DA"] = 1e9
    default_param["min_elevation"] = 0
    default_param["prefix"] = ""
    default_param = AGPD.ingest_param(default_param, sys.argv)

    choice_of_method = ["min_area", "main_basin", "from_range"]

    if (default_param["help"] or len(sys.argv) == 1 or "help" in sys.argv):
        print("""
Command-line tool to prechoose the basins used for other analysis. Outputs a file with outlet coordinates readable from other command-line tools and a basin perimeter csv readable by GISs to if the basins corresponds to your needs.
Takes several arguments (the values after = are example values to adapt): 
file=NameOfFile.tif -> The code NEEDS the neame of the raster to process.
already_preprocessed -> OPTIONAL Tell the code your raster does not need preprocessing, otherwise carve the DEM (see lsdtt-depressions for more options)
test_edges -> OPTIONAL will test if the basin extracted are potentially influenced by nodata and threfore uncomplete. WARNING, will take out ANY basin potentially cut, if you know what you are doing, you can turn off.
prefix=test -> OPTIONAL Add a prefix to each outputted file (handy for automation)
method=from_range -> DEFAULT from_range: determine the method to select basin. Can be
		from_range -> select largest basins bigger than min_DA but smaller than max_DA (in m^2)
		min_area -> select largest basins bigger than min_DA
		main_basin -> select the largest basin
		Other methods to come.
min_elevation=45 -> DEFAULT 0. Ignore any basin bellow that elevation
area_threshold=3500 -> DEFAULT 5000. River network area threshold in number of pixels (part of the basin selection is based on river junctions HIGHLY sensitive to that variable).

Example:
lsdtt-concFFS-spawn-outlets file=DEM.tif already_preprocessed min_DA=1e7 max_DA=1e9 area_threshold=3500
			""")
        return 0

    # Checks if the method requested is valid or not
    if (default_param["method"].lower() not in choice_of_method):
        print("I cannot recognise the method! Please choose from:")
        print(choice_of_method)
        return 0

    # Formatting parameters
    area_threshold = int(default_param["area_threshold"])
    min_DA = float(default_param["min_DA"])
    max_DA = float(default_param["max_DA"])
    min_elevation = float(default_param["min_elevation"])

    # Reading DEM
    mydem = LSDDEM(file_name=default_param["file"],
                   path=default_param["path"],
                   already_preprocessed=default_param["already_preprocessed"],
                   remove_seas=True,
                   sea_level=min_elevation)
    if (default_param["already_preprocessed"] == False):
        mydem.PreProcessing()
    # Extracting basins
    mydem.CommonFlowRoutines()
    print("Done with flow routines")
    mydem.ExtractRiverNetwork(method="area_threshold",
                              area_threshold_min=area_threshold)
    # Get the outlet coordinates of all the extracted basins
    print("Extracted rivers")
    df_outlet = mydem.DefineCatchment(
        method=default_param["method"],
        min_area=min_DA,
        max_area=max_DA,
        test_edges=default_param["test_edges"]
    )  #, X_coords = [X_coordinates_outlets[7]], Y_coords = [Y_coordinates_outlets[7]])
    print("Extracted")

    for key, val in df_outlet.items():
        df_outlet[key] = np.array(df_outlet[key])
    # Getting the rivers
    mydem.GenerateChi(theta=0.4, A_0=1)
    # Saing the rivers to csv
    mydem.df_base_river.to_csv(default_param["prefix"] + "rivers.csv",
                               index=False)
    #Saving the outlet
    df_outlet["area_threshold"] = np.full(df_outlet["X"].shape[0],
                                          area_threshold)

    # print(df_outlet)

    pd.DataFrame(df_outlet).to_csv(default_param["prefix"] + "outlets.csv",
                                   index=False)

    # Getting the perimeter of basins
    this = mydem.cppdem.extract_perimeter_of_basins()
    df_perimeter = {"X": [], "Y": [], "Z": [], "IDs": []}
    for key, val in this.items():
        df_perimeter["X"].append(np.array(val["X"]))
        df_perimeter["Y"].append(np.array(val["Y"]))
        df_perimeter["Z"].append(np.array(val["Z"]))
        df_perimeter["IDs"].append(np.full(np.array(val["Z"]).shape[0], key))

    for key, val in df_perimeter.items():
        df_perimeter[key] = np.concatenate(val)

    pd.DataFrame(df_perimeter).to_csv(default_param["prefix"] +
                                      "perimeters.csv",
                                      index=False)
Example #9
0
def main_concavity():
    # Here are the different parameters and their default value fr this script
    default_param = AGPD.get_common_default_param()
    default_param["quick_movern"] = False
    default_param["X"] = None
    default_param["Y"] = None

    default_param = AGPD.ingest_param(default_param, sys.argv)

    # Checking the param
    if (isinstance(default_param["X"], str)):
        X = [float(default_param["X"])]
        Y = [float(default_param["Y"])]
    else:
        try:
            X = [float(i) for i in default_param["X"]]
            Y = [float(i) for i in default_param["Y"]]
        except:
            pass

    if (default_param["help"] or len(sys.argv) == 2 or "help" in sys.argv):
        print("""
			This command-line tool run concavity analysis tools from LSDTopoTools.
			Description of the algorithms in Mudd et al., 2018 -> https://www.earth-surf-dynam.net/6/505/2018/
			To use, run the script with relevant options, for example:
				lsdtt-concavity-tools.py file=myraster.tif quick_movern X=5432 Y=78546

			option available:
				file: name of the raster (file=name.tif)
				path: path to the file (default = current folder)
				quick_movern: run disorder metrics and plot a result figure (you jsut need to write it)
				X: X coordinate of the outlet (So far only single basin is supported as this is an alpha tool)
				Y: Y coordinate of the outlet (So far only single basin is supported as this is an alpha tool)
				help: if written, diplay this message. Documentation soon to be written.
			""")
        quit()

    print(
        "Welcome to the command-line tool to constrain your river network concavity. Refer to Mudd et al., 2018 -> https://www.earth-surf-dynam.net/6/505/2018/ for details about these algorithms."
    )
    print("Let me first load the raster ...")
    try:
        mydem = LSDDEM(file_name=default_param["file"],
                       path=default_param["path"],
                       already_preprocessed=False,
                       verbose=False)
    except:
        print("Testing data still to build")
    print("Got it. Now dealing with the depressions ...")
    mydem.PreProcessing(filling=True,
                        carving=True,
                        minimum_slope_for_filling=0.0001
                        )  # Unecessary if already preprocessed of course.
    print("Done! Extracting the river network")
    mydem.ExtractRiverNetwork(method="area_threshold", area_threshold_min=1000)

    print("I have some rivers for you! Defining the watershed of interest...")
    mydem.DefineCatchment(method="from_XY",
                          X_coords=X,
                          Y_coords=Y,
                          test_edges=False,
                          coord_search_radius_nodes=25,
                          coord_threshold_stream_order=1)
    print("I should have it now.")
    print("I got all the common info, now I am running what you want!")

    if (default_param["quick_movern"]):
        print("Initialising Chi-culations (lol)")
        mydem.GenerateChi()
        print(
            "Alright, getting the disorder metrics for each chi values. LSDTopoTools can split a lot of messages, sns."
        )
        mydem.cppdem.calculate_movern_disorder(
            0.15, 0.05, 17, 1, 1000)  # start theta, delta, n, A0, threashold
        print("I am done, plotting the results now")
        qmn.plot_disorder_results(mydem,
                                  legend=False,
                                  normalise=True,
                                  cumulative_best_fit=False)
        qp.plot_check_catchments(mydem)
        qmn.plot_disorder_map(mydem, cmap="RdBu_r")
        print("FInished with quick disorder metric")

    print("Finished!")