{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# Universal Format (UF)" ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "import cmweather # noqa\n", "import xarray as xr\n", "from open_radar_data import DATASETS\n", "\n", "import xradar as xd" ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "## Download\n", "\n", "Fetching Universal Format radar data file from [open-radar-data](https://github.com/openradar/open-radar-data) repository." ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "import atexit\n", "from pathlib import Path\n", "from tempfile import TemporaryDirectory\n", "\n", "tmpdir_obj = TemporaryDirectory()\n", "atexit.register(tmpdir_obj.cleanup) # remove even if you forget\n", "tmpdir = Path(tmpdir_obj.name)\n", "\n", "\n", "def get_temp_file(fname):\n", " import gzip\n", " import shutil\n", "\n", " fnamei = Path(DATASETS.fetch(fname))\n", " fnameo = tmpdir / fnamei.stem\n", " with gzip.open(fnamei) as fin:\n", " with open(fnameo, \"wb\") as fout:\n", " shutil.copyfileobj(fin, fout)\n", " return fnameo\n", "\n", "\n", "fname = get_temp_file(\"20110427_164233_rvp8-rel_v001_SUR.uf.gz\")" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "## xr.open_dataset\n", "\n", "Making use of the xarray `uf` backend. We also need to provide the group. Note, that we are using CfRadial2 group access pattern." ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "ds = xr.open_dataset(fname, group=\"sweep_0\", engine=\"uf\")\n", "display(ds)" ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "np.testing.assert_almost_equal(ds.sweep_fixed_angle.values, 0.703125)" ] }, { "cell_type": "markdown", "id": "7", "metadata": {}, "source": [ "### Plot Time vs. Azimuth" ] }, { "cell_type": "code", "execution_count": null, "id": "8", "metadata": {}, "outputs": [], "source": [ "ds.azimuth.plot()" ] }, { "cell_type": "markdown", "id": "9", "metadata": {}, "source": [ "### Plot Range vs. Time\n", "\n", "We need to sort by time and specify the y-coordinate." ] }, { "cell_type": "code", "execution_count": null, "id": "10", "metadata": {}, "outputs": [], "source": [ "ds.DBZH.sortby(\"time\").plot(y=\"time\", cmap=\"HomeyerRainbow\")" ] }, { "cell_type": "markdown", "id": "11", "metadata": {}, "source": [ "### Plot Range vs. Azimuth\n" ] }, { "cell_type": "code", "execution_count": null, "id": "12", "metadata": {}, "outputs": [], "source": [ "ds.DBZH.plot(cmap=\"HomeyerRainbow\")" ] }, { "cell_type": "markdown", "id": "13", "metadata": {}, "source": [ "## backend_kwargs\n", "\n", "Beside `first_dim` there are several additional backend_kwargs for the `uf` backend, which handle different aspects of angle alignment. This comes into play, when azimuth and/or elevation arrays are not evenly spacend and other issues." ] }, { "cell_type": "code", "execution_count": null, "id": "14", "metadata": {}, "outputs": [], "source": [ "help(xd.io.UFBackendEntrypoint)" ] }, { "cell_type": "code", "execution_count": null, "id": "15", "metadata": {}, "outputs": [], "source": [ "ds = xr.open_dataset(fname, group=\"sweep_0\", engine=\"uf\", first_dim=\"time\")\n", "display(ds)" ] }, { "cell_type": "markdown", "id": "16", "metadata": {}, "source": [ "## open_uf_datatree\n", "\n", "The same works analoguous with the datatree loader. But additionally we can provide a sweep string, number or list." ] }, { "cell_type": "code", "execution_count": null, "id": "17", "metadata": {}, "outputs": [], "source": [ "help(xd.io.open_uf_datatree)" ] }, { "cell_type": "code", "execution_count": null, "id": "18", "metadata": {}, "outputs": [], "source": [ "dtree = xd.io.open_uf_datatree(fname, sweep=4)\n", "display(dtree)" ] }, { "cell_type": "markdown", "id": "19", "metadata": {}, "source": [ "### Plot Sweep Range vs. Time" ] }, { "cell_type": "code", "execution_count": null, "id": "20", "metadata": {}, "outputs": [], "source": [ "dtree[\"sweep_4\"].ds.DBZH.sortby(\"time\").plot(y=\"time\", cmap=\"HomeyerRainbow\")" ] }, { "cell_type": "markdown", "id": "21", "metadata": {}, "source": [ "### Plot Sweep Range vs. Azimuth" ] }, { "cell_type": "code", "execution_count": null, "id": "22", "metadata": {}, "outputs": [], "source": [ "dtree[\"sweep_4\"].ds.DBZH.plot(cmap=\"HomeyerRainbow\")" ] }, { "cell_type": "code", "execution_count": null, "id": "23", "metadata": {}, "outputs": [], "source": [ "dtree = xd.io.open_uf_datatree(fname, sweep=\"sweep_8\")\n", "display(dtree)" ] }, { "cell_type": "code", "execution_count": null, "id": "24", "metadata": {}, "outputs": [], "source": [ "dtree = xd.io.open_uf_datatree(fname, sweep=[0, 1, 8])\n", "display(dtree)" ] }, { "cell_type": "code", "execution_count": null, "id": "25", "metadata": {}, "outputs": [], "source": [ "dtree[\"sweep_0\"][\"sweep_fixed_angle\"].values" ] }, { "cell_type": "code", "execution_count": null, "id": "26", "metadata": {}, "outputs": [], "source": [ "dtree[\"sweep_8\"][\"sweep_fixed_angle\"].values" ] }, { "cell_type": "code", "execution_count": null, "id": "27", "metadata": {}, "outputs": [], "source": [ "dtree = xd.io.open_uf_datatree(fname)\n", "display(dtree)" ] }, { "cell_type": "code", "execution_count": null, "id": "28", "metadata": {}, "outputs": [], "source": [ "dtree[\"sweep_1\"]" ] }, { "cell_type": "markdown", "id": "29", "metadata": {}, "source": [ "## clean up" ] }, { "cell_type": "code", "execution_count": null, "id": "30", "metadata": {}, "outputs": [], "source": [ "tmpdir_obj.cleanup()" ] } ], "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.8" } }, "nbformat": 4, "nbformat_minor": 5 }