{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# Example IMD Radar Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import glob\n",
    "import pyart\n",
    "import requests\n",
    "import subprocess\n",
    "import pyscancf as pcf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['GOA210516024101-IMD-B.nc', 'GOA210516024101-IMD-B.nc.1', 'GOA210516024101-IMD-B.nc.2', 'GOA210516024101-IMD-B.nc.3', 'GOA210516024101-IMD-B.nc.4', 'GOA210516024101-IMD-B.nc.5', 'GOA210516024101-IMD-B.nc.6', 'GOA210516024101-IMD-B.nc.7', 'GOA210516024101-IMD-B.nc.8', 'GOA210516024101-IMD-B.nc.9']\n"
     ]
    }
   ],
   "source": [
    "# GitHub raw content base URL (for downloading)\n",
    "base_url = (\n",
    "    \"https://raw.githubusercontent.com/syedhamidali/pyscancf_examples/main/data/goa16/\"\n",
    ")\n",
    "api_url = (\n",
    "    \"https://api.github.com/repos/syedhamidali/pyscancf_examples/contents/data/goa16\"\n",
    ")\n",
    "response = requests.get(api_url)\n",
    "files = [item[\"name\"] for item in response.json()]\n",
    "print(files)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading GOA210516024101-IMD-B.nc...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "100 1413k  100 1413k    0     0  1524k      0 --:--:-- --:--:-- --:--:-- 1522k\n",
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloaded GOA210516024101-IMD-B.nc\n",
      "Downloading GOA210516024101-IMD-B.nc.1...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100 1413k  100 1413k    0     0  1432k      0 --:--:-- --:--:-- --:--:-- 1431k\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloaded GOA210516024101-IMD-B.nc.1\n",
      "Downloading GOA210516024101-IMD-B.nc.2...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "100 1413k  100 1413k    0     0  2025k      0 --:--:-- --:--:-- --:--:-- 2024k\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloaded GOA210516024101-IMD-B.nc.2\n",
      "Downloading GOA210516024101-IMD-B.nc.3...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "100 1413k  100 1413k    0     0  1736k      0 --:--:-- --:--:-- --:--:-- 1736k\n",
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloaded GOA210516024101-IMD-B.nc.3\n",
      "Downloading GOA210516024101-IMD-B.nc.4...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100 1413k  100 1413k    0     0  2101k      0 --:--:-- --:--:-- --:--:-- 2103k\n",
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloaded GOA210516024101-IMD-B.nc.4\n",
      "Downloading GOA210516024101-IMD-B.nc.5...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100 1413k  100 1413k    0     0  2070k      0 --:--:-- --:--:-- --:--:-- 2069k\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloaded GOA210516024101-IMD-B.nc.5\n",
      "Downloading GOA210516024101-IMD-B.nc.6...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "100 1413k  100 1413k    0     0  2063k      0 --:--:-- --:--:-- --:--:-- 2063k\n",
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloaded GOA210516024101-IMD-B.nc.6\n",
      "Downloading GOA210516024101-IMD-B.nc.7...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100 1413k  100 1413k    0     0  1861k      0 --:--:-- --:--:-- --:--:-- 1862k\n",
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloaded GOA210516024101-IMD-B.nc.7\n",
      "Downloading GOA210516024101-IMD-B.nc.8...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100 1413k  100 1413k    0     0  1975k      0 --:--:-- --:--:-- --:--:-- 1973k\n",
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloaded GOA210516024101-IMD-B.nc.8\n",
      "Downloading GOA210516024101-IMD-B.nc.9...\n",
      "Downloaded GOA210516024101-IMD-B.nc.9\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100 1413k  100 1413k    0     0  1731k      0 --:--:-- --:--:-- --:--:-- 1729k\n"
     ]
    }
   ],
   "source": [
    "# Target directory to save downloaded files\n",
    "target_dir = os.path.join(os.getenv(\"GITHUB_WORKSPACE\", \".\"), \"goa_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": 12,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_dir = target_dir\n",
    "out_dir = \"outdir\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2025-04-19 01:33:16,845 - INFO - Packing 10 sweeps into 1 volumes\n",
      "2025-04-19 01:33:16,846 - INFO - Processing volume 1\n",
      "2025-04-19 01:33:17,773 - INFO - Saved cfrad_GOA210516024101-IMD-B.nc\n",
      "2025-04-19 01:33:17,774 - INFO - Data merging done \n",
      "Total Time Elapsed: 11:02:19.057401\n"
     ]
    }
   ],
   "source": [
    "pcf.cfrad(\n",
    "    input_dir,\n",
    "    out_dir,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['outdir/cfrad_GOA210516024101-IMD-B.nc']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "files = sorted(glob.glob(out_dir + \"/*.nc\"))\n",
    "files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "...............................\n",
      "Plotting Max-REF 20210516024104\n",
      "...............................\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 521696 characters]",
      "text/plain": [
       "<Figure size 1030x1000 with 5 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for file in files:\n",
    "    radar = pyart.io.read(file)\n",
    "    grid = pcf.get_grid(radar, grid_shape=(20, 401, 401), height=10, length=250)\n",
    "    grid.fields[\"REF\"] = grid.fields.pop(\"Z\")\n",
    "    pcf.plot_cappi(grid, \"REF\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "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.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
