Skip to content

Commit e58e5a0

Browse files
committed
Add new Grid class for handling multiple grids without preferencing polygons
1 parent 53a0ed7 commit e58e5a0

File tree

22 files changed

+1326
-645
lines changed

22 files changed

+1326
-645
lines changed

docs/api/conventions/arakawa-c.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@ Arakawa C grid datasets have four :ref:`grids <grids>`:
1414
*face*, *left*, *back*, and *node*.
1515
:class:`ArakawaCGridKind` represents this.
1616
Each grid is :ref:`indexed <indexing>` by a grid kind and two integers *i* and *j*.
17-
The convention native index type is :data:`ArakawaCIndex`.
1817

1918
.. autoclass:: ArakawaCGridKind
2019
:members:
2120
:undoc-members:
2221

23-
.. autodata:: ArakawaCIndex
24-
2522
Topology
2623
========
2724

docs/api/conventions/grid.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@ Indexing
1717
CF grid datasets have one :ref:`grid <grids>`: *face*.
1818
:class:`CFGridKind` represents this.
1919
Each face is :ref:`indexed <indexing>` by two integers *x* and *y*.
20-
The convention native index type is :data:`CFGridIndex`.
2120

2221
.. autoclass:: CFGridKind
2322
:members:
2423
:undoc-members:
2524

26-
.. autodata:: CFGridIndex
27-
2825
Topology
2926
========
3027

docs/api/conventions/interface.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ the :class:`~emsarray.conventions.Convention` interface.
2121
.. autoclass:: emsarray.conventions.DimensionConvention
2222

2323
.. autoattribute:: grid_dimensions
24-
.. automethod:: unpack_index
25-
.. automethod:: pack_index
24+
25+
.. autoclass:: emsarray.conventions.Grid
26+
:members:
27+
28+
.. autoclass:: emsarray.conventions.DimensionGrid
29+
:members: dimensions
2630

2731
.. type:: GridKind
2832

docs/api/conventions/ugrid.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,11 @@ Indexing
1313
UGRID datasets have three :ref:`grids <grids>`: *face*, *edge* and *node*.
1414
:class:`UGridKind` represents this.
1515
Each grid is :ref:`indexed <indexing>` by a single integer.
16-
The convention native index type is :data:`UGridIndex`.
1716

1817
.. autoclass:: UGridKind
1918
:members:
2019
:undoc-members:
2120

22-
.. autodata:: UGridIndex
23-
2421
Topology
2522
========
2623

src/emsarray/cli/commands/export_geometry.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33
from collections.abc import Callable
44
from pathlib import Path
55

6-
import xarray
7-
86
import emsarray
97
from emsarray.cli import BaseCommand, CommandException
108
from emsarray.operations import geometry
11-
from emsarray.types import Pathish
129

1310
logger = logging.getLogger(__name__)
1411

15-
Writer = Callable[[xarray.Dataset, Pathish], None]
16-
format_writers: dict[str, Writer] = {
12+
13+
format_writers: dict[str, Callable] = {
1714
'geojson': geometry.write_geojson,
1815
'shapefile': geometry.write_shapefile,
1916
'wkt': geometry.write_wkt,
@@ -46,6 +43,14 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
4643
"The output format will be guessed using the output extension by default"
4744
))
4845

46+
parser.add_argument(
47+
"-g", "--grid-kind", type=str,
48+
default=None,
49+
help=(
50+
"Grid kind to export. Will export the default grid if not specified."
51+
),
52+
)
53+
4954
def guess_format(self, output_path: Path) -> str:
5055
extension = output_path.suffix
5156
if extension in {'.json', '.geojson'}:
@@ -69,13 +74,31 @@ def handle(self, options: argparse.Namespace) -> None:
6974
output_format = self.guess_format(output_path)
7075
logger.debug("Guessed output format as %r", output_format)
7176

72-
count = dataset.ems.polygons[dataset.ems.mask].size
73-
logger.debug("Dataset contains %d polygons", count)
77+
if options.grid_kind is None:
78+
grid_kind = dataset.ems.default_grid_kind
79+
else:
80+
grid_kind_names = {str(grid_kind): grid_kind for grid_kind in dataset.ems.grid_kinds}
81+
try:
82+
grid_kind = grid_kind_names[options.grid_kind]
83+
except KeyError:
84+
grid_kind_choices = ", ".join(grid_kind_names.keys())
85+
raise CommandException(
86+
f"Unknown grid kind {options.grid_kind!r}. "
87+
f"Valid choices are: {grid_kind_choices}."
88+
)
7489

7590
try:
7691
writer = format_writers[output_format]
7792
except KeyError:
7893
raise CommandException(f"Unknown output format {output_format!r}")
7994

95+
grid = dataset.ems.grids[grid_kind]
96+
geometries = grid.geometry[grid.mask]
97+
count = geometries.size
98+
99+
logger.debug("Grid kind: %s", grid_kind)
100+
logger.debug("Geometry type: %s", grid.geometry_type.__name__)
101+
logger.debug("Geometry count: %d", count)
80102
logger.debug("Exporting geometry as %r", output_format)
81-
writer(dataset, output_path)
103+
104+
writer(dataset, output_path, grid_kind=grid_kind)

src/emsarray/conventions/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
Convention instances can be instantiated directly in this case.
1414
Refer to each Convention implementation for details.
1515
"""
16-
from ._base import Convention, DimensionConvention, Specificity
16+
from ._base import (
17+
Convention, DimensionConvention, DimensionGrid, Grid, Specificity
18+
)
1719
from ._registry import get_dataset_convention, register_convention
1820
from ._utils import open_dataset
1921
from .arakawa_c import ArakawaC
@@ -23,6 +25,7 @@
2325

2426
__all__ = [
2527
"Convention", "DimensionConvention",
28+
"Grid", "DimensionGrid",
2629
"Specificity",
2730
"get_dataset_convention", "register_convention",
2831
"open_dataset",

0 commit comments

Comments
 (0)