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 burn_rast_val_to_csv():
	"""
		This scripst burns raster data to a csv containing point coordinate. For example if you want to add lithology ID to river points.
		Usage:
			lsdtt-burn2point file=name_of_raster.tif csv=name_of_csv.csv new_column=burned_data_column_name
	"""
	# Here are the different parameters and their default value fr this script
	default_param = AGPD.get_common_default_param()
	default_param["new_column"] = "burned_data"
	default_param["read_csv"] = "to_burn.csv"
	default_param["save_csv"] = "burned.csv"
	default_param["X_col"] = "X"
	default_param["Y_col"] = "Y"

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

	if(default_param["help"] or len(sys.argv)==1):
		print("""
			This command-line tool is a command line tool. Documentation to write.
			lsdtt-burn2csv file=example.tif new_column=tectonic_unit read_csv=name_of_csv.csv save_csv=my_new_csv.csv X_col=X Y_col=Y

			""")
		quit()


	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 = True, verbose = False)
	df = pd.read_csv(default_param["read_csv"])
	res = mydem.cppdem.burn_rast_val_to_xy(df[default_param["X_col"]].values,df[default_param["Y_col"]].values)
	df[default_param["new_column"]] = pd.Series(data = res, index = df.index)
	df.to_csv(default_param["save_csv"], index = False)
def load_n_plot(name):
    MD = LSDDEM(file_name=name, already_preprocessed=True)
    qp.plot_nice_topography(MD,
                            figure_width=4,
                            figure_width_units="inches",
                            cmap="gist_earth",
                            hillshade=True,
                            alpha_hillshade=0.45,
                            color_min=None,
                            color_max=None,
                            dpi=300,
                            output="save",
                            format_figure="png",
                            fontsize_ticks=6,
                            fontsize_label=8,
                            hillshade_cmin=0,
                            hillshade_cmax=250,
                            colorbar=False,
                            fig=None,
                            ax=None,
                            colorbar_label=None,
                            colorbar_ax=None,
                            force_path=True,
                            path_to_force="./")
    term = MD.path + MD.prefix + "/"
    subprocess.run("rm -r %s" % (term), shell=True)
Example #5
0
 def plotty(direc, fname, ret=False, figt=None, dpi=300, HS_alpha=0.85):
     tdem = LSDDEM(path=direc,
                   file_name=fname,
                   already_preprocessed=True)
     mx = np.percentile(tdem.cppdem.get_PP_raster(), 90)
     if (ret):
         return qp.plot_nice_topography(tdem,
                                        figure_width=5,
                                        figure_width_units="inches",
                                        cmap="gist_earth",
                                        hillshade=True,
                                        alpha_hillshade=HS_alpha,
                                        color_min=0,
                                        color_max=mx,
                                        dpi=500,
                                        output="nothing",
                                        format_figure="png",
                                        fontsize_ticks=6,
                                        fontsize_label=8,
                                        hillshade_cmin=None,
                                        hillshade_cmax=None,
                                        fig=figt,
                                        colorbar=True)
     else:
         qp.plot_nice_topography(tdem,
                                 figure_width=5,
                                 figure_width_units="inches",
                                 cmap="gist_earth",
                                 hillshade=True,
                                 alpha_hillshade=HS_alpha,
                                 color_min=0,
                                 color_max=mx,
                                 dpi=500,
                                 output="save",
                                 format_figure="png",
                                 fontsize_ticks=6,
                                 fontsize_label=8,
                                 hillshade_cmin=None,
                                 hillshade_cmax=None,
                                 colorbar=True)
Example #6
0
def remove_seas():
    # Here are the different parameters and their default value fr this script
    default_param = AGPD.get_common_default_param()
    default_param["sea_level"] = 0
    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.

			option available:
				file: name of the raster (file=name.tif)
				path: path to the file (default = current folder)
				sea_level: elevation threshold (default = 0)

			""")
        quit()

    print("Loading the dem")
    mydem = LSDDEM(file_name=default_param["file"],
                   path=default_param["path"],
                   already_preprocessed=True,
                   verbose=False)
    print("Processing the sea, removing everything %s ..." %
          (float(default_param["sea_level"])))
    topo = np.copy(mydem.cppdem.get_PP_raster())
    topo[topo < float(default_param["sea_level"])] = -9999
    print("done, let me save the raster")
    rl.save_raster(topo,
                   mydem.x_min,
                   mydem.x_max,
                   mydem.y_max,
                   mydem.y_min,
                   mydem.resolution,
                   mydem.crs,
                   mydem.path + mydem.prefix + "_sea_removed.tif",
                   fmt='GTIFF')
Example #7
0
def topomap():
    # Here are the different parameters and their default value fr this script
    default_param = AGPD.get_common_default_param()
    default_param["save_hillshade"] = True
    default_param["topomap"] = True

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

    if (default_param["help"] or len(sys.argv) == 1):
        print("""
			This command-line tool provides first order topographic visualisation:

			option available:
				file: name of the raster (file=name.tif)
				path: path to the file (default = current folder)
				save_hillshade: save a tif hillshade file
				topomap: save a nice topographic map

			""")
        quit()

    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=True,
                   verbose=False)

    if (default_param["topomap"]):
        qp.plot_nice_topography(mydem,
                                figure_width=6,
                                figure_width_units="inches",
                                cmap="gist_earth",
                                hillshade=True,
                                alpha_hillshade=0.45,
                                color_min=None,
                                color_max=None,
                                dpi=500,
                                output="save",
                                format_figure="png",
                                fontsize_ticks=6,
                                fontsize_label=8,
                                hillshade_cmin=0,
                                hillshade_cmax=250,
                                colorbar=True,
                                fig=None,
                                ax=None,
                                colorbar_label=None,
                                colorbar_ax=None)

    if (default_param["save_hillshade"]):
        print("Saving the Hillshade raster ...")
        rl.save_raster(mydem.get_hillshade(),
                       mydem.x_min,
                       mydem.x_max,
                       mydem.y_max,
                       mydem.y_min,
                       mydem.resolution,
                       mydem.crs,
                       mydem.path + mydem.prefix + "_hs.tif",
                       fmt='GTIFF')
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 #9
0
    def plot_anim(self, fps=10, rename_suffix="", dpi=300, HS_alpha=0.75):

        from matplotlib.animation import FFMpegWriter
        from matplotlib import pyplot as plt

        # Beware of the nested function
        def plotty(direc, fname, ret=False, figt=None, dpi=300, HS_alpha=0.85):
            tdem = LSDDEM(path=direc,
                          file_name=fname,
                          already_preprocessed=True)
            mx = np.percentile(tdem.cppdem.get_PP_raster(), 90)
            if (ret):
                return qp.plot_nice_topography(tdem,
                                               figure_width=5,
                                               figure_width_units="inches",
                                               cmap="gist_earth",
                                               hillshade=True,
                                               alpha_hillshade=HS_alpha,
                                               color_min=0,
                                               color_max=mx,
                                               dpi=500,
                                               output="nothing",
                                               format_figure="png",
                                               fontsize_ticks=6,
                                               fontsize_label=8,
                                               hillshade_cmin=None,
                                               hillshade_cmax=None,
                                               fig=figt,
                                               colorbar=True)
            else:
                qp.plot_nice_topography(tdem,
                                        figure_width=5,
                                        figure_width_units="inches",
                                        cmap="gist_earth",
                                        hillshade=True,
                                        alpha_hillshade=HS_alpha,
                                        color_min=0,
                                        color_max=mx,
                                        dpi=500,
                                        output="save",
                                        format_figure="png",
                                        fontsize_ticks=6,
                                        fontsize_label=8,
                                        hillshade_cmin=None,
                                        hillshade_cmax=None,
                                        colorbar=True)

        base_df = pd.read_csv(self.save_dir + self.prefix + "_model_info.csv")
        first_frame = base_df["Frame_num"].iloc[0] if (
            base_df["Frame_num"].iloc[0] != -1
        ) else base_df["Frame_num"].iloc[1]
        tdem = LSDDEM(path=self.save_dir,
                      file_name=self.prefix + str(first_frame) + ".bil",
                      already_preprocessed=True)
        mx = np.percentile(tdem.cppdem.get_PP_raster(), 90)
        fig, ax = qp.plot_nice_topography(tdem,
                                          figure_width=5,
                                          figure_width_units="inches",
                                          cmap="gist_earth",
                                          hillshade=True,
                                          alpha_hillshade=HS_alpha,
                                          color_min=0,
                                          color_max=mx,
                                          dpi=500,
                                          output="return",
                                          format_figure="png",
                                          fontsize_ticks=6,
                                          fontsize_label=8,
                                          hillshade_cmin=None,
                                          hillshade_cmax=None,
                                          colorbar=True)
        moviewriter = FFMpegWriter(fps=fps)
        with moviewriter.saving(fig,
                                self.save_dir + self.prefix + rename_suffix +
                                ".mp4",
                                dpi=dpi):

            for j in base_df["Frame_num"]:
                if (j != -1):
                    plotty(self.save_dir,
                           "%s.bil" % (self.prefix + str(j)),
                           True,
                           fig,
                           HS_alpha=HS_alpha)
                    moviewriter.grab_frame()
                    plt.clf()
Example #10
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!")
        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.

mydem.ExtractRiverNetwork(method="area_threshold", area_threshold_min=500)
mydem.DefineCatchment(method="from_XY",
                      X_coords=[527107, 527033, 530832],
                      Y_coords=[6190656, 6191745, 6191015])
mydem.GenerateChi(theta=0.45, A_0=1)

print("Starting movern extraction")
mydem.cppdem.calculate_movern_disorder(
Example #12
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 #13
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 #14
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!")
Authors: B.G. 
"""

# The following lines import the module we will use. Any python scripts starts with importing other bits of codes we need
from lsdtopytools import LSDDEM # I am telling python I will need this module to run.
from lsdtopytools import quickplot, quickplot_ksn_knickpoints, quickplot_movern # from the same tools, I am importing some quick plotting modules
# You can add any extra dependencies you need here. For example pandas, numpy or matplotlib.

# First step is to load the DEM
## The name "mydem" can be changed into whatever suits you
## Let's have a clean and organised approach and save things into variables
path_to_dem = "/adapt/here/the/path/to/your/dem/" # You need to obviously adapt that path to your case
dem_name = "whatever_name.tif" # You also need to adapt that file name... 
## Now we can load the dem into LSDTopytools: 
### already_preprocessed can be turn to True if you are 100% sure that your dem does not need preprocessing before flow routines
mydem = LSDDEM(path = path_to_dem, file_name = dem_name, already_preprocessed = False)

# Alright the dem is in the system and now needs to be preprocessed (if not done yet)
mydem.PreProcessing(filling = True, carving = True, minimum_slope_for_filling = 0.0001) # Unecessary if already preprocessed of course.

#Need to pregenerate a number of routines, it calculates flow direction, flow accumulation, drainage area , ...
mydem.CommonFlowRoutines()

# This define the river network, it is required to actually calculate other metrics
mydem.ExtractRiverNetwork( method = "area_threshold", area_threshold_min = 1500)

# Defining catchment of interest: it extracts the catchments by outlet coordinates. You also need to adpat these obviously!!
## they need to be in the same coordinate system than the raster.
mydem.DefineCatchment( method="from_XY", X_coords = [532297,521028], Y_coords = [6188085,6196305])

# Calculates chi coordinate with an according theta
from lsdtktools import Window
from tkinter import *
import numpy as np
from matplotlib import cm, pyplot as plt
from lsdtopytools import LSDDEM
from skimage.transform import resize as imresize
# from pillow import image

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!

mydem = LSDDEM(path=my_raster_path,
               file_name=file_name,
               already_preprocessed=True)

root = Tk()
my_window = Window(root)

print("lasjdflajsdlf")

myarray = imresize(mydem.cppdem.get_PP_raster(), (300, 400))

print(np.uint8(cm.gist_earth(myarray) * 255))

my_window.plot_array(myarray)

root.mainloop()
Example #17
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 #18
0
# The following code download a test site in scotland. Replace it with your own raster if you need
# Requires wget, a small python portage of linux command wget to all OSs
# "pip install wget" will install it easily
# import wget
# 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.
Example #19
0
def Polyfit_Metrics():

    # Here are the different parameters and their default value fr this script
    default_param = AGPD.get_common_default_param()
    default_param["window_radius"] = 30

    default_param["average_elevation"] = False
    default_param["slope"] = False
    default_param["aspect"] = False
    default_param["curvature"] = False
    default_param["planform_curvature"] = False
    default_param["profile_curvature"] = False
    default_param["tangential_curvature"] = False
    default_param["TSP"] = False

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

    if (default_param["help"] or len(sys.argv) == 1):
        print("""
			Get the polyfit metrics: average_elevation, slope, aspect, curvature, planform_curvature, profile_curvature, tangential_curvature
			by fitting a plane equation for each pixels through the neighboring node within a distance.
			
			Quick example: lsdtt-polyfits file=myraster.tif slope window_radius=17
			option available:
				file: name of the raster (file=name.tif)
				path: path to the file (default = current folder)
				average_elevation(add keyword to activate): get the average_elevation of the polyfit raster
				slope(add keyword to activate): get the slope of the polyfit raster
				aspect(add keyword to activate): get the aspect of the polyfit raster
				curvature(add keyword to activate): get the curvature of the polyfit raster
				planform_curvature(add keyword to activate): get the planform_curvature of the polyfit raster
				profile_curvature(add keyword to activate): get the profile_curvature of the polyfit raster
				tangential_curvature(add keyword to activate): get the tangential_curvature of the polyfit raster
				window_radius: distance around each pixels to fit the plane

			Future improvements:
			- Automatic plotting option
			- others?
			""")
        quit()

    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=True,
                   verbose=False)

    res = mydem.get_polyfit_metrics(
        window_radius=default_param["window_radius"],
        average_elevation=default_param["average_elevation"],
        slope=default_param["slope"],
        aspect=default_param["aspect"],
        curvature=default_param["curvature"],
        planform_curvature=default_param["planform_curvature"],
        profile_curvature=default_param["profile_curvature"],
        tangential_curvature=default_param["tangential_curvature"],
        TSP=default_param["TSP"],
        save_to_rast=True)

    if (default_param["slope"]):
        fig, ax = plt.subplots()
        cb = ax.imshow(res['slope'],
                       extent=mydem.extent,
                       vmin=np.percentile(res['slope'], 20),
                       vmax=np.percentile(res['slope'], 80))
        QU.fix_map_axis_to_kms(ax, 8, 4)
        plt.colorbar(cb)
        plt.savefig(mydem.path + mydem.prefix + "_slope_%s.png" %
                    (default_param["window_radius"]),
                    dpi=500)
    import wget
    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!

mydem = LSDDEM(
    path=my_raster_path, file_name=file_name
)  # If your dem is already preprocessed: filled or carved basically, add: , is_preprocessed = True
mydem.PreProcessing(filling=True,
                    carving=True,
                    minimum_slope_for_filling=0.0001
                    )  # Unecessary if already preprocessed of course.
mydem.ExtractRiverNetwork(method="area_threshold", area_threshold_min=1500)
mydem.GenerateChi(theta=0.4)
mydem.DefineCatchment(method="from_XY",
                      X_coords=[532297, 521028],
                      Y_coords=[6188085, 6196305],
                      test_edges=False)
mydem.ksn_MuddEtAl2014(target_nodes=70, n_iterations=60, skip=1, nthreads=1)
quickplot_ksn_knickpoints.plot_ksn_map(mydem, ksn_colormod="percentile")

print("Finished")