Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local hypsometric interpolation #36

Merged
merged 17 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
5e23674
Added hypsometry binning function
erikmannerfelt Mar 10, 2021
786b385
Added bin size (in georeferenced units or percent) as an argument and…
erikmannerfelt Mar 10, 2021
5853875
Added NMAD calculation. Updated documentation. Added masked array che…
erikmannerfelt Mar 10, 2021
115fdf6
Added hypsometric bin interpolation and updated tests
erikmannerfelt Mar 10, 2021
b7ee471
Improved documentation, masked_array compatibility, and added test fo…
erikmannerfelt Mar 10, 2021
80d9bae
Changed the hypsometry aggregation functions to be any callable; now …
erikmannerfelt Mar 10, 2021
efd4e21
Added representative hypsometry area function.
erikmannerfelt Mar 10, 2021
9bd3317
Removed duplicated test
erikmannerfelt Mar 11, 2021
c98858e
Added timeframe argument to calculate_hypsometry_area()
erikmannerfelt Mar 11, 2021
02754e1
Merge branch 'main' into local_hypsometric
erikmannerfelt Mar 12, 2021
5ba2410
Merge branch 'main' of https://github.com/GlacioHack/xdem into local_…
erikmannerfelt Mar 15, 2021
ecdfdea
Merge branch 'local_hypsometric' of github.com:erikmannerfelt/xdem in…
erikmannerfelt Mar 15, 2021
3b33d4b
Revised binning kind arguments. Added 'equal area' binning and 'custo…
erikmannerfelt Mar 15, 2021
14a4217
Changed binning kind 'equal_count' to 'fixed_count'
erikmannerfelt Mar 15, 2021
60a466f
Removed temporary warning suppression
erikmannerfelt Mar 15, 2021
5646dca
Changed hypsometric method names
erikmannerfelt Mar 15, 2021
4f1339e
Changed binning and area behaviour to use only the reference DEM as t…
erikmannerfelt Mar 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions tests/test_volume.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import geoutils as gu

import xdem


def test_local_hypsometric():
xdem.examples.download_longyearbyen_examples(overwrite=False)

dem_2009 = gu.georaster.Raster(xdem.examples.FILEPATHS["longyearbyen_ref_dem"])
dem_1990 = gu.georaster.Raster(xdem.examples.FILEPATHS["longyearbyen_tba_dem"]).reproject(dem_2009)

outlines = gu.geovector.Vector(xdem.examples.FILEPATHS["longyearbyen_glacier_outlines"])
# Filter to only look at the Scott Turnerbreen glacier
outlines.ds = outlines.ds.loc[outlines.ds["NAME"] == "Scott Turnerbreen"]

mask = outlines.create_mask(dem_2009) == 255

ddem = dem_2009.data - dem_1990.data

ddem_bins = xdem.volume.hypsometric_binning(ddem.squeeze()[mask], dem_2009.data.squeeze()[mask])
5 changes: 1 addition & 4 deletions xdem/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
from . import coreg
from . import spatial_tools
from . import dem
from . import examples
from . import coreg, dem, examples, spatial_tools, volume
67 changes: 67 additions & 0 deletions xdem/volume.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from __future__ import annotations

import numpy as np
import pandas as pd


def hypsometric_binning(ddem: np.ndarray, dem: np.ndarray, bin_size=50,
normalized_bin_size: bool = False) -> pd.DataFrame:
"""
Separate the dDEM in discrete elevation bins.

:param ddem: The dDEM as a 2D or 1D array.
:param dem: The reference DEM as a 2D or 1D array.
:param bin_size: The bin interval size in georeferenced units (or percent; 0-100, if normalized_bin_size=True)
:param normalized_bin_size: If the given bin size should be parsed as a percentage of the glacier's elevation range.

:returns: A Pandas DataFrame with elevation bins and dDEM statistics.
"""

assert ddem.shape == dem.shape
# Remove all nans, and flatten the inputs.
nan_mask = np.isnan(ddem) | np.isnan(dem)
ddem = ddem[~nan_mask]
dem = dem[~nan_mask]

# Calculate the mean representative elevations between the two DEMs
mean_dem = dem - (ddem / 2)

# If the bin size should be seen as a percentage.
if normalized_bin_size:
assert bin_size > 0 and bin_size < 100

# Get the statistical elevation range to normalize the bin size with
elevation_range = np.percentile(mean_dem, 99) - np.percentile(mean_dem, 1)
bin_size = elevation_range / bin_size

# Generate bins and get bin indices from the mean DEM
bins = np.arange(mean_dem.min(), mean_dem.max() + bin_size, step=bin_size)
indices = np.digitize(mean_dem, bins=bins)

# Calculate statistics for each bin.
# If no values exist, all stats should be nans (except count with should be 0)
medians = np.zeros(shape=bins.shape[0] - 1, dtype=ddem.dtype) + np.nan
means = medians.copy()
stds = medians.copy()
counts = np.zeros_like(medians, dtype=int)
for i in np.arange(indices.min(), indices.max() + 1):
values_in_bin = ddem[indices == i]
# Skip if no values are in the bin.
if values_in_bin.shape[0] == 0:
continue

medians[i - 1] = np.median(values_in_bin)
means[i - 1] = np.mean(values_in_bin)
stds[i - 1] = np.std(values_in_bin)
counts[i - 1] = values_in_bin.shape[0]

# Collect the results in a dataframe
output = pd.DataFrame(
index=pd.IntervalIndex.from_breaks(bins),
data=np.vstack([
medians, means, stds, counts
]).T,
columns=["median", "mean", "std", "count"]
)

return output