Skip to content

Commit

Permalink
Merge pull request #190 from ncasuk/alex-review-xarray
Browse files Browse the repository at this point in the history
Alex review xarray
  • Loading branch information
nf679 authored Nov 8, 2024
2 parents 38d16bf + df5a3b4 commit b68064f
Show file tree
Hide file tree
Showing 36 changed files with 8,118 additions and 10,958 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*.pyo
*__pycache__*
*.ipynb_checkpoints*
environ
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ This repository holds teaching materials for the NCAS Introduction to Scientific

| Lesson | Exercise | Solution |
| ------ | -------- | -------- |
| __xarray:__ Introduction to [multidimensional arrays](https://geohackweek.github.io/nDarrays/01-introduction/), [xarray](https://geohackweek.github.io/nDarrays/02-xarray-architecture/) and [indexing](https://geohackweek.github.io/nDarrays/03-label-based-indexing/) | [Exercise 01](/python-data/exercises/ex01_xarray_intro.ipynb) | [Solution 01](/python-data/solutions/ex01_xarray_intro.ipynb) |
| __xarray:__ [Plotting](https://geohackweek.github.io/nDarrays/04-plotting/) and [Aggregation](https://geohackweek.github.io/nDarrays/05-aggregation/) | [Exercise 02](/python-data/exercises/ex02_plotting_and_aggregation.ipynb) | [Solution 02](/python-data/solutions/ex02_plotting_and_aggregation.ipynb) |
| __xarray:__ [GroupBy processing](https://geohackweek.github.io/nDarrays/07-groupby-processing/) and [masking](https://geohackweek.github.io/nDarrays/09-masking/) | [Exercise 03](/python-data/exercises/ex03_groupby_processing_and_masking.ipynb) | [Solution 03](/python-data/solutions/ex03_groupby_processing_and_masking.ipynb) |
| __xarray:__ Introduction to [multidimensional arrays](https://tutorial.xarray.dev/fundamentals/01_data_structures.html), [xarray data structures](https://tutorial.xarray.dev/fundamentals/01_datastructures.html) and [indexing](https://tutorial.xarray.dev/fundamentals/02.1_indexing_Basic.html) | [Exercise 01](/python-data/exercises/ex01_xr_intro.ipynb) [Exercise 01.5](/python-data/exercises/ex01.5_xr_label_based_indexing.ipynb)| [Solution 01](/python-data/solutions/ex01_xarray_intro.ipynb) [Solution 01.5](/python-data/solutions/ex01.5_xr_label_based_indexing.ipynb)|
| __xarray:__ [Plotting](https://tutorial.xarray.dev/fundamentals/04.1_basic_plotting.html) and [Aggregation](https://tutorial.xarray.dev/fundamentals/03.1_computation_with_xarray.html) | [Exercise 02](/python-data/exercises/ex02_xr_plotting.ipynb) [Exercise 02.5](/python-data/exercises/ex02.5_xr_aggregation.ipynb)| [Solution 02](/python-data/solutions/ex02_plotting.ipynb) [Solution 02.5](/python-data/solutions/ex02.5_xr_aggregation.ipynb)|
| __xarray:__ [GroupBy processing](https://tutorial.xarray.dev/fundamentals/03.2_groupby_with_xarray.html) and [masking](https://tutorial.xarray.dev/intermediate/indexing/boolean-masking-indexing.html) | [Exercise 03](/python-data/exercises/ex03_xr_groupby.ipynb) [Exercise 03.5](/python-data/exercises/ex03.5_xr_masking.ipynb)| [Solution 03](/python-data/solutions/ex03_groupby.ipynb) [Solution 03.5](/python-data/solutions/ex03.5_masking.ipynb)|
| [cf-python]() | [Exercise 04](/python-data/exercises/ex04_cf_python.ipynb) | [Solution 04](/python-data/solutions/ex04_cf_python.ipynb) |
| [matplotlib](https://matplotlib.org/stable/users/explain/quick_start.html) | [Exercise 05](/python-data/exercises/ex05_matplotlib.ipynb) | [Solution 05](/python-data/solutions/ex05_matplotlib.ipynb) |
| [numpy](https://numpy.org/doc/stable/user/quickstart.html) | [Exercise 06](/python-data/exercises/ex06_numpy.ipynb) | [Solution 06](/python-data/solutions/ex06_numpy.ipynb) |
Expand Down
6 changes: 3 additions & 3 deletions python-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ In this folder you will find all the material used to run the course.

Presentation material is used from the links listed below:

1. xarray: [Introduction to multidimensional arrays](https://geohackweek.github.io/nDarrays/01-introduction/), [intro to xarray](https://geohackweek.github.io/nDarrays/02-xarray-architecture/) and [label-based indexing](https://geohackweek.github.io/nDarrays/03-label-based-indexing/).
2. xarray: [Plotting](https://geohackweek.github.io/nDarrays/04-plotting/) and [aggregation](https://geohackweek.github.io/nDarrays/05-aggregation/)
3. xarray: [Groupby processing](https://geohackweek.github.io/nDarrays/07-groupby-processing/) and [masking](https://geohackweek.github.io/nDarrays/09-masking/)
1. xarray: [Introduction to multidimensional arrays](https://tutorial.xarray.dev/fundamentals/01_data_structures.html), and [intro to xarray data structures](https://tutorial.xarray.dev/fundamentals/01_datastructures.html) and[label-based indexing](https://tutorial.xarray.dev/fundamentals/02.1_indexing_Basic.html).
2. xarray: [Plotting](https://tutorial.xarray.dev/fundamentals/04.1_basic_plotting.html) and [aggregation](https://tutorial.xarray.dev/fundamentals/03.1_computation_with_xarray.html)
3. xarray: [Groupby processing](https://tutorial.xarray.dev/fundamentals/03.2_groupby_with_xarray.html) and [masking](https://tutorial.xarray.dev/intermediate/indexing/boolean-masking-indexing.html)
4. [cf-python]()
5. [matplotlib](https://matplotlib.org/stable/users/explain/quick_start.html)
6. [numpy](https://numpy.org/doc/stable/user/quickstart.html)
Expand Down
Binary file added python-data/data/xbhubo.pgc0apr.nc
Binary file not shown.
239 changes: 239 additions & 0 deletions python-data/exercises/ex01.5_xr_label_based_indexing.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "77fa81b4-1601-4743-b297-ae21ac49fd49",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"# Exercise 1.5: Label-based indexing"
]
},
{
"cell_type": "markdown",
"id": "c1f847cc-4003-42a1-b484-7f7fc283b4c5",
"metadata": {},
"source": [
"## Aim: Learn how to index data arrays"
]
},
{
"cell_type": "markdown",
"id": "6374f110-5ea1-4959-b226-4eeb18cf4899",
"metadata": {},
"source": [
"Find the teaching resources here: https://tutorial.xarray.dev/fundamentals/02.1_indexing_Basic.html."
]
},
{
"cell_type": "markdown",
"id": "e0cc034f-61e8-430f-b5ea-511594ff8d42",
"metadata": {},
"source": [
"### Issues Covered: \n",
"- Indexing, using `.loc()`, `.isel()` and `.sel()`"
]
},
{
"cell_type": "markdown",
"id": "a33d279d-ad68-4790-8c97-f32cf1faa019",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"Q1. Open the `'../data/tas_rcp45_2055_mon_avg_change.nc'` dataset and load it into an xarray `Dataset` called `ds`.\n",
"(Hint: Don't forget to import any packages you need).\n",
"This file is a model run for HadCM3 run as part of the RAPID study: https://catalogue.ceda.ac.uk/uuid/6bbab8394124b252f8b1b036f9eb6b6b/"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "778346e3-ed07-408f-83e7-3b12632761e5",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "0a21a6b9-ebae-47fa-940b-3b7f92d3ad2b",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"Q2. Select a subset of the `temperature` array using label-based indexing to get data at the position [0,0,0]."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "d0818a4f-7255-4b90-9cb5-c8e1810cd7e3",
"metadata": {
"editable": true,
"execution": {
"iopub.execute_input": "2024-11-08T14:55:33.495959Z",
"iopub.status.busy": "2024-11-08T14:55:33.495440Z",
"iopub.status.idle": "2024-11-08T14:55:33.510961Z",
"shell.execute_reply": "2024-11-08T14:55:33.510269Z"
},
"slideshow": {
"slide_type": ""
},
"tags": [
"clear_answer_cell"
]
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "b706418f-ed69-4d32-8a2c-b9fd6accf9b6",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"Q3. Use `.loc` to find the temperature 5 meters below the sea surface in south atlantic where latitiude is -50.625 and longitude is 0."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "84ef9cec-9839-41fe-98e7-caaeb6e6e147",
"metadata": {
"editable": true,
"execution": {
"iopub.execute_input": "2024-11-08T14:55:33.513642Z",
"iopub.status.busy": "2024-11-08T14:55:33.513261Z",
"iopub.status.idle": "2024-11-08T14:55:33.523273Z",
"shell.execute_reply": "2024-11-08T14:55:33.522720Z"
},
"slideshow": {
"slide_type": ""
},
"tags": [
"clear_answer_cell"
]
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "09285680-d82a-44c7-b40a-092a47b568e9",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"Q4. It's not ideal to have to keep track of which dimension is in which position. Instead, use `.isel` to use the dimension names to get the data in the same place: this is depth position 0, latitude position 31 and longitude position 0."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "41ce3da8-ab39-4e62-b1f6-e27b13527f42",
"metadata": {
"editable": true,
"execution": {
"iopub.execute_input": "2024-11-08T14:55:33.525735Z",
"iopub.status.busy": "2024-11-08T14:55:33.525438Z",
"iopub.status.idle": "2024-11-08T14:55:33.536074Z",
"shell.execute_reply": "2024-11-08T14:55:33.535518Z"
},
"scrolled": true,
"slideshow": {
"slide_type": ""
},
"tags": [
"clear_answer_cell"
]
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "422eddac-26f7-4c9b-b6ad-4e7386117b25",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"Q5. The previous method is still referring to a selection by integer position. Use `.sel` to give a labelled index with the named dimension to find the data at `time=2065-12-30`, `lat=-78.5`, `lon=11.0`."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "09dac653-01d8-4e60-9444-42ef7743cf99",
"metadata": {
"editable": true,
"execution": {
"iopub.execute_input": "2024-11-08T14:55:33.538580Z",
"iopub.status.busy": "2024-11-08T14:55:33.538287Z",
"iopub.status.idle": "2024-11-08T14:55:33.550492Z",
"shell.execute_reply": "2024-11-08T14:55:33.549854Z"
},
"slideshow": {
"slide_type": ""
},
"tags": [
"clear_answer_cell"
]
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 + Jaspy",
"language": "python",
"name": "jaspy"
},
"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.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit b68064f

Please sign in to comment.