|
| 1 | +import numpy as np |
| 2 | +import xarray as xr |
| 3 | + |
| 4 | +from mpas_tools.transects import lon_lat_to_cartesian |
| 5 | + |
| 6 | + |
| 7 | +def recompute_angle_edge(ds_mesh): |
| 8 | + """ |
| 9 | + Recompute ``angleEdge`` from edge and vertex locations on the sphere. |
| 10 | +
|
| 11 | + Parameters |
| 12 | + ---------- |
| 13 | + ds_mesh : xarray.Dataset |
| 14 | + An MPAS spherical mesh dataset containing edge and vertex locations. |
| 15 | +
|
| 16 | + Returns |
| 17 | + ------- |
| 18 | + angle_edge : xarray.DataArray |
| 19 | + ``angleEdge`` recomputed from spherical geometry. |
| 20 | + """ |
| 21 | + normal_east_north = calc_edge_normal_vector(ds_mesh) |
| 22 | + angle_edge = xr.zeros_like(ds_mesh.angleEdge) |
| 23 | + angle_edge.values = np.atan2( |
| 24 | + normal_east_north[:, 1], normal_east_north[:, 0] |
| 25 | + ) |
| 26 | + return angle_edge |
| 27 | + |
| 28 | + |
| 29 | +def calc_edge_normal_vector(ds_mesh): |
| 30 | + """ |
| 31 | + Compute edge-normal vectors projected onto local east/north coordinates. |
| 32 | +
|
| 33 | + Parameters |
| 34 | + ---------- |
| 35 | + ds_mesh : xarray.Dataset |
| 36 | + An MPAS spherical mesh dataset containing edge and vertex locations. |
| 37 | +
|
| 38 | + Returns |
| 39 | + ------- |
| 40 | + normal_east_north : numpy.ndarray |
| 41 | + A ``(nEdges, 2)`` array of unit normal vectors in local east/north |
| 42 | + coordinates. |
| 43 | + """ |
| 44 | + edge_cartesian = np.array( |
| 45 | + lon_lat_to_cartesian( |
| 46 | + ds_mesh.lonEdge, ds_mesh.latEdge, 1.0, degrees=False |
| 47 | + ) |
| 48 | + ) |
| 49 | + |
| 50 | + vertex_1 = ds_mesh.verticesOnEdge.isel(TWO=0).values - 1 |
| 51 | + vertex_2 = ds_mesh.verticesOnEdge.isel(TWO=1).values - 1 |
| 52 | + |
| 53 | + lon_vertex_1 = ds_mesh.lonVertex.isel(nVertices=vertex_1) |
| 54 | + lat_vertex_1 = ds_mesh.latVertex.isel(nVertices=vertex_1) |
| 55 | + lon_vertex_2 = ds_mesh.lonVertex.isel(nVertices=vertex_2) |
| 56 | + lat_vertex_2 = ds_mesh.latVertex.isel(nVertices=vertex_2) |
| 57 | + |
| 58 | + vertex_1_cartesian = np.array( |
| 59 | + lon_lat_to_cartesian(lon_vertex_1, lat_vertex_1, 1.0, degrees=False) |
| 60 | + ) |
| 61 | + vertex_2_cartesian = np.array( |
| 62 | + lon_lat_to_cartesian(lon_vertex_2, lat_vertex_2, 1.0, degrees=False) |
| 63 | + ) |
| 64 | + |
| 65 | + dvertex_cartesian = vertex_2_cartesian - vertex_1_cartesian |
| 66 | + normal_cartesian = np.cross(dvertex_cartesian, edge_cartesian, axis=0) |
| 67 | + |
| 68 | + edge_east, edge_north = calc_vector_east_north( |
| 69 | + edge_cartesian[0, :], edge_cartesian[1, :], edge_cartesian[2, :] |
| 70 | + ) |
| 71 | + |
| 72 | + normal_east_north = np.zeros((ds_mesh.sizes['nEdges'], 2)) |
| 73 | + normal_east_north[:, 0] = np.sum(edge_east * normal_cartesian, axis=0) |
| 74 | + normal_east_north[:, 1] = np.sum(edge_north * normal_cartesian, axis=0) |
| 75 | + |
| 76 | + norm = np.linalg.norm(normal_east_north, axis=1) |
| 77 | + nonzero = norm > 0.0 |
| 78 | + normal_east_north[nonzero, :] /= norm[nonzero, np.newaxis] |
| 79 | + |
| 80 | + return normal_east_north |
| 81 | + |
| 82 | + |
| 83 | +def calc_vector_east_north(x, y, z): |
| 84 | + """ |
| 85 | + Compute local east and north unit vectors on the sphere. |
| 86 | +
|
| 87 | + Parameters |
| 88 | + ---------- |
| 89 | + x, y, z : numpy.ndarray |
| 90 | + Cartesian coordinates of points on the unit sphere. |
| 91 | +
|
| 92 | + Returns |
| 93 | + ------- |
| 94 | + east, north : tuple of numpy.ndarray |
| 95 | + Local east and north unit vectors, each with shape ``(3, nPoints)``. |
| 96 | + """ |
| 97 | + axis = np.array([0.0, 0.0, 1.0]) |
| 98 | + xyz = np.stack((x, y, z), axis=1) |
| 99 | + east = np.cross(axis, np.transpose(xyz), axis=0) |
| 100 | + north = np.cross(np.transpose(xyz), east, axis=0) |
| 101 | + |
| 102 | + east /= np.linalg.norm(east, axis=0) |
| 103 | + north /= np.linalg.norm(north, axis=0) |
| 104 | + |
| 105 | + return east, north |
0 commit comments