-
Notifications
You must be signed in to change notification settings - Fork 300
Chunk Control documentation #5597
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
trexfeathers
merged 17 commits into
SciTools:FEATURE_chunk_control
from
ESadek-MO:cc_docs
Nov 23, 2023
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5fd0bee
init PR, skeleton TP
ESadek-MO d1ebec3
whoops, missed the TP.
ESadek-MO d38dc31
fixed doctests in rst file
ESadek-MO ffad33c
correct triple chevron to elipses
ESadek-MO f3a0d39
updated set doctest to better show functionality
ESadek-MO 02feb2a
removed in-progress doctest code
ESadek-MO 430c12e
Review comments, part 1
ESadek-MO c29312c
Review comments, part 2
ESadek-MO cd2e9fe
changed numpy docs dict
ESadek-MO e4e3458
wait, this way is better
ESadek-MO e85dd15
fixed linkcheck failures (maybe)
ESadek-MO b30dc1e
fixed :meth:
ESadek-MO ad06f4a
fixed a couple doc bits
ESadek-MO 8fdabb9
hopefully fixed doctests
ESadek-MO e820c50
newest review comments
ESadek-MO 04014df
fixed rendering, and wording in docstring
ESadek-MO a05757d
fixed docstring numpyness
ESadek-MO 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 |
|---|---|---|
|
|
@@ -11,3 +11,4 @@ Extra information on specific technical issues. | |
|
|
||
| um_files_loading.rst | ||
| missing_data_handling.rst | ||
| netcdf_io.rst | ||
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,141 @@ | ||
| .. _netcdf_io: | ||
|
|
||
| .. testsetup:: chunk_control | ||
|
|
||
| import iris | ||
| from iris.fileformats.netcdf.loader import CHUNK_CONTROL | ||
|
|
||
| from pathlib import Path | ||
| import dask | ||
| import shutil | ||
| import tempfile | ||
|
|
||
| tmp_dir = Path(tempfile.mkdtemp()) | ||
| tmp_filepath = tmp_dir / "tmp.nc" | ||
|
|
||
| cube = iris.load(iris.sample_data_path("E1_north_america.nc"))[0] | ||
| iris.save(cube, tmp_filepath, chunksizes=(120, 37, 49)) | ||
| old_dask = dask.config.get("array.chunk-size") | ||
| dask.config.set({'array.chunk-size': '500KiB'}) | ||
|
|
||
|
|
||
| .. testcleanup:: chunk_control | ||
|
|
||
| dask.config.set({'array.chunk-size': old_dask}) | ||
| shutil.rmtree(tmp_dir) | ||
|
|
||
|
|
||
| ============================= | ||
| NetCDF I/O Handling in Iris | ||
| ============================= | ||
|
|
||
| This document provides a basic account of how Iris loads and saves NetCDF files. | ||
|
|
||
| .. admonition:: Under Construction | ||
|
|
||
| This document is still a work in progress, so might include blank or unfinished sections, | ||
| watch this space! | ||
|
|
||
|
|
||
| Chunk Control | ||
| -------------- | ||
|
|
||
| Default Chunking | ||
| ^^^^^^^^^^^^^^^^ | ||
|
|
||
| Chunks are, by default, optimised by Iris on load. This will automatically | ||
| decide the best chunksize for your data without any user input. This is | ||
| calculated based on a number of factors, including: | ||
|
|
||
| - File Variable Chunking | ||
| - Full Variable Shape | ||
| - Dask Default Chunksize | ||
| - Dimension Order: Earlier (outer) dimensions will be prioritised to be split over later (inner) dimensions. | ||
|
|
||
| .. doctest:: chunk_control | ||
|
|
||
| >>> cube = iris.load_cube(tmp_filepath) | ||
| >>> | ||
| >>> print(cube.shape) | ||
| (240, 37, 49) | ||
| >>> print(cube.core_data().chunksize) | ||
| (60, 37, 49) | ||
|
|
||
| For more user control, functionality was updated in :pull:`5588`, with the | ||
| creation of the :data:`iris.fileformats.netcdf.loader.CHUNK_CONTROL` class. | ||
|
|
||
| Custom Chunking: Set | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| There are three context manangers within :data:`iris.fileformats.netcdf.loader.CHUNK_CONTROL`. The most basic is | ||
trexfeathers marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| :meth:`iris.fileformats.netcdf.loader.CHUNK_CONTROL.set`. This allows you to specify the chunksize for each dimension, | ||
trexfeathers marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| and to specify a `var_name` specifically to change. | ||
|
|
||
| Using ``-1`` in place of a chunksize will ensure the chunksize stays the same | ||
| as the shape, i.e. no optimisation occurs on that dimension. | ||
|
|
||
| .. doctest:: chunk_control | ||
|
|
||
| >>> with CHUNK_CONTROL.set("air_temperature", time=180, latitude=-1, longitude=25): | ||
| ... cube = iris.load_cube(tmp_filepath) | ||
| >>> | ||
| >>> print(cube.core_data().chunksize) | ||
| (180, 37, 25) | ||
|
|
||
| Note that ``var_name`` is optional, and that you don't need to specify every dimension. If you | ||
| specify only one dimension, the rest will be optimised using Iris' default behaviour. | ||
|
|
||
| .. doctest:: chunk_control | ||
|
|
||
| >>> with CHUNK_CONTROL.set(longitude=25): | ||
| ... cube = iris.load_cube(tmp_filepath) | ||
| >>> | ||
| >>> print(cube.core_data().chunksize) | ||
| (120, 37, 25) | ||
|
|
||
| Custom Chunking: From File | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| The second context manager is :meth:`iris.fileformats.netcdf.loader.CHUNK_CONTROL.from_file`. | ||
| This takes chunksizes as defined in the NetCDF file. Any dimensions without specified chunks | ||
| will default to Iris optimisation. | ||
|
|
||
| .. doctest:: chunk_control | ||
|
|
||
| >>> with CHUNK_CONTROL.from_file(): | ||
| ... cube = iris.load_cube(tmp_filepath) | ||
| >>> | ||
| >>> print(cube.core_data().chunksize) | ||
| (120, 37, 49) | ||
|
|
||
| Custom Chunking: As Dask | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| The final context manager, :meth:`iris.fileformats.netcdf.loader.CHUNK_CONTROL.as_dask`, bypasses | ||
| Iris' optimisation all together, and will take its chunksizes from Dask's behaviour. | ||
|
|
||
| .. doctest:: chunk_control | ||
|
|
||
| >>> with CHUNK_CONTROL.as_dask(): | ||
| ... cube = iris.load_cube(tmp_filepath) | ||
| >>> | ||
| >>> print(cube.core_data().chunksize) | ||
| (70, 37, 49) | ||
|
|
||
|
|
||
| Split Attributes | ||
| ----------------- | ||
|
|
||
| TBC | ||
|
|
||
|
|
||
| Deferred Saving | ||
| ---------------- | ||
|
|
||
| TBC | ||
|
|
||
|
|
||
| Guess Axis | ||
| ----------- | ||
|
|
||
| TBC | ||
trexfeathers marked this conversation as resolved.
Show resolved
Hide resolved
|
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
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.
Uh oh!
There was an error while loading. Please reload this page.