-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add quick map plot for debugging (#69)
- Loading branch information
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# (C) Copyright 2024 Anemoi contributors. | ||
# | ||
# This software is licensed under the terms of the Apache Licence Version 2.0 | ||
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# In applying this licence, ECMWF does not waive the privileges and immunities | ||
# granted to it by virtue of its status as an intergovernmental organisation | ||
# nor does it submit to any jurisdiction. | ||
|
||
|
||
import cartopy.crs as ccrs | ||
import cartopy.feature as cfeature | ||
import matplotlib.pyplot as plt | ||
import matplotlib.tri as tri | ||
import numpy as np | ||
|
||
"""FOR DEVELOPMENT PURPOSES ONLY | ||
This module contains | ||
""" | ||
|
||
# TODO: use earthkit-plots | ||
|
||
|
||
def fix(lons): | ||
return np.where(lons > 180, lons - 360, lons) | ||
|
||
|
||
def plot_values(values, latitudes, longitudes, title=None, **kwargs): | ||
|
||
_, ax = plt.subplots(subplot_kw={"projection": ccrs.PlateCarree()}) | ||
ax.coastlines() | ||
ax.add_feature(cfeature.BORDERS, linestyle=":") | ||
|
||
missing_values = np.isnan(values) | ||
|
||
values = values[~missing_values] | ||
longitudes = longitudes[~missing_values] | ||
latitudes = latitudes[~missing_values] | ||
triangulation = tri.Triangulation(fix(longitudes), latitudes) | ||
|
||
levels = kwargs.pop("levels", 10) | ||
|
||
_ = ax.tricontourf(triangulation, values, levels=levels, transform=ccrs.PlateCarree()) | ||
|
||
options = dict( | ||
levels=levels, | ||
colors="black", | ||
linewidths=0.5, | ||
transform=ccrs.PlateCarree(), | ||
) | ||
|
||
options.update(kwargs) | ||
|
||
ax.tricontour( | ||
triangulation, | ||
values, | ||
**options, | ||
) | ||
|
||
if title is not None: | ||
ax.set_title(title) | ||
|
||
return ax | ||
|
||
|
||
def plot_field(field, title=None, **kwargs): | ||
values = field.to_numpy(flatten=True) | ||
latitudes, longitudes = field.grid_points() | ||
return plot_values(values, latitudes, longitudes, title=title, **kwargs) |