Skip to main content

A Python package for analyzing upstream basins and resampling DEM data

Project description

User Guide for Analyzing Upstream Basin

This guide provides instructions for using the tool to delineate upstream catchment areas from Digital Elevation Model (DEM) data. The tool processes DEM data, visualizes catchment areas, and optionally resamples the DEM for improved analysis.

Requirements

To use this tool, ensure you have the following Python packages installed:

  • numpy: pip install numpy
  • pysheds: pip install pysheds
  • matplotlib: pip install matplotlib
  • scipy: pip install scipy
  • pyproj: pip install pyproj

Function Overview

Analyze Upstream Basin

from upstream_analyzer.analyze_basin import analyze_upstream_basin

analyze_upstream_basin(asc_file_path, col, row, threshold, xytype='index', crs='EPSG:4326', clip_to=False, new_asc_file_path=None)

Parameters

  • asc_file_path (str): Path to the DEM ASCII file.
  • col (int/float): Column coordinate or longitude value.
  • row (int/float): Row coordinate or latitude value.
  • threshold (int): Minimum flow accumulation threshold for identifying high-accumulation cells.
  • xytype (str, optional): Type of coordinates used. Set to 'index' for grid cell indices or 'coordinate' for geographic coordinates. Default is 'index'.
  • crs (str, optional): EPSG code of the coordinate reference system (e.g., "EPSG:32610"). Default is "EPSG:4326".
  • clip_to (bool, optional): If set to True, the catchment area will be clipped for visualization.
  • new_asc_file_path (str, optional): Path to save the processed DEM file in ASCII format. If specified, the filled and resolved DEM will be saved to this path.

Output

The function displays a plot of the upstream catchment area. The plot includes a color-coded representation of the catchment with flow accumulation. If new_asc_file_path is provided, the processed DEM will be saved to the specified file.

Resample DEM

from upstream_analyzer.analyze_basin import resample_dem

resample_dem(input_asc, resample_asc, crs="EPSG:4326", scale_factor=0.5, interpolation_order=1)

Parameters

  • input_asc (str): Path to the input DEM ASCII file.
  • resample_asc (str): Path to save the resampled DEM ASCII file.
  • crs (str, optional): EPSG code of the coordinate reference system (e.g., "EPSG:32610"). Default is "EPSG:4326".
  • scale_factor (float, optional): Scaling factor for resampling. Default is 0.5 (reduces resolution by half).
  • interpolation_order (int, optional): Interpolation method to use (0 - nearest, 1 - bilinear, etc.). Default is 1.

Output

The function saves the resampled DEM to the specified output file.

Instructions

1. Analyze Upstream Basin

The function requires a DEM ASCII file path and coordinates for the outlet point. You have two options for specifying the coordinates:

Option 1: Using Grid Index Coordinates

Specify the outlet point using grid index coordinates (col and row).

# Define the input parameters
asc_file_path = 'WA_Samish/Data_Inputs90m/m_1_DEM/Samish_DredgeMask_EEX.asc'
col, row = 108, 207
threshold = 500

# Call the function
analyze_upstream_basin(asc_file_path, col, row, threshold, xytype='index')

Option 2: Using Geographic Coordinates

Specify the outlet point using geographic coordinates (latitude and longitude) and transform them to the target CRS.

# Define the input parameters
asc_file_path = 'WA_Samish/Data_Inputs90m/m_1_DEM/Samish_DredgeMask_EEX.asc'
lat, long = 48.54594127, -122.3382169
threshold = 500

# Call the function with coordinate transformation and save processed DEM to a new file
target_crs = "EPSG:32610"
new_asc_file_path = 'New_Samish.asc'
analyze_upstream_basin(asc_file_path, long, lat, threshold, xytype='coordinate', crs=target_crs, new_asc_file_path=new_asc_file_path)

2. Resample DEM

The resample_dem function allows you to resample the resolution of a DEM file to a new ASCII file.

# Define the input parameters
input_asc = 'WA_Samish/Data_Inputs90m/m_1_DEM/Samish_DredgeMask_EEX.asc'
resample_asc = 'Resample_dem.asc'

# Resample the DEM to half the original resolution
resample_dem(input_asc, resample_asc, crs="EPSG:32610", scale_factor=0.5)

3. Explanation of Processing Steps

  1. Initialize Grid and Read DEM: The DEM data is read using PySheds.
  2. Fill Pits in DEM: Pits (local depressions) in the DEM are filled to ensure proper flow direction calculations.
  3. Fill Depressions and Resolve Flats: Depressions are filled, and flat areas are resolved for accurate flow calculations.
  4. Compute Flow Direction: Flow direction is calculated based on the processed DEM.
  5. Extract Upstream Basin:
    • For xytype = 'coordinate', the specified outlet point is snapped to the nearest high-accumulation cell, with optional CRS transformation.
    • For xytype = 'index', the outlet point is treated as a grid cell index.
  6. Visualize Catchment Area: The catchment area and flow accumulation data are visualized.

4. Visualization

The function will produce a plot displaying the upstream basin and flow accumulation values.

  • Blues Color Map: Indicates the extent of the catchment area.
  • Binary Color Map: Represents flow accumulation values greater than the specified threshold.

The visualization provides a clear indication of the upstream area that contributes to the specified outlet point.

Example Outputs

The following images represent typical outputs from the function:

Upstream Basin from Specified Outlet
Figure: Upstream Basin from Specified Outlet

Notes

  • Ensure that the DEM ASCII file is accessible and correctly formatted.
  • The threshold value affects the snapping process for geographic coordinates; a higher value will snap the outlet point to cells with greater flow accumulation.
  • The coordinate transformation is performed using the PyProj library.
  • If new_asc_file_path is provided, the processed DEM file will be saved for further use.

Troubleshooting

  • FileNotFoundError: Verify that the provided DEM file path is correct.
  • IndexError: Ensure the provided coordinates are within the grid bounds.
  • Coordinate Transformation Issues: Ensure that the crs value is a valid EPSG code.

Additional Resources

Feel free to customize the parameters as needed to explore different areas of interest within your DEM data!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

upstream_analyzer-0.1.2.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

upstream_analyzer-0.1.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file upstream_analyzer-0.1.2.tar.gz.

File metadata

  • Download URL: upstream_analyzer-0.1.2.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for upstream_analyzer-0.1.2.tar.gz
Algorithm Hash digest
SHA256 dedd0377e27ab2d3ca26fa3644decde1031d1355d7d6d59bf30026d293c36ae8
MD5 ec78b8ea4bd20ab117f8b404cd753a08
BLAKE2b-256 e7e91cade0befd16bc8ca406f7bfddee16a9279ce732c4b4416e0140224c1efd

See more details on using hashes here.

File details

Details for the file upstream_analyzer-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for upstream_analyzer-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 593280c4239e98c5c023dd6f0f3a0c8fc35a4094eb5fbfb355e8a5a221af4492
MD5 188648935f5bf39ae8bb99653282e93d
BLAKE2b-256 0174e276ab15e1978755d7e7bcc2ab79f40f627335117f8e9a0f0fe0d6927175

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page