{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nWork with Fields in aWherePy\n============================\n\nLearn how to create, get, update, and delete fields with aWherePy.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create, Get, Update, and Delete Fields with aWherePy\n----------------------------------------------------\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>The example below will show you how to use the ``create_field()``,\n   ``get_fields()``, ``update_field()``, and ``delete_field()`` functions to\n   work with fields in the aWhere API.</p></div>\n\nIn this vignette, you will create an example field located in Manchester,\nVermont. You will also get the field, update the field, and delete the field.\naWhere API data can be accessed by inputting locations or fields (which\nalso have locations) into the API calls. A field created with this workflow\ncan be used with other aWherePy modules.\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 awherepy.fields as awf"
      ]
    },
    {
      "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": [
        "Create an aWhere Field\n----------------------\n\nTo create a field within the aWhere API, a you must specify a field ID and\nlocation (longitude, latitude) associated with the field. You may also\ninclude a field name, farm ID, and area in acres, but this is not required\nfor the funtion to work. You create an aWhere field with the\n``create_field()`` function.\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    )"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Get an aWhere Field\n-------------------\n\nCreating an aWhere field returns the field in geodataframe format when the\nfield is created. However, once a field is created, another field with the\nsame field ID cannot be created unless the original field is deleted first.\nWhen you want to access and store that field in a geodataframe after it has\nalready been created, you used the ``get_fields()`` function. This function\nallows you to access a specific field (by field ID) or all fields within an\naWhere application.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Get all fields associated with an aWhere application\nall_fields = awf.get_fields(awhere_api_key, awhere_api_secret)\n\n\n# Get a single field, specified by field ID\nsingle_field = awf.get_fields(\n    awhere_api_key, awhere_api_secret, kwargs={\"field_id\": \"VT-Manchester\"}\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Update an aWhere Field\n----------------------\n\nThere may be a time when you need to update some of the parameters in a\nfield. This can be completed with the ``update_field()`` function. Note that\nat this time, the aWhere API only supports updates of the field name and farm\nid. The field ID is required input so that the aWhere API can update the\ncorrect field.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Define update info\nupdate_info = {\n    \"field_id\": \"VT-Manchester\",\n    \"field_name\": \"VT-Manchester-Field-Update\",\n    \"farm_id\": \"VT-Mancheseter-Farm-Update\",\n}\n\n# Update field\nfield = awf.update_field(\n    awhere_api_key, awhere_api_secret, field_info=update_info\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Delete an aWhere Field\n----------------------\n\nIf you need to delete a field, you use the ``delete_field()`` function. This\nmay be necessary if you want to re-use a field ID, as you cannot have more\nthan one field with the same field ID. The field ID is used as the identifer\nto locate the field for deletion.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Delete field\noutput_message = awf.delete_field(\n    awhere_api_key, awhere_api_secret, field_id=\"VT-Manchester\"\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
}