Skip to content

Commit

Permalink
Fix scan plot and elevation angles in old files
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasmarke committed Jan 23, 2025
1 parent 44a7b50 commit 7895378
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
2 changes: 2 additions & 0 deletions mwrpy/level1/rpg_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ def read_blb(file_name: str) -> tuple[dict, dict]:
header |= _read_one(file, [("_n_f", "<i4")])
header |= _read_one(file, [("_f", "<f", header["_n_f"]), ("_n_ang", "<i4")])
header |= _read_one(file, [("_ang", "<f", header["_n_ang"])])
if np.max(header["_ang"]) > 100000.0:
header["_ang"] = np.round(header["_ang"] - 100000.0, 1)
dt: list[Field] = [("time", "<i4"), ("rain", "b")]
for n in range(header["_n_f"]):
dt += [(f"tb_{n}", "<f", header["_n_ang"])]
Expand Down
31 changes: 21 additions & 10 deletions mwrpy/plots/generate_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from matplotlib.axes import Axes
from matplotlib.colors import BoundaryNorm, Colormap, ListedColormap
from matplotlib.patches import Patch
from matplotlib.pyplot import Figure, clabel
from matplotlib.pyplot import Figure
from matplotlib.ticker import (
FixedLocator,
FormatStrFormatter,
Expand All @@ -22,7 +22,6 @@
from matplotlib.transforms import Affine2D, Bbox, ScaledTranslation
from mpl_toolkits.axes_grid1 import make_axes_locatable
from numpy import ma, ndarray
from numpy.f2py.auxfuncs import isstring

from mwrpy.atmos import abs_hum, dir_avg, t_dew_rh
from mwrpy.plots.plot_meta import _COLORS, ATTRIBUTES
Expand Down Expand Up @@ -130,7 +129,10 @@ def generate_figure(

for ax, field, name in zip(axes, valid_fields, valid_names):
ax.set_facecolor(_COLORS["lightgray"])
field = _elevation_filter(nc_file, field, ele_range)
if "angle" in name:
time = _read_time_vector(nc_file)
else:
field = _elevation_filter(nc_file, field, ele_range)
is_height = _is_height_dimension(nc_file, name)
if image_name and "_scan" in image_name:
name = image_name
Expand Down Expand Up @@ -1578,7 +1580,7 @@ def _plot_scan(data_in: ma.MaskedArray, name: str, time: ndarray, nc_file: str,
"""Plot for scans of integrated quantities (LWP, IWV)."""
elevation = read_nc_fields(nc_file, "elevation_angle")
angles = np.unique(np.round(elevation[elevation < 89.0]))
if len(angles) == 0:
if (len(angles) == 0) | (data_in[np.isin(elevation, angles)].mask.all()):
ax.set_title("empty")
else:
fig, axs = plt.subplots(
Expand Down Expand Up @@ -1616,8 +1618,15 @@ def _plot_scan(data_in: ma.MaskedArray, name: str, time: ndarray, nc_file: str,
scan = pd.DataFrame(
{"time": time_s0, "azimuth": azimuth, "var": data_s0}
)
scan["blocks"] = (scan["azimuth"] < scan["azimuth"].shift()).cumsum()
scan = scan[scan.groupby(scan["blocks"]).transform("size") == 36]
az_pl = np.unique(azimuth)
if np.diff(az_pl).all() > 0:
scan["blocks"] = (scan["azimuth"].diff() <= 0.0).cumsum()
else:
scan["blocks"] = (scan["azimuth"].diff() >= 0.0).cumsum()
scan = scan[
scan.groupby(scan["blocks"]).transform("size") == len(az_pl)
]

scan_mean = scan.groupby("blocks")["var"].mean()
time_median = scan.groupby("blocks")["time"].median()
scan_std = scan.groupby("blocks")["var"].std()
Expand All @@ -1626,18 +1635,20 @@ def _plot_scan(data_in: ma.MaskedArray, name: str, time: ndarray, nc_file: str,
np.abs(scan["diff"]) > 3 * scan_std[scan["blocks"].values].values,
"diff",
] = np.nan
az_pl = np.unique(azimuth)
var_pl = np.vstack(
scan.groupby("blocks")["diff"].apply(list).to_numpy()
)

vmin, vmax = -np.nanmax(np.abs(var_pl)), np.nanmax(np.abs(var_pl))
pl = axi.contourf(
cmap = plt.colormaps[str(ATTRIBUTES[name].cbar)]
levels = np.linspace(vmin, vmax, 11)
norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)
pl = axi.pcolormesh(
time_median,
az_pl,
np.transpose(var_pl),
cmap=ATTRIBUTES[name].cbar,
levels=np.linspace(vmin, vmax, 11),
cmap=cmap,
norm=norm,
)

gtim = _gap_array(time_median.values, case_date, 120.0 / 60.0)
Expand Down
2 changes: 1 addition & 1 deletion mwrpy/plots/plot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def _nan_time_gaps(time: ndarray, tgap: float = 5.0 / 60.0) -> ndarray:
time_diff = ma.diff(ma.masked_invalid(time))
gaps = np.where(time_diff > tgap)[0] + 1
if len(gaps) > 0:
time[gaps[0 : np.min([len(time), gaps[-1]])]] = np.nan
time[gaps[0 : ma.min([len(time), gaps[-1]])]] = ma.masked
return time


Expand Down

0 comments on commit 7895378

Please sign in to comment.