{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fc5f7b4b-a30e-4766-96ef-8dfc86dc44b5",
   "metadata": {},
   "source": [
    "# A Soft Starter: Max Daylight"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0b9da87-fb66-4b05-8b29-ab86f747b2a8",
   "metadata": {},
   "source": [
    "This notebook will show you the process of using a simple geocat-comp function. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03056bc5",
   "metadata": {},
   "source": [
    "## Finding an old NCL function\n",
    "Say you have an old NCL script that you are looking to convert into python that uses the `daylight_fao56` routine and you want to see if GeoCAT has converted the function to python.\n",
    "\n",
    "<img src=\"../images/geocat-comp/NCL_doc.png\" width=\"100%\" alt=\"daylight_fao56 NCL documentation\">"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41128e1d",
   "metadata": {},
   "source": [
    "The best way to find the python equivalent function, if it exists, is to go to GeoCAT-comp's documentation and search for the function name, as shown below. If functions are a translation of NCL functions, they will have their NCL counterparts linked in the documentation.\n",
    "\n",
    "<img src=\"../images/geocat-comp/search.png\" width=\"100%\" alt=\"searching for NCL function daylight_fao56 in geocat-comp's docs\">"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fad08c6d",
   "metadata": {},
   "source": [
    "We can see that the GeoCAT-comp equivalent of NCL's `daylight_fao56` is `max_daylight`, which we can now use in our python script to provide the same functionality as existed in NCL."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82c6c121",
   "metadata": {},
   "source": [
    "## Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f59d3a95",
   "metadata": {},
   "outputs": [],
   "source": [
    "from geocat.comp import max_daylight\n",
    "import xarray as xr\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2def03d",
   "metadata": {},
   "source": [
    "Now, let's look at `max_daylight`'s [documentation](https://geocat-comp.readthedocs.io/en/latest/user_api/generated/geocat.comp.crop.max_daylight.html) to see what it does. \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74e74554",
   "metadata": {
    "vscode": {
     "languageId": "html"
    }
   },
   "source": [
    "<iframe src=\"https://geocat-comp.readthedocs.io/en/latest/user_api/generated/geocat.comp.crop.max_daylight.html\" title=\"geocat-comp max_daylight documentation\" width=100% height=600></iframe>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39f8de41",
   "metadata": {},
   "source": [
    "## Calculate maximum daylight hours"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab7573b0",
   "metadata": {},
   "source": [
    "So, let's get the number of daylight hours for an entire year in Boulder (lat approx 40N)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca2c5406",
   "metadata": {},
   "outputs": [],
   "source": [
    "daylight_hours = max_daylight(np.arange(0, 365), 40)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15831cd2",
   "metadata": {},
   "source": [
    "Now, let's put our data into an xarray DataArray. \n",
    "\n",
    "This could, of course, have all been done in one step, but we wanted to introduce the absolute basics of geocat-comp."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abb1e9b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "daylight_hours = xr.DataArray(daylight_hours, dims=(\"days\", \"hours of daylight\")) \n",
    "daylight_hours"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78f3de3a",
   "metadata": {},
   "source": [
    "## A little visualization"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5b6dc9f",
   "metadata": {},
   "source": [
    "And now, let's take a little peek at the data using xarray's built-in plotting functionality:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9015fd59",
   "metadata": {},
   "outputs": [],
   "source": [
    "daylight_hours.plot()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.6 ('geocat')",
   "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"
  },
  "vscode": {
   "interpreter": {
    "hash": "7961b2108ee7d1ae8b8cda2085110605487fed8e1d2fc7de0f719df8d0b8bd85"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
