torch_harmonics.ResampleS2#
- class torch_harmonics.ResampleS2(
- nlat_in,
- nlon_in,
- nlat_out,
- nlon_out,
- grid_in='equiangular',
- grid_out='equiangular',
- mode='bilinear',
Bases:
ModuleResampling module for signals on the 2-sphere \(S^2\).
This module resamples a spherical signal from one grid resolution (and type) to another. Interpolation is performed independently along latitudes and longitudes, with proper handling of periodicity in \(\lambda\) and pole expansion when the output grid extends beyond the input latitude range.
Two interpolation modes are available:
"bilinear"– Standard bilinear (linear-linear) interpolation. For two neighbouring grid values \(f_0\) and \(f_1\) with interpolation weight \(t \in [0, 1]\), the interpolated value is\[f(t) = (1 - t)\, f_0 + t\, f_1\]This is applied first along the latitudinal (\(\theta\)) and then along the longitudinal (\(\lambda\)) direction.
"bilinear-spherical"– Spherical linear interpolation (slerp). Instead of a straight line in value space, neighbouring samples are interpolated along a great-circle arc:\[f(t) = \frac{\sin\!\bigl((1-t)\,\omega\bigr)}{\sin\omega}\, f_0 + \frac{\sin(t\,\omega)}{\sin\omega}\, f_1\]where \(\omega = f_1 - f_0\) is the angular difference. This mode is better suited for fields that represent angular quantities (e.g.directions or phases) and falls back to linear interpolation when \(\omega \approx 0\) by applying the approximation \(\sin(x) \approx x\) for small \(x\) to the above expression.
- Parameters:
nlat_in (int) – Number of latitude points in the input grid
nlon_in (int) – Number of longitude points in the input grid
nlat_out (int) – Number of latitude points in the output grid
nlon_out (int) – Number of longitude points in the output grid
grid_in (str, optional) – Input grid type (
"equiangular","legendre-gauss","lobatto"), by default"equiangular"grid_out (str, optional) – Output grid type (
"equiangular","legendre-gauss","lobatto"), by default"equiangular"mode (str, optional) – Interpolation mode (
"bilinear","bilinear-spherical"), by default"bilinear". See above for a description of each mode.
Examples
>>> import torch >>> import torch_harmonics as th >>> resample = th.ResampleS2(64, 128, 128, 256).cuda() >>> x = torch.randn(1, 64, 128, device="cuda") >>> y = resample(x) >>> y.shape torch.Size([1, 128, 256])
- forward(x)[source]#
Resample a spherical signal onto the output grid.
- Parameters:
x (torch.Tensor) – Input signal of shape
(..., nlat_in, nlon_in). Resampling acts on the last two (spatial) dimensions; any leading batch/channel dimensions are preserved.- Returns:
Resampled signal of shape
(..., nlat_out, nlon_out).- Return type: