{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# CB_Temperature.py\n\nThis script illustrates multiple color schemes for color maps which will allow for those\nimpacted by color blindness to see visualizations. Using rainbow color schemes is also\na poor choice in color scheme for images that may be transferred to a black and white \nscale for printing. This code addresses a handful of options to use in place of rainbow \ncolor schemes for use in the matplotlib.pyplot library.\n\nMore information on this subject can be found here:\n    - https://agilescientific.com/blog/2017/12/14/no-more-rainbows\n    - `https://www.researchgate.net/publication/328361220 <https://www.researchgate.net/publication/328361220_The_Effect_of_Color_Scales_on_Climate_Scientists'_Objective_and_Subjective_Performance_in_Spatial_Data_Analysis_Tasks>`_\n\nMore color schemes can be found here:\n    - https://matplotlib.org/3.1.1/tutorials/colors/colormaps.html\n\nSee following URL to see the reproduced plot & script from the GeoCAT examples gallery:\n    - https://geocat-examples.readthedocs.io/en/latest/gallery/Contours/NCL_ce_3_1_lg.html#sphx-glr-gallery-contours-ncl-ce-3-1-lg-py\n\nFigure 1. \n   - The rainbow color scheme is problematic due to the lack of a natural perceived ordering of colors,\n     perceptual changes in the colors (ex: yellow and green blend together easily), and is sensitive to \n     deficiencies in vision\n\nFigure 2. \n   - The coolwarm diverging scheme should be used when both high and low values are interesting. \n     However, be careful using this scheme if the projection will be printed to black and white. \n\nFigure 3. \n  - This is an example of a less distinct contrasting color gradient. This choice in color scheme would \n    be a good choice for printing in black and white but may create some challenges for individuals who \n    experience blue-green colorblindness. \n\nFigure 4.\n - This plot shows how drastically contrasting colors can be incredibly useful for plotting this type of data.\n   This color scheme will work well for color blind impacted individuals and is black and white print friendly.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Import packages:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport xarray as xr\nimport cartopy.feature as cfeature\nimport cartopy.crs as ccrs\nimport matplotlib.pyplot as plt\n\nimport geocat.datafiles as gdf\nfrom geocat.viz import cmaps as gvcmaps\nfrom geocat.viz import util as gvutil"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Read in data:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Open a netCDF data file using xarray default engine and load the data into xarrays\nds = xr.open_dataset(gdf.get(\"netcdf_files/h_avg_Y0191_D000.00.nc\"),\n                     decode_times=False)\n\n# Extract a slice of the data\nt = ds.T.isel(time=0, z_t=0).sel(lat_t=slice(-60, 30), lon_t=slice(30, 120))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig = plt.figure(figsize=(12, 12))\n\ndef Plot(color, row, col, pos, title):\n\n    # Generate axes, using Cartopy, drawing coastlines, and adding features\n    projection = ccrs.PlateCarree()\n    ax1 = plt.subplot(row, col, pos, projection=projection)\n    ax1.coastlines(linewidths=0.5)\n    ax1.add_feature(cfeature.LAND, facecolor=\"lightgray\")\n\n    # Import an NCL colormap\n    newcmp = color\n\n    # Contourf-plot data\n    temp = t.plot.contourf(ax=ax1,\n                           transform=projection,\n                           levels=40,\n                           vmin=0,\n                           vmax=32,\n                           cmap=newcmp,\n                           add_colorbar=False)\n    \n    # Add color bar\n    cbar_ticks = np.arange(0, 32, 2)\n    cbar = plt.colorbar(temp, \n                        orientation='vertical', \n                        shrink=0.8, pad=0.05, \n                        extendrect=True,\n                        ticks=cbar_ticks)\n    \n    cbar.ax.tick_params(labelsize=10)\n\n    # Use geocat.viz.util convenience function to set axes parameters without calling several matplotlib functions\n    # Set axes limits, and tick values\n    gvutil.set_axes_limits_and_ticks(ax1, xlim=(30, 120), ylim=(-60, 30))\n\n    # Use geocat.viz.util convenience function to set titles and labels without calling several matplotlib functions\n    gvutil.set_titles_and_labels(ax1,\n                                 maintitle=title,\n                                 maintitlefontsize=14,\n                                 xlabel=\"\",\n                                 ylabel=\"\")\n\n# Plot first color map\nPlot(gvcmaps.BlAqGrYeOrRe, 2, 2, 1, \"Figure 1: \\n Rainbow Color Projection\")\n\n# plot second color map\nPlot(\"coolwarm\", 2, 2, 2, \"Figure 2: \\n Coolwarm Color Projection\")\n\n# plot third color map\nPlot(\"viridis\", 2, 2, 3, \"Figure 3: \\n Viridis Color Projection\")\n\n# Plot fourth color map\nPlot(\"magma\", 2, 2, 4, \"Figure 4: \\n Magma Color Projection\")\n\nfig.suptitle(\"Projections of Temperature\", x=.5, y=.95, fontsize=18)"
      ]
    }
  ],
  "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.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}