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=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)
../_images/4e0cd62746bfb3f893b78df1a35148ab5198d2211f79b8a6915e89d1bdcaedb8.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/11d31e9b669a413aee57c34e17e76966bde538728a44940f6bfdcade2e9cd202.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 1.04 s, sys: 2.75 ms, total: 1.04 s
Wall time: 975 ms
../_images/7e6b8de121069f3016a69b54a37a182fb29dff6879fa98e93ab53c2a7c966d70.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 161 ms, sys: 0 ns, total: 161 ms
Wall time: 93.2 ms
../_images/eb4b26e1345a0c0aeef1beba9f2c2b16d379337ad2a3b62b6f9e72938bdadb69.png
%load_ext watermark
%watermark -d -iv -m -g -h
Compiler    : GCC 11.3.0
OS          : Linux
Release     : 5.15.0-1004-aws
Machine     : x86_64
Processor   : x86_64
CPU cores   : 2
Architecture: 64bit

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

Git hash: d3c80c0576ae4838c0e04a0157734eb0c977e613

pop_tools : 2023.3.0.post2+dirty
matplotlib: 3.7.1
numpy     : 1.24.4
xarray    : 2023.6.0