-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADD: Transform function CF1 to CF2 and vice versa (#224)
* ADD: Transform function CF1 to CF2 * FIX: Linting * Replace datatree with xarray in docstrings * ADD: Transform notebook to docs/usage * ADD: transform module to history * ENH: Tests for accessors * ENH: Enhance pytest and coverage for accessors * FIX: Markdown Fix
- Loading branch information
1 parent
1336b60
commit be4730e
Showing
10 changed files
with
557 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Transform" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Imports" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import xarray as xr\n", | ||
"from open_radar_data import DATASETS\n", | ||
"\n", | ||
"import xradar as xd" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Load Data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"file = DATASETS.fetch(\"cfrad.20080604_002217_000_SPOL_v36_SUR.nc\")\n", | ||
"dtree = xd.io.open_cfradial1_datatree(file)\n", | ||
"display(dtree)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Transform CF2 to CF1" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"ds = dtree.xradar.to_cfradial1_dataset()\n", | ||
"display(ds)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Transform CF1 to CF2" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"dtree = ds.xradar.to_cfradial2_datatree()\n", | ||
"display(dtree)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"del ds, dtree" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Alternate Method" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We can directly use xarray to read the data and then transform it to CF2 datatree." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"ds = xr.open_dataset(file)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"ds" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"radar = ds.xradar.to_cf2()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"display(radar)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.12.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env python | ||
# Copyright (c) 2024, openradar developers. | ||
# Distributed under the MIT License. See LICENSE for more info. | ||
|
||
import xarray as xr | ||
from open_radar_data import DATASETS | ||
|
||
import xradar as xd | ||
|
||
|
||
def test_to_cfradial1(): | ||
"""Test the conversion from DataTree to CfRadial1 format.""" | ||
file = DATASETS.fetch("cfrad.20080604_002217_000_SPOL_v36_SUR.nc") | ||
dtree = xd.io.open_cfradial1_datatree(file) | ||
|
||
# Call the conversion function | ||
ds_cf1 = xd.transform.to_cfradial1(dtree) | ||
|
||
# Verify key attributes and data structures in the resulting dataset | ||
assert isinstance(ds_cf1, xr.Dataset), "Output is not a valid xarray Dataset" | ||
assert "Conventions" in ds_cf1.attrs and ds_cf1.attrs["Conventions"] == "Cf/Radial" | ||
assert "sweep_mode" in ds_cf1.variables, "Missing sweep_mode in converted dataset" | ||
assert ds_cf1.attrs["version"] == "1.2", "Incorrect CfRadial version" | ||
|
||
|
||
def test_to_cfradial2(): | ||
"""Test the conversion from CfRadial1 to CfRadial2 DataTree format.""" | ||
file = DATASETS.fetch("cfrad.20080604_002217_000_SPOL_v36_SUR.nc") | ||
dtree = xd.io.open_cfradial1_datatree(file) | ||
|
||
# Convert to CfRadial1 dataset first | ||
ds_cf1 = xd.transform.to_cfradial1(dtree) | ||
|
||
# Call the conversion back to CfRadial2 | ||
dtree_cf2 = xd.transform.to_cfradial2(ds_cf1) | ||
|
||
# Verify key attributes and data structures in the resulting datatree | ||
assert isinstance(dtree_cf2, xr.DataTree), "Output is not a valid DataTree" | ||
assert "radar_parameters" in dtree_cf2, "Missing radar_parameters in DataTree" | ||
assert dtree_cf2.attrs == ds_cf1.attrs, "Attributes mismatch between formats" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.