{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Wind and Wind Stress"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "HpKoz3iy_OeX"
},
"source": [
"This notebook will try to map wind and wind stress over some of the waters near Rhode Island, using the NOAA/NCDC datasets in the [RIDDC ERDDAP server](https://pricaimcit.services.brown.edu/erddap/index.html).\n",
"\n",
"**Note: The urls used in this code constrain latitude & longitude in a way that may or may not be ideal. I haven't yet figured out what the best lat/lon constraints are for this.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "rF558jte-gSl"
},
"outputs": [],
"source": [
"!pip install netCDF4\n",
"import requests\n",
"from netCDF4 import Dataset as NetCDFFile\n",
"import matplotlib.pyplot as plt\n",
"#To use cartopy in a Colab notebook, I need a particular sequence of installs (as shown at this notebook: https://colab.research.google.com/github/adamlamee/CODINGinK12/blob/master/notebooks/quakes.ipynb#scrollTo=3LkZkXvnMAr4)\n",
"!apt-get -qq install python-cartopy python3-cartopy;\n",
"!pip uninstall -y shapely;\n",
"!pip install shapely --no-binary shapely;\n",
"import cartopy.crs as ccrs\n",
"import cartopy.feature as cfeature\n",
"from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter\n",
"from matplotlib.axes import Axes\n",
"from cartopy.mpl.geoaxes import GeoAxes\n",
"GeoAxes._pcolormesh_patched = Axes.pcolormesh"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "IcvwRi00evbp"
},
"source": [
"The following code should make a pcolormesh of wind speed, but as you can see, it is not working for some reason..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 305
},
"colab_type": "code",
"id": "qbKd7YIm-16D",
"outputId": "8c87db6f-e8cf-4a99-81b9-3ffd903c6890"
},
"outputs": [
{
"ename": "OSError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mOSError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0mr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrequests\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mallow_redirects\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'test.nc'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'wb'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcontent\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 19\u001b[0;31m \u001b[0mnc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mNetCDFFile\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'test.nc'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 20\u001b[0m \u001b[0mnc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0;31m#defining some variables from the dataset:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32mnetCDF4/_netCDF4.pyx\u001b[0m in \u001b[0;36mnetCDF4._netCDF4.Dataset.__init__\u001b[0;34m()\u001b[0m\n",
"\u001b[0;32mnetCDF4/_netCDF4.pyx\u001b[0m in \u001b[0;36mnetCDF4._netCDF4._ensure_nc_success\u001b[0;34m()\u001b[0m\n",
"\u001b[0;31mOSError\u001b[0m: [Errno -51] NetCDF: Unknown file format: b'test.nc'"
]
}
],
"source": [
"#the code dealing with datetime here is based on code at this link: https://stackoverflow.com/questions/1060279/iterating-through-a-range-of-dates-in-python\n",
"#from google.colab import drive\n",
"#drive.mount('/content/gdrive')\n",
"#images_dir = '/content/gdrive/Shared drives/BFK_BOG/SURF_UG/Benny/Images/erdMH1par08day'\n",
"import numpy as np\n",
"from datetime import date, timedelta\n",
"#generating a list of dates:\n",
"dates2 = []\n",
"start_date = date(2003, 1, 5) #can change dates if you want. This may or may not be the actual start date of the dataset, and the end date will change as it is updated\n",
"end_date = date(2020, 5, 20)\n",
"delta = timedelta(weeks=20)\n",
"while start_date<=end_date:\n",
" dates2.append(str(start_date))\n",
" start_date += delta\n",
"#I got the following url interactively from the ERDDAP server:\n",
"url = 'https://pricaimcit.services.brown.edu/erddap/griddap/ncdcOw6hr_LonPM180.nc?u[(2011-09-30T18:00:00Z):1:(2011-09-30T18:00:00Z)][(10):1:(10.0)][(40.5):1:(42.25)][(-72.75):1:(-69.75)],v[(2011-09-30T18:00:00Z):1:(2011-09-30T18:00:00Z)][(10):1:(10.0)][(40.5):1:(41.5)][(-72.65):1:(-70.25)]'\n",
"r = requests.get(url, allow_redirects=True)\n",
"open('test.nc', 'wb').write(r.content)\n",
"nc = NetCDFFile('test.nc')\n",
"nc\n",
"#defining some variables from the dataset:\n",
"lat = nc.variables['latitude'][:]\n",
"lon = nc.variables['longitude'][:]\n",
"time = nc.variables['time'][:]\n",
"u = nc.variables['u'][:]\n",
"v = nc.variables['v'][:]\n",
"#plotting:\n",
"plt.pcolormesh(lon, lat, v[0, 0, :, :])\n",
"plt.colorbar()\n",
"plt.show()\n",
"plt.pcolormesh(lon, lat, u[0, 0, :, :])\n",
"plt.colorbar()\n",
"plt.show()\n",
"#since it's not working right now, I haven't bothered to finish with labeling"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "TmitYDKJ3g7w"
},
"source": [
"Graphing wind stress seems to work better, although it is very low resolution. This code will make pcolormesh plots of wind stress for the x and y components separately. Then I attempt to make a quiver plot for it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "yyHiPqlR0dNj"
},
"outputs": [],
"source": [
"#the code dealing with datetime here is based on code at this link: https://stackoverflow.com/questions/1060279/iterating-through-a-range-of-dates-in-python\n",
"import numpy as np\n",
"from datetime import date, timedelta\n",
"import xarray as xr\n",
"dates2 = []\n",
"start_date = date(2003, 1, 5) #can change dates if you want\n",
"end_date = date(2020, 5, 20)\n",
"delta = timedelta(weeks=20)\n",
"while start_date<=end_date:\n",
" dates2.append(str(start_date))\n",
" start_date += delta\n",
"#making a url, opening the dataset:\n",
"url = 'https://pricaimcit.services.brown.edu/erddap/griddap/ncdcOwDlyStrs_LonPM180.nc?taux[(2002-05-01T09:00:00Z):1:(2002-05-01T09:00:00Z)][(0.0):1:(0.0)][(40.5):1:(42.25)][(-72.75):1:(-69.75)],tauy[(2002-05-01T09:00:00Z):1:(2002-05-01T09:00:00Z)][(0.0):1:(0.0)][(40.5):1:(42.25)][(-72.75):1:(-69.75)]'\n",
"#url = 'https://pricaimcit.services.brown.edu/erddap/griddap/ncdcOwDlyStrs_LonPM180.nc?taux[(1987-07-09T09:00:00Z):1:(2011-09-30T09:00:00Z)][(0.0):1:(0.0)][(40.5):1:(42.25)][(-72.75):1:(-69.75)],tauy[(1987-07-09T09:00:00Z):1:(2011-09-30T09:00:00Z)][(0.0):1:(0.0)][(40.5):1:(42.25)][(-72.75):1:(-69.75)]'\n",
"r = requests.get(url, allow_redirects=True)\n",
"open('test.nc', 'wb').write(r.content)\n",
"nc = NetCDFFile('test.nc')\n",
"nc\n",
"#defining some dataset variables:\n",
"lat = nc.variables['latitude'][:]\n",
"lon = nc.variables['longitude'][:]\n",
"time = nc.variables['time'][:]\n",
"u = nc.variables['taux'][:]\n",
"v = nc.variables['tauy'][:]\n",
"ax = plt.axes(projection = ccrs.PlateCarree())\n",
"ax.coastlines(resolution='10m', color = 'red')\n",
"ax.set_extent([287.25, 290.25, 40.5, 42.25])\n",
"plt.pcolormesh(lon, lat, v[0, 0, :, :])\n",
"plt.colorbar()\n",
"plt.show()\n",
"ax = plt.axes(projection = ccrs.PlateCarree())\n",
"ax.coastlines(resolution='10m', color = 'red')\n",
"ax.set_extent([287.25, 290.25, 40.5, 42.25])\n",
"plt.pcolormesh(lon, lat, u[0, 0, :, :])\n",
"plt.colorbar()\n",
"plt.show()\n",
"\n",
"print(len(u))\n",
"print(u)\n",
"#ax = plt.axes(projection = ccrs.PlateCarree())\n",
"#ax.coastlines(resolution='10m', color = 'red')\n",
"#ax.set_extent([287.25, 290.25, 40.5, 42.25])\n",
"plt.quiver(lon, lat, u[0,0,:,:], v[0,0,:,:], angles='xy')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "h_4D2g70TjVa"
},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"include_colab_link": true,
"name": "Wind & Wind Stress",
"provenance": []
},
"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.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}