-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'GlacioHack:main' into main
- Loading branch information
Showing
37 changed files
with
3,823 additions
and
1,252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# This workflow will install Python dependencies, run tests and lint with a single version of Python | ||
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions | ||
|
||
name: build | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
build: | ||
name: ${{ matrix.os }}, python ${{ matrix.python-version}} | ||
runs-on: ${{ matrix.os }} | ||
|
||
strategy: | ||
matrix: | ||
os: ["ubuntu-latest"] | ||
python-version: ["3.7", "3.8", "3.9"] | ||
|
||
|
||
# Run all shells using bash (including Windows) | ||
defaults: | ||
run: | ||
shell: bash -l {0} | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
# Set up the conda-forge environment with mamba | ||
- name: Set up Python ${{ matrix.python-version }} and dependencies | ||
uses: conda-incubator/setup-miniconda@v2 | ||
with: | ||
auto-update-conda: true | ||
python-version: ${{ matrix.python-version }} | ||
mamba-version: "*" | ||
channels: conda-forge | ||
channel-priority: strict | ||
environment-file: dev-environment.yml | ||
activate-environment: xdem-dev | ||
|
||
- name: Install project | ||
run: | | ||
pip install . --no-dependencies | ||
- name: Lint with flake8 | ||
run: | | ||
# stop the build if there are Python syntax errors or undefined names | ||
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics | ||
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide | ||
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics | ||
- name: Test with pytest | ||
run: | | ||
pytest -rA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# .readthedocs.yml | ||
# Read the Docs configuration file | ||
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details | ||
|
||
# Required | ||
version: 2 | ||
|
||
# Build documentation in the docs/ directory with Sphinx | ||
sphinx: | ||
configuration: docs/source/conf.py | ||
fail_on_warning: false | ||
|
||
# Optionally build your docs in additional formats such as PDF and ePub | ||
formats: [] | ||
|
||
python: | ||
version: 3.7 | ||
install: | ||
- requirements: dev-requirements.txt | ||
- method: pip | ||
path: . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: xdem-dev | ||
channels: | ||
- conda-forge | ||
dependencies: | ||
- python>=3.7 | ||
- proj>=7.2 | ||
- geopandas | ||
- numba | ||
- matplotlib | ||
- opencv | ||
- pyproj | ||
- rasterio | ||
- scipy | ||
- tqdm | ||
- scikit-image | ||
- proj-data | ||
- scikit-gstat | ||
- pytransform3d | ||
- geoutils | ||
|
||
# Development-specific | ||
- pytest | ||
- pytest-xdist | ||
- sphinx | ||
- sphinx_rtd_theme | ||
- numpydoc | ||
- sphinxcontrib-programoutput | ||
- flake8 | ||
- sphinx-autodoc-typehints |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
"""Example code for the DEM comparison chapter (it's made like this to test the syntax).""" | ||
####################### | ||
# SECTION: Example data | ||
####################### | ||
from datetime import datetime | ||
|
||
import geoutils as gu | ||
import numpy as np | ||
|
||
import xdem | ||
|
||
# Download the necessary data. This may take a few minutes. | ||
xdem.examples.download_longyearbyen_examples(overwrite=False) | ||
|
||
# Load a reference DEM from 2009 | ||
dem_2009 = xdem.DEM(xdem.examples.FILEPATHS["longyearbyen_ref_dem"], datetime=datetime(2009, 8, 1)) | ||
# Load a DEM from 1990 | ||
dem_1990 = xdem.DEM(xdem.examples.FILEPATHS["longyearbyen_tba_dem"], datetime=datetime(1990, 8, 1)) | ||
# Load glacier outlines from 1990. | ||
glaciers_1990 = gu.Vector(xdem.examples.FILEPATHS["longyearbyen_glacier_outlines"]) | ||
glaciers_2010 = gu.Vector(xdem.examples.FILEPATHS["longyearbyen_glacier_outlines_2010"]) | ||
|
||
# Make a dictionary of glacier outlines where the key represents the associated date. | ||
outlines = { | ||
datetime(1990, 8, 1): glaciers_1990, | ||
datetime(2009, 8, 1): glaciers_2010, | ||
} | ||
|
||
# Fake a future DEM to have a time-series of three DEMs | ||
dem_2060 = dem_2009.copy() | ||
# Assume that all glacier values will be 30 m lower than in 2009 | ||
dem_2060.data[glaciers_2010.create_mask(dem_2060)] -= 30 | ||
dem_2060.datetime = datetime(2060, 8, 1) | ||
|
||
############################## | ||
# SECTION: Subtracting rasters | ||
############################## | ||
|
||
dem1, dem2 = dem_2009, dem_1990 | ||
|
||
ddem_data = dem1.data - dem2.data | ||
# If we want to inherit the georeferencing information: | ||
ddem_raster = xdem.DEM.from_array(ddem_data, dem1.transform, dem2.crs) | ||
|
||
# TEXT HERE | ||
|
||
ddem_raster = xdem.spatial_tools.subtract_rasters(dem1, dem2) | ||
|
||
############################# | ||
# SECTION: dDEM interpolation | ||
############################# | ||
|
||
ddem = xdem.dDEM( | ||
raster=xdem.spatial_tools.subtract_rasters(dem_2009, dem_1990), | ||
start_time=dem_1990.datetime, | ||
end_time=dem_2009.datetime | ||
) | ||
|
||
# The example DEMs are void-free, so let's make some random voids. | ||
ddem.data.mask = np.zeros_like(ddem.data, dtype=bool) # Reset the mask | ||
# Introduce 50000 nans randomly throughout the dDEM. | ||
ddem.data.mask.ravel()[np.random.choice(ddem.data.size, 50000, replace=False)] = True | ||
|
||
# SUBSECTION: Linear spatial interpolation | ||
|
||
ddem.interpolate(method="linear") | ||
|
||
# SUBSECTION: Local hypsometric interpolation | ||
|
||
ddem.interpolate(method="local_hypsometric", reference_elevation=dem_2009, mask=glaciers_1990) | ||
|
||
# SUBSECTION: Regional hypsometric interpolation | ||
|
||
ddem.interpolate(method="regional_hypsometric", reference_elevation=dem_2009, mask=glaciers_1990) | ||
|
||
################################### | ||
# SECTION: The DEMCollection object | ||
################################### | ||
|
||
dems = xdem.DEMCollection( | ||
[dem_1990, dem_2009, dem_2060], | ||
outlines=outlines, | ||
reference_dem=dem_2009 | ||
) | ||
|
||
# TEXT HERE | ||
|
||
dems.subtract_dems() | ||
dems.get_cumulative_series(kind="dh", outlines_filter="NAME == 'Scott Turnerbreen'") | ||
|
||
# Create an object that can be printed in the documentation. | ||
scott_series = dems.get_cumulative_series(kind="dh", outlines_filter="NAME == 'Scott Turnerbreen'") |
58 changes: 58 additions & 0 deletions
58
docs/source/code/comparison_plot_local_hypsometric_interpolation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"""Plot an example of local hypsometric interpolation at Scott Turnerbreen, Svalbard.""" | ||
import geoutils as gu | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
import xdem | ||
|
||
xdem.examples.download_longyearbyen_examples(overwrite=False) | ||
|
||
dem_2009 = xdem.DEM(xdem.examples.FILEPATHS["longyearbyen_ref_dem"]) | ||
dem_1990 = xdem.DEM(xdem.examples.FILEPATHS["longyearbyen_tba_dem"]) | ||
outlines_1990 = gu.Vector(xdem.examples.FILEPATHS["longyearbyen_glacier_outlines"]) | ||
|
||
ddem = xdem.dDEM( | ||
xdem.spatial_tools.subtract_rasters(dem_2009, dem_1990, resampling_method="nearest"), | ||
start_time=np.datetime64("1990-08-01"), | ||
end_time=np.datetime64("2009-08-01") | ||
) | ||
|
||
ddem.data /= (2009 - 1990) | ||
|
||
scott_1990 = outlines_1990.query("NAME == 'Scott Turnerbreen'") | ||
mask = scott_1990.create_mask(ddem) | ||
|
||
ddem_bins = xdem.volume.hypsometric_binning(ddem.data[mask], dem_2009.data[mask]) | ||
stds = xdem.volume.hypsometric_binning(ddem.data[mask], dem_2009.data[mask], aggregation_function=np.std) | ||
|
||
plt.figure(figsize=(8, 8)) | ||
plt.grid(zorder=0) | ||
plt.plot(ddem_bins["value"], ddem_bins.index.mid, linestyle="--", zorder=1) | ||
|
||
plt.barh( | ||
y=ddem_bins.index.mid, | ||
width=stds["value"], | ||
left=ddem_bins["value"] - stds["value"] / 2, | ||
height=(ddem_bins.index.left - ddem_bins.index.right) * 1, | ||
zorder=2, | ||
edgecolor="black", | ||
) | ||
for bin in ddem_bins.index: | ||
plt.vlines(ddem_bins.loc[bin, "value"], bin.left, bin.right, color="black", zorder=3) | ||
|
||
plt.xlabel("Elevation change (m / a)") | ||
plt.twiny() | ||
plt.barh( | ||
y=ddem_bins.index.mid, | ||
width=ddem_bins["count"] / ddem_bins["count"].sum(), | ||
left=0, | ||
height=(ddem_bins.index.left - ddem_bins.index.right) * 1, | ||
zorder=2, | ||
alpha=0.2, | ||
) | ||
plt.xlabel("Normalized area distribution (hypsometry)") | ||
|
||
plt.ylabel("Elevation (m a.s.l.)") | ||
|
||
plt.tight_layout() | ||
plt.show() |
Oops, something went wrong.