Skip to content

Zarr tutorial #323

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

Merged
merged 25 commits into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
df745a5
adding the links to all indexing materials
negin513 Jul 11, 2023
155c810
typo fix + remove a redundant example.
negin513 Jul 11, 2023
bb65245
Merge branch 'xarray-contrib:main' into main
negin513 Jun 6, 2024
fb13212
Merge branch 'xarray-contrib:main' into main
negin513 Jul 8, 2024
89aa027
Merge branch 'xarray-contrib:main' into main
negin513 Jul 4, 2025
407542e
initial commits and contents
negin513 Jul 4, 2025
2c1b2bf
data storage
negin513 Jul 4, 2025
605ab43
updates and cloud storage
negin513 Jul 4, 2025
95644a0
updates and cloud storage
negin513 Jul 4, 2025
be3c311
additional resources
negin513 Jul 4, 2025
95868b8
updates
negin513 Jul 4, 2025
9a4e97c
more organized
negin513 Jul 4, 2025
a12435e
sharding
negin513 Jul 4, 2025
ab1d366
sharding added
negin513 Jul 4, 2025
59fe17b
zarr added
negin513 Jul 4, 2025
5856441
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 4, 2025
1674e45
minor updates to zarr tutorial
jhamman Jul 6, 2025
b8edee6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 6, 2025
2863ac8
Merge branch 'main' into zarr-tutorial
negin513 Jul 7, 2025
416e46e
Merge branch 'main' into zarr-tutorial
scottyhq Jul 7, 2025
3bf111d
ensure zarr notebook is rendered, add cell metadata for large outputs
scottyhq Jul 7, 2025
e33159b
remove accidentaly advaced-indexing edit
scottyhq Jul 7, 2025
c411993
use data subfolder for io notebook as in zarr notebook
scottyhq Jul 7, 2025
78acafa
fix cat and tree paths, toggle output
scottyhq Jul 7, 2025
fe50cbf
link to cmip6 notebook at end
scottyhq Jul 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ parts:
- file: intermediate/indexing/boolean-masking-indexing.ipynb
- file: intermediate/hierarchical_computation.ipynb
- file: intermediate/xarray_and_dask
- file: intermediate/intro-to-zarr.ipynb
- file: intermediate/xarray_ecosystem
- file: intermediate/hvplot
- file: intermediate/remote_data/index
Expand Down
75 changes: 44 additions & 31 deletions fundamentals/01.1_io.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,27 @@
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": null,
"id": "2",
"metadata": {},
"outputs": [],
"source": [
"# Ensure we start with a clean directory for the tutorial\n",
"import pathlib\n",
"import shutil\n",
"\n",
"datadir = pathlib.Path('../data/io-tutorial')\n",
"if datadir.exists():\n",
" shutil.rmtree(datadir)\n",
"else:\n",
" datadir.mkdir()"
]
},
{
"cell_type": "markdown",
"id": "3",
"metadata": {},
"source": [
"The constructor of `Dataset` takes three parameters:\n",
"\n",
Expand All @@ -66,7 +84,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "3",
"id": "4",
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -94,16 +112,16 @@
")\n",
"\n",
"# write datasets\n",
"ds1.to_netcdf(\"ds1.nc\")\n",
"ds2.to_netcdf(\"ds2.nc\")\n",
"ds1.to_netcdf(datadir / \"ds1.nc\")\n",
"ds2.to_netcdf(datadir / \"ds2.nc\")\n",
"\n",
"# write dataarray\n",
"ds1.a.to_netcdf(\"da1.nc\")"
"ds1.a.to_netcdf(datadir / \"da1.nc\")"
]
},
{
"cell_type": "markdown",
"id": "4",
"id": "5",
"metadata": {},
"source": [
"Reading those files is just as simple:\n"
Expand All @@ -112,26 +130,26 @@
{
"cell_type": "code",
"execution_count": null,
"id": "5",
"id": "6",
"metadata": {},
"outputs": [],
"source": [
"xr.open_dataset(\"ds1.nc\")"
"xr.open_dataset(datadir / \"ds1.nc\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6",
"id": "7",
"metadata": {},
"outputs": [],
"source": [
"xr.open_dataarray(\"da1.nc\")"
"xr.open_dataarray(datadir / \"da1.nc\")"
]
},
{
"cell_type": "markdown",
"id": "7",
"id": "8",
"metadata": {},
"source": [
"<img src=\"https://zarr.readthedocs.io/en/stable/_static/logo1.png\" align=\"right\" width=\"20%\">\n",
Expand All @@ -151,16 +169,16 @@
{
"cell_type": "code",
"execution_count": null,
"id": "8",
"id": "9",
"metadata": {},
"outputs": [],
"source": [
"ds1.to_zarr(\"ds1.zarr\", mode=\"w\")"
"ds1.to_zarr(datadir / \"ds1.zarr\", mode=\"w\")"
]
},
{
"cell_type": "markdown",
"id": "9",
"id": "10",
"metadata": {},
"source": [
"We can then read the created file with:\n"
Expand All @@ -169,16 +187,16 @@
{
"cell_type": "code",
"execution_count": null,
"id": "10",
"id": "11",
"metadata": {},
"outputs": [],
"source": [
"xr.open_zarr(\"ds1.zarr\", chunks=None)"
"xr.open_zarr(datadir / \"ds1.zarr\", chunks=None)"
]
},
{
"cell_type": "markdown",
"id": "11",
"id": "12",
"metadata": {},
"source": [
"setting the `chunks` parameter to `None` avoids `dask` (more on that in a later\n",
Expand All @@ -187,7 +205,7 @@
},
{
"cell_type": "markdown",
"id": "12",
"id": "13",
"metadata": {},
"source": [
"**tip:** You can write to any dictionary-like (`MutableMapping`) interface:"
Expand All @@ -196,7 +214,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "13",
"id": "14",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -207,7 +225,7 @@
},
{
"cell_type": "markdown",
"id": "14",
"id": "15",
"metadata": {},
"source": [
"## Raster files using rioxarray\n",
Expand All @@ -220,7 +238,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "15",
"id": "16",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -241,16 +259,16 @@
{
"cell_type": "code",
"execution_count": null,
"id": "16",
"id": "17",
"metadata": {},
"outputs": [],
"source": [
"da.rio.to_raster('ds1_a.tiff')"
"da.rio.to_raster(datadir / 'ds1_a.tiff')"
]
},
{
"cell_type": "markdown",
"id": "17",
"id": "18",
"metadata": {},
"source": [
"NOTE: you can now load this file into GIS tools like [QGIS](https://www.qgis.org)! Or open back into Xarray:"
Expand All @@ -259,11 +277,11 @@
{
"cell_type": "code",
"execution_count": null,
"id": "18",
"id": "19",
"metadata": {},
"outputs": [],
"source": [
"DA = xr.open_dataarray('ds1_a.tiff', engine='rasterio')\n",
"DA = xr.open_dataarray(datadir / 'ds1_a.tiff', engine='rasterio')\n",
"DA.rio.crs"
]
}
Expand All @@ -279,11 +297,6 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
},
"vscode": {
"interpreter": {
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
}
}
},
"nbformat": 4,
Expand Down
Loading
Loading