|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "You will need to install the following packages:\n", |
| 8 | + "- `ipyleaflet`\n", |
| 9 | + "- `requests`\n", |
| 10 | + "- `xarray`\n", |
| 11 | + "- `netcdf4`" |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": null, |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "# Set up for JupyterLite\n", |
| 21 | + "try:\n", |
| 22 | + " import piplite\n", |
| 23 | + " await piplite.install(['ipyleaflet')\n", |
| 24 | + "except ImportError:\n", |
| 25 | + " pass" |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "code", |
| 30 | + "execution_count": 1, |
| 31 | + "metadata": {}, |
| 32 | + "outputs": [], |
| 33 | + "source": [ |
| 34 | + "import ipyleaflet\n", |
| 35 | + "from ipyleaflet.velocity import Velocity\n", |
| 36 | + "import xarray as xr\n", |
| 37 | + "from branca.colormap import linear" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "code", |
| 42 | + "execution_count": 2, |
| 43 | + "metadata": {}, |
| 44 | + "outputs": [], |
| 45 | + "source": [ |
| 46 | + "center = [44.33956524809713, -130.60546875000003]\n", |
| 47 | + "zoom = 3\n", |
| 48 | + "m = ipyleaflet.Map(\n", |
| 49 | + " center=center,\n", |
| 50 | + " zoom=zoom,\n", |
| 51 | + " interpolation=\"nearest\",\n", |
| 52 | + " basemap=ipyleaflet.basemaps.CartoDB.DarkMatter,\n", |
| 53 | + ")" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": 3, |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [], |
| 61 | + "source": [ |
| 62 | + "import os\n", |
| 63 | + "\n", |
| 64 | + "if not os.path.exists(\"wind-global.nc\"):\n", |
| 65 | + " url = \"https://github.com/benbovy/xvelmap/raw/master/notebooks/wind-global.nc\"\n", |
| 66 | + " import requests\n", |
| 67 | + "\n", |
| 68 | + " r = requests.get(url)\n", |
| 69 | + " wind_data = r.content\n", |
| 70 | + " with open(\"wind-global.nc\", \"wb\") as f:\n", |
| 71 | + " f.write(wind_data)" |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "code", |
| 76 | + "execution_count": 4, |
| 77 | + "metadata": {}, |
| 78 | + "outputs": [], |
| 79 | + "source": [ |
| 80 | + "ds = xr.open_dataset(\"wind-global.nc\")" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "code", |
| 85 | + "execution_count": 5, |
| 86 | + "metadata": {}, |
| 87 | + "outputs": [], |
| 88 | + "source": [ |
| 89 | + "colormap = linear.viridis" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": 6, |
| 95 | + "metadata": {}, |
| 96 | + "outputs": [], |
| 97 | + "source": [ |
| 98 | + "display_options = {\n", |
| 99 | + " \"velocityType\": \"Global Wind\",\n", |
| 100 | + " \"displayPosition\": \"bottomleft\",\n", |
| 101 | + " \"displayEmptyString\": \"No wind data\",\n", |
| 102 | + "}\n", |
| 103 | + "wind = Velocity(\n", |
| 104 | + " data=ds,\n", |
| 105 | + " zonal_speed=\"u_wind\",\n", |
| 106 | + " meridional_speed=\"v_wind\",\n", |
| 107 | + " latitude_dimension=\"lat\",\n", |
| 108 | + " longitude_dimension=\"lon\",\n", |
| 109 | + " velocity_scale=0.01,\n", |
| 110 | + " max_velocity=20,\n", |
| 111 | + " colormap = colormap,\n", |
| 112 | + " display_options=display_options,\n", |
| 113 | + ")" |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "code", |
| 118 | + "execution_count": 7, |
| 119 | + "metadata": {}, |
| 120 | + "outputs": [ |
| 121 | + { |
| 122 | + "data": { |
| 123 | + "application/vnd.jupyter.widget-view+json": { |
| 124 | + "model_id": "935083f2ac9046bb822298214e4f541d", |
| 125 | + "version_major": 2, |
| 126 | + "version_minor": 0 |
| 127 | + }, |
| 128 | + "text/plain": [ |
| 129 | + "Map(center=[44.33956524809713, -130.60546875000003], controls=(ZoomControl(options=['position', 'zoom_in_text'…" |
| 130 | + ] |
| 131 | + }, |
| 132 | + "metadata": {}, |
| 133 | + "output_type": "display_data" |
| 134 | + } |
| 135 | + ], |
| 136 | + "source": [ |
| 137 | + "colormap_control = ipyleaflet.ColormapControl(\n", |
| 138 | + " caption='Unemployment rate',\n", |
| 139 | + " colormap=colormap,\n", |
| 140 | + " value_min=wind.min_velocity,\n", |
| 141 | + " value_max=wind.max_velocity,\n", |
| 142 | + " position='topright',\n", |
| 143 | + " transparent_bg=False\n", |
| 144 | + ")\n", |
| 145 | + "m.add(wind)\n", |
| 146 | + "m.add(colormap_control)" |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "code", |
| 151 | + "execution_count": null, |
| 152 | + "metadata": {}, |
| 153 | + "outputs": [], |
| 154 | + "source": [] |
| 155 | + } |
| 156 | + ], |
| 157 | + "metadata": { |
| 158 | + "kernelspec": { |
| 159 | + "display_name": "Python 3 (ipykernel)", |
| 160 | + "language": "python", |
| 161 | + "name": "python3" |
| 162 | + }, |
| 163 | + "language_info": { |
| 164 | + "codemirror_mode": { |
| 165 | + "name": "ipython", |
| 166 | + "version": 3 |
| 167 | + }, |
| 168 | + "file_extension": ".py", |
| 169 | + "mimetype": "text/x-python", |
| 170 | + "name": "python", |
| 171 | + "nbconvert_exporter": "python", |
| 172 | + "pygments_lexer": "ipython3", |
| 173 | + "version": "3.9.13" |
| 174 | + } |
| 175 | + }, |
| 176 | + "nbformat": 4, |
| 177 | + "nbformat_minor": 4 |
| 178 | +} |
0 commit comments