-
Notifications
You must be signed in to change notification settings - Fork 113
DataTree tutorial with GPM_3IMERGHH_07 #307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
eni-awowale
wants to merge
6
commits into
xarray-contrib:main
Choose a base branch
from
eni-awowale:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9b765a6
datatree imerghh_7 notebook
eni-awowale dde09c5
Merge branch 'xarray-contrib:main' into main
eni-awowale 16cb113
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] fe39686
added more methods and suggestions
eni-awowale cd7b423
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c543604
Merge branch 'main' into main
eni-awowale File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,305 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"\n", | ||
"# How to use `xarray.DataTree` with hierarchical data\n", | ||
"\n", | ||
"\n", | ||
"## Overview: \n", | ||
"\n", | ||
"This notebook will demonstrate how to use `xarray.DataTree` with [_GPM IMERG Final Precipitation L3 Half Hourly 0.1 degree x 0.1 degree V07 (GPM_3IMERGHH_07)_](https://disc.gsfc.nasa.gov/datasets/GPM_3IMERGHH_07/summary) and use xarray's plotting capabilities to plot precipitation in the Gulf of Mexico during Hurricane Ida. GPM_3IMERGHH_07 is a L3 gridded product with a group hierarchical structure." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import cartopy.crs as ccrs\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"from xarray import open_datatree\n", | ||
"from metpy.plots import ctables" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Opening the dataset with `open_datatree()`" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gpm_imerghh_7 = open_datatree(\n", | ||
" '~/Downloads/3B-HHR.MS.MRG.3IMERG.20210829-S073000-E075959.0450.V07B.HDF5', engine='h5netcdf'\n", | ||
")\n", | ||
"gpm_imerghh_7" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### List all of the groups with `.groups`" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gpm_imerghh_7.groups" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Accessing variables in a nested groups\n", | ||
"Nested variables and groups can be accessed with either dict-like syntax or method based syntax." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gpm_imerghh_7['/Grid']\n", | ||
"\n", | ||
"# Returns only the data contained in the \"/Grid\" group" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gpm_imerghh_7['/Grid/precipitation']" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gpm_imerghh_7.Grid.precipitation" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Get the parent and child nodes from a group" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gpm_imerghh_7['/Grid/Intermediate'].parent" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gpm_imerghh_7.Grid.children" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### `Xarray.DataTree` objects and `xarray.Dataset` objects have the same key properties like:\n", | ||
"\n", | ||
"- `dims`: a dictionary mapping of dimension names to lengths, for the variables in a node, and a node’s ancestors.\n", | ||
"\n", | ||
"- `data_vars`: a dict-like container of DataArrays corresponding to variables in a node.\n", | ||
"\n", | ||
"- `coords`: another dict-like container of DataArrays, corresponding to coordinate variables in a node, and a node’s ancestors.\n", | ||
"\n", | ||
"- `attrs`: dict with metadata relevant to data in a node.\n", | ||
"\n", | ||
"With `DataTree` you can get these properties at any of the nodes (groups) they are defined in." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gpm_imerghh_7.dims\n", | ||
"# Note there are no dimensions, coordinates, or data variables defined at the root node" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gpm_imerghh_7.attrs" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gpm_imerghh_7['/Grid'].dims" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gpm_imerghh_7['/Grid/Intermediate'].dims" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gpm_imerghh_7['/Grid/Intermediate'].data_vars" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Plotting precipitation data with DataTree\n", | ||
"Xarray’s plotting capabilities are centered around DataArray objects. To plot DataTree objects we access their relevant DataArrays in this case, `gpm_imerghh_7['/Grid/precipitation']`. \n", | ||
"\n", | ||
"We use the `.where()` method to get a subset of precipitation data over the Gulf of Mexico." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"precipitation_subset = gpm_imerghh_7['/Grid/precipitation'].where(\n", | ||
" (gpm_imerghh_7['/Grid/precipitation'].lat >= 20)\n", | ||
" & (gpm_imerghh_7['/Grid/precipitation'].lat <= 35)\n", | ||
" & (gpm_imerghh_7['/Grid/precipitation'].lon >= -110)\n", | ||
" & (gpm_imerghh_7['/Grid/precipitation'].lon <= -78),\n", | ||
" drop=True,\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Data masking\n", | ||
"We add a data mask to the precipitation values that are zero." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"precipitation_subset_mask = precipitation_subset.where(precipitation_subset > 0.0)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Add a custom precipitation color map from [metpy](https://unidata.github.io/MetPy/latest/api/generated/metpy.plots.ctables.html)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"clevs = [0, 1, 2.5, 5, 7.5, 10, 15, 20, 30, 40, 50, 70, 100, 150, 200, 250, 300, 400, 500, 600, 750]\n", | ||
"norm, cmap = ctables.registry.get_with_boundaries('precipitation', clevs)\n", | ||
"cmap" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Plot the data with `.plot()`\n", | ||
"Note since this data is two-dimensional it calls `xarray.plot.pcolormesh()` by default with just the `.plot()` method." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Set the figure size, projection, extent and grid lines to the plot\n", | ||
"fig = plt.figure(figsize=(8, 8))\n", | ||
"ax = plt.axes(projection=ccrs.PlateCarree())\n", | ||
"ax.set_extent([-100, -80, 20, 35])\n", | ||
"ax.coastlines()\n", | ||
"gl = ax.gridlines(draw_labels=True, linewidth=1, color='black', linestyle='--')\n", | ||
"gl.right_labels = False\n", | ||
"gl.top_labels = False\n", | ||
"\n", | ||
"# Get the minimum and maximum values in the array\n", | ||
"min = precipitation_subset_mask.min()\n", | ||
"max = precipitation_subset_mask.max()\n", | ||
"\n", | ||
"# Plot the precipitation data\n", | ||
"precipitation_subset_mask[0].plot(\n", | ||
" x=\"lon\",\n", | ||
" y=\"lat\",\n", | ||
" ax=ax,\n", | ||
" cmap=cmap,\n", | ||
" cbar_kwargs={\"orientation\": \"horizontal\", \"pad\": 0.05},\n", | ||
" vmin=min,\n", | ||
" vmax=max,\n", | ||
")\n", | ||
"\n", | ||
"plt.title('Half-hourly precipitation rate in the Gulf of Mexico on August 29, 2021 at 07:30')" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is going to change once this is merged pydata/xarray-data#31