|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import warnings |
| 4 | + |
| 5 | +import cartopy.feature as cfeature |
| 6 | +import numpy as np |
| 7 | +import shapely |
| 8 | +from cartopy.mpl.geoaxes import GeoAxes |
| 9 | + |
| 10 | +from mosaic.contour import ContourGraph, MPASContourGenerator |
| 11 | +from mosaic.descriptor import Descriptor |
| 12 | + |
| 13 | + |
| 14 | +def coastlines( |
| 15 | + ax: GeoAxes, descriptor: Descriptor, color: str = "black", **kwargs |
| 16 | +) -> None: |
| 17 | + """ |
| 18 | + Plot coastal **outlines** using the connectivity info from the MPAS mesh |
| 19 | +
|
| 20 | + Parameters |
| 21 | + ---------- |
| 22 | + ax : cartopy.mpl.geoaxes.GeoAxes |
| 23 | + The cartopy axes to add the coastlines to. |
| 24 | + descriptor : Descriptor |
| 25 | + The descriptor containing the projection and dataset information. |
| 26 | + **kwargs |
| 27 | + Additional keyword arguments. See |
| 28 | + :py:class:`matplotlib.collections.Collection` for supported options. |
| 29 | + """ |
| 30 | + |
| 31 | + if not isinstance(ax, GeoAxes): |
| 32 | + msg = ( |
| 33 | + "Must provide a `cartopy.mpl.geoaxes` instance for " |
| 34 | + "`mosaic.coastlines` to work. " |
| 35 | + ) |
| 36 | + raise TypeError(msg) |
| 37 | + |
| 38 | + if "edgecolor" in kwargs and "ec" in kwargs: |
| 39 | + msg = "Cannot specify both 'edgecolor' and 'ec'." |
| 40 | + raise TypeError(msg) |
| 41 | + if "edgecolor" in kwargs: |
| 42 | + color = kwargs.pop("edgecolor") |
| 43 | + elif "ec" in kwargs: |
| 44 | + color = kwargs.pop("ec") |
| 45 | + |
| 46 | + if "facecolor" in kwargs and "fc" in kwargs: |
| 47 | + msg = "Cannot specify both 'facecolor' and 'fc'." |
| 48 | + raise TypeError(msg) |
| 49 | + if "facecolor" in kwargs or "fc" in kwargs: |
| 50 | + warnings.warn( |
| 51 | + "'facecolor (fc)' is not supported for `mosaic.coastlines` " |
| 52 | + "and will be ignored.", |
| 53 | + stacklevel=2, |
| 54 | + ) |
| 55 | + kwargs.pop("facecolor", None) |
| 56 | + kwargs.pop("fc", None) |
| 57 | + |
| 58 | + kwargs["edgecolor"] = color |
| 59 | + kwargs["facecolor"] = "none" |
| 60 | + |
| 61 | + generator = MPASCoastlineGenerator(descriptor) |
| 62 | + coastlines = generator.create_coastlines() |
| 63 | + |
| 64 | + geometries = shapely.GeometryCollection( |
| 65 | + [shapely.LineString(cl) for cl in coastlines] |
| 66 | + ) |
| 67 | + |
| 68 | + feature = cfeature.ShapelyFeature(geometries, descriptor.projection) |
| 69 | + ax.add_feature(feature, **kwargs) |
| 70 | + |
| 71 | + |
| 72 | +class MPASCoastlineGenerator(MPASContourGenerator): |
| 73 | + def __init__(self, descriptor: Descriptor): |
| 74 | + # pass a dummy field to the parent class |
| 75 | + super().__init__(descriptor, descriptor.ds.nCells) |
| 76 | + |
| 77 | + self.domain = descriptor.projection.domain |
| 78 | + self.boundary = descriptor.projection.boundary |
| 79 | + |
| 80 | + shapely.prepare(self.domain) |
| 81 | + |
| 82 | + def create_coastlines(self) -> list[np.ndarray]: |
| 83 | + graph = self._create_coastline_graph() |
| 84 | + lines = self._split_and_order_graph(graph) |
| 85 | + |
| 86 | + return self._snap_lines_to_boundary(lines) |
| 87 | + |
| 88 | + def _create_coastline_graph(self) -> ContourGraph: |
| 89 | + edge_mask = (self.ds.cellsOnEdge == -1).any("TWO").values |
| 90 | + |
| 91 | + vertex_1 = self.ds.verticesOnEdge[edge_mask].isel(TWO=0).values |
| 92 | + vertex_2 = self.ds.verticesOnEdge[edge_mask].isel(TWO=1).values |
| 93 | + |
| 94 | + return ContourGraph(vertex_1, vertex_2) |
| 95 | + |
| 96 | + def _snap_lines_to_boundary( |
| 97 | + self, lines: list[np.ndarray] |
| 98 | + ) -> list[np.ndarray]: |
| 99 | + def snap(point: np.ndarray): |
| 100 | + return self.boundary.interpolate( |
| 101 | + self.boundary.project(shapely.Point(point)) |
| 102 | + ) |
| 103 | + |
| 104 | + complete_lines = [] |
| 105 | + for line in lines: |
| 106 | + # only need to snap lines that are not already closed loops |
| 107 | + if np.array_equal(line[0], line[-1]): |
| 108 | + complete_lines.append(line) |
| 109 | + continue |
| 110 | + |
| 111 | + contain_mask = shapely.contains_xy(self.domain, *line.T) |
| 112 | + if not contain_mask.any(): |
| 113 | + continue |
| 114 | + |
| 115 | + clipped = line[contain_mask] |
| 116 | + |
| 117 | + if len(clipped) == 1: |
| 118 | + # if only one point inside domain, |
| 119 | + # all snapped points will lie along the same line |
| 120 | + continue |
| 121 | + |
| 122 | + # TODO: For coastlines with end points outside domain it would be |
| 123 | + # better to cut at boundary intersection point rather than snapping |
| 124 | + p0, p1 = snap(clipped[0]), snap(clipped[-1]) |
| 125 | + |
| 126 | + complete_lines.append( |
| 127 | + np.concatenate([np.array(p0.xy).T, clipped, np.array(p1.xy).T]) |
| 128 | + ) |
| 129 | + |
| 130 | + return complete_lines |
0 commit comments