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 1 commit
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
49 changes: 49 additions & 0 deletions xdem/volume.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from __future__ import annotations

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd


def hypsometric_binning(ddem: np.ndarray, dem: np.ndarray):

assert ddem.shape == dem.shape
nan_mask = np.isnan(ddem) | np.isnan(dem)
ddem = ddem[~nan_mask]
dem = dem[~nan_mask]

old_dem = dem - ddem
elevation_range = np.percentile(old_dem, 99) - np.percentile(old_dem, 1)

bin_size = 50 if elevation_range > 500 else elevation_range / 10

bins = np.arange(old_dem.min(), old_dem.max() + bin_size, step=bin_size)
indices = np.digitize(old_dem, bins=bins)

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.unique(indices):
values_in_bin = ddem[indices == i]
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]

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

# more options can be specified also
print(output.to_string())
raise ValueError

return output