From 149f8eac3b15ba03319709788b57d4f653bdde33 Mon Sep 17 00:00:00 2001 From: Xylar Asay-Davis Date: Wed, 18 Jan 2023 15:45:36 -0600 Subject: [PATCH] Fix depth slicing with non-ocean cells Previously, when there were cells with `maxLevelCell = 0`, this caused a failure because there was no valid depth coordinate for that cell and therefore no closest depth index could be found. This fix simply skips cells that are not in the ocean and masks them out. --- mpas_analysis/ocean/remap_depth_slices_subtask.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/mpas_analysis/ocean/remap_depth_slices_subtask.py b/mpas_analysis/ocean/remap_depth_slices_subtask.py index 490119cf5..4b426efb9 100644 --- a/mpas_analysis/ocean/remap_depth_slices_subtask.py +++ b/mpas_analysis/ocean/remap_depth_slices_subtask.py @@ -116,6 +116,10 @@ def run_task(self): zMid = compute_zmid(ds.bottomDepth, ds.maxLevelCell-1, ds.layerThickness) + ocean_mask = (ds.maxLevelCell > 0) + + print(f'zMid min: {zMid.min().values}, max: {zMid.max().values}') + print(f'maxLevelCell min: {ds.maxLevelCell.min().values}') nVertLevels = zMid.shape[1] zMid.coords['verticalIndex'] = \ @@ -135,6 +139,8 @@ def run_task(self): for depthIndex, depth in enumerate(self.depths): depth = self.depths[depthIndex] + print(f'depth: {depth}') + if depth == 'top': # switch to zero-based index verticalIndices[depthIndex, :] = 0 @@ -144,10 +150,12 @@ def run_task(self): verticalIndices[depthIndex, :] = self.maxLevelCell.values mask[depthIndex, :] = self.maxLevelCell.values >= 0 else: + + diff = np.abs(zMid - depth).where(ocean_mask, drop=True) + verticalIndex = diff.argmin(dim='nVertLevels') - verticalIndex = np.abs(zMid - depth).argmin(dim='nVertLevels') - - verticalIndices[depthIndex, :] = verticalIndex.values + verticalIndices[depthIndex, ocean_mask.values] = \ + verticalIndex.values mask[depthIndex, :] = np.logical_and(depth <= zTop, depth >= zBot).values