{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Plotting Wind Direction Using Barbs\n\nThis example demonstrates using barbs to plot wind speed contours and wind\ndirection barbs from wind vector component input data. The vector components\nare co-located in space in this case.\n\nThe magnitude of the wind in the original data is low and so doesn't illustrate\nthe full range of barbs. The wind is scaled to simulate a storm that better\nillustrates the range of barbs that are available.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nimport iris\nimport iris.plot as iplt\nimport iris.quickplot as qplt\n\n\ndef main():\n    # Load the u and v components of wind from a pp file\n    infile = iris.sample_data_path(\"wind_speed_lake_victoria.pp\")\n\n    uwind = iris.load_cube(infile, \"x_wind\")\n    vwind = iris.load_cube(infile, \"y_wind\")\n\n    uwind.convert_units(\"knot\")\n    vwind.convert_units(\"knot\")\n\n    # To illustrate the full range of barbs, scale the wind speed up to pretend\n    # that a storm is passing over\n    magnitude = (uwind**2 + vwind**2) ** 0.5\n    magnitude.convert_units(\"knot\")\n    max_speed = magnitude.collapsed(\n        (\"latitude\", \"longitude\"), iris.analysis.MAX\n    ).data\n    max_desired = 65\n\n    uwind = uwind / max_speed * max_desired\n    vwind = vwind / max_speed * max_desired\n\n    # Create a cube containing the wind speed\n    windspeed = (uwind**2 + vwind**2) ** 0.5\n    windspeed.rename(\"windspeed\")\n    windspeed.convert_units(\"knot\")\n\n    plt.figure()\n\n    # Plot the wind speed as a contour plot\n    qplt.contourf(windspeed)\n\n    # Add wind barbs except for the outermost values which overhang the edge\n    # of the plot if left\n    iplt.barbs(uwind[1:-1, 1:-1], vwind[1:-1, 1:-1], pivot=\"middle\", length=6)\n\n    plt.title(\"Wind speed during a simulated storm\")\n    qplt.show()\n\n\nif __name__ == \"__main__\":\n    main()"
      ]
    }
  ],
  "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.8"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}