# Extract A Radar Column Along a Line of Azimuth

**URL:** <https://openradar.discourse.group/t/extract-a-radar-column-along-a-line-of-azimuth/207>\
**Category:** General\
**Tags:** py-art, data\
**Created:** [April 6, 2023, 9:38pm UTC](https://openradar.discourse.group/t/extract-a-radar-column-along-a-line-of-azimuth/207 "2023-04-06T21:38:31Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gitbix](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/gitbix/32/144_2.png) [@Gitbix](https://openradar.discourse.group/u/Gitbix)\
**Post date:** [April 6, 2023, 9:38pm UTC](https://openradar.discourse.group/t/extract-a-radar-column-along-a-line-of-azimuth/207/1 "2023-04-06T21:38:31Z")

</div>

Using the attached figure as an example, I want to extract column data along a 242 degree azimuth black dotted line from radar (0 -125 km) volume PPI plot (0.8 deg elevation). The file is in NC format. How can I go about it using Py-ART? I’m aiming to plot these data (dBZ) against the radar range 125 km.

![example_plot_033412](https://global.discourse-cdn.com/free1/uploads/openradar/original/1X/14b825e3d056281968298456d6ef6fc417530c8e.jpeg)

---

<div class="post-metadata">

**Author:** ![mgrover1](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/mgrover1/32/7_2.png) [@mgrover1](https://openradar.discourse.group/u/mgrover1)\
**Post date:** [April 7, 2023, 1:26pm UTC](https://openradar.discourse.group/t/extract-a-radar-column-along-a-line-of-azimuth/207/2 "2023-04-07T13:26:28Z")

</div>

Thanks for the question @Gitbix - I would suggest using [xradar](https://xradar.readthedocs.io/) here, which enables you to do this!

Here is a sample code block, using some sample data.

```python
import xradar as xd
from open_radar_data import DATASETS

# Load in a sample file from the Southern Great Plains ARM Site
file = DATASETS.fetch('sample_sgp_data.nc')

# Read the data using xradar
dtree = xd.io.open_cfradial1_datatree(
    file,
)

# Select the lowest sweep (0), and the closes azimuth to 242 degrees, and plot it!
dtree['sweep_1'].sel(azimuth=242, method='nearest')['corrected_reflectivity_horizontal'].plot()

```

Here is the result (note, there are some missing values, and we did not explicitly interpolate):

 ![Screenshot 2023-04-07 at 8.22.55 AM](https://global.discourse-cdn.com/free1/uploads/openradar/original/1X/c764ceaa85a7be85f7175ee3cf60ad36401f9bb9.jpeg)

---

<div class="post-metadata">

**Author:** ![Gitbix](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/gitbix/32/144_2.png) [@Gitbix](https://openradar.discourse.group/u/Gitbix)\
**Post date:** [April 7, 2023, 5:29pm UTC](https://openradar.discourse.group/t/extract-a-radar-column-along-a-line-of-azimuth/207/3 "2023-04-07T17:29:10Z")

</div>

Hey, Thanks Max. I’d be using this xradar method for the first time. What if I want to plot only some dBZ data in a fixed range say between 40 to 50 Km and not all?

---

<div class="post-metadata">

**Author:** ![mgrover1](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/mgrover1/32/7_2.png) [@mgrover1](https://openradar.discourse.group/u/mgrover1)\
**Post date:** [April 7, 2023, 5:53pm UTC](https://openradar.discourse.group/t/extract-a-radar-column-along-a-line-of-azimuth/207/4 "2023-04-07T17:53:30Z")

</div>

@Gitbix you could add another selection here (note range units are meters):

```python
ds.sel(range=slice(40_000, 50_000))

```

or the full line:

```python
dtree['sweep_1'].sel(azimuth=242, method='nearest').sel(range=slice(40_000, 50_000))['corrected_reflectivity_horizontal'].plot()

```

---

<div class="post-metadata">

**Author:** ![Gitbix](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/gitbix/32/144_2.png) [@Gitbix](https://openradar.discourse.group/u/Gitbix)\
**Post date:** [April 7, 2023, 6:01pm UTC](https://openradar.discourse.group/t/extract-a-radar-column-along-a-line-of-azimuth/207/5 "2023-04-07T18:01:16Z")

</div>

Thank you very much.

---

<div class="post-metadata">

**Author:** ![Gitbix](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/gitbix/32/144_2.png) [@Gitbix](https://openradar.discourse.group/u/Gitbix)\
**Post date:** [April 8, 2023, 8:41pm UTC](https://openradar.discourse.group/t/extract-a-radar-column-along-a-line-of-azimuth/207/6 "2023-04-08T20:41:38Z")

</div>

How can the dataset in ‘file’ be read with “dtree = xd.io.open\_cfradial1\_datatree(file)” if the dataset contains variable “nsweep” and not ‘sweep\_number’? Error from the line above is seen below.

 ![error](https://global.discourse-cdn.com/free1/uploads/openradar/original/1X/3befe12697f9871a88876edea7a0da2f009d2810.jpeg)

---

<div class="post-metadata">

**Author:** ![mgrover1](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/mgrover1/32/7_2.png) [@mgrover1](https://openradar.discourse.group/u/mgrover1)\
**Post date:** [April 10, 2023, 4:21pm UTC](https://openradar.discourse.group/t/extract-a-radar-column-along-a-line-of-azimuth/207/7 "2023-04-10T16:21:36Z")

</div>

> [@Gitbix](#):
>
> “nsweep” and not ‘sweep\_num

Are you sure the data you are reading is a cfradial file? Can you rename the variable using netCDF? The reader is assuming the cfradial1 standard, which uses the name `sweep_number`.

---

<div class="post-metadata">

**Author:** ![Gitbix](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/gitbix/32/144_2.png) [@Gitbix](https://openradar.discourse.group/u/Gitbix)\
**Post date:** [April 10, 2023, 4:58pm UTC](https://openradar.discourse.group/t/extract-a-radar-column-along-a-line-of-azimuth/207/8 "2023-04-10T16:58:58Z")

</div>

Yes, the data is a cfradial NC file having the variable sweep\_number. The version from metadata is CF-Radial 1.4. Also, I’m not sure the data type exist in the datasets module above.

 ![S_N](https://global.discourse-cdn.com/free1/uploads/openradar/original/1X/f08e26f3935f2dadb38fb7fa74d82a13311b2a22.jpeg)

---

<div class="post-metadata">

**Author:** ![mgrover1](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/mgrover1/32/7_2.png) [@mgrover1](https://openradar.discourse.group/u/mgrover1)\
**Post date:** [April 10, 2023, 5:36pm UTC](https://openradar.discourse.group/t/extract-a-radar-column-along-a-line-of-azimuth/207/9 "2023-04-10T17:36:25Z")

</div>

Can you share a copy of the code block you are running?

---

<div class="post-metadata">

**Author:** ![Gitbix](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/gitbix/32/144_2.png) [@Gitbix](https://openradar.discourse.group/u/Gitbix)\
**Post date:** [April 10, 2023, 6:05pm UTC](https://openradar.discourse.group/t/extract-a-radar-column-along-a-line-of-azimuth/207/10 "2023-04-10T18:05:37Z")

</div>

Yes, here is it. Having installed and imported all modules.

 ![code](https://global.discourse-cdn.com/free1/uploads/openradar/original/1X/f67c7fa2b3cc44d0dd1467ebe818625dd27833c4.jpeg)

---

<div class="post-metadata">

**Author:** ![mgrover1](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/mgrover1/32/7_2.png) [@mgrover1](https://openradar.discourse.group/u/mgrover1)\
**Post date:** [April 10, 2023, 8:12pm UTC](https://openradar.discourse.group/t/extract-a-radar-column-along-a-line-of-azimuth/207/11 "2023-04-10T20:12:13Z")

</div>

@Gitbix it looks like that file is missing sweep\_number still - using following fixes the read in, by adding a new `sweep_number` variable to the dataset using the existing `sweep` variable, writing to disk, then reading back in using `xradar`

```python
# Open the original file
ds = xr.open_dataset('cfrad.20211211_120304.494_to_20211211_120801.176_KBSI_SUR.nc')

# Make sure that sweep number is one of the variables in the dataset
ds['sweep_number'] = ds.sweep

# Write this new netcdf file to disk
ds.to_netcdf('cfrad.20211211_120304.494_to_20211211_120801.176_KBSI_SUR.nc',)

# Read the data back in using xradar
tree = xd.io.open_cfradial1_datatree('cfrad.20211211_120304.494_to_20211211_120801.176_KBSI_SUR.nc')

```

---

<div class="post-metadata">

**Author:** ![Gitbix](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/gitbix/32/144_2.png) [@Gitbix](https://openradar.discourse.group/u/Gitbix)\
**Post date:** [April 10, 2023, 11:06pm UTC](https://openradar.discourse.group/t/extract-a-radar-column-along-a-line-of-azimuth/207/12 "2023-04-10T23:06:51Z")

</div>

Oh really. So, the solution is to ingest the sweep\_number variable into the same file?  
Would try check it out. Thank you.

---

<div class="post-metadata">

**Author:** ![Gitbix](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/gitbix/32/144_2.png) [@Gitbix](https://openradar.discourse.group/u/Gitbix)\
**Post date:** [April 10, 2023, 11:49pm UTC](https://openradar.discourse.group/t/extract-a-radar-column-along-a-line-of-azimuth/207/13 "2023-04-10T23:49:51Z")

</div>

I plotted the data but it seems to be empty. What could be wrong?  
 ![data_plot](https://global.discourse-cdn.com/free1/uploads/openradar/original/1X/8e68fb1fa34dd0d19fefba5ebc97562419da4b78.png)

---

<div class="post-metadata">

**Author:** ![mgrover1](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/mgrover1/32/7_2.png) [@mgrover1](https://openradar.discourse.group/u/mgrover1)\
**Post date:** [April 11, 2023, 3:02pm UTC](https://openradar.discourse.group/t/extract-a-radar-column-along-a-line-of-azimuth/207/14 "2023-04-11T15:02:20Z")

</div>

@Gitbix it looks like there was a bug with the way dask vs. numpy arrays were saved to disk - this should resolve that issue, and I opened an issue on the xradar side to reflect these issues

```python
# Open the original file
file = 'cfrad.20211211_120304.494_to_20211211_120801.176_KBSI_SUR.nc'
ds = xr.open_dataset(file)

# Make sure that sweep number is one of the variables in the dataset
ds['sweep_number'] = ('sweep', ds.sweep.values)

# Make sure the full dataset is loaded, which ensures values are passed correctly
ds = ds.compute()

# Write this new netcdf file to disk
ds.to_netcdf(file)

# Read the data back in using xradar
tree = xd.io.open_cfradial1_datatree(file)

# Plot the second sweep, using the closest azimuth
tree['sweep_1'].sel(azimuth=242, method='nearest').sel(range=slice(40_000, 50_000))['corrected_reflectivity'].plot()

```

Which resulted in this plot

 ![Screenshot 2023-04-11 at 10.01.55 AM](https://global.discourse-cdn.com/free1/uploads/openradar/original/1X/c15e3d1b5d3296d78170f8b49d2611b8b668abb8.jpeg)

---

<div class="post-metadata">

**Author:** ![Gitbix](https://yyz2.discourse-cdn.com/free1/user_avatar/openradar.discourse.group/gitbix/32/144_2.png) [@Gitbix](https://openradar.discourse.group/u/Gitbix)\
**Post date:** [April 11, 2023, 4:29pm UTC](https://openradar.discourse.group/t/extract-a-radar-column-along-a-line-of-azimuth/207/15 "2023-04-11T16:29:24Z")

</div>

Oh really. I would check the opened issue. Thank you sir
