def plot_all_tif_of_folder(): # Here are the different parameters and their default value fr this script default_param = AGPD.get_common_default_param() default_param["n_threads"] = 4 default_param = AGPD.ingest_param(default_param, sys.argv) file_to_process = [] suffix = [] cpt = 1 for file in glob.glob("*.tif"): file_to_process.append((file, )) name = str(cpt) cpt += 1 for file in glob.glob("*.tiff"): file_to_process.append((file, )) if (len(file_to_process) == 0): print("I did not find any file! Aborting.") quit() with multiprocessing.Pool( processes=int(default_param["n_threads"])) as pool: pool.starmap(load_n_plot, file_to_process) print("Done with all the rasters")
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 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')
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)
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')
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!")
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 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)
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)
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!")
def concavity_multiple_basin(): """ Command-line tool to constrain concavity for a multiple basin from their XY coordinates using Normalised disorder method. 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) csv=outlets.csv -> Name of the csv file containing the following columns: "X", "Y" and "area_threshold" for each basins to investigate. Can be generated automatically from lsdtt-concFFS-spawn-outlets n_proc=4 -> DEFAULT is 1. Number of processors to use in parallel when possible. prefix=test -> OPTIONAL Add a prefix to each outputted file (handy for automation) mode=g -> DEFAULT is g . Processing mode: can be "g" for generating data, "p" or plotting previously generated data, "d" for plotting disorder map (WARNING takes time and memory) "all" for everything. Example: lsdtt-concFFS-multiple file=DEM.tif already_preprocessed csv=FeatherRiveroutlets.csv prefix=FeatherRiver mode=g """ # 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["prefix"] = "" default_param["csv"] = "" default_param["n_proc"] = 1 default_param["area_thershold_basin_extraction"] = 500 default_param["precipitation_file"] = "" default_param["mode"] = "g" default_param["n_tribs_by_combinations"] = 3 default_param = AGPD.ingest_param(default_param, sys.argv) prefix = default_param["prefix"] if (default_param["help"] or len(sys.argv) == 1 or "help" in sys.argv): print(""" Command-line tool to constrain concavity for a multiple basin from their XY coordinates using Normalised disorder method. 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) csv=outlets.csv -> Name of the csv file containing the following columns: "X", "Y" and "area_threshold" for each basins to investigate. Can be generated automatically from lsdtt-concFFS-spawn-outlets n_proc=4 -> DEFAULT is 1. Number of processors to use in parallel when possible. prefix=test -> OPTIONAL Add a prefix to each outputted file (handy for automation) mode=g -> DEFAULT is g . Processing mode: can be "g" for generating data, "p" or plotting previously generated data, "all" for everything. Example: lsdtt-concFFS-multiple file=DEM.tif already_preprocessed csv=FeatherRiveroutlets.csv prefix=FeatherRiver mode=g """) # Reading the csv file df = pd.read_csv(default_param["csv"]) # Reformatting stuff n_proc = int(default_param["n_proc"]) default_param["n_tribs_by_combinations"] = int( default_param["n_tribs_by_combinations"]) area_thershold_basin_extraction = float( default_param["area_thershold_basin_extraction"]) dem_name = default_param["file"] if (default_param["precipitation_file"] == ""): precipitation = False else: precipitation = True # Processing options if ("all" in default_param["mode"].lower() or "g" in default_param["mode"].lower()): lsd.concavity_automator.process_multiple_basins( dem_name, dem_path="./", already_preprocessed=default_param["already_preprocessed"], prefix=default_param["prefix"], X_outlets=df["X"].values, Y_outlets=df["Y"].values, n_proc=n_proc, area_threshold=df["area_threshold"].values, area_thershold_basin_extraction=area_thershold_basin_extraction, n_tribs_by_combo=default_param["n_tribs_by_combinations"], use_precipitation_raster=precipitation, name_precipitation_raster=default_param["precipitation_file"]) lsd.concavity_automator.post_process_analysis_for_Dstar( default_param["prefix"], n_proc=n_proc, base_raster_full_name=dem_name) if ("z" in default_param["mode"].lower()): lsd.concavity_automator.post_process_analysis_for_Dstar( default_param["prefix"], n_proc=n_proc, base_raster_full_name=dem_name) if ("p" in default_param["mode"].lower()): lsd.concavity_automator.plot_main_figures(default_param["prefix"]) if ("all" in default_param["mode"].lower() or "d" in default_param["mode"].lower()): lsd.concavity_automator.plot_multiple_basins( dem_name, dem_path="./", already_preprocessed=default_param["already_preprocessed"], prefix=default_param["prefix"], X_outlets=df["X"].values, Y_outlets=df["Y"].values, n_proc=n_proc, area_threshold=df["area_threshold"].values, area_thershold_basin_extraction=area_thershold_basin_extraction, plot_Dstar=False, n_tribs_by_combo=default_param["n_tribs_by_combinations"]) lsd.concavity_automator.plot_Dstar_maps_for_all_concavities( default_param["prefix"], n_proc=n_proc) # Saving logs for key, val in default_param.items(): default_param[key] = [val] pd.DataFrame(default_param).to_csv( prefix + "log_concavity_FFS_multiple_basin_sstuff.txt", index=False)
def concavity_single_basin(): """ Command-line tool to constrain concavity for a single basin from its XY coordinates using Normalised disorder method. 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) 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) AT=5000 -> Area threshold in number of pixel to initiate a river: lower nummber <=> denser network (quickly increases the processing time) prefix=test -> OPTIONAL Add a prefix to each outputted file (handy for automation) mode=g -> DEFAULT is g . Processing mode: can be "g" for generating data, "p" or plotting previously generated data or "all" for everything. Example: lsdtt-concFFS-single file=DEM.tif already_preprocessed X=43422 Y=5353497 AT=2500 prefix=FeatherRiver mode=all """ # 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"] = None default_param["Y"] = None default_param["AT"] = None default_param["prefix"] = "" default_param["mode"] = "g" default_param["n_tribs_by_combinations"] = 3 # Ingesting the parameters default_param = AGPD.ingest_param(default_param, sys.argv) prefix = default_param["prefix"] if (default_param["help"] or len(sys.argv) == 1 or "help" in sys.argv): print(""" Command-line tool to constrain concavity for a single basin from its XY coordinates using Normalised disorder method. 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) 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) AT=5000 -> Area threshold in number of pixel to initiate a river: lower nummber <=> denser network (quickly increases the processing time) prefix=test -> OPTIONAL Add a prefix to each outputted file (handy for automation) mode=g -> DEFAULT is g . Processing mode: can be "g" for generating data, "p" or plotting previously generated data or "all" for everything. Example: lsdtt-concFFS-single file=DEM.tif already_preprocessed X=43422 Y=5353497 AT=2500 prefix=FeatherRiver mode=all """) quit() # Reformatting some values that sometimes are not formatted correctly X = float(default_param["X"]) Y = float(default_param["Y"]) area_threshold = float(default_param["AT"]) dem_name = default_param["file"] default_param["n_tribs_by_combinations"] = int( default_param["n_tribs_by_combinations"]) # Wrapper to the processing function (the convoluted ls method makes multiprocessing easier when processing several basins) ls = [0, X, Y, area_threshold, prefix] # Calling th requested codes if ("all" in default_param["mode"].lower() or "g" in default_param["mode"].lower()): lsd.concavity_automator.process_basin( ls, ignore_numbering=True, overwrite_dem_name=dem_name, n_tribs_by_combo=default_param["n_tribs_by_combinations"]) if ("all" in default_param["mode"].lower() or "p" in default_param["mode"].lower()): lsd.concavity_automator.plot_basin(ls, ignore_numbering=True, overwrite_dem_name=dem_name) # Saving a log of processing for key, val in default_param.items(): default_param[key] = [val] pd.DataFrame(default_param).to_csv( prefix + "log_concavity_FFS_single_basin_sstuff.txt", index=False)
def temp_concavity_FFS_all(): # 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["process_main"] = False default_param["X"] = None default_param["Y"] = None default_param["AT"] = None default_param["ATM"] = None default_param["n_proc"] = None default_param["min_DA"] = None default_param["max_DA"] = None default_param["prefix"] = "" prefix = 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(""" Experimental command-line tools for concavity constraining. Deprecated now. """) quit() X = float(default_param["X"]) Y = float(default_param["Y"]) min_DA = float(default_param["min_DA"]) max_DA = float(default_param["max_DA"]) area_threshold_main_basin = float(default_param["ATM"]) area_threshold = float(default_param["AT"]) n_proc = int(default_param["n_proc"]) dem_name = default_param["file"] if (default_param["process_main"]): lsd.concavity_automator.process_main_basin( dem_name, dem_path="./", already_preprocessed=True, X_outlet=X, Y_outlet=Y, area_threshold=area_threshold, area_threshold_to_keep_river=area_threshold_main_basin, prefix=default_param["prefix"]) else: lsd.concavity_automator.get_all_concavity_in_range_of_DA_from_baselevel( dem_name, dem_path="./", already_preprocessed=True, X_outlet=X, Y_outlet=Y, min_DA=min_DA, max_DA=max_DA, area_threshold=area_threshold, n_proc=n_proc, prefix=default_param["prefix"]) lsd.concavity_automator.post_process_basins( prefix=default_param["prefix"]) for key, val in default_param.items(): default_param[key] = [val] pd.DataFrame(default_param).to_csv(prefix + "log_concavity_FFS_sstuff.txt", index=False)
def csv_conversions(): """ 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 """ # Different mode usable for the conversion so far possible_modes = ["feather2csv", "csv2feather"] # Default parameters default_param = AGPD.get_common_default_param() default_param["mode"] = "feather2csv" default_param["all"] = False default_param["extension"] = ".feather" # Calling the parameter parser ingester default_param = AGPD.ingest_param(default_param, sys.argv) # Help message if(default_param["help"] or len(sys.argv)==1): print(""" This command-line tool is a command line tool to convert from and to csv using pandas engine. Example: lsdtt-csv-conversion mode=feather2csv file=myfile.feather Note that you force conversion of all files with an extension with the option all and extension=.feather: lsdtt-csv-conversion mode=feather2csv all extension=.feather """) print("Possible conversion mode are:", possible_modes) print(""" Different format descriptions: feather files: Very efficient file structure by apache arrow. Fast I/O, optimised memory, preferred format for the code. However not readable by human. csv: Most user-friendly format: compatible with excel or any text editor, however much slower to process by softwares and quickly takes space on disk. """) quit() if(default_param["mode"] not in possible_modes): print("I did not understand your conversion mode, it needs to be one of the followings:") print(possible_modes) return 0 #Starting the list of file to converts file_to_convert = [] # If all, I am getting all the files with a certain extention if(default_param["all"]): for file in glob.glob(default_param["extension"]): file_to_convert.append(file) else: # Otherwise just the requested files file_to_convert.append(default_param["file"]) ##### COnvert feather files (very fast to process by the code but not readable by humans) to csv if(default_param["mode"] == "feather2csv"): print("converting", file_to_convert,"from feather to csv") for file in file_to_convert: print("converting file:", file) df = pd.read_feather(file) df.to_csv(file.split(".")[:-1]+".csv", index = False) ##### convert csv to feather if(default_param["mode"] == "csv2feather"): for file in file_to_convert: print("converting file:", file) df = pd.read_csv(file) df.to_feather(file.split(".")[:-1]+".feather")
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)
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)