import os
import math
import copy
import warnings
import numpy as np
import numpy.ma as ma
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import xarray as xr
import wradlib as wrl
try:
get_ipython().run_line_magic("matplotlib inline")
except:
plt.ion()
warnings.filterwarnings('ignore')
filename = "CAPPI-2km-Depth-H&V.nc"
grid = xr.open_dataset(filename)
display(grid)
<xarray.Dataset> Dimensions: (time: 1, x: 500, y: 500, z: 1, nradar: 1) Coordinates: * time (time) datetime64[ns] 2022-02-15T21:00:11 * x (x) float64 -2.5e+05 -2.49e+05 ... 2.5e+05 * y (y) float64 -2.5e+05 -2.49e+05 ... 2.5e+05 * z (z) float64 2e+03 Dimensions without coordinates: nradar Data variables: (12/16) origin_latitude (time) float64 ... origin_longitude (time) float64 ... origin_altitude (time) float64 ... projection int32 ... ProjectionCoordinateSystem int32 ... radar_latitude (nradar) float64 ... ... ... DBZV (time, z, y, x) float32 ... UV (time, z, y, x) float32 ... UH (time, z, y, x) float32 ... DBZH (time, z, y, x) float32 ... R_KDP_Corr_h_depth (time, z, y, x) float32 ... R_KDP_Corr_v_depth (time, z, y, x) float32 ... Attributes: (12/14) Conventions: Cf/Radial instrument_parameters radar_parameters title: PPIVol institution: EEC references: EEC source: EDGE history: original ... ... site_name: 9921GUA n_gates_vary: false volume_number: 1 platform_type: fixed instrument_type: radar primary_axis: axis_z
# Load data from text file
obs = np.loadtxt('obs.txt')
obs.shape
(85,)
#obs
# Load coordinate text file data
obs_coords = np.loadtxt('obs_coords.txt')
obs_coords.shape
(85, 2)
#obs_coords
# select base 2d grid
grid = grid.isel(time=0, z=0)
nanmask_h = xr.where(np.isnan(grid.R_KDP_Corr_h_depth), True, False)
radar_h = grid.R_KDP_Corr_h_depth.fillna(0)
radar_h_stack = radar_h.stack(radar_obs=("x", "y"))
display(radar_h_stack)
<xarray.DataArray 'R_KDP_Corr_h_depth' (radar_obs: 250000)> array([0., 0., 0., ..., 0., 0., 0.], dtype=float32) Coordinates: time datetime64[ns] 2022-02-15T21:00:11 z float64 2e+03 * radar_obs (radar_obs) object MultiIndex * x (radar_obs) float64 -2.5e+05 -2.5e+05 ... 2.5e+05 2.5e+05 * y (radar_obs) float64 -2.5e+05 -2.49e+05 ... 2.49e+05 2.5e+05 Attributes: long_name: Rainfall Depth Horizontal units: mm standard_name: Rainfall Depth Horizontal Conventions: CF-1.7 title: Rainfall Depth Horizontal institution: IME history: 2024-03-21 17:11:47.362010 Elton
nanmask_v = xr.where(np.isnan(grid.R_KDP_Corr_v_depth), True, False)
radar_v = grid.R_KDP_Corr_v_depth.fillna(0)
radar_v.plot()
radar_v_stack = radar_v.stack(radar_obs=("x", "y"))
display(radar_v_stack)
<xarray.DataArray 'R_KDP_Corr_v_depth' (radar_obs: 250000)> array([0., 0., 0., ..., 0., 0., 0.], dtype=float32) Coordinates: time datetime64[ns] 2022-02-15T21:00:11 z float64 2e+03 * radar_obs (radar_obs) object MultiIndex * x (radar_obs) float64 -2.5e+05 -2.5e+05 ... 2.5e+05 2.5e+05 * y (radar_obs) float64 -2.5e+05 -2.49e+05 ... 2.49e+05 2.5e+05 Attributes: long_name: Rainfall Depth Vertical units: mm standard_name: Rainfall Depth Vertical Conventions: CF-1.7 title: Rainfall Depth Vertical institution: IME history: 2024-03-21 17:11:49.284166 Elton
# get grid coords
grid_coords = xr.concat([radar_h_stack.x, radar_h_stack.y], "xy").transpose(..., "xy")
display(grid_coords)
<xarray.DataArray 'x' (radar_obs: 250000, xy: 2)> array([[-250000. , -250000. ], [-250000. , -248997.99599198], [-250000. , -247995.99198397], ..., [ 250000. , 247995.99198397], [ 250000. , 248997.99599198], [ 250000. , 250000. ]]) Coordinates: time datetime64[ns] 2022-02-15T21:00:11 z float64 2e+03 * radar_obs (radar_obs) object MultiIndex * x (radar_obs) float64 -2.5e+05 -2.5e+05 ... 2.5e+05 2.5e+05 * y (radar_obs) float64 -2.5e+05 -2.49e+05 ... 2.49e+05 2.5e+05 Dimensions without coordinates: xy Attributes: long_name: X distance on the projection plane from the origin units: m standard_name: projection_x_coordinate axis: X
# Estrutura dos seus dados
print("obs:", obs.shape)
print("obs_coords:", obs_coords.shape)
print("radar_h:", radar_h.shape)
print("grid_coords:", grid_coords.shape)
print("radar_v:", radar_v.shape)
obs: (85,) obs_coords: (85, 2) radar_h: (500, 500) grid_coords: (250000, 2) radar_v: (500, 500)
%%time
# Additive Error Model with OrdinaryKriging Exponential
# class wradlib.ipol.OrdinaryKriging(src, trg, cov='1.0 Exp(10000.)', nnearest=12)
addadjuster_ok_exp = wrl.adjust.AdjustAdd(obs_coords,
grid_coords.values,
cov = '1.0 Exp(10000.)',
ipclass=wrl.ipol.OrdinaryKriging)
addadjusted_ok_exp_values = addadjuster_ok_exp(obs, radar_h_stack.values)
CPU times: user 20.9 s, sys: 243 ms, total: 21.2 s Wall time: 21 s
addadjusted_ok_exp = xr.zeros_like(radar_h_stack)
addadjusted_ok_exp.values = addadjusted_ok_exp_values
addadjusted_ok_exp = addadjusted_ok_exp.unstack().transpose("y", "x")
display(addadjusted_ok_exp)
<xarray.DataArray 'R_KDP_Corr_h_depth' (y: 500, x: 500)> array([[0.14140616, 0.14140616, 0.14140616, ..., 0.58856927, 0.58856927, 0.5022548 ], [0.14140616, 0.14140616, 0.14140616, ..., 0.58856927, 0.58856927, 0.5022548 ], [0.14140616, 0.14140616, 0.14140616, ..., 0.58856927, 0.5022548 , 0.5022548 ], ..., [0.25972893, 0.25972893, 0.25972893, ..., 0.38615136, 0.38615131, 0.38615127], [0.25972893, 0.25972893, 0.25972893, ..., 0.38615134, 0.3861513 , 0.38615126], [0.25972893, 0.25972893, 0.25972893, ..., 0.38615133, 0.38615128, 0.38615125]]) Coordinates: * x (x) float64 -2.5e+05 -2.49e+05 -2.48e+05 ... 2.49e+05 2.5e+05 * y (y) float64 -2.5e+05 -2.49e+05 -2.48e+05 ... 2.49e+05 2.5e+05 time datetime64[ns] 2022-02-15T21:00:11 z float64 2e+03 Attributes: long_name: Rainfall Depth Horizontal units: mm standard_name: Rainfall Depth Horizontal Conventions: CF-1.7 title: Rainfall Depth Horizontal institution: IME history: 2024-03-21 17:11:47.362010 Elton
# open figure
fig = plt.figure(figsize=(10, 3.5))
# "Radar Rainfall: Real"
ax = fig.add_subplot(121)
radar_h.where(~nanmask_h).plot(ax=ax)
ax.set_title("Radar Rainfall: Real")
# "Additive Error Model with Nearest"
ax = fig.add_subplot(122)
addadjusted_ok_exp.where(~nanmask_h).plot(ax=ax)
ax.set_title("Additive Error Model with OK EXPl")
plt.tight_layout()
# salva figura
Docstring excerpt:
src_drift : :class:`numpy:numpy.ndarray`
ndarray of floats, shape (nsrcpoints, )
values of the external drift at each source point
trg_drift : :class:`numpy:numpy.ndarray`
ndarray of floats, shape (ntrgpoints, )
values of the external drift at each target point
Not sure, if the now provided values for src_drift
and trg_drift
are the correct ones to provide there as drift
.
%%time
addadjuster_kde_exp = wrl.adjust.AdjustAdd(obs_coords,
grid_coords.values,
cov = '1.0 Exp(10000.)',
nnearest=12,
src_drift=obs, # need obs values here
trg_drift=radar_v_stack.values, # need radar v values here
ipclass=wrl.ipol.ExternalDriftKriging)
addadjusted_kde_exp_values = addadjuster_kde_exp(obs, radar_h_stack.values)
CPU times: user 22.8 s, sys: 237 ms, total: 23 s Wall time: 22.9 s
addadjusted_kde_exp = xr.zeros_like(radar_h_stack)
addadjusted_kde_exp.values = addadjusted_kde_exp_values
addadjusted_kde_exp = addadjusted_kde_exp.unstack().transpose("y", "x")
display(addadjusted_kde_exp)
<xarray.DataArray 'R_KDP_Corr_h_depth' (y: 500, x: 500)> array([[0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.]]) Coordinates: * x (x) float64 -2.5e+05 -2.49e+05 -2.48e+05 ... 2.49e+05 2.5e+05 * y (y) float64 -2.5e+05 -2.49e+05 -2.48e+05 ... 2.49e+05 2.5e+05 time datetime64[ns] 2022-02-15T21:00:11 z float64 2e+03 Attributes: long_name: Rainfall Depth Horizontal units: mm standard_name: Rainfall Depth Horizontal Conventions: CF-1.7 title: Rainfall Depth Horizontal institution: IME history: 2024-03-21 17:11:47.362010 Elton
# open figure
fig = plt.figure(figsize=(10, 3.5))
# "Radar Rainfall: Real"
ax = fig.add_subplot(121)
radar_h.where(~nanmask_h).plot(ax=ax)
ax.set_title("Radar Rainfall: Real")
# "Additive Error Model with Nearest"
ax = fig.add_subplot(122)
addadjusted_kde_exp.where(~nanmask_h).plot(ax=ax)
ax.set_title("Additive Error Model with OK EXPl")
plt.tight_layout()
# salva figura