Skip to content

Commit

Permalink
New release 2.0.4
Browse files Browse the repository at this point in the history
Correctly detect cmap names from colormaps and matplotlib packages
Fixed a bug that did not allow winds to be plotted
Keep 125 as dpi value
  • Loading branch information
wxguy committed Aug 26, 2024
1 parent 3545226 commit ae5b8ee
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,6 @@ cython_debug/

# VS Code related dirs
.vscode

# Kdevelp
.kdev4/
2 changes: 1 addition & 1 deletion wrfplot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
__version__ = version("wrfplot")
except:
# package is not installed
# Get the version from local file
# Get the version from the local file
import _version
__version__ = _version.__version__
finally:
Expand Down
10 changes: 6 additions & 4 deletions wrfplot/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import configparser
import utils
import colormaps as cmaps
import matplotlib.pyplot as plt


def dir_path(path):
Expand Down Expand Up @@ -142,11 +143,12 @@ def validate_cmap(cmap):
Results:
str: Name of colormap if supported
"""
if not cmap in dir(cmaps):
print("\nColormap", utils.quote(cmap), "is nor supported by wrfplot. Use '--list-cmaps' option to find list of supported colormaps.")
if cmap not in dir(cmaps) + plt.colormaps():
print(f"\nColormap {utils.quote(cmap)} is not supported by wrfplot."
f"\nUse '--list-cmaps' option to find list of supported colormaps.")
return False
else:
print("\nUsing user provided colormap :", utils.quote(cmap))
print(f"\nUsing user provided colormap : {utils.quote(cmap)}")

return cmap

Expand All @@ -157,7 +159,7 @@ def valid_range(ulevel):
print("Upper level" + utils.quote(str(ulevel)) + " can not be more than 1000")
return False
elif ulevel < 50:
print("Upper level" + utils.quote(str(ulevel)) + " can not be less than 1000")
print("Upper level" + utils.quote(str(ulevel)) + " can not be less than 50")
return False
else:
return True
Expand Down
7 changes: 6 additions & 1 deletion wrfplot/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ def interpolate_to(self, var_name, u_data=None, v_data=None, idx_time=None, p_le
t_high_hpa = interplevel(u_temp, pressure, 850)
var_data_interp = t_high_hpa - t_low_hpa
return var_data_interp, u_data, v_data
elif var_name == 'u_winds_temp':
u, v = getvar(self.nc_fh, "uvmet", timeidx=idx_time, units='kt')
u_data = interplevel(u, pressure, p_level)
v_data = interplevel(v, pressure, p_level)
var_data = getvar(self.nc_fh, 'temp', timeidx=idx_time, units='degC')
else:
var_data = getvar(self.nc_fh, var_name.replace('u_', ''), timeidx=idx_time)

Expand Down Expand Up @@ -295,7 +300,7 @@ def make_map(self, var_name, idx_time=None, time_fcst=None, p_level=None):
if var_name == 'slp':
img_path = self.plot.contour(var_name=var_name, lons=lons, lats=lats, data=data, clevels=clevels,
title=self.get_title(var_name, time_fcst), colors='blue', fcst_time=time_fcst)
elif var_name in ['winds', 'u_winds', 'u_stream']:
elif var_name in ['winds', 'u_winds', 'u_stream', 'u_winds_temp']:
img_path = self.plot.winds(var_name=var_name, lons=lons, lats=lats, u_data=u_data, v_data=v_data,
wspd=data, title=self.get_title(var_name, time_fcst, p_level), level=p_level,
clevels=clevels, colors='black', cmap=self.cmap, fcst_time=time_fcst)
Expand Down
8 changes: 8 additions & 0 deletions wrfplot/data/wrf_variables.ini
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@ clevels=20,30,40,50,60,80,100,120,140
c_bar_extend=max
clable=True

[u_winds_temp]
title=Wind Speed & Direction and Temperature
unit=Kt
cmap=rainbow
clevels=auto
c_bar_extend=max
clable=True

[u_cin]
title=Convective Inhibition
unit=$J kg^{1}$
Expand Down
24 changes: 14 additions & 10 deletions wrfplot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,17 @@ def winds(self, var_name, lons, lats, u_data, v_data, wspd, title, clevels, colo
thin = utils.get_auto_resolution(to_np(lats))
flip_array = (lats < 0)
self.clear_plots()
if not var_name == "u_stream":
self.contour_fill(var_name=var_name, lons=lons[::thin, ::thin], lats=lats[::thin, ::thin],
data=wspd[::thin, ::thin], title=title, clevels=clevels, colors=colors, cmap=cmap,
fcst_time=fcst_time)

if var_name == "u_stream":
self.stream = self.ax.streamplot(to_np(lons), to_np(lats), to_np(u_data), to_np(v_data), color='black', transform=ccrs.PlateCarree())
else:
if var_name == 'u_winds_temp':
# FIXME: u_winds_temp plot not working at the moment
self.ax.text(lons, lats, wspd, transform=ccrs.PlateCarree())
elif var_name == "u_stream":
# FIXME: Stream plot not working at the moment
self.stream = self.ax.streamplot(to_np(lons), to_np(lats), to_np(u_data), to_np(v_data), color='black',
transform=ccrs.PlateCarree())
if var_name not in ["u_stream", "u_winds_temp"]:
self.contour_fill(var_name=var_name, lons=lons, lats=lats, data=wspd, title=title, clevels=clevels,
colors=colors, cmap=cmap, fcst_time=fcst_time)
self.barbs = self.ax.barbs(to_np(lons)[::thin, ::thin], to_np(lats)[::thin, ::thin],
to_np(u_data)[::thin, ::thin], to_np(v_data)[::thin, ::thin],
transform=ccrs.PlateCarree(), length=5.5, sizes={"spacing": 0.2},
Expand Down Expand Up @@ -192,10 +195,10 @@ def save_fig(self, var, fcst_time, _level=None):
"""Save plotted image to local filesystem"""
file_id = "%s_%s" % (var, fcst_time)
if "u_" in var:
file_id = "%s_%s_%s" % (var, _level, fcst_time)
file_id = "%s_%s_%s" % (var, int(_level), fcst_time)
filename = "%s.%s" % (file_id.replace(" ", "_"), self.fig_format)
# Windows fix
# Widows does not accept file containing ":" in the file name. So replace it with '_'.
# Widows does not accept a file containing ":" in the file name. So replace it with '_'.
filename = filename.replace(":", "_")
if not os.path.exists(self.output_dir):
os.makedirs(self.output_dir)
Expand All @@ -222,8 +225,9 @@ def clear_plots(self):
self.cax.remove()
self.cbar = False
if self.stream:
self.stream.remove()
for collection in self.stream:
collection.remove()
collection.remove()
"""if self.cbar:
self.cbar.remove()
self.cbar = False"""
Expand Down
8 changes: 8 additions & 0 deletions wrfplot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ def get_cmap(name):
cmap: Maplotlib's cmap instance
"""
all_cmap = dir(cmaps)
mpl_cmaps = plt.colormaps()
if name in all_cmap:
return getattr(cmaps, name)
elif name in mpl_cmaps:
return plt.get_cmap(name)

if isinstance(name, list):
return name

try:
cmap = getattr(cmaps, name)
except AttributeError:
Expand Down Expand Up @@ -97,6 +101,10 @@ def get_auto_resolution(data):
x_data = data.shape
thin = int(x_data[0] / x_size) - 2

# Ensure that we don't return 0 or negative value as a thin factor which will result in error while slicing data.
if thin == 0 or thin < 0:
return 1

return thin


Expand Down
3 changes: 2 additions & 1 deletion wrfplot/wrfplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def arg_praser():
"--dpi",
metavar="<value>",
type=int,
default=125,
help="Increase or decrease the plotted image resolution. Default is 125. More is higher resolution and less is "
"course resolution. Higher values will reduce the speed of plot.",
)
Expand Down Expand Up @@ -187,7 +188,7 @@ def main():
total_time = timeit.default_timer() - start_time
mins, secs = divmod(total_time, 60)
hours, mins = divmod(mins, 60)
print(f"\nPlotting process completed. It took %dH:%dM:%fS\n" % (hours, mins, secs))
print(f"\nIt took '%dH:%dM:%fS' to complete all processes.\n" % (hours, mins, secs))
except Exception as e:
# print(e)
print(traceback.format_exc())
Expand Down

0 comments on commit ae5b8ee

Please sign in to comment.