{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from netCDF4 import Dataset\n",
    "import numpy as np\n",
    "import cartopy.crs as ccrs\n",
    "import cartopy.feature as cfeature\n",
    "from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER\n",
    "from cartopy.util import add_cyclic_point\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.ticker as mticker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_example_nc_file = './prmsl.2000.nc'\n",
    "fh = Dataset(my_example_nc_file, mode='r')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(366, 91, 180)\n"
     ]
    }
   ],
   "source": [
    "lons = fh.variables['lon'][:]\n",
    "lats = fh.variables['lat'][:]\n",
    "times = fh.variables['time'][:]\n",
    "times_unit = fh.variables['time'].units\n",
    "var = fh.variables['prmsl']\n",
    "var_units = fh.variables['prmsl'].units\n",
    "\n",
    "print(np.shape(var))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Daily Pressure at Mean Sea Level')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 167104 characters]",
      "text/plain": [
       "<Figure size 816x612 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.subplots(figsize=(8, 6), dpi=102)\n",
    "plt.axis('off')\n",
    "\n",
    "ax = plt.axes(projection=ccrs.Robinson(central_longitude=10.0))\n",
    "ax.coastlines()\n",
    "\n",
    "# Plot Data\n",
    "data = np.squeeze(var[1,:,:])\n",
    "#cs = ax.pcolormesh(lons, lats, data, transform=ccrs.PlateCarree(), shading='auto')\n",
    "lon = np.arange(0, 360, 2)\n",
    "data, lon = add_cyclic_point(data, coord=lon)\n",
    "cs = ax.contourf(lon, lats, data, transform=ccrs.PlateCarree(), transform_first=False)\n",
    "\n",
    "# Add Grid Lines\n",
    "gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=0.5, color=\"grey\")\n",
    "gl.xlocator = mticker.FixedLocator(np.arange(-170,191,36))\n",
    "gl.ylocator = mticker.FixedLocator(np.arange(-90,90,15))\n",
    "gl.rotate_labels = False\n",
    "gl.top_labels = False\n",
    "gl.xformatter = LONGITUDE_FORMATTER\n",
    "gl.yformatter = LATITUDE_FORMATTER\n",
    "\n",
    "# Add Colorbar\n",
    "cbar = plt.colorbar(cs,orientation=\"horizontal\")\n",
    "cbar.set_label(var_units)\n",
    "\n",
    "# Add Title\n",
    "plt.title('Daily Pressure at Mean Sea Level')"
   ]
  }
 ],
 "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.10.6"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
