{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# IMD Radar Data" ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "The India Meteorological Department (IMD) provides radar data in a format similar to IRIS/Sigmet, but with some differences in structure and formatting. `radarx` offers specialized support for processing and analyzing this IMD radar data, ensuring compatibility and ease of use despite the variations from standard IRIS/Sigmet formats." ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "import os\n", "import subprocess\n", "import radarx as rx" ] }, { "cell_type": "markdown", "id": "3", "metadata": {}, "source": [ "## Load IMD Radar Data" ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "# Base URL for the radar data files on GitHub\n", "base_url = (\n", " \"https://raw.githubusercontent.com/syedhamidali/pyscancf_examples/main/data/goa16/\"\n", ")\n", "\n", "# List of radar data files to download\n", "files = [\n", " \"GOA210516024101-IMD-B.nc\",\n", " \"GOA210516024101-IMD-B.nc.1\",\n", " \"GOA210516024101-IMD-B.nc.2\",\n", " \"GOA210516024101-IMD-B.nc.3\",\n", " \"GOA210516024101-IMD-B.nc.4\",\n", " \"GOA210516024101-IMD-B.nc.5\",\n", " \"GOA210516024101-IMD-B.nc.6\",\n", " \"GOA210516024101-IMD-B.nc.7\",\n", " \"GOA210516024101-IMD-B.nc.8\",\n", " \"GOA210516024101-IMD-B.nc.9\",\n", "]\n", "\n", "# Target directory to save downloaded files\n", "target_dir = os.path.join(os.getenv(\"GITHUB_WORKSPACE\", \".\"), \"radarx_data\")\n", "os.makedirs(target_dir, exist_ok=True)\n", "\n", "\n", "# Function to download files using curl\n", "def download_with_curl(file_name):\n", " file_url = base_url + file_name\n", " file_path = os.path.join(target_dir, file_name)\n", "\n", " if not os.path.exists(file_path):\n", " print(f\"Downloading {file_name}...\")\n", " subprocess.run([\"curl\", \"-o\", file_path, file_url], check=True)\n", " print(f\"Downloaded {file_name}\")\n", " else:\n", " print(f\"{file_name} already exists.\")\n", "\n", " return file_path\n", "\n", "\n", "# Download the files\n", "downloaded_files = []\n", "for file_name in files:\n", " file_path = download_with_curl(file_name)\n", " downloaded_files.append(file_path)" ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "print(f\"Number of files: {len(downloaded_files)}\")" ] }, { "cell_type": "markdown", "id": "6", "metadata": {}, "source": [ "## Read a Sweep" ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "# Load the first radar file using radarx\n", "swp = rx.io.read_sweep(downloaded_files[0])\n", "swp" ] }, { "cell_type": "markdown", "id": "8", "metadata": {}, "source": [ "We have successfully read one sweep in the above cell. Now, let's create a volume scan." ] }, { "cell_type": "markdown", "id": "9", "metadata": {}, "source": [ "## Create a Volume" ] }, { "cell_type": "markdown", "id": "10", "metadata": {}, "source": [ "We have a total of 10 files, each representing a different sweep. Our goal is to combine these individual sweeps into a single CF-Radial volume scan dataset." ] }, { "cell_type": "code", "execution_count": null, "id": "11", "metadata": {}, "outputs": [], "source": [ "for file in downloaded_files:\n", " print(os.path.basename(file))" ] }, { "cell_type": "code", "execution_count": null, "id": "12", "metadata": {}, "outputs": [], "source": [ "vol = rx.io.read_volume(downloaded_files)" ] }, { "cell_type": "code", "execution_count": null, "id": "13", "metadata": {}, "outputs": [], "source": [ "vol" ] }, { "cell_type": "markdown", "id": "14", "metadata": {}, "source": [ "We have successfully generated a volume, which can now be explored further. The volume is stored in a DataTree, a high-level structure that organizes and manages the data within the xarray framework." ] }, { "cell_type": "code", "execution_count": null, "id": "15", "metadata": {}, "outputs": [], "source": [ "vol.groups" ] }, { "cell_type": "code", "execution_count": null, "id": "16", "metadata": {}, "outputs": [], "source": [ "ds = vol[\"volume_0\"].ds" ] }, { "cell_type": "code", "execution_count": null, "id": "17", "metadata": {}, "outputs": [], "source": [ "ds" ] }, { "cell_type": "markdown", "id": "18", "metadata": {}, "source": [ "We have successfully created a volume, and we can export it to CF-Radial formatted NetCDF using `xarray`." ] }, { "cell_type": "code", "execution_count": null, "id": "19", "metadata": {}, "outputs": [], "source": [ "# We can save all these cfradial1 files by iterating over the vol object\n", "\n", "outdir = \"IMD_OUT\"\n", "os.makedirs(outdir, exist_ok=True)\n", "for key in vol.children:\n", " ds = vol[key].ds\n", " time_str = ds.time.min().dt.strftime(\"%Y%m%d_%H%M%S\").item()\n", " ds.to_netcdf(os.path.join(outdir, f\"GOA_{time_str}_cfrad1.nc\"))" ] }, { "cell_type": "code", "execution_count": null, "id": "20", "metadata": {}, "outputs": [], "source": [ "os.listdir(\"IMD_OUT/\")" ] }, { "cell_type": "markdown", "id": "21", "metadata": {}, "source": [ "## Convert to CF/Radial2" ] }, { "cell_type": "markdown", "id": "22", "metadata": {}, "source": [ "We can also convert it to cfradial2 data using to_cfradial2 function" ] }, { "cell_type": "code", "execution_count": null, "id": "23", "metadata": {}, "outputs": [], "source": [ "dtree = rx.io.to_cfradial2_volumes(vol)" ] }, { "cell_type": "code", "execution_count": null, "id": "24", "metadata": {}, "outputs": [], "source": [ "dtree.groups" ] }, { "cell_type": "code", "execution_count": null, "id": "25", "metadata": {}, "outputs": [], "source": [ "ds = dtree[\"volume_0\"].ds" ] }, { "cell_type": "code", "execution_count": null, "id": "26", "metadata": {}, "outputs": [], "source": [ "# Export all the data in a dtree\n", "\n", "# for volume in dtree:\n", "# dtree[volume].to_netcdf()" ] } ], "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.4" } }, "nbformat": 4, "nbformat_minor": 5 }