12th Symposium on Advances in Modeling and Analysis Using Python, AMS Meeting 2022
Session: Python Tools in the Atmospheric and Oceanic Sciences. Part I
Date: January 25, 2022
from scipy.ndimage import uniform_filter, gaussian_filter
blurred_cat = uniform_filter(octocat)
Algorithm:
\begin{align*}
\bar{d}_0 & = d \\
\bar{d}_k & = \bar{d}_{k-1} + \frac{1}{s_k} \Delta \bar{d}_{k-1}
\end{align*}
|
|
import xarray as xr
import gcm_filters
list(gcm_filters.GridType)
[<GridType.REGULAR: 1>, <GridType.REGULAR_AREA_WEIGHTED: 2>, <GridType.REGULAR_WITH_LAND: 3>, <GridType.REGULAR_WITH_LAND_AREA_WEIGHTED: 4>, <GridType.IRREGULAR_WITH_LAND: 5>, <GridType.MOM5U: 6>, <GridType.MOM5T: 7>, <GridType.TRIPOLAR_REGULAR_WITH_LAND_AREA_WEIGHTED: 8>, <GridType.TRIPOLAR_POP_WITH_LAND: 9>, <GridType.VECTOR_C_GRID: 10>]
gcm_filters.required_grid_vars(gcm_filters.GridType.TRIPOLAR_POP_WITH_LAND)
['wet_mask', 'dxe', 'dye', 'dxn', 'dyn', 'tarea']
specs = {
'filter_scale': 100000,
'filter_shape': gcm_filters.FilterShape.GAUSSIAN,
}
filter = gcm_filters.Filter(
**specs,
grid_type=gcm_filters.GridType.TRIPOLAR_POP_WITH_LAND,
grid_vars={
'wet_mask': wet_mask,
'dxe': dxe, 'dye': dye, 'dxn': dxn, 'dyn': dyn,
'tarea': area
},
dx_min=dxe.where(wet_mask).min().values,
)
filter
Filter(filter_scale=100000, dx_min=array(2245.78304344), filter_shape=<FilterShape.GAUSSIAN: 1>, transition_width=3.141592653589793, ndim=2, n_steps=49, grid_type=<GridType.TRIPOLAR_POP_WITH_LAND: 9>)
KE.data # 1 snapshot of kinetic energy
|
KE_filtered = filter.apply(KE, dims=['nlat', 'nlon']) # apply filter lazily: computation is deferred
KE_filtered.data
|
import cupy as cp
KE_gpu.data
|
KE_gpu_filtered = filter_gpu.apply(KE_gpu, dims=['nlat', 'nlon']) # apply filter lazily
KE_gpu_filtered.data
|
%time computed_cpu = KE_filtered.isel(z_t=0).compute() # trigger filter computation on CPU
CPU times: user 17.4 s, sys: 11.1 s, total: 28.6 s Wall time: 29.3 s
%time computed_gpu = KE_gpu_filtered.isel(z_t=0).compute() # trigger filter computation on GPU
CPU times: user 1.81 s, sys: 887 ms, total: 2.7 s Wall time: 2.98 s
u_filtered = filter.apply(u, dims=['nlat', 'nlon'])
v_filtered = filter.apply(v, dims=['nlat', 'nlon'])
MKE = 0.5 * (u_filtered**2 + v_filtered**2)
EKE = KE_filtered - MKE
conda install -c conda-forge gcm_filters