Skip to content

Commit

Permalink
Merge pull request #534 from xylar/fix-planar-transect
Browse files Browse the repository at this point in the history
Fixes to transect extraction and plotting
  • Loading branch information
xylar authored Oct 28, 2023
2 parents 960ace8 + 525e376 commit 436d930
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
9 changes: 7 additions & 2 deletions conda_package/mpas_tools/ocean/transects.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,14 @@ def _get_vertical_coordinate(dsTransect, layerThickness, bottomDepth,
layerThicknessTransect = (layerThicknessTransect*interpCellWeights).sum(
dim='nHorizWeights')

interpMask = maxLevelCell.isel(nCells=interpCellIndices) >= 0
interpHorizCellWeights = interpMask*dsTransect.interpHorizCellWeights
weightSum = interpHorizCellWeights.sum(dim='nHorizWeights')
interpHorizCellWeights = \
(interpHorizCellWeights/weightSum).where(interpMask)

sshTransect = ssh.isel(nCells=interpCellIndices)
sshTransect = (sshTransect*dsTransect.interpHorizCellWeights).sum(
dim='nHorizWeights')
sshTransect = (sshTransect*interpHorizCellWeights).sum(dim='nHorizWeights')

zBot = sshTransect - layerThicknessTransect.cumsum(dim='nVertLevels')
zTop = zBot + layerThicknessTransect
Expand Down
19 changes: 15 additions & 4 deletions conda_package/mpas_tools/viz/transects.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import matplotlib.pyplot as plt
import numpy
import xarray

from scipy.spatial import cKDTree
from shapely.geometry import LineString, Point

Expand Down Expand Up @@ -516,9 +516,9 @@ def find_planar_transect_cells_and_weights(xTransect, yTransect, dsTris, dsMesh,
xIntersection = numpy.array(xIntersection)
yIntersection = numpy.array(yIntersection)
nodeWeights = numpy.array(nodeWeights)
node0Inter = numpy.array(node0Inter)
node1Inter = numpy.array(node1Inter)
trisInter = numpy.array(trisInter)
node0Inter = numpy.array(node0Inter, dtype=int)
node1Inter = numpy.array(node1Inter, dtype=int)
trisInter = numpy.array(trisInter, dtype=int)

dNodeLocal = dStart + distances

Expand Down Expand Up @@ -626,6 +626,17 @@ def _sort_intersections(dNode, tris, nodes, xOut, yOut, zOut, interpCells,
dSorted = dNode[sortIndices]
trisSorted = tris[sortIndices]

# sometimes we end up with redundant intersections in the transect, and
# these need to be removed
unique_d_tris = dict()
for index in range(len(dSorted)):
unique_d_tris[(dSorted[index], trisSorted[index])] = index

unique_indices = list(unique_d_tris.values())
sortIndices = sortIndices[unique_indices]
dSorted = dSorted[unique_indices]
trisSorted = trisSorted[unique_indices]

nodesAreSame = numpy.abs(dSorted[1:] - dSorted[:-1]) < epsilon
if nodesAreSame[0]:
# the first two nodes are the same, so the first transect point is in a
Expand Down

0 comments on commit 436d930

Please sign in to comment.