{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# PyScanCf Example 1\n",
    "- author: @syedhamidali\n",
    "- date: Sep 8, 2023"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "## You are using the Python ARM Radar Toolkit (Py-ART), an open source\n",
      "## library for working with weather radar data. Py-ART is partly\n",
      "## supported by the U.S. Department of Energy as part of the Atmospheric\n",
      "## Radiation Measurement (ARM) Climate Research Facility, an Office of\n",
      "## Science user facility.\n",
      "##\n",
      "## If you use this software to prepare a publication, please cite:\n",
      "##\n",
      "##     JJ Helmus and SM Collis, JORS 2016, doi: 10.5334/jors.119\n",
      "\n",
      "\n",
      "## Cite PyScanCf:\n",
      "\n",
      "## Syed, H. A., Sayyed, I., Kalapureddy, M. C. R., & Grandhi, K. K. (2021).\n",
      "## PyScanCf – The library for individual sweep datasets of IMD weather radars.\n",
      "## Zenodo. https://doi.org/10.5281/zenodo.5574160\n",
      "\n",
      "999\n"
     ]
    }
   ],
   "source": [
    "import glob\n",
    "\n",
    "import pyart\n",
    "\n",
    "import pyscancf as pcf\n",
    "\n",
    "print(pcf.__version__)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "Setup the paths for the input and output data, `!ls $inp` shows the raw IMD data files, this is how the B-type (short range - high resolution) IMD DWR data looks like."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[31mGOA210516024101-IMD-B.nc\u001b[m\u001b[m   \u001b[31mGOA210516030244-IMD-B.nc\u001b[m\u001b[m\n",
      "\u001b[31mGOA210516024101-IMD-B.nc.1\u001b[m\u001b[m \u001b[31mGOA210516030244-IMD-B.nc.1\u001b[m\u001b[m\n",
      "\u001b[31mGOA210516024101-IMD-B.nc.2\u001b[m\u001b[m \u001b[31mGOA210516030244-IMD-B.nc.2\u001b[m\u001b[m\n",
      "\u001b[31mGOA210516024101-IMD-B.nc.3\u001b[m\u001b[m \u001b[31mGOA210516030244-IMD-B.nc.3\u001b[m\u001b[m\n",
      "\u001b[31mGOA210516024101-IMD-B.nc.4\u001b[m\u001b[m \u001b[31mGOA210516030244-IMD-B.nc.4\u001b[m\u001b[m\n",
      "\u001b[31mGOA210516024101-IMD-B.nc.5\u001b[m\u001b[m \u001b[31mGOA210516030244-IMD-B.nc.5\u001b[m\u001b[m\n",
      "\u001b[31mGOA210516024101-IMD-B.nc.6\u001b[m\u001b[m \u001b[31mGOA210516030244-IMD-B.nc.6\u001b[m\u001b[m\n",
      "\u001b[31mGOA210516024101-IMD-B.nc.7\u001b[m\u001b[m \u001b[31mGOA210516030244-IMD-B.nc.7\u001b[m\u001b[m\n",
      "\u001b[31mGOA210516024101-IMD-B.nc.8\u001b[m\u001b[m \u001b[31mGOA210516030244-IMD-B.nc.8\u001b[m\u001b[m\n",
      "\u001b[31mGOA210516024101-IMD-B.nc.9\u001b[m\u001b[m \u001b[31mGOA210516030244-IMD-B.nc.9\u001b[m\u001b[m\n"
     ]
    }
   ],
   "source": [
    "inp = \"/Users/syed44/Downloads/Others/Git_Stuff/imd_temp_radar/B/\"\n",
    "out = \"/Users/syed44/Downloads/Others/Git_Stuff/imd_temp_radar/out/\"\n",
    "!ls $inp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function cfrad in module pyscancf.pyscancf:\n",
      "\n",
      "cfrad(\n",
      "    input_dir,\n",
      "    output_dir,\n",
      "    scan_type='B',\n",
      "    dualpol=False,\n",
      "    gridder=False,\n",
      "    plot=None,\n",
      "    nf=None\n",
      ")\n",
      "    Aggregate radar data into CfRadial1 format.\n",
      "\n",
      "    Parameters\n",
      "    ----------\n",
      "    input_dir : str\n",
      "        The directory path containing single-sweep radar data files.\n",
      "\n",
      "    output_dir : str\n",
      "        The directory path where the output data will be saved in CfRadial1 format.\n",
      "\n",
      "    scan_type : str, optional\n",
      "        The scan type, either \"B\" for short-range PPI (Plan Position Indicator)\n",
      "        or \"C\" for long-range PPI. Default is \"B\".\n",
      "\n",
      "    dualpol : bool, optional\n",
      "        Specifies whether the radar data contains dual-polarization products\n",
      "        such as ZDR (Differential Reflectivity) and RHOHV (Correlation Coefficient).\n",
      "        Set to True if present, otherwise False. Default is False.\n",
      "\n",
      "    gridder : bool, optional\n",
      "        Indicates whether data gridding should be performed. Set to True for data\n",
      "        gridding, otherwise False. Default is False.\n",
      "\n",
      "    plot : str, optional\n",
      "        Type of plots to generate for visualization. Options include \"REF\" for\n",
      "        reflectivity, \"VEL\" for velocity, \"WIDTH\" for spectrum width, or \"ALL\" for\n",
      "        all available plots. Default is None, which generates no plots.\n",
      "\n",
      "    nf : int, optional\n",
      "        Number of data files to group together during aggregation. Default is None,\n",
      "        meaning all available files will be aggregated together.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(pcf.cfrad)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "Let's see what we have got into `pcf.cfrad()` function. \n",
    "- It aggregates data to cfradial1 data. It takes inputs such as `input_dir`, `output_dir`, `scan_type`, `dualpol`, `gridder`, `plot`, and `nf`.\n",
    "- `input_dir` specifies the path of single sweep data directory, while `output_dir` specifies the path for output data.\n",
    "- `scan_type` takes two options: `B` for short-range PPI, and `C` for long-range PPI. \n",
    "- The `dualpol` parameter is set to True if the data contains dual-polarization products like ZDR and RHOHV. \n",
    "- `gridder` is a boolean parameter, which is set to True if the user wants to create a gridded output. \n",
    "- The `plot` argument is used to generate a cappi plot. It can be set to `REF`, `VELH`, `WIDTH`, or `ALL`. The names are not case-sensitive and will be taken care of by the `plot_cappi()` function. For example, if the data has a `reflectivity` field named `DBZ`, you can still write `REF` or `reflectivity`, and same is for other radar moments.\n",
    "- Lastly, `nf` is an integer parameter that specifies the number of files to group together, it is usually `10` for type `B`, and `2` or `3` for type `C` data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2025-04-19 11:46:32,063 - INFO - Packing 20 sweeps into 2 volumes\n",
      "2025-04-19 11:46:32,064 - INFO - Processing volume 1\n",
      "2025-04-19 11:46:33,041 - INFO - Saved cfrad_GOA210516024101-IMD-B.nc\n",
      "2025-04-19 11:46:46,753 - INFO - Saved grid_GOA210516024101-IMD-B.nc\n",
      "2025-04-19 11:46:46,753 - INFO - Processing volume 2\n",
      "2025-04-19 11:46:47,707 - INFO - Saved cfrad_GOA210516030244-IMD-B.nc\n",
      "2025-04-19 11:47:01,375 - INFO - Saved grid_GOA210516030244-IMD-B.nc\n",
      "2025-04-19 11:47:01,376 - INFO - Data merging done \n",
      "Time Taken: 0:00:54.063542\n"
     ]
    }
   ],
   "source": [
    "pcf.cfrad(input_dir=inp, output_dir=out, scan_type=\"B\", gridder=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Max-REF_GOA_20210516024104.png cfrad_GOA210516030244-IMD-B.nc\n",
      "Max-REF_GOA_20210516030306.png grid_GOA210516024101-IMD-B.nc\n",
      "cfrad_GOA210516024101-IMD-B.nc grid_GOA210516030244-IMD-B.nc\n"
     ]
    }
   ],
   "source": [
    "!ls $out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "files = glob.glob(out + \"grid*\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "...............................\n",
      "Plotting Max-Z 20210516024104\n",
      "...............................\n",
      "\n",
      "...............................\n",
      "Plotting Max-Z 20210516030306\n",
      "...............................\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 714360 characters]",
      "text/plain": [
       "<Figure size 1030x1000 with 5 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 713876 characters]",
      "text/plain": [
       "<Figure size 1030x1000 with 5 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for file in files:\n",
    "    grid = pyart.io.read_grid(file)\n",
    "    pcf.plot_cappi(\n",
    "        grid,\n",
    "        \"Z\",\n",
    "        cmap=\"SyedSpectral\",  # optional\n",
    "        crosshair=False,  # optional\n",
    "        savedir=None,  # optional\n",
    "        show_figure=True,  # optional\n",
    "        vmin=-10,\n",
    "        vmax=60,\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "...............................\n",
      "Plotting Max-Z 20210516024104\n",
      "...............................\n",
      "\n",
      "Figure(s) saved as /Users/syed44/Downloads/Others/Git_Stuff/imd_temp_radar/out//Max-Z_GOA_20210516024104.png\n",
      "...............................\n",
      "Plotting Max-Z 20210516030306\n",
      "...............................\n",
      "\n",
      "Figure(s) saved as /Users/syed44/Downloads/Others/Git_Stuff/imd_temp_radar/out//Max-Z_GOA_20210516030306.png\n"
     ]
    }
   ],
   "source": [
    "for file in files:\n",
    "    grid = pyart.io.read_grid(file)\n",
    "    pcf.plot_cappi(\n",
    "        grid,\n",
    "        \"Z\",\n",
    "        cmap=\"HomeyerRainbow\",  # optional\n",
    "        crosshair=False,  # optional\n",
    "        savedir=out,  # optional\n",
    "        show_figure=False,  # optional\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "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.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
