Lateral fill concept#

When regidding datasets for initial conditions, it is necessary to ensure that all model points have data. In many cases, differences between land-sea masks yield regions along the margins that require filling. The lateral_fill_np_array() routine applies an iterative filling procedure to accomplish this. This is illustrated here.

Import packages#

%matplotlib inline

import matplotlib.pyplot as plt
import numpy as np
import xarray as xr

import pop_tools

Generate some psuedo-data with coastline#

dx, dy = 0.05, 0.05

y, x = np.mgrid[slice(1 - dy, 3 + dy, dy), slice(1 - dx, 5 + dx, dx)]

z_orig = np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)

valid_points = np.ones(z_orig.shape, dtype=np.bool)
valid_points = np.where(y < 0.5 * np.sin(5 * x) + 1.5, False, valid_points)

z_orig = np.where(~valid_points, np.nan, z_orig)
z_orig[0, :] = np.nan


cb = plt.pcolormesh(z_orig, vmin=-1, vmax=2.0)
h = plt.colorbar(cb)
/tmp/ipykernel_2629/4059193687.py:7: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  valid_points = np.ones(z_orig.shape, dtype=np.bool)
../_images/05afd13defd5796b25df81b4b6522b5bb023149dd9a99266ba0a0233cd20fa14.png

Add missing values in one embayment and a random block in the top of the domain. Put some “blobs” of elevated values to show periodicity.

z_miss = z_orig.copy()

z_miss[:20, 62:] = np.nan
z_miss[35:, 55:70] = np.nan

z_miss[15:18, 0:2] = 10.0

z_miss[-2:, 12:20] = 10.0

cb = plt.pcolormesh(z_miss, vmin=-1, vmax=2.0)
h = plt.colorbar(cb)
../_images/212ef744cb14bf0f811eb233460f4de52fc9e8fc58f05c26c8b89ed723d3d44b.png

Perform lateral fill#

%%time

z_fill = pop_tools.lateral_fill_np_array(z_miss, valid_points, ltripole=False)

plt.figure()
cb = plt.pcolormesh(z_fill, vmin=-1, vmax=2.0)
h = plt.colorbar(cb)
CPU times: user 983 ms, sys: 6.67 ms, total: 990 ms
Wall time: 921 ms
../_images/8bd6573f4202ae37f847c6899b7f1f346ba65576b3f6ec50e1cd58bf4bb42cfd.png

Setting ltripole = True makes the domain periodic across the top boundary.

%%time
z_fill = pop_tools.lateral_fill_np_array(z_miss, valid_points, ltripole=True)

plt.figure()
cb = plt.pcolormesh(z_fill, vmin=-1, vmax=2.0)
h = plt.colorbar(cb)
CPU times: user 164 ms, sys: 4.27 ms, total: 168 ms
Wall time: 97 ms
../_images/edad5fff25d1f39ca554c01af67c1452a2a676bd04511dd75257ce49734338ea.png
%load_ext watermark
%watermark -d -iv -m -g -h
Compiler    : GCC 10.3.0
OS          : Linux
Release     : 5.15.0-1004-aws
Machine     : x86_64
Processor   : x86_64
CPU cores   : 2
Architecture: 64bit

Hostname: build-17279343-project-451810-pop-tools

Git hash: f8dc20b9962e0fdc7b9a1995503e368ff326e3e7

xarray    : 2022.3.0
matplotlib: 3.5.2
numpy     : 1.22.4
pop_tools : 2021.5.28.post40+dirty