fesomp.mesh
FESOM2 mesh module.
This module provides classes and functions for loading and working with FESOM2 unstructured mesh data.
- class fesomp.mesh.Mesh[source]
Bases:
objectRepresents a FESOM2 unstructured mesh.
This class holds the core mesh data including node coordinates, element connectivity, and vertical structure. Topology, geometry, and spatial indexing are lazily computed on first access.
- lon
Longitude of nodes in degrees, shape (n2d,).
- Type:
np.ndarray
- lat
Latitude of nodes in degrees, shape (n2d,).
- Type:
np.ndarray
- triangles
Triangle connectivity, shape (nelem, 3), 0-indexed.
- Type:
np.ndarray
- depth_levels
Depth at each level interface, shape (nlev,).
- Type:
np.ndarray
- depth_layers
Depth at layer centers, shape (nlev-1,).
- Type:
np.ndarray
- node_levels
Number of active levels at each node, shape (n2d,).
- Type:
np.ndarray
- elem_levels
Number of active levels at each element, shape (nelem,).
- Type:
np.ndarray
- node_bottom_depth
Bottom depth at each node, shape (n2d,).
- Type:
np.ndarray
- elem_bottom_depth
Bottom depth at each element, shape (nelem,).
- Type:
np.ndarray
- property topology: Topology
Get mesh topology (edges, neighbors, etc.).
Computed lazily on first access if not pre-loaded from NetCDF.
- property geometry: Geometry
Get mesh geometry (areas, gradients, etc.).
Computed lazily on first access if not pre-loaded from NetCDF.
- property spatial_index: SpatialIndex
Get spatial index for efficient point queries.
Built lazily on first access.
- property lon_elem: ndarray
Longitude of element (triangle) centers in degrees.
Computed lazily on first access. Handles cyclic triangles that cross the dateline correctly.
- property lat_elem: ndarray
Latitude of element (triangle) centers in degrees.
Computed lazily on first access.
- get_triangulation(mask_cyclic=True)[source]
Create a matplotlib Triangulation object for plotting.
- Parameters:
mask_cyclic (bool, optional) – If True (default), mask triangles that cross the dateline to prevent ugly lines spanning the globe.
- Returns:
Triangulation object ready for use with triplot, tripcolor, etc.
- Return type:
Example
>>> tri = mesh.get_triangulation() >>> plt.triplot(tri, 'b-', linewidth=0.2)
- __init__(lon, lat, triangles, nlev, depth_levels, depth_layers, node_levels, elem_levels, node_bottom_depth, elem_bottom_depth, _preloaded_topology=None, _preloaded_geometry=None, _topology=None, _geometry=None, _spatial_index=None, _lon_elem=None, _lat_elem=None)
- Parameters:
lon (ndarray)
lat (ndarray)
triangles (ndarray)
nlev (int)
depth_levels (ndarray)
depth_layers (ndarray)
node_levels (ndarray)
elem_levels (ndarray)
node_bottom_depth (ndarray)
elem_bottom_depth (ndarray)
_preloaded_topology (Topology | None)
_preloaded_geometry (Geometry | None)
_topology (Topology | None)
_geometry (Geometry | None)
_spatial_index (SpatialIndex | None)
_lon_elem (ndarray | None)
_lat_elem (ndarray | None)
- Return type:
None
- fesomp.mesh.load_mesh(path)[source]
Load a FESOM2 mesh from a file or directory.
Automatically detects the format based on the path: - If path is a .nc file: loads from NetCDF - If path is a directory: loads from ASCII files
- Parameters:
path (str or Path) – Path to the mesh file (NetCDF) or directory (ASCII).
- Returns:
The loaded mesh object.
- Return type:
- Raises:
FileNotFoundError – If the path does not exist.
ValueError – If the file format cannot be determined.
- class fesomp.mesh.Topology[source]
Bases:
objectMesh topology information (edges, connectivity, neighbors).
- edges
Edge node pairs, shape (nedges, 2), 0-indexed. Each row contains [node_i, node_j] where node_i < node_j.
- Type:
np.ndarray
- face_edges
Edge indices for each face, shape (nelem, 3). face_edges[i, j] is the edge index opposite to vertex j of face i.
- Type:
np.ndarray
- face_neighbors
Neighbor face indices, shape (nelem, 3). face_neighbors[i, j] is the face adjacent to face i across edge j. -1 indicates a boundary edge with no neighbor.
- Type:
np.ndarray
- edge_faces
Face indices for each edge, shape (nedges, 2). edge_faces[e, 0] and edge_faces[e, 1] are the faces sharing edge e. -1 indicates a boundary edge with only one adjacent face.
- Type:
np.ndarray
- node_elements
For each node, the list of element indices containing that node.
- Type:
list[np.ndarray]
- get_boundary_edges()[source]
Get indices of boundary edges.
- Returns:
Indices of edges that have only one adjacent face.
- Return type:
np.ndarray
- class fesomp.mesh.Geometry[source]
Bases:
objectMesh geometry information (areas, gradients).
- elem_area
Area of each element in m^2, shape (nelem,).
- Type:
np.ndarray
- node_area
Area associated with each node at each level, shape (nlev, n2d).
- Type:
np.ndarray
- gradient_sca
Gradient operator for scalar fields (x, y components).
- Type:
tuple[np.ndarray, np.ndarray] | None
- gradient_vec
Gradient operator for vector fields (x, y components).
- Type:
tuple[np.ndarray, np.ndarray] | None
- edge_cross_dxdy
Edge crossing distances, shape (4, nedges).
- Type:
np.ndarray | None
- class fesomp.mesh.SpatialIndex[source]
Bases:
objectSpatial index for efficient nearest-neighbor queries on mesh nodes.
Uses a 3D KD-tree in Cartesian coordinates for accurate spherical queries.
- Parameters:
lon (np.ndarray) – Longitude of nodes in degrees, shape (n2d,).
lat (np.ndarray) – Latitude of nodes in degrees, shape (n2d,).
- find_nearest(lon, lat, k=1)[source]
Find the k nearest nodes to given point(s).
- Parameters:
- Returns:
Indices of the k nearest nodes. If single point, shape is (k,) for k>1 or scalar for k=1. If multiple points, shape is (npoints, k).
- Return type:
np.ndarray
- find_in_radius(lon, lat, radius_km)[source]
Find all nodes within a given radius of point(s).
- Parameters:
- Returns:
Indices of nodes within the radius. For single point: 1D array of indices. For multiple points: list of arrays.
- Return type:
np.ndarray or list[np.ndarray]
- fesomp.mesh.scalar_r2g(alpha, beta, gamma, rlon, rlat)[source]
Convert coordinates from rotated to geographical coordinate system.
- Parameters:
- Returns:
(lon, lat) - Longitudes and latitudes in geographical coordinates (degrees).
- Return type:
tuple[np.ndarray, np.ndarray]
- fesomp.mesh.scalar_g2r(alpha, beta, gamma, lon, lat)[source]
Convert coordinates from geographical to rotated coordinate system.
- Parameters:
- Returns:
(rlon, rlat) - Longitudes and latitudes in rotated coordinates (degrees).
- Return type:
tuple[np.ndarray, np.ndarray]
- fesomp.mesh.vec_rotate_r2g(alpha, beta, gamma, lon, lat, urot, vrot, flag)[source]
Rotate vectors from rotated to geographical coordinates.
- Parameters:
alpha (float) – Alpha Euler angle in degrees.
beta (float) – Beta Euler angle in degrees.
gamma (float) – Gamma Euler angle in degrees.
lon (np.ndarray) – Longitudes (in rotated or geographical coordinates, see flag).
lat (np.ndarray) – Latitudes (in rotated or geographical coordinates, see flag).
urot (np.ndarray) – U (eastward) component of vector in rotated coordinates.
vrot (np.ndarray) – V (northward) component of vector in rotated coordinates.
flag (int) – Coordinate system of input lon/lat: - 1: lon/lat are in geographical coordinates - 0: lon/lat are in rotated coordinates
- Returns:
(u, v) - Vector components in geographical coordinates.
- Return type:
tuple[np.ndarray, np.ndarray]
- fesomp.mesh.vec_rotate_g2r(alpha, beta, gamma, lon, lat, ugeo, vgeo, flag)[source]
Rotate vectors from geographical to rotated coordinates.
- Parameters:
alpha (float) – Alpha Euler angle in degrees.
beta (float) – Beta Euler angle in degrees.
gamma (float) – Gamma Euler angle in degrees.
lon (np.ndarray) – Longitudes (in rotated or geographical coordinates, see flag).
lat (np.ndarray) – Latitudes (in rotated or geographical coordinates, see flag).
ugeo (np.ndarray) – U (eastward) component of vector in geographical coordinates.
vgeo (np.ndarray) – V (northward) component of vector in geographical coordinates.
flag (int) – Coordinate system of input lon/lat: - 1: lon/lat are in geographical coordinates - 0: lon/lat are in rotated coordinates
- Returns:
(u, v) - Vector components in rotated coordinates.
- Return type:
tuple[np.ndarray, np.ndarray]
Mesh Class
Core Mesh class for FESOM2 unstructured mesh data.
- class fesomp.mesh.mesh.Mesh[source]
Bases:
objectRepresents a FESOM2 unstructured mesh.
This class holds the core mesh data including node coordinates, element connectivity, and vertical structure. Topology, geometry, and spatial indexing are lazily computed on first access.
- lon
Longitude of nodes in degrees, shape (n2d,).
- Type:
np.ndarray
- lat
Latitude of nodes in degrees, shape (n2d,).
- Type:
np.ndarray
- triangles
Triangle connectivity, shape (nelem, 3), 0-indexed.
- Type:
np.ndarray
- depth_levels
Depth at each level interface, shape (nlev,).
- Type:
np.ndarray
- depth_layers
Depth at layer centers, shape (nlev-1,).
- Type:
np.ndarray
- node_levels
Number of active levels at each node, shape (n2d,).
- Type:
np.ndarray
- elem_levels
Number of active levels at each element, shape (nelem,).
- Type:
np.ndarray
- node_bottom_depth
Bottom depth at each node, shape (n2d,).
- Type:
np.ndarray
- elem_bottom_depth
Bottom depth at each element, shape (nelem,).
- Type:
np.ndarray
- property topology: Topology
Get mesh topology (edges, neighbors, etc.).
Computed lazily on first access if not pre-loaded from NetCDF.
- property geometry: Geometry
Get mesh geometry (areas, gradients, etc.).
Computed lazily on first access if not pre-loaded from NetCDF.
- property spatial_index: SpatialIndex
Get spatial index for efficient point queries.
Built lazily on first access.
- property lon_elem: ndarray
Longitude of element (triangle) centers in degrees.
Computed lazily on first access. Handles cyclic triangles that cross the dateline correctly.
- property lat_elem: ndarray
Latitude of element (triangle) centers in degrees.
Computed lazily on first access.
- get_triangulation(mask_cyclic=True)[source]
Create a matplotlib Triangulation object for plotting.
- Parameters:
mask_cyclic (bool, optional) – If True (default), mask triangles that cross the dateline to prevent ugly lines spanning the globe.
- Returns:
Triangulation object ready for use with triplot, tripcolor, etc.
- Return type:
Example
>>> tri = mesh.get_triangulation() >>> plt.triplot(tri, 'b-', linewidth=0.2)
- __init__(lon, lat, triangles, nlev, depth_levels, depth_layers, node_levels, elem_levels, node_bottom_depth, elem_bottom_depth, _preloaded_topology=None, _preloaded_geometry=None, _topology=None, _geometry=None, _spatial_index=None, _lon_elem=None, _lat_elem=None)
- Parameters:
lon (ndarray)
lat (ndarray)
triangles (ndarray)
nlev (int)
depth_levels (ndarray)
depth_layers (ndarray)
node_levels (ndarray)
elem_levels (ndarray)
node_bottom_depth (ndarray)
elem_bottom_depth (ndarray)
_preloaded_topology (Topology | None)
_preloaded_geometry (Geometry | None)
_topology (Topology | None)
_geometry (Geometry | None)
_spatial_index (SpatialIndex | None)
_lon_elem (ndarray | None)
_lat_elem (ndarray | None)
- Return type:
None
- fesomp.mesh.mesh.load_mesh(path)[source]
Load a FESOM2 mesh from a file or directory.
Automatically detects the format based on the path: - If path is a .nc file: loads from NetCDF - If path is a directory: loads from ASCII files
- Parameters:
path (str or Path) – Path to the mesh file (NetCDF) or directory (ASCII).
- Returns:
The loaded mesh object.
- Return type:
- Raises:
FileNotFoundError – If the path does not exist.
ValueError – If the file format cannot be determined.
Topology
Mesh topology data structures and computation.
- class fesomp.mesh.topology.Topology[source]
Bases:
objectMesh topology information (edges, connectivity, neighbors).
- edges
Edge node pairs, shape (nedges, 2), 0-indexed. Each row contains [node_i, node_j] where node_i < node_j.
- Type:
np.ndarray
- face_edges
Edge indices for each face, shape (nelem, 3). face_edges[i, j] is the edge index opposite to vertex j of face i.
- Type:
np.ndarray
- face_neighbors
Neighbor face indices, shape (nelem, 3). face_neighbors[i, j] is the face adjacent to face i across edge j. -1 indicates a boundary edge with no neighbor.
- Type:
np.ndarray
- edge_faces
Face indices for each edge, shape (nedges, 2). edge_faces[e, 0] and edge_faces[e, 1] are the faces sharing edge e. -1 indicates a boundary edge with only one adjacent face.
- Type:
np.ndarray
- node_elements
For each node, the list of element indices containing that node.
- Type:
list[np.ndarray]
- get_boundary_edges()[source]
Get indices of boundary edges.
- Returns:
Indices of edges that have only one adjacent face.
- Return type:
np.ndarray
Geometry
Mesh geometry data structures and computation.
- class fesomp.mesh.geometry.Geometry[source]
Bases:
objectMesh geometry information (areas, gradients).
- elem_area
Area of each element in m^2, shape (nelem,).
- Type:
np.ndarray
- node_area
Area associated with each node at each level, shape (nlev, n2d).
- Type:
np.ndarray
- gradient_sca
Gradient operator for scalar fields (x, y components).
- Type:
tuple[np.ndarray, np.ndarray] | None
- gradient_vec
Gradient operator for vector fields (x, y components).
- Type:
tuple[np.ndarray, np.ndarray] | None
- edge_cross_dxdy
Edge crossing distances, shape (4, nedges).
- Type:
np.ndarray | None
- fesomp.mesh.geometry.spherical_triangle_area(lon, lat, triangles)[source]
Compute the spherical area of triangles on a sphere.
Uses the spherical excess formula for computing the area of spherical triangles. The formula is:
Area = R^2 * E
where E is the spherical excess (sum of angles - π).
- Parameters:
lon (np.ndarray) – Longitude of nodes in degrees, shape (n2d,).
lat (np.ndarray) – Latitude of nodes in degrees, shape (n2d,).
triangles (np.ndarray) – Triangle connectivity, shape (nelem, 3), 0-indexed.
- Returns:
Area of each triangle in m^2, shape (nelem,).
- Return type:
np.ndarray
- fesomp.mesh.geometry.compute_node_area(elem_area, triangles, node_levels, nlev)[source]
Compute the area associated with each node at each level.
Each node receives 1/3 of the area of each adjacent element. For nodes with fewer levels, deeper levels have zero area.
- Parameters:
elem_area (np.ndarray) – Area of each element, shape (nelem,).
triangles (np.ndarray) – Triangle connectivity, shape (nelem, 3), 0-indexed.
node_levels (np.ndarray) – Number of active levels at each node, shape (n2d,).
nlev (int) – Total number of vertical levels.
- Returns:
Area at each node and level, shape (nlev, n2d).
- Return type:
np.ndarray
- fesomp.mesh.geometry.compute_geometry(lon, lat, triangles, node_levels, nlev, topology)[source]
Compute mesh geometry from coordinates and connectivity.
- Parameters:
lon (np.ndarray) – Longitude of nodes in degrees, shape (n2d,).
lat (np.ndarray) – Latitude of nodes in degrees, shape (n2d,).
triangles (np.ndarray) – Triangle connectivity, shape (nelem, 3), 0-indexed.
node_levels (np.ndarray) – Number of active levels at each node, shape (n2d,).
nlev (int) – Total number of vertical levels.
topology (Topology) – Pre-computed topology.
- Returns:
Computed geometry object.
- Return type:
Spatial Index
Spatial indexing for efficient point queries on mesh data.
- fesomp.mesh.spatial.lonlat_to_cartesian(lon, lat)[source]
Convert longitude/latitude to 3D Cartesian coordinates on unit sphere.
- Parameters:
lon (np.ndarray) – Longitude in degrees.
lat (np.ndarray) – Latitude in degrees.
- Returns:
Cartesian coordinates, shape (…, 3).
- Return type:
np.ndarray
- fesomp.mesh.spatial.chord_to_arc_distance(chord, radius=6371.0)[source]
Convert chord distance to arc (great-circle) distance.
- fesomp.mesh.spatial.arc_to_chord_distance(arc_km, radius=6371.0)[source]
Convert arc (great-circle) distance to chord distance on unit sphere.
- class fesomp.mesh.spatial.SpatialIndex[source]
Bases:
objectSpatial index for efficient nearest-neighbor queries on mesh nodes.
Uses a 3D KD-tree in Cartesian coordinates for accurate spherical queries.
- Parameters:
lon (np.ndarray) – Longitude of nodes in degrees, shape (n2d,).
lat (np.ndarray) – Latitude of nodes in degrees, shape (n2d,).
- find_nearest(lon, lat, k=1)[source]
Find the k nearest nodes to given point(s).
- Parameters:
- Returns:
Indices of the k nearest nodes. If single point, shape is (k,) for k>1 or scalar for k=1. If multiple points, shape is (npoints, k).
- Return type:
np.ndarray
- find_in_radius(lon, lat, radius_km)[source]
Find all nodes within a given radius of point(s).
- Parameters:
- Returns:
Indices of nodes within the radius. For single point: 1D array of indices. For multiple points: list of arrays.
- Return type:
np.ndarray or list[np.ndarray]
Coordinate Transformations
Coordinate transformations between rotated and geographical coordinates.
This module provides functions for converting coordinates and vectors between rotated (model) coordinates and geographical coordinates using Euler angle rotations. These transformations are necessary when working with FESOM2 meshes that use rotated coordinate systems.
- fesomp.mesh.coordinates.scalar_r2g(alpha, beta, gamma, rlon, rlat)[source]
Convert coordinates from rotated to geographical coordinate system.
- Parameters:
- Returns:
(lon, lat) - Longitudes and latitudes in geographical coordinates (degrees).
- Return type:
tuple[np.ndarray, np.ndarray]
- fesomp.mesh.coordinates.scalar_g2r(alpha, beta, gamma, lon, lat)[source]
Convert coordinates from geographical to rotated coordinate system.
- Parameters:
- Returns:
(rlon, rlat) - Longitudes and latitudes in rotated coordinates (degrees).
- Return type:
tuple[np.ndarray, np.ndarray]
- fesomp.mesh.coordinates.vec_rotate_r2g(alpha, beta, gamma, lon, lat, urot, vrot, flag)[source]
Rotate vectors from rotated to geographical coordinates.
- Parameters:
alpha (float) – Alpha Euler angle in degrees.
beta (float) – Beta Euler angle in degrees.
gamma (float) – Gamma Euler angle in degrees.
lon (np.ndarray) – Longitudes (in rotated or geographical coordinates, see flag).
lat (np.ndarray) – Latitudes (in rotated or geographical coordinates, see flag).
urot (np.ndarray) – U (eastward) component of vector in rotated coordinates.
vrot (np.ndarray) – V (northward) component of vector in rotated coordinates.
flag (int) – Coordinate system of input lon/lat: - 1: lon/lat are in geographical coordinates - 0: lon/lat are in rotated coordinates
- Returns:
(u, v) - Vector components in geographical coordinates.
- Return type:
tuple[np.ndarray, np.ndarray]
- fesomp.mesh.coordinates.vec_rotate_g2r(alpha, beta, gamma, lon, lat, ugeo, vgeo, flag)[source]
Rotate vectors from geographical to rotated coordinates.
- Parameters:
alpha (float) – Alpha Euler angle in degrees.
beta (float) – Beta Euler angle in degrees.
gamma (float) – Gamma Euler angle in degrees.
lon (np.ndarray) – Longitudes (in rotated or geographical coordinates, see flag).
lat (np.ndarray) – Latitudes (in rotated or geographical coordinates, see flag).
ugeo (np.ndarray) – U (eastward) component of vector in geographical coordinates.
vgeo (np.ndarray) – V (northward) component of vector in geographical coordinates.
flag (int) – Coordinate system of input lon/lat: - 1: lon/lat are in geographical coordinates - 0: lon/lat are in rotated coordinates
- Returns:
(u, v) - Vector components in rotated coordinates.
- Return type:
tuple[np.ndarray, np.ndarray]
Readers
Mesh file readers for different formats.
- class fesomp.mesh.readers.NetCDFReader[source]
Bases:
MeshReaderReader for FESOM2 mesh diagnostic NetCDF files.
Reads from fesom.mesh.diag.nc which contains pre-computed topology and geometry information.
- class fesomp.mesh.readers.ASCIIReader[source]
Bases:
MeshReaderReader for FESOM2 ASCII mesh files.
Reads from a directory containing: - nod2d.out: Node coordinates - elem2d.out: Triangle connectivity - aux3d.out: Vertical structure and bottom depths
Base class for mesh readers.
NetCDF reader for FESOM2 mesh files.
- class fesomp.mesh.readers.netcdf.NetCDFReader[source]
Bases:
MeshReaderReader for FESOM2 mesh diagnostic NetCDF files.
Reads from fesom.mesh.diag.nc which contains pre-computed topology and geometry information.
ASCII reader for FESOM2 mesh files.
- class fesomp.mesh.readers.ascii.ASCIIReader[source]
Bases:
MeshReaderReader for FESOM2 ASCII mesh files.
Reads from a directory containing: - nod2d.out: Node coordinates - elem2d.out: Triangle connectivity - aux3d.out: Vertical structure and bottom depths