Advertisement
Abhisek92

Ascat_Fac.py

Mar 18th, 2024 (edited)
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. import numpy as np
  2. import rasterio as rio
  3. from functools import partial
  4. from statsmodels.nonparametric.smoothers_lowess import lowess
  5.  
  6.  
  7. partial_lowess = partial(
  8.     lowess,
  9.     frac=2/3,
  10.     it=3,
  11.     delta=0.0,
  12.     xvals=None,
  13.     is_sorted=False,
  14.     missing='drop',
  15.     return_sorted=True
  16. )
  17.  
  18. with rio.open("ascat_raw_mask.tif") as ascat_src, rio.open("fac_ais_mask.tif") as fac_src:
  19.     ascat_meta = ascat_src.meta.copy()
  20.     fac_meta = fac_src.meta.copy()
  21.     assert ascat_src.crs == fac_src.crs, "CRS mismatch!"
  22.     ascat_data = ascat_src.read(masked=True)
  23.     fac_data = fac_src.read(masked=True)
  24.     ascat_forward = np.array(ascat_src.transform).reshape((3, 3))
  25.     fac_backward = np.array(~fac_src.transform).reshape((3, 3))
  26.  
  27.     ascat_mask = np.any(ascat_data.mask, axis=0)
  28.     fac_mask = np.any(fac_data.mask, axis=0)
  29.     fac_mask |= fac_data.mean(axis=0, keepdims=False) > 5
  30.  
  31.     ascat_row, ascat_col = np.where(~ascat_mask)
  32.     ascat_indexes = np.stack(
  33.         [(ascat_col + 0.5), (ascat_row + 0.5), np.ones_like(ascat_col)],
  34.         axis=-1
  35.     )
  36.     fac_indexes = np.round(
  37.         (
  38.             np.einsum(
  39.                 "ij,kj -> ki",
  40.                 fac_backward, np.einsum(
  41.                     "ij,kj -> ki",
  42.                     ascat_forward,
  43.                     ascat_indexes
  44.                 )
  45.             )[:, :-1] - 0.5
  46.         ),
  47.         decimals=0
  48.     ).astype(int)
  49.     fac_col, fac_row = np.array_split(
  50.         ary=fac_indexes,
  51.         indices_or_sections=fac_indexes.shape[-1],
  52.         axis=-1
  53.     )
  54.  
  55.     fac_col = np.squeeze(a=fac_col, axis=-1)
  56.     fac_row = np.squeeze(a=fac_row, axis=-1)
  57.     valid = np.logical_and(
  58.         np.logical_and((fac_col >= 0), (fac_col < fac_src.width)),
  59.         np.logical_and((fac_row >= 0), (fac_row < fac_src.height))
  60.     )
  61.     fac_col = fac_col[valid]
  62.     fac_row = fac_row[valid]
  63.     ascat_col = ascat_col[valid]
  64.     ascat_row = ascat_row[valid]
  65.  
  66.     indices = ~fac_mask[fac_row, fac_col]
  67.     fac_col = fac_col[indices]
  68.     fac_row = fac_row[indices]
  69.     ascat_row = ascat_row[indices]
  70.     ascat_col = ascat_col[indices]
  71.  
  72.     fac_values = fac_data[:, fac_row, fac_col].filled()
  73.     ascat_values = ascat_data[:, ascat_row, ascat_col].filled()
  74.     smooth = partial_lowess(ascat_values.ravel(), fac_values.ravel())
  75.     smooth = smooth.reshape(
  76.         (ascat_values.shape[0], ascat_values.shape[1], 2)
  77.     )
  78.  
  79.     fac_pred, ascat_pred = np.array_split(ary=smooth, indices_or_sections=2, axis=-1)
  80.     fac_pimg = np.full(shape=fac_data.shape, fill_value=np.nan, dtype=float)
  81.     ascat_pimg = np.full(shape=ascat_data.shape, fill_value=np.nan, dtype=float)
  82.     fac_pimg[:, fac_row, fac_col] = fac_pred.squeeze(-1)
  83.     ascat_pimg[:, ascat_row, ascat_col] = ascat_pred.squeeze(-1)
  84.     fac_rimg = fac_data.filled() - fac_pimg
  85.     ascat_rimg = ascat_data.filled() - ascat_pimg
  86.  
  87.     ascat_meta["dtype"] = ascat_rimg.dtype
  88.     ascat_meta["nodata"] = np.nan
  89.     with rio.open("ascat_residual.tif", "w", **ascat_meta) as ascat_dst:
  90.         ascat_dst.write(ascat_rimg)
  91.  
  92.     fac_meta["dtype"] = fac_rimg.dtype
  93.     fac_meta["nodata"] = np.nan
  94.     with rio.open("fac_residual.tif", "w", **fac_meta) as fac_dst:
  95.         fac_dst.write(fac_rimg)
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement