""" This file is part of MSS. :copyright: Copyright 2021-2022 by the MSS team, see AUTHORS. :license: APACHE-2.0, see LICENSE for details. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ import logging import warnings import numpy as np import matplotlib.pyplot as plt import mpl_toolkits.axes_grid1.inset_locator import matplotlib.colors import mpl_toolkits.basemap from matplotlib import patheffects from mslib.mswms.mpl_hsec import MPLBasemapHorizontalSectionStyle from mslib.mswms.utils import Targets, get_style_parameters, get_cbar_label_format, make_cbar_labels_readable from mslib.utils import thermolib from mslib.utils.units import convert_to class HS_GeopotentialWindStyle_PL(MPLBasemapHorizontalSectionStyle): """ Upper Air Field: Geopotential and Wind """ name = "PLGeopWind" title = "Geopotential Height (m) and Horizontal Wind (m/s)" styles = [ ("default", "Wind Speed 10-85 m/s"), ("wind_10_105", "Wind Speed 10-105 m/s"), ("wind_10_65", "Wind Speed 10-65 m/s"), ("wind_20_55", "Wind Speed 20-55 m/s"), ("wind_15_55", "Wind Speed 15-55 m/s")] # Variables with the highest number of dimensions first (otherwise # MFDatasetCommonDims will throw an exception)! required_datafields = [ ("pl", "geopotential_height", "m"), ("pl", "eastward_wind", "m/s"), ("pl", "northward_wind", "m/s")] def _plot_style(self): """ """ bm = self.bm ax = self.bm.ax data = self.data # Compute wind speed. u = data["eastward_wind"] v = data["northward_wind"] wind = np.hypot(u, v) # Plot wind contours. # NOTE: Setting alpha=0.8 raises the transparency problem in the client # (the imshow issue, see ../issues/transparency; surfaces with alpha # values < 1 are mixed with grey). Hence, it is better to disable # alpha blending here until a fix has been found. (mr 2011-02-01) wind_contours = np.arange(10, 90, 5) # default wind contours if self.style.lower() == "wind_10_65": wind_contours = np.arange(10, 70, 5) elif self.style.lower() == "wind_20_55": wind_contours = np.arange(20, 60, 5) elif self.style.lower() == "wind_15_55": wind_contours = np.arange(15, 60, 5) elif self.style.lower() == "wind_10_105": wind_contours = np.arange(10, 110, 5) cs = bm.contourf(self.lonmesh, self.latmesh, wind, wind_contours, cmap=plt.cm.inferno_r) self.add_colorbar(cs, "Wind Speed (m/s)") # Plot geopotential height contours. gpm = self.data["geopotential_height"] gpm_interval = 20 if self.level <= 20: gpm_interval = 120 elif self.level <= 100: gpm_interval = 80 elif self.level <= 500: gpm_interval = 40 geop_contours = np.arange(400, 55000, gpm_interval) cs = bm.contour(self.lonmesh, self.latmesh, gpm, geop_contours, colors="green", linewidths=2) if cs.levels[0] in geop_contours[::2]: lablevels = cs.levels[::2] else: lablevels = cs.levels[1::2] ax.clabel(cs, lablevels, fontsize=14, fmt='%.0f') # Convert wind data from m/s to knots for the wind barbs. uk = convert_to(u, "m/s", "knots") vk = convert_to(v, "m/s", "knots") # Transform wind vector field to fit map. lons2 = ((self.lons + 180) % 360) - 180 lons2_ind = lons2.argsort() udat, vdat, xv, yv = bm.transform_vector(uk[:, lons2_ind], vk[:, lons2_ind], lons2[lons2_ind], self.lats, 16, 16, returnxy=True, masked=True) # Plot wind barbs. bm.barbs(xv, yv, udat, vdat, barbcolor='firebrick', flagcolor='firebrick', pivot='middle', linewidths=0.5, length=6, zorder=1) # Plot title. titlestring = "Geopotential Height (m) and Horizontal Wind (m/s) " \ f"at {self.level:.0f} hPa" titlestring += f'\nValid: {self.valid_time.strftime("%a %Y-%m-%d %H:%M UTC")}' if self.uses_inittime_dimension(): time_step = self.valid_time - self.init_time time_step_hrs = (time_step.days * 86400 + time_step.seconds) // 3600 titlestring += f' (step {time_step_hrs:d} hrs from {self.init_time.strftime("%a %Y-%m-%d %H:%M UTC")})' if not self.noframe: ax.set_title(titlestring, horizontalalignment='left', x=0, fontsize=14) else: ax.text(bm.llcrnrx, bm.llcrnry, titlestring, fontsize=10, bbox=dict(facecolor='white', alpha=0.6))