mom6_bathy.grid module
- class mom6_bathy.grid.Grid(lenx: float, leny: float, nx: int = None, ny: int = None, resolution: float | None = None, xstart: float = 0.0, ystart: float | None = None, cyclic_x: bool = False, name: str | None = None, type: str = 'uniform_spherical')
Bases:
objectHorizontal MOM6 grid. The first step of constructing a MOM6 grid within the CESM simpler models framework is to create a Grid instance.
- tlon
array of t-grid longitudes
- Type:
xr.DataArray
- tlat
array of t-grid latitudes
- Type:
xr.DataArray
- ulon
array of u-grid longitudes
- Type:
xr.DataArray
- ulat
array of u-grid latitudes
- Type:
xr.DataArray
- vlon
array of v-grid longitudes
- Type:
xr.DataArray
- vlat
array of v-grid latitudes
- Type:
xr.DataArray
- qlon
array of corner longitudes
- Type:
xr.DataArray
- qlat
array of corner latitudes
- Type:
xr.DataArray
- dxt
x-distance between U points, centered at t
- Type:
xr.DataArray
- dyt
y-distance between V points, centered at t
- Type:
xr.DataArray
- dxCv
x-distance between q points, centered at v
- Type:
xr.DataArray
- dyCu
y-distance between q points, centered at u
- Type:
xr.DataArray
- dxCu
x-distance between y points, centered at u
- Type:
xr.DataArray
- dyCv
y-distance between t points, centered at v
- Type:
xr.DataArray
- angle
angle grid makes with latitude line
- Type:
xr.DataArray
- angle_q
angle q-grid makes with latitude line
- Type:
xr.DataArray
- tarea
T-cell area
- Type:
xr.DataArray
Grid instance constructor.
- Parameters:
lenx (float) – grid length in x direction, e.g., 360.0 (degrees)
leny (float) – grid length in y direction, e.g., 160.0 (degrees)
nx (int, optional) – Number of grid points in x direction
ny (int, optional) – Number of grid points in y direction
resolution (float, optional) – grid resolution in degrees. If provided, the grid dimensions are computed based on the resolution: nx = int(lenx / resolution) and ny = int(leny / resolution)
xstart (float, optional) – starting x coordinate. 0.0 by default.
ystart (float, optional) – starting y coordinate. -0.5*leny by default.
cyclic_x (bool, optional) – flag to make the grid cyclic in x direction. False by default.
name (str, optional) – name of the grid. None by default.
type (str, optional) – If not specified, creates an uniform_degree grid. Options are uniform_degree or rectilinear_cartesian
- static check_supergrid(supergrid: Dataset) None
Check if a given supergrid contains the necessary attributes and has consistent units.
- Parameters:
supergrid (xarray.Dataset) – MOM6 Supergrid dataset
- classmethod from_center(center_lat: float, center_lon: float, width_m: float, height_m: float, resolution_m: float, angle_deg: float = 0.0, name: str | None = None) Grid
Create a rotated rectangular grid centred at a geographic point.
Uses an azimuthal equidistant projection centred at (center_lat, center_lon) and rotates the domain by angle_deg clockwise from north. Useful for aligning a regional domain with a coastline feature — for example, placing one grid boundary perpendicular to an estuary mouth.
- Parameters:
center_lat (float) – Geographic centre of the domain in degrees.
center_lon (float) – Geographic centre of the domain in degrees.
width_m (float) – Domain width (x) and height (y) in metres.
height_m (float) – Domain width (x) and height (y) in metres.
resolution_m (float) – Grid resolution in metres.
angle_deg (float, optional) – Clockwise rotation from north in degrees. Default 0 (north-up). Example: angle_deg=45 places the x-axis along a NE–SW estuary mouth.
name (str, optional) – Name of the grid.
- Return type:
- classmethod from_projection(crs, x_min: float, x_max: float, y_min: float, y_max: float, resolution_m: float, name: str | None = None) Grid
Create a Grid from projected coordinate extents.
Builds a uniform grid in the given pyproj CRS (e.g., polar stereographic, Lambert conformal) and reprojects to geographic coordinates. Grid metrics are computed using exact great-circle geometry, making this accurate at high latitudes where RectilinearCartesianSupergrid degrades.
- Parameters:
crs (pyproj.CRS, int, or str) –
- Map projection. Examples:
”EPSG:3995” — Arctic Polar Stereographic “EPSG:3031” — Antarctic Polar Stereographic
x_min (float) – Projected x extent in metres.
x_max (float) – Projected x extent in metres.
y_min (float) – Projected y extent in metres.
y_max (float) – Projected y extent in metres.
resolution_m (float) – Grid resolution in metres.
name (str, optional) – Name of the grid.
- Return type:
- classmethod from_supergrid(path: str, name: str | None = None) Grid
Create a Grid instance from a supergrid file.
- Parameters:
path (str) – Path to the supergrid file to be written
name (str, optional) – Name of the new grid. If provided, it will be used as the name of the grid. If not provided, the name will be derived from the file name.
- Returns:
The Grid instance created from the supergrid file.
- Return type:
- classmethod from_supergrid_ds(ds: Dataset, name: str | None = None) Grid
Create a Grid instance from a supergrid file.
- Parameters:
ds (xr.Dataset) – xarray Dataset of the supergrid file to be written
name (str, optional) – Name of the new grid. If provided, it will be used as the name of the grid.
- Returns:
The Grid instance created from the supergrid file.
- Return type:
- classmethod get_bounding_boxes_of_rectangular_grid(hgrid)
Extract lat/lon bounding boxes for each edge of a rectangular regional MOM6 grid. This function is used when subsetting global datasets (e.g. GLORYS) down to the lat/lon ranges required for efficient regridding:
north, south, east, west boundaries
Entire domain initial condition files
- Parameters:
hgrid (Grid or xarray.Dataset in the supergrid format)
- Returns:
- A dictionary containing bounding boxes for:
”east”
”west”
”north”
”south”
”ic” (full domain for initial conditions)
- Return type:
dict
- get_indices(tlat: float, tlon: float) tuple[int, int]
Get the i, j indices of a given tlat and tlon pair.
- Parameters:
tlat (float) – The latitude value.
tlon (float) – The longitude value.
- Returns:
The j, i indices of the given tlat and tlon pair.
- Return type:
Tuple[int, int]
- static is_cyclic_x(supergrid) bool
Check if a given supergrid x coordinate array is cyclic along the x-axis.
- Parameters:
supergrid (xr.DataArray or np.array or SupergridBase) – Supergrid to check for cyclic x.
- is_rectangular(rtol=0.001) bool
Check if the grid is a rectangular lat-lon grid by comparing the first and last rows and columns of the tlon and tlat arrays.
- static is_tripolar(supergrid) bool
Check if the given supergrid x coordinates form a tripolar grid.
- Parameters:
supergrid (xr.DataArray or np.array or SupergridBase) – Supergrid to check if tripolar.
- property kdtree: cKDTree
KDTree for fast nearest neighbor search.
- property lenx: float
Length of the grid in the x-direction.
- property leny: float
Length of the grid in the y-direction.
- property name: str
Name of the grid.
- property nx
Number of cells in x-direction.
- property ny
Number of cells in y-direction.
- plot(property_name)
Plot a given grid property using cartopy. Warning: cartopy module must be installed seperately
- Parameters:
property_name (str) – The name of the grid property to plot, e.g., ‘tlat’.
- plot_cross_section(property_name, iy=None, ix=None)
Plot the cross-section of a given grid metric.
- Parameters:
property_name (str) – The name of the grid property to plot, e.g., ‘tlat’.
iy (int) – y-index of the cross section
ix (int) – x-inted of the cross section
- classmethod subgrid_from_supergrid(path: str, llc: tuple[float, float], urc: tuple[float, float], name: str) Grid
Create a Grid instance from a subset of a supergrid file.
- Parameters:
path (str) – Path to the full supergrid file to be carved out.
llc (tuple[float, float]) – Lower left corner coordinates (lat, lon) of the subdomain to extract
urc (tuple[float, float]) – Upper right corner coordinates (lat, lon) of the subset to extract
name (str) – Name of the subgrid
- Returns:
The Grid instance created from the supergrid file.
- Return type:
- property supergrid: SupergridBase
MOM6 supergrid contains the grid metrics and the areas at twice the nominal resolution (by default) of the actual computational grid.
- update_supergrid(xdat: array, ydat: array) None
Update the supergrid x and y coordinates. Running this method also updates the nominal grid coordinates and metrics.
- Parameters:
xdat (np.array) – 2-dimensional array of the new x coordinates.
ydat (np.array) – 2-dimensional array of the new y coordinates.
- write_supergrid(path: str | None = None, author: str | None = None) None
Write supergrid to a netcdf file. The supergrid file is to be read in by MOM6 during runtime.
- Parameters:
path (str, optional) – Path to the supergrid file to be written.
author (str, optional) – Name of the author. If provided, the name will appear in files as metadata.