Abhisek92

Image_Variogram.py

Apr 21st, 2022 (edited)
1,062
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.91 KB | None | 0 0
  1. import warnings
  2. import numpy as np
  3. import gstools as gs
  4. import rasterio as rio
  5. from pathlib import Path
  6. from matplotlib import pyplot as plt
  7. from gstools import (
  8.     Gaussian,
  9.     Exponential,
  10.     Matern,
  11.     Stable,
  12.     Rational,
  13.     Cubic,
  14.     Circular,
  15.     Spherical,
  16.     HyperSpherical,
  17.     SuperSpherical,
  18.     JBessel
  19. )
  20.  
  21. models_available = {
  22.     'gaussian': Gaussian,
  23.     'exponential': Exponential,
  24.     'matern': Matern,
  25.     'stable': Stable,
  26.     'rational': Rational,
  27.     'cubic': Cubic,
  28.     'circular': Circular,
  29.     'spherical': Spherical,
  30.     'hyper_spherical': HyperSpherical,
  31.     'super_spherical': SuperSpherical,
  32.     'bessel': JBessel
  33. }
  34.  
  35. def grid_smapling(src_path, n, band=1):
  36.     with warnings.catch_warnings():
  37.         warnings.filterwarnings(
  38.             "ignore", category=rio.errors.NotGeoreferencedWarning
  39.         )
  40.         with rio.open(src_path, 'r') as src:
  41.             h = src.height
  42.             w = src.width
  43.             nh = np.around((((n * h) / w) ** 0.5), decimals=0).astype(int)
  44.             nw = np.around((((n * w) / h) ** 0.5), decimals=0).astype(int)
  45.             h_gap = h / nh
  46.             w_gap = w / nw
  47.             h_marks = h_gap * np.arange(start=0, stop=nh, step=1)
  48.             w_marks = w_gap * np.arange(start=0, stop=nw, step=1)
  49.             h_marks = np.around((h_marks + (h_gap / 2)), decimals=0).astype(int)
  50.             w_marks = np.around((w_marks + (w_gap / 2)), decimals=0).astype(int)
  51.             h_marks, w_marks = np.meshgrid(h_marks, w_marks)
  52.             h_marks = h_marks.ravel()
  53.             w_marks = w_marks.ravel()
  54.             arr = src.read(band)
  55.             z = arr[h_marks, w_marks]
  56.     x, y = rio.transform.xy(src.transform, h_marks, w_marks)
  57.     return x, y, z
  58.  
  59. def make_variogram(
  60.     x, y, z, model=models_available['exponential']
  61. ):
  62.     bin_center, gamma = gs.vario_estimate((x, y), z)
  63.     fit_model = model(
  64.         dim=2,
  65.         var=1.0,
  66.         len_scale=1.0,
  67.         nugget=0.0,
  68.         anis=1.0,
  69.         angles=0.0,
  70.         integral_scale=None,
  71.         rescale=None,
  72.         latlon=False,
  73.         var_raw=None,
  74.         hankel_kw=None
  75.     )
  76.     fit_model.fit_variogram(bin_center, gamma, nugget=True)
  77.     return fit_model, bin_center, gamma
  78.  
  79. def plot_variogram(fit_model, bin_center, gamma, save_fig='/tmp/foo.png'):
  80.     if save_fig:
  81.         ax = fit_model.plot(x_max=max(bin_center))
  82.         ax.scatter(bin_center, gamma)
  83.         plt.savefig(save_fig)
  84.         del ax
  85.  
  86. if __name__ == '__main__':
  87.     img_path = Path("test_vario.tif")
  88.     dst_dir = '/tmp'
  89.     x, y, z = grid_smapling(img_path, n=10000, band=1)
  90.     out = make_variogram(x, y, z, models_available['matern'])
  91.     plot_variogram(*out, '/tmp/foo.png')
  92.     #for m in models_available.keys():
  93.         #out = make_variogram(
  94.             #x, y, z, models_available[m], f'/tmp/Figs/{m}.png'
  95.         #)
  96.         #plot_variogram(*out, f'{dst_dir}/tmp/{m}.png')
  97.  
Add Comment
Please, Sign In to add comment