Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/history.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# History

## Unreleased
ADD: Cfradial2 module to read cfradial2 compliant data. ({issue}`287`) by [@aladinor](https://github.com/aladinor), ({pull}`288`) by [@syedhamidali](https://github.com/syedhamidali)

## 0.10.0 (2025-07-11)

* FIX: Use fixture for making temp file to avoid permission issue on Windows
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ pyproj
scipy
xarray >= 2024.10.0
xmltodict
zarr
37 changes: 37 additions & 0 deletions tests/io/test_cfradial2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python
# Copyright (c) 2023-2025, openradar developers.
# Distributed under the MIT License. See LICENSE for more info.

import pytest
import xarray as xr
from open_radar_data import DATASETS

import xradar as xd


@pytest.fixture
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@syedhamidali Maybe my suggestion was a bit brief, but this fixture is already defined in conftest.py as well as the tmp_file fixture. You should be able to just use them here and can remove the re-definitions here.

def cfradial1_file():
return DATASETS.fetch("cfrad.20080604_002217_000_SPOL_v36_SUR.nc")


@pytest.fixture
def temp_file(tmp_path):
return tmp_path / "cfradial2.nc"


def test_open_cfradial2(temp_file, cfradial1_file):
# Open the original file using CfRadial1 reader
dtree = xd.io.open_cfradial1_datatree(cfradial1_file)

# Write to CfRadial2 NetCDF format
dtree.to_netcdf(temp_file)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure, but you might have to add kwarg engine="netcdf4" here, since the tmp_file doesn't have an extension.


# Read back using CfRadial2 reader
dtree2 = xd.io.open_cfradial2_datatree(temp_file)

# Check structural correctness
assert dtree2 is not None, "Failed to open CfRadial2 datatree"
assert hasattr(dtree2, "children"), "Returned object is not a valid DataTree"
assert "sweep_0" in dtree2.children, "Missing expected sweep node"
assert "DBZ" in dtree2["sweep_0"].data_vars, "Missing reflectivity field"
xr.testing.assert_isomorphic(dtree, dtree2)
2 changes: 2 additions & 0 deletions xradar/io/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
:maxdepth: 4

.. automodule:: xradar.io.backends.cfradial1
.. automodule:: xradar.io.backends.cfradial2
.. automodule:: xradar.io.backends.gamic
.. automodule:: xradar.io.backends.odim
.. automodule:: xradar.io.backends.furuno
Expand All @@ -24,6 +25,7 @@
"""

from .cfradial1 import * # noqa
from .cfradial2 import * # noqa
from .furuno import * # noqa
from .gamic import * # noqa
from .iris import * # noqa
Expand Down
51 changes: 51 additions & 0 deletions xradar/io/backends/cfradial2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python
# Copyright (c) 2022-2025, openradar developers.
# Distributed under the MIT License. See LICENSE for more info.

"""

CfRadial2
=========

This submodule provides an xarray backend for reading CfRadial2-compliant radar data
into Xarray `DataTree` structures. It serves as a wrapper around `xarray.open_datatree`.

Example::

import xradar as xd
dtree = xd.io.open_cfradial2_datatree(filename)

.. autosummary::
:nosignatures:
:toctree: generated/

{}

"""

__all__ = [
"open_cfradial2_datatree",
]

__doc__ = __doc__.format("\n ".join(__all__))

from xarray import open_datatree


def open_cfradial2_datatree(filename, **kwargs):
"""
Open a CfRadial2 file as an xarray DataTree.

Parameters
----------
filename : str
Path to the CfRadial2 file.
**kwargs : dict
Additional keyword arguments passed to `xarray.open_datatree`.

Returns
-------
xarray.DataTree
The opened DataTree containing the radar data.
"""
return open_datatree(filename, **kwargs)
Loading