Catchment#

class mhm_tools.pre.catchment.Catchment(ds, var_name, var='data', ftype=None, transform=None, out_var_name=None, do_shift=False, resolutions: Resolution = None, upscale=False, latlon=True, l0_precision: int = 9)[source]#

Bases: object

Catchment class deliniating catchmetns with pyflowdir.

Methods

add_dem(latlon)

Init the FlwdirRaster class from dem.

add_fdir(latlon)

Init the FlwdirRaster class from fdir.

calc_upstream_area()

Use pyflwdir to calculate the upstream area from flow direction by providing cell areas.

calculate_shape_area_on_current_grid(...[, ...])

Calculate the area covered by a shape on the current grid in km2.

compute_cell_area()

Create a cell area data array in km2.

correct_gauge_location_l1_from_shape(...[, ...])

Correct gauge coordinates at L1 using L0 shape similarity and upstream area.

create_frame(ds[, frame, frame_value])

If a frame is used this frame is set to no data values as a frame.

cut_to_filled_area([buffer, repeat, ...])

Create lat and lon slices to cut the data to the filled area.

delineate_basin(gauge[, max_distance_cells, ...])

Delineate the basin for a given lat and lon.

delineation_sanity_check(catchment_mask, ...)

Perform sanity checks on the delineated basin.

derive_gauge_coords_from_shape(...[, ...])

Use the highest upstream area inside a shape as initial gauge location.

fill_adjacent_missing_with_sink(da, ...)

Replace all missing values adjacent to non-missing values with 0 in an xarray Dataset.

find_best_gauge_location_shape(...[, ...])

Find best gauge location using shape similarity.

get_basins()

Perform the calculation of the catchment ids.

get_best_gauge_coordinate(upstream_area, ...)

Get best gauge coordinates given target catchment area.

get_current_coordinates()

Build L1 coordinate arrays based on the dataset extent and L1 resolution.

get_facc()

Get the flow accumulation area.

get_fdir()

Perform the calculation of the flow direction.

get_grid_area()

Perform the calculation of the catchment area.

get_upscaling_factor([max_resolution, l1, l2])

Create upscaling factor.

get_upstream_area()

Perform the calculation of the upstream catchment area.

processing_data_variable(var_name, cut_by_basin)

Process data variable, masking it and croping it spatial dimensions.

save_coords(gauge_id, gauge_lat, gauge_lon)

Save gauge coordinates.

update_gauge_coords(gauge_id, gauge_lat, ...)

Update stored gauge coordinates for a gauge_id or add it if missing.

upscale(var)

Upscale flow direction to l1_resolution if that is int multipe of data resolution.

upscale_mask_with_correct_coords(da[, ...])

Coarsen a 2D mask-like field by integer factor and assign correct coarse coords.

write(out_path[, single_file, format, ...])

Write the produced data to one or multiple files.

write_basin_id_file(data_vars, frame, out_path)

Write the basin_id file to specified path and set a sink value frame if specified.

write_basin_shape(out_dir[, gauge_id, ...])

Write a basin shapefile from a basin mask.

write_mask_file(ds, mask_file)

Write basin mask to specified path.

write_single_variable_file(data_var, ...)

Write a single data variable to a specified file path.

static create_frame(ds, frame=0, frame_value=0)[source]#

If a frame is used this frame is set to no data values as a frame.

add_dem(latlon)[source]#

Init the FlwdirRaster class from dem.

add_fdir(latlon)[source]#

Init the FlwdirRaster class from fdir.

calc_upstream_area()[source]#

Use pyflwdir to calculate the upstream area from flow direction by providing cell areas.

calculate_shape_area_on_current_grid(reference_shape, shape_label='reference shape')[source]#

Calculate the area covered by a shape on the current grid in km2.

compute_cell_area()[source]#

Create a cell area data array in km2.

correct_gauge_location_l1_from_shape(l0_shape_gdf, gauge_coords, max_distance_cells=5, max_error=0.5, ref_catchment_area=None, reference_upstream_area=None)[source]#

Correct gauge coordinates at L1 using L0 shape similarity and upstream area.

cut_to_filled_area(buffer=0, repeat=False, raise_on_l2_alignment_mismatch=False)[source]#

Create lat and lon slices to cut the data to the filled area.

delineate_basin(gauge, max_distance_cells=5, max_error=0.25, raise_on_sanity_check=True, upstream_area=None, mask_catchment: bool | None = True, save_coords: bool | None = True, gauge_opti_method: str | None = 'basinex', shape_folder: str | None = None, raise_on_fallback: bool | None = True)[source]#

Delineate the basin for a given lat and lon.

delineation_sanity_check(catchment_mask, basin, uparea_at_outlet, ref_catchment_area, max_error, raise_on_sanity_check, gauge_id)[source]#

Perform sanity checks on the delineated basin.

derive_gauge_coords_from_shape(upstream_area, reference_shape, lat_values=None, lon_values=None, shape_label='reference shape')[source]#

Use the highest upstream area inside a shape as initial gauge location.

fill_adjacent_missing_with_sink(da, fill_value, sink_value)[source]#

Replace all missing values adjacent to non-missing values with 0 in an xarray Dataset.

Parameters:

da (xr.Dataset) (Input dataset.)

Returns:

xr.Dataset

Return type:

Dataset with adjacent missing values replaced with 0.

find_best_gauge_location_shape(upstream_area, gauge_coords, ref_catchment_area, shape_folder, gauge_id, max_distance_cells=2, max_error=0.25, reference_shape_gdf=None, lat_values=None, lon_values=None, limit_by_error=True, started_from_shape=False)[source]#

Find best gauge location using shape similarity.

get_basins()[source]#

Perform the calculation of the catchment ids.

get_best_gauge_coordinate(upstream_area, gauge_coords, ref_catchment_area, max_distance_cells, max_error, method, shape_folder=None, gauge_id=None, raise_on_fallback=True)[source]#

Get best gauge coordinates given target catchment area.

get_current_coordinates()[source]#

Build L1 coordinate arrays based on the dataset extent and L1 resolution.

get_facc()[source]#

Get the flow accumulation area.

get_fdir()[source]#

Perform the calculation of the flow direction.

get_grid_area()[source]#

Perform the calculation of the catchment area.

get_upscaling_factor(max_resolution=False, l1=False, l2=True)[source]#

Create upscaling factor.

get_upstream_area()[source]#

Perform the calculation of the upstream catchment area.

processing_data_variable(var_name, cut_by_basin, lat_slice=None, lon_slice=None, lat_slice_idx=None, lon_slice_idx=None)[source]#

Process data variable, masking it and croping it spatial dimensions.

save_coords(gauge_id, gauge_lat, gauge_lon)[source]#

Save gauge coordinates.

update_gauge_coords(gauge_id, gauge_lat, gauge_lon)[source]#

Update stored gauge coordinates for a gauge_id or add it if missing.

upscale(var)[source]#

Upscale flow direction to l1_resolution if that is int multipe of data resolution.

upscale_mask_with_correct_coords(da: DataArray, factor: int | None = None, lon_name: str = 'lon', lat_name: str = 'lat') DataArray[source]#

Coarsen a 2D mask-like field by integer factor and assign correct coarse coords.

so that coarse edges equal fine edges of the cropped window.

write(out_path, single_file=True, format='nc', cellsize=None, cut_by_basin=False, mask_file=None, frame=1, buffer=0, variables=None, id_gauges_out_path=None)[source]#

Write the produced data to one or multiple files.

write_basin_id_file(data_vars, frame, out_path)[source]#

Write the basin_id file to specified path and set a sink value frame if specified.

write_basin_shape(out_dir, gauge_id=None, basin_mask=None)[source]#

Write a basin shapefile from a basin mask.

write_mask_file(ds, mask_file)[source]#

Write basin mask to specified path.

write_single_variable_file(data_var, var_name, out_path, cellsize, format)[source]#

Write a single data variable to a specified file path.