{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nWork with Models in aWherePy\n============================\n\nLearn how to get models, model details, and model results with aWherePy.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Get Models, Model Details, and Model Results with aWherePy\n----------------------------------------------------------\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>The example below will show you how to use the ``get_models()``,\n   ``get_model_details()``, and ``get_model_results()`` functions to work\n   to work with agronomic models in the aWhere API.</p></div>\n\nIn this vignette, you will get a list of available agronomic models in the\naWhere API and get details about those models. Then, you will create an\nexample field in Manchester, Vermont, add a hard red wheat crop to that field\nand get the model results for that crop.\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Import Packages\n---------------\n\nIn order to use the functionality in this example, the following packages\nneed to be imported.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\nimport random\nimport awherepy.fields as awf\nimport awherepy.plantings as awp\nimport awherepy.models as awm"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Prerequisites\n-------------\n\nIn order to make calls to any aWhere API, you must provide a valid API key\nand secret. The key and secret used in this example are stored as\nenvironment variables.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Define aWhere API key and secret\nawhere_api_key = os.environ.get(\"AWHERE_API_KEY\")\nawhere_api_secret = os.environ.get(\"AWHERE_API_SECRET\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Get All Models\n--------------\n\nTo get a dataframe with all available aWhere models, you use the\n``get_models()`` function with default parameters. This can be useful for\ncases when you need information about all models in the aWhere API, as\nopposed to a single model.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Get all models\nall_models = awm.get_models(key=awhere_api_key, secret=awhere_api_secret)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Get a Single Model\n------------------\n\nTo get a dataframe with a single aWhere model, you use the ``get_models()``\nfunction and include the specific model ID. This example selects a random\nmodel ID from the list of all available models.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Get single model\nmodel = awm.get_models(\n    key=awhere_api_key,\n    secret=awhere_api_secret,\n    model_id=random.choice(awm.MODELS_LIST),\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Get All Model Details\n---------------------\n\nTo get a dataframe with all available aWhere model details, you use the\n``get_model_details()`` function with default parameters. This can be useful\nfor cases when you need information about all models in the aWhere API, as\nopposed to a single model. The ``get_model_details()`` function returns both\nthe model base details as well as details about the model stages.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Get all model details\nbase_details, stage_details = awm.get_model_details(\n    awhere_api_key, awhere_api_secret\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Get Details for a Single Model\n------------------------------\n\nTo get a dataframe with details for a single aWhere model, you use the\n``get_model_details()`` function and include the specific model ID. This\nexample selects a random model ID from the list of all available models. The\n``get_model_details()`` function returns both the model base details as well\nas details about the model stages.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Get details for a single model\nbase_details, stage_details = awm.get_model_details(\n    awhere_api_key, awhere_api_secret, model_id=random.choice(awm.MODELS_LIST),\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Get Model Results for a Planted Crop\n------------------------------------\n\nTo get model results for a crop, you must first create an aWhere field\n(using the ``awherepy.crops`` module) and plant a crop associated with that\nfield (using the ``awherepy.plantings`` module). You then use the\n``get_model_results()`` function to get the results of a specified model.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Define field paramaters\nfield_info = {\n    \"field_id\": \"VT-Manchester\",\n    \"field_name\": \"VT-Manchester-Field\",\n    \"farm_id\": \"VT-Manchester-Farm\",\n    \"center_latitude\": 43.1636875,\n    \"center_longitude\": -73.0723269,\n    \"acres\": 10,\n}\n\n# Create field\ntry:\n    field = awf.create_field(\n        awhere_api_key, awhere_api_secret, field_info=field_info\n    )\n\n# Delete field if already exists\nexcept KeyError:\n    awf.delete_field(\n        awhere_api_key, awhere_api_secret, field_id=field_info.get(\"field_id\"),\n    )\n\n    # Create field again\n    field = awf.create_field(\n        awhere_api_key, awhere_api_secret, field_info=field_info\n    )\n\n# Define planting parameters\nplanting_info = {\n    \"crop\": \"wheat-hardred\",\n    \"planting_date\": \"2020-05-01\",\n    \"projected_harvest_date\": \"2020-09-30\",\n    \"projected_yield_amount\": 200,\n    \"projected_yield_units\": \"boxes\",\n}\n\n# Create planting\nplanting = awp.create_planting(\n    awhere_api_key,\n    awhere_api_secret,\n    field_id=\"VT-Manchester\",\n    planting_info=planting_info,\n)\n\n# Get model results\nresults = awm.get_model_results(\n    awhere_api_key,\n    awhere_api_secret,\n    field_id=\"VT-Manchester\",\n    model_id=\"WheatHardRedMSU\",\n)"
      ]
    }
  ],
  "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.8.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}