Exemplo n.º 1
0
class TestdDEM:
    dem_2009 = xdem.DEM(xdem.examples.get_path("longyearbyen_ref_dem"))
    dem_1990 = xdem.DEM(xdem.examples.get_path("longyearbyen_tba_dem"))
    outlines_1990 = gu.Vector(xdem.examples.get_path("longyearbyen_glacier_outlines"))

    ddem = xdem.dDEM(
        dem_2009 - dem_1990,
        start_time=np.datetime64("1990-08-01"),
        end_time=np.datetime64("2009-08-01")
    )

    def test_init(self):
        """Test that the dDEM object was instantiated correctly."""
        assert isinstance(self.ddem, xdem.dDEM)
        assert isinstance(self.ddem.data, np.ma.masked_array)

    def test_copy(self):
        """Test that copying works as it should."""
        ddem2 = self.ddem.copy()

        assert isinstance(ddem2, xdem.dDEM)

        ddem2.data += 1

        assert self.ddem != ddem2

    def test_filled_data(self):
        """Test that the filled_data property points to the right data."""
        ddem2 = self.ddem.copy()

        assert not np.any(np.isnan(ddem2.data)) or np.all(~ddem2.data.mask)
        assert ddem2.filled_data is not None

        assert np.count_nonzero(np.isnan(ddem2.data)) == 0
        ddem2.data.ravel()[0] = np.nan

        assert np.count_nonzero(np.isnan(ddem2.data)) == 1

        assert ddem2.filled_data is None

        ddem2.interpolate(method="linear")

        assert ddem2.fill_method is not None

    def test_regional_hypso(self):
        """Test the regional hypsometric approach."""
        ddem = self.ddem.copy()
        ddem.data.mask = np.zeros_like(ddem.data, dtype=bool)
        ddem.data.mask.ravel()[np.random.choice(ddem.data.size, 50000, replace=False)] = True
        assert np.count_nonzero(ddem.data.mask) > 0

        assert ddem.filled_data is None

        ddem.interpolate(
            method="regional_hypsometric",
            reference_elevation=self.dem_2009,
            mask=self.outlines_1990
        )

        assert ddem._filled_data is not None
        assert type(ddem.filled_data) == np.ndarray

        assert ddem.filled_data.shape == ddem.data.shape

        assert np.abs(np.nanmean(self.ddem.data - ddem.filled_data)) < 1

    def test_local_hypso(self):
        """Test the local hypsometric approach."""
        ddem = self.ddem.copy()
        scott_1990 = self.outlines_1990.query("NAME == 'Scott Turnerbreen'")
        ddem.data.mask = np.zeros_like(ddem.data, dtype=bool)
        ddem.data.mask.ravel()[np.random.choice(ddem.data.size, 50000, replace=False)] = True
        assert np.count_nonzero(ddem.data.mask) > 0

        assert ddem.filled_data is None

        ddem.interpolate(
            method="local_hypsometric",
            reference_elevation=self.dem_2009.data,
            mask=self.outlines_1990
        )
        assert np.abs(np.mean(self.ddem.data - ddem.filled_data)) < 1
"""Plot an example of spatial interpolation of randomly generated errors."""
import geoutils as gu
import matplotlib.pyplot as plt
import numpy as np

import xdem

dem_2009 = xdem.DEM(xdem.examples.get_path("longyearbyen_ref_dem"))
dem_1990 = xdem.DEM(xdem.examples.get_path("longyearbyen_tba_dem"))
outlines_1990 = gu.Vector(
    xdem.examples.get_path("longyearbyen_glacier_outlines"))

ddem = xdem.dDEM(dem_2009 - dem_1990,
                 start_time=np.datetime64("1990-08-01"),
                 end_time=np.datetime64("2009-08-01"))
# 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

ddem.interpolate(method="linear")

ylim = (300, 100)
xlim = (800, 1050)

plt.figure(figsize=(8, 5))
plt.subplot(121)
plt.imshow(ddem.data.squeeze(), cmap="coolwarm_r", vmin=-50, vmax=50)
plt.ylim(ylim)
plt.xlim(xlim)
Exemplo n.º 3
0
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 = dem1 - dem2

#############################
# SECTION: dDEM interpolation
#############################

ddem = xdem.dDEM(raster=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",