Skip to content

Commit

Permalink
add better warning message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jukent committed Jul 26, 2023
1 parent b090c42 commit ff17710
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
6 changes: 4 additions & 2 deletions docs/examples/plot_extrema_labels.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@
" ccrs.Geodetic(),\n",
" proj,\n",
" label_locations=lowClevels,\n",
" label='L')\n",
" label='L',\n",
" show_warnings=False)\n",
"gv.plot_extrema_labels(wrap_pressure,\n",
" ccrs.Geodetic(),\n",
" proj,\n",
" label_locations=highClevels,\n",
" label='H')\n",
" label='H',\n",
" show_warnings=False)\n",
"\n",
"\n",
"plt.show();"
Expand Down
28 changes: 21 additions & 7 deletions src/geocat/viz/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import typing
import math

import xarray as xr

Expand Down Expand Up @@ -1284,7 +1285,8 @@ def plot_extrema_labels(da: xr.DataArray,
label: str = 'L',
fontsize: int = 22,
whitebbox: bool = False,
horizontal: bool = True) -> list:
horizontal: bool = True,
show_warnings: bool = True) -> list:
"""Utility function to plot contour labels.
High/Low contour labels will be plotted using text boxes for more accurate label values
Expand Down Expand Up @@ -1354,9 +1356,24 @@ def plot_extrema_labels(da: xr.DataArray,
clabel_points = proj.transform_points(
transform, np.array([x[0] for x in label_locations]),
np.array([x[1] for x in label_locations]))
transformed_locations = [(x[0], x[1])
for x in clabel_points
if not np.isnan(x[0]) and not np.isnan(x[1])]
transformed_locations = [(x[0], x[1]) for x in clabel_points]

nan_indices = [
i for i, (x, y) in enumerate(transformed_locations)
if math.isnan(x) or math.isnan(y)
]
transformed_locations = [
loc for i, loc in enumerate(transformed_locations)
if i not in nan_indices
]

if show_warnings:
bad_locations = [label_locations[i] for i in nan_indices]
bad_locations_str = ", ".join([str(loc) for loc in bad_locations])
if len(bad_locations) > 0:
warnings.warn(
f'The following locations could not be translated into the desired projection: {bad_locations_str}. These locations will be dropped.',
stacklevel=2)

for loc in range(len(transformed_locations)):

Expand Down Expand Up @@ -1416,9 +1433,6 @@ def set_vector_density(data: xr.DataArray,
- `NCL_vector_3.py <https://geocat-examples.readthedocs.io/en/latest/gallery/Vectors/NCL_vector_3.html?highlight=set_vector_density>`_
"""
import math
import warnings

if minDistance <= 0:
raise Exception('minDistance cannot be negative or zero.')
else:
Expand Down

0 comments on commit ff17710

Please sign in to comment.