From b8b3ee1be45330c164a0b06c3d5a4002f4b5c882 Mon Sep 17 00:00:00 2001 From: mgrover1 Date: Fri, 13 Dec 2024 13:40:46 -0600 Subject: [PATCH] ADD: Add xradar io to correct examples --- examples/correct/plot_attenuation.py | 4 +++- examples/correct/plot_zdr_check.py | 12 ++++++++---- pyart/correct/attenuation.py | 5 +++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/examples/correct/plot_attenuation.py b/examples/correct/plot_attenuation.py index 6bdbb3ee745..f9b9eeee446 100644 --- a/examples/correct/plot_attenuation.py +++ b/examples/correct/plot_attenuation.py @@ -14,13 +14,15 @@ # License: BSD 3 clause import matplotlib.pyplot as plt +import xradar as xd import pyart file = pyart.testing.get_test_data("sgpcsaprsurcmacI7.c0.20110520.095101.nc") # read in the data -radar = pyart.io.read_cfradial(file) +tree = xd.io.open_cfradial1_datatree(file) +radar = tree.pyart.to_radar() # remove existing corrections radar.fields.pop("specific_attenuation") diff --git a/examples/correct/plot_zdr_check.py b/examples/correct/plot_zdr_check.py index df98780e0cf..b3632e76e9f 100644 --- a/examples/correct/plot_zdr_check.py +++ b/examples/correct/plot_zdr_check.py @@ -6,31 +6,35 @@ The technique here uses a vertically pointing scan in regions of light rain. In these regions, raindrops should be approximately spherical and therefore their ZDR near zero. Therefore, we want the average ZDR in these regions. -This code applies reflectivity and cross correlation ratio-based thresholds to the ZDR +This code applies reflectivity and cross correlation ratio-based thresholradar to the ZDR bias calculation to ensure that we are taking the average ZDR in light rain. """ import matplotlib.pyplot as plt +import xradar as xd from open_radar_data import DATASETS import pyart # Read in example data filename = DATASETS.fetch("sgpxsaprcfrvptI4.a1.20200205.100827.nc") -ds = pyart.io.read(filename) + +# Read in the data +tree = xd.io.open_cfradial1_datatree(filename) +radar = tree.pyart.to_radar() # Set up a typical filter for ZDR bias calculation in birdbath scan # Light rain and RhoHV near 1 ensures that raindrops are close to spherical # Therefore ZDR should be zero in these regions -gatefilter = pyart.filters.GateFilter(ds) +gatefilter = pyart.filters.GateFilter(radar) gatefilter.exclude_below("cross_correlation_ratio_hv", 0.995) gatefilter.exclude_above("cross_correlation_ratio_hv", 1) gatefilter.exclude_below("reflectivity", 10) gatefilter.exclude_above("reflectivity", 30) results = pyart.correct.calc_zdr_offset( - ds, + radar, zdr_var="differential_reflectivity", gatefilter=gatefilter, height_range=(1000, 3000), diff --git a/pyart/correct/attenuation.py b/pyart/correct/attenuation.py index 85dbca3ac56..1281e0860c6 100644 --- a/pyart/correct/attenuation.py +++ b/pyart/correct/attenuation.py @@ -10,6 +10,7 @@ from warnings import warn import numpy as np +import numpy.ma as ma from scipy.integrate import cumulative_trapezoid from ..config import get_field_name, get_fillvalue, get_metadata @@ -1058,6 +1059,10 @@ def calculate_attenuation( cor_z = get_metadata(corr_refl_field) cor_z["data"] = atten + reflectivity_horizontal + z_offset + + # If the numpy arrays are not masked arrays, convert it before returning + if isinstance(cor_z["data"], np.ndarray): + cor_z["data"] = ma.masked_invalid(cor_z["data"]) cor_z["data"].mask = init_refl_correct.mask cor_z["_FillValue"] = get_fillvalue()