Skip to content
This repository has been archived by the owner on Jan 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #329 from jrleeman/xarray_updates
Browse files Browse the repository at this point in the history
Xarray updates
  • Loading branch information
dopplershift authored Aug 30, 2018
2 parents e54b032 + a96ccae commit 3893411
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 1,075 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![My Binder](http://mybinder.org/badge.svg)](http://mybinder.org/repo/Unidata/unidata-python-workshop)
[![Binder](https://mybinder.org/badge.svg)](https://mybinder.org/v2/gh/Unidata/unidata-python-workshop/master)

[![Travis Status](https://travis-ci.org/Unidata/unidata-python-workshop.svg?branch=master)](https://travis-ci.org/Unidata/unidata-python-workshop)

Expand Down
32 changes: 7 additions & 25 deletions notebooks/Satellite_Data/GOES_Interactive_Plot.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@
"from matplotlib import patheffects\n",
"import matplotlib.pyplot as plt\n",
"import metpy\n",
"from metpy.plots import add_timestamp\n",
"import numpy as np\n",
"from scipy import interpolate\n",
"from siphon.catalog import TDSCatalog\n",
"import xarray as xr\n",
"from xarray.backends import NetCDF4DataStore"
"from siphon.catalog import TDSCatalog"
]
},
{
Expand All @@ -53,9 +51,7 @@
" cat = TDSCatalog('http://thredds-test.unidata.ucar.edu/thredds/catalog/satellite/goes16/GOES16/'\n",
" '{}/Channel{:02d}/{:%Y%m%d}/catalog.xml'.format(region, channel, date))\n",
" ds = cat.datasets[idx]\n",
" ds = ds.remote_access(service='OPENDAP') \n",
" ds = NetCDF4DataStore(ds)\n",
" ds = xr.open_dataset(ds)\n",
" ds = ds.remote_access(service='OPENDAP', use_xarray=True) \n",
" return ds"
]
},
Expand Down Expand Up @@ -86,24 +82,10 @@
" im = ax.imshow(dat, extent=(x.min(), x.max(), y.min(), y.max()), origin='upper')\n",
" timestamp = datetime.strptime(ds.start_date_time, '%Y%j%H%M%S')\n",
" \n",
" # Add text (aligned to the right); save the returned object so we can manipulate it.\n",
" text_time = ax.text(0.99, 0.01, timestamp.strftime('%d %B %Y %H%MZ'),\n",
" horizontalalignment='right', transform=ax.transAxes,\n",
" color='white', fontsize='x-large', weight='bold')\n",
"\n",
" text_channel = ax.text(0.5, 0.94, 'Channel {}'.format(channel),\n",
" horizontalalignment='center', transform=ax.transAxes,\n",
" color='white', fontsize='large', weight='bold')\n",
" \n",
" text_experimental = ax.text(0.5, 0.97, 'GOES-16 Data',\n",
" horizontalalignment='center', transform=ax.transAxes,\n",
" color='white', fontsize='large', weight='bold')\n",
"\n",
" # Make the text stand out even better using matplotlib's path effects\n",
" outline_effect = [patheffects.withStroke(linewidth=2, foreground='black')]\n",
" text_time.set_path_effects(outline_effect)\n",
" text_channel.set_path_effects(outline_effect)\n",
" text_experimental.set_path_effects(outline_effect)\n",
" add_timestamp(ax, time=timestamp, high_contrast=True, \n",
" pretext='GOES 16 Ch.{} - '.format(channel),\n",
" time_format='%d %B %Y %H%MZ', y=0.01,\n",
" fontsize=18)\n",
" display(fig)\n",
" plt.close()"
]
Expand Down
36 changes: 8 additions & 28 deletions notebooks/Satellite_Data/Working with Satellite Data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"outputs": [],
"source": [
"from datetime import datetime\n",
"import metpy\n",
"from siphon.catalog import TDSCatalog\n",
"\n",
"date = datetime.utcnow()\n",
Expand Down Expand Up @@ -105,7 +106,7 @@
"outputs": [],
"source": [
"ds = cat.datasets[-2]\n",
"ds = ds.remote_access(service='OPENDAP')"
"ds = ds.remote_access(service='OPENDAP', use_xarray=True)"
]
},
{
Expand All @@ -115,19 +116,6 @@
"Now we'll use xarray to open the dataset as a netCDF data store."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import metpy\n",
"import xarray as xr\n",
"from xarray.backends import NetCDF4DataStore\n",
"ds = NetCDF4DataStore(ds)\n",
"ds = xr.open_dataset(ds)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -146,7 +134,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Your code goes here"
"# Your code goes here\n"
]
},
{
Expand All @@ -159,9 +147,7 @@
"cat = TDSCatalog('http://thredds-test.unidata.ucar.edu/thredds/catalog/casestudies/irma/goes16/Mesoscale-1/Channel09/20170910/catalog.xml')\n",
"ds = cat.datasets.filter_time_nearest(datetime(2017, 9, 10, 0))\n",
"print(ds.name)\n",
"ds = ds.remote_access(service='OPENDAP')\n",
"ds = NetCDF4DataStore(ds)\n",
"ds = xr.open_dataset(ds)\n",
"ds = ds.remote_access(service='OPENDAP', use_xarray=True)\n",
"</pre></code>\n",
"</div>"
]
Expand Down Expand Up @@ -570,7 +556,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Your code goes here"
"# Your code goes here\n"
]
},
{
Expand All @@ -583,9 +569,7 @@
"cat = TDSCatalog('http://thredds-test.unidata.ucar.edu/thredds/catalog/casestudies/irma/goes16/CONUS/Channel10/20170909/catalog.xml')\n",
"ds = cat.datasets.filter_time_nearest(datetime(2017, 9, 9, 6))\n",
"print(ds.name)\n",
"ds = ds.remote_access(service='OPENDAP')\n",
"ds = NetCDF4DataStore(ds)\n",
"ds = xr.open_dataset(ds)\n",
"ds = ds.remote_access(service='OPENDAP', use_xarray=True)\n",
"timestamp = datetime.strptime(ds.start_date_time, '%Y%j%H%M%S')\n",
"dat = ds.metpy.parse_cf('Sectorized_CMI')\n",
"\n",
Expand Down Expand Up @@ -685,9 +669,7 @@
"\n",
"# Grab the first dataset and make the figure using its projection information\n",
"ds = datasets[0]\n",
"ds = ds.remote_access(service='OPENDAP')\n",
"ds = NetCDF4DataStore(ds)\n",
"ds = xr.open_dataset(ds)\n",
"ds = ds.remote_access(service='OPENDAP', use_xarray=True)\n",
"dat = ds.metpy.parse_cf('Sectorized_CMI')\n",
"proj = dat.metpy.cartopy_crs\n",
"\n",
Expand All @@ -704,9 +686,7 @@
"for ds in datasets[::6]:\n",
"\n",
" # Open the data\n",
" ds = ds.remote_access(service='OPENDAP')\n",
" ds = NetCDF4DataStore(ds)\n",
" ds = xr.open_dataset(ds)\n",
" ds = ds.remote_access(service='OPENDAP', use_xarray=True)\n",
" dat = ds.metpy.parse_cf('Sectorized_CMI')\n",
" \n",
" # Pull out the image data, x and y coordinates, and the time. Also go ahead and\n",
Expand Down
164 changes: 19 additions & 145 deletions notebooks/Siphon/Siphon Overview.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -81,126 +81,11 @@
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime, timedelta\n",
"from siphon.catalog import TDSCatalog\n",
"top_cat = TDSCatalog('http://thredds-test.unidata.ucar.edu/thredds/catalog.xml')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"That takes care of download the catalog, parsing the XML, and doing useful things. From here we can do things like look at all the catalog references..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"print(top_cat.catalog_refs)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"So we can see what's available at the top level. We can also extract exactly what we're looking for using the name of the item:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ref = top_cat.catalog_refs['Unidata case studies']\n",
"ref.href"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Or we can just access by position:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ref = top_cat.catalog_refs[5]\n",
"ref.href"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"and then resolve that catalog reference to get a new catalog."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"new_cat = ref.follow()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Often, it can be simpler to just start the catalog at the data collection of interest. For instance, we can manually go find the catalog for some satellite data of interest from http://thredds-test.unidata.ucar.edu:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cat = TDSCatalog('http://thredds-test.unidata.ucar.edu/thredds/catalog/casestudies/irma/goes16/CONUS/Channel02/20170905/catalog.xml')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From this catalog, we can look at the first 10 datasets available:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cat.datasets[:10]"
"date = datetime.utcnow() - timedelta(days=1)\n",
"cat = TDSCatalog('http://thredds.ucar.edu/thredds/catalog/nexrad/level3/'\n",
" 'N0Q/DGX/{dt:%Y%m%d}/catalog.xml'.format(dt=date))"
]
},
{
Expand Down Expand Up @@ -233,7 +118,8 @@
"outputs": [],
"source": [
"from datetime import datetime, timedelta\n",
"ds = cat.datasets.filter_time_nearest(datetime(2017, 9, 5, 18, 30))\n",
"request_time = datetime(date.year, date.month, date.day, 18, 30)\n",
"ds = cat.datasets.filter_time_nearest(request_time)\n",
"ds"
]
},
Expand All @@ -250,8 +136,7 @@
"metadata": {},
"outputs": [],
"source": [
"time = datetime(2017, 9, 5, 18, 30)\n",
"datasets = cat.datasets.filter_time_range(time, time + timedelta(hours=1))\n",
"datasets = cat.datasets.filter_time_range(request_time, request_time + timedelta(hours=1))\n",
"print(datasets)"
]
},
Expand All @@ -262,9 +147,9 @@
"<div class=\"alert alert-success\">\n",
" <b>EXERCISE</b>:\n",
" <ul>\n",
" <li>Starting from http://thredds-test.unidata.ucar.edu/thredds/catalog/casestudies/irma/goes16/catalog.html, find the Mesoscale sector 1 imagery for Channel 2 for 6 September 2017. This is probably easiest using a browser, but you can also do this using Siphon's API.</li>\n",
" <li>If you use a browser, grab the URL and create a TDSCatalog instance.</li>\n",
" <li>Hurricane Irma reached peak intensity at 21Z on 6 September 2017. Using Siphon, find the data available in the catalog for an hour on either side of this time.</li>\n",
" <li>Starting from http://thredds.ucar.edu/thredds/catalog/satellite/SFC-T/SUPER-NATIONAL_1km/, find the composites for the previous day.</li>\n",
" <li>Grab the URL and create a TDSCatalog instance.</li>\n",
" <li>Using Siphon, find the data available in the catalog between 12Z and 18Z on the previous day.</li>\n",
" </ul>\n",
"</div>"
]
Expand All @@ -275,7 +160,7 @@
"metadata": {},
"outputs": [],
"source": [
"# cat = TDSCatalog(...)"
"# Your code goes here\n"
]
},
{
Expand All @@ -285,15 +170,10 @@
"<button data-toggle=\"collapse\" data-target=\"#sol1\" class='btn btn-primary'>View Solution</button>\n",
"<div id=\"sol1\" class=\"collapse\">\n",
"<code><pre>\n",
"cat = TDSCatalog('http://thredds-test.unidata.ucar.edu/thredds/catalog/'\n",
" 'casestudies/irma/goes16/catalog.xml')\n",
"meso = cat.catalog_refs['Mesoscale-1'].follow()\n",
"channel = meso.catalog_refs['Channel02'].follow()\n",
"date_cat = channel.catalog_refs['20170906'].follow()\n",
"\n",
"date = datetime(2017, 9, 6, 21)\n",
"datasets = date_cat.datasets.filter_time_range(date - timedelta(hours=1),\n",
" date + timedelta(hours=1))\n",
"date = datetime.utcnow() - timedelta(days=1)\n",
"cat = TDSCatalog('http://thredds.ucar.edu/thredds/catalog/satellite/SFC-T/SUPER-NATIONAL_1km/{dt:%Y%m%d}/catalog.xml'.format(dt=date))\n",
"request_time = datetime(date.year, date.month, date.day, 12, 0)\n",
"datasets = cat.datasets.filter_time_range(request_time, request_time + timedelta(hours=6))\n",
"print(datasets)\n",
"</pre></code>\n",
"</div>"
Expand Down Expand Up @@ -325,7 +205,7 @@
"source": [
"Accessing catalogs is only part of the story; Siphon is much more useful if you're trying to access/download datasets.\n",
"\n",
"For instance, using our mesoscale data that we just retrieved:"
"For instance, using our data that we just retrieved:"
]
},
{
Expand All @@ -334,13 +214,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Same as before\n",
"cat = TDSCatalog('http://thredds-test.unidata.ucar.edu/thredds/catalog/'\n",
" 'casestudies/irma/goes16/Mesoscale-1/Channel02/'\n",
" '20170906/catalog.xml')\n",
"\n",
"# Just ask for the file nearest to the time of interest\n",
"ds = cat.datasets.filter_time_nearest(datetime(2017, 9, 6, 21))"
"ds = datasets[0]"
]
},
{
Expand All @@ -360,7 +234,7 @@
"metadata": {},
"outputs": [],
"source": [
"ds.download('goes-data.nc4')"
"ds.download('data.nc4')"
]
},
{
Expand Down Expand Up @@ -462,7 +336,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
"version": "3.6.6"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 3893411

Please sign in to comment.