Source code for sgs_tools.geometry.grid
from abc import ABC, abstractmethod
from dataclasses import dataclass
import numpy as np
from numpy.typing import NDArray
from xarray import DataArray
[docs]
@dataclass(frozen=True)
class Grid(ABC):
"""Abstract base grid object.
* :meth:`mesh`: return a dictionary of type
{axis_name : `np.meshgrid` of coordinate}
* :meth:`coords`: return a dictionary of type {axis_name : 1d coordinate values}
"""
[docs]
@abstractmethod
def mesh(self, shape: list[int]) -> dict[str, NDArray]:
""":returns: a dictionary of type {axis_name : `np.meshgrid` of coordinate}"""
...
[docs]
@abstractmethod
def coords(self, shape: list[int]) -> dict[str, NDArray]:
""":returns: a dictionary of type {axis_name : 1d coordinate values}"""
...
[docs]
@dataclass(frozen=True)
class CoordScalar:
"""prescribe a scalar field that is a grid coordinate
scaled with a constant amplitude
:ivar grid: grid which provides coordinate variables
:ivar dimension: label of coordinate direction
:ivar amplitude: factor by which to scale the coordinate
"""
grid: Grid
dimension: str # coordinate direction
amplitude: float # scaling of scalar (multiplying by the coordinate)
def scalar(self, shape: list[int]) -> DataArray:
coords = self.grid.coords(shape)
scalar = self.amplitude * self.grid.mesh(shape)[self.dimension]
return DataArray(scalar, dims=list(coords.keys()), coords=coords)