Skip to main content

GeostatTools: A geostatistical toolbox.

Project description

Welcome to GSTools

DOI PyPI version Build Status Build status Coverage Status Documentation Status Code style: black



GeoStatTools provides geostatistical tools for random field generation and variogram estimation based on many readily provided and even user-defined covariance models.


The package can be installed via pip. On Windows you can install WinPython to get Python and pip running.

pip install gstools

Documentation for GSTools

You can find the documentation under

Tutorials and Examples

The documentation also includes some tutorials, showing the most important use cases of GSTools, which are

Some more examples are provided in the examples folder.

Spatial Random Field Generation

The core of this library is the generation of spatial random fields. These fields are generated using the randomisation method, described by Heße et al. 2014.


Gaussian Covariance Model

This is an example of how to generate a 2 dimensional spatial random field with a gaussian covariance model.

from gstools import SRF, Gaussian
import matplotlib.pyplot as plt
# structured field with a size 100x100 and a grid-size of 1x1
x = y = range(100)
model = Gaussian(dim=2, var=1, len_scale=10)
srf = SRF(model)
field = srf((x, y), mesh_type='structured')

Random field

Truncated Power Law Model

GSTools also implements truncated power law variograms, which can be represented as a superposition of scale dependant modes in form of standard variograms, which are truncated by an upper lengthscale lu.

This example shows the truncated power law based on the stable model and is given by

Truncated Power Law - Stable

which gives Gaussian modes for alpha=2 or exponential modes for alpha=1

This results in:

Truncated Power Law - Stable

import numpy as np
import matplotlib.pyplot as plt
from gstools import SRF, TPLStable
x = y = np.linspace(0, 100, 100)
model = TPLStable(
    dim=2,           # spatial dimension
    var=1,           # variance (C is calculated internally, so that the variance is actually 1)
    len_low=0,       # lower truncation of the power law
    len_scale=10,    # length scale (a.k.a. range), len_up = len_low + len_scale
    nugget=0.1,      # nugget
    anis=0.5,        # anisotropy between main direction and transversal ones
    angles=np.pi/4,  # rotation angles
    alpha=1.5,       # shape parameter from the stable model
    hurst=0.7,       # hurst coefficient from the power law
srf = SRF(model, mean=1, mode_no=1000, seed=19970221, verbose=True)
field = srf((x, y), mesh_type='structured', force_moments=True)
# show the field in xy coordinates
plt.imshow(field.T, origin="lower")

Random field

Estimating and fitting variograms

The spatial structure of a field can be analyzed with the variogram, which contains the same information as the covariance function.

All covariance models can be used to fit given variogram data by a simple interface.


This is an example of how to estimate the variogram of a 2 dimensional unstructured field and estimate the parameters of the covariance model again.

import numpy as np
from gstools import SRF, Exponential, Stable, vario_estimate_unstructured
from gstools.covmodel.plot import plot_variogram
import matplotlib.pyplot as plt
# generate a synthetic field with an exponential model
x = np.random.RandomState(19970221).rand(1000) * 100.
y = np.random.RandomState(20011012).rand(1000) * 100.
model = Exponential(dim=2, var=2, len_scale=8)
srf = SRF(model, mean=0, seed=19970221)
field = srf((x, y))
# estimate the variogram of the field with 40 bins
bins = np.arange(40)
bin_center, gamma = vario_estimate_unstructured((x, y), field, bins)
plt.plot(bin_center, gamma)
# fit the variogram with a stable model. (no nugget fitted)
fit_model = Stable(dim=2)
fit_model.fit_variogram(bin_center, gamma, nugget=False)
plot_variogram(fit_model, x_max=40)
# output

Which gives:

Stable(dim=2, var=1.92, len_scale=8.15, nugget=0.0, anis=[1.], angles=[0.], alpha=1.05)


User defined covariance models

One of the core-features of GSTools is the powerfull CovModel class, which allows to easy define covariance models by the user.


Here we reimplement the Gaussian covariance model by defining just the correlation function:

from gstools import CovModel
import numpy as np
# use CovModel as the base-class
class Gau(CovModel):
    def correlation(self, r):
        return np.exp(-(r/self.len_scale)**2)

And that's it! With Gau you now have a fully working covariance model, which you could use for field generation or variogram fitting as shown above.

Have a look at the documentation for further information on incorporating optional parameters and optimizations.

VTK Export

After you have created a field, you may want to save it to file, so we provide a handy VTK export routine:

from gstools import SRF, Gaussian, vtk_export
x = y = range(100)
model = Gaussian(dim=2, var=1, len_scale=10)
srf = SRF(model)
field = srf((x, y), mesh_type='structured')
vtk_export("field", (x, y), field, mesh_type='structured')

Which gives a RectilinearGrid VTK file field.vtr.



You can contact us via


GPL © 2018-2019

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gstools-1.0.0.tar.gz (2.1 MB view hashes)

Uploaded Source

Built Distributions

gstools-1.0.0-cp36-cp36m-win_amd64.whl (271.9 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

gstools-1.0.0-cp36-cp36m-win32.whl (254.3 kB view hashes)

Uploaded CPython 3.6m Windows x86

gstools-1.0.0-cp36-cp36m-manylinux1_x86_64.whl (538.2 kB view hashes)

Uploaded CPython 3.6m

gstools-1.0.0-cp36-cp36m-manylinux1_i686.whl (512.4 kB view hashes)

Uploaded CPython 3.6m

gstools-1.0.0-cp36-cp36m-macosx_10_6_intel.whl (368.2 kB view hashes)

Uploaded CPython 3.6m macOS 10.6+ intel

gstools-1.0.0-cp35-cp35m-win_amd64.whl (269.2 kB view hashes)

Uploaded CPython 3.5m Windows x86-64

gstools-1.0.0-cp35-cp35m-win32.whl (252.4 kB view hashes)

Uploaded CPython 3.5m Windows x86

gstools-1.0.0-cp35-cp35m-manylinux1_x86_64.whl (529.8 kB view hashes)

Uploaded CPython 3.5m

gstools-1.0.0-cp35-cp35m-manylinux1_i686.whl (502.2 kB view hashes)

Uploaded CPython 3.5m

gstools-1.0.0-cp35-cp35m-macosx_10_6_intel.whl (359.8 kB view hashes)

Uploaded CPython 3.5m macOS 10.6+ intel

gstools-1.0.0-cp27-cp27mu-manylinux1_x86_64.whl (527.9 kB view hashes)

Uploaded CPython 2.7mu

gstools-1.0.0-cp27-cp27mu-manylinux1_i686.whl (503.3 kB view hashes)

Uploaded CPython 2.7mu

gstools-1.0.0-cp27-cp27m-win_amd64.whl (276.0 kB view hashes)

Uploaded CPython 2.7m Windows x86-64

gstools-1.0.0-cp27-cp27m-win32.whl (258.3 kB view hashes)

Uploaded CPython 2.7m Windows x86

gstools-1.0.0-cp27-cp27m-manylinux1_x86_64.whl (527.9 kB view hashes)

Uploaded CPython 2.7m

gstools-1.0.0-cp27-cp27m-manylinux1_i686.whl (503.2 kB view hashes)

Uploaded CPython 2.7m

gstools-1.0.0-cp27-cp27m-macosx_10_6_intel.whl (375.3 kB view hashes)

Uploaded CPython 2.7m macOS 10.6+ intel

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page