{
 "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": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pyscancf as pcf\n",
    "import pyart\n",
    "import glob\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": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "inp = \"/Users/syed44/Downloads/Git_Stuff/imd_temp_radar/B/\"\n",
    "out = \"/Users/syed44/Downloads/Git_Stuff/imd_temp_radar/out/\"\n",
    "!ls $inp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "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": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "pcf.cfrad(input_dir=inp, output_dir=out, scan_type=\"B\", gridder=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "!ls $out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "files = glob.glob(out + \"grid*\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "for file in files:\n",
    "    grid = pyart.io.read_grid(file)\n",
    "    pcf.plot_cappi(\n",
    "        grid,\n",
    "        \"REF\",\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": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "for file in files:\n",
    "    grid = pyart.io.read_grid(file)\n",
    "    pcf.plot_cappi(\n",
    "        grid,\n",
    "        \"REF\",\n",
    "        cmap=\"pyart_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": {
  "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.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
