Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a9926cf

Browse files
eni-awowalepre-commit-ci[bot]ianhi
authored
Hierarchical trees of related groups (DataTree) for SciPy35 (#331)
* datatree imerghh_7 notebook * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * added more methods and suggestions * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * chore: pull sync fork * feature: datatree notebook * chore: updated yml * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix header order for myst * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ian Hunt-Isaak <ianhuntisaak@gmail.com>
1 parent 0d7ce69 commit a9926cf

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-0
lines changed

‎_toc.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ parts:
1818
- file: fundamentals/01_datastructures
1919
- file: fundamentals/01.1_creating_data_structures
2020
- file: fundamentals/01.1_io
21+
- file: fundamentals/01_datatree_hierarchical_data.ipynb
2122
- file: fundamentals/02_labeled_data.md
2223
sections:
2324
- file: fundamentals/02.1_indexing_Basic.ipynb
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# `xarray.DataTree` and hierarchical data\n",
8+
"\n",
9+
"In this lesson, we will learn how to use `xarray.DataTree` with hierarchical data. By the end of the lesson, we will be able to:\n",
10+
"\n",
11+
":::{admonition} Learning Goals\n",
12+
"- Understand a basic `DataTree` structure (nodes, parents and children)\n",
13+
"- Selecting `DataTree`\n",
14+
"- Understand coordinate inheritance :::"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": null,
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"import xarray as xr"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"## Opening a dataset with `open_datatree()`\n",
31+
"\n",
32+
"Let's open up a precipitation dataset. This dataset was derived from \"GPM_3IMERGHH_07\" and \"M2T1NXFLX_5.12.4\" products."
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": null,
38+
"metadata": {},
39+
"outputs": [],
40+
"source": [
41+
"precipitation = xr.tutorial.open_datatree('precipitation.nc4')"
42+
]
43+
},
44+
{
45+
"cell_type": "markdown",
46+
"metadata": {},
47+
"source": [
48+
"## Nodes\n",
49+
"Groups in a netcdf4 or hdf5 file in the DataTree model are represented as \"nodes\" in the DataTree model.\n",
50+
"We can list all of the groups with `.groups`"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": null,
56+
"metadata": {},
57+
"outputs": [],
58+
"source": [
59+
"precipitation.groups"
60+
]
61+
},
62+
{
63+
"cell_type": "markdown",
64+
"metadata": {},
65+
"source": [
66+
"## Accessing variables in a nested groups\n",
67+
"Nested variables and groups can be accessed with either dict-like syntax or method based syntax."
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": null,
73+
"metadata": {},
74+
"outputs": [],
75+
"source": [
76+
"precipitation['observed']\n",
77+
"\n",
78+
"# Returns a DataTree object, containing the variables, dimensions, and coordinates in the \"observed\" node"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": null,
84+
"metadata": {},
85+
"outputs": [],
86+
"source": [
87+
"precipitation['/observed/precipitation']"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": null,
93+
"metadata": {},
94+
"outputs": [],
95+
"source": [
96+
"precipitation.reanalysis.precipitation\n",
97+
"\n",
98+
"# Method based syntax"
99+
]
100+
},
101+
{
102+
"cell_type": "markdown",
103+
"metadata": {},
104+
"source": [
105+
"## Get the parent and child nodes from a group"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": null,
111+
"metadata": {},
112+
"outputs": [],
113+
"source": [
114+
"precipitation['reanalysis'].parent"
115+
]
116+
},
117+
{
118+
"cell_type": "code",
119+
"execution_count": null,
120+
"metadata": {},
121+
"outputs": [],
122+
"source": [
123+
"precipitation.children"
124+
]
125+
},
126+
{
127+
"cell_type": "markdown",
128+
"metadata": {},
129+
"source": [
130+
"## Inheritance\n",
131+
"DataTree implements a simple inheritance mechanism. Coordinates, dimensions and their associated indices are propagated downward from the root node to all descendent nodes.\n",
132+
"\n",
133+
"Let's take a look at some inherited coordinates with our precipitation dataset"
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": null,
139+
"metadata": {},
140+
"outputs": [],
141+
"source": [
142+
"precipitation.time"
143+
]
144+
},
145+
{
146+
"cell_type": "markdown",
147+
"metadata": {},
148+
"source": [
149+
"The `\"time\"` dimension is defined at the root node of our dataset and is propagated downward to the \"observed\" and \"reanalysis\" group"
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": null,
155+
"metadata": {},
156+
"outputs": [],
157+
"source": [
158+
"precipitation.observed"
159+
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": null,
164+
"metadata": {},
165+
"outputs": [],
166+
"source": [
167+
"precipitation.reanalysis"
168+
]
169+
}
170+
],
171+
"metadata": {
172+
"language_info": {
173+
"codemirror_mode": {
174+
"name": "ipython",
175+
"version": 3
176+
},
177+
"file_extension": ".py",
178+
"mimetype": "text/x-python",
179+
"name": "python",
180+
"nbconvert_exporter": "python",
181+
"pygments_lexer": "ipython3"
182+
}
183+
},
184+
"nbformat": 4,
185+
"nbformat_minor": 4
186+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /