|
| 1 | +.. _data structures: |
| 2 | + |
| 3 | +Reading and Writing Files |
| 4 | +========================= |
| 5 | + |
| 6 | +.. note:: |
| 7 | + |
| 8 | + This page builds on the information given in xarray's main page on |
| 9 | + `reading and writing files <https://docs.xarray.dev/en/stable/user-guide/io.html>`_, |
| 10 | + so it is suggested that you are familiar with those first. |
| 11 | + |
| 12 | + |
| 13 | +netCDF |
| 14 | +------ |
| 15 | + |
| 16 | +Groups |
| 17 | +~~~~~~ |
| 18 | + |
| 19 | +Whilst netCDF groups can only be loaded individually as Dataset objects, a whole file of many nested groups can be loaded |
| 20 | +as a single ``:py:class::DataTree`` object. |
| 21 | +To open a whole netCDF file as a tree of groups use the ``:py:func::open_datatree()`` function. |
| 22 | +To save a DataTree object as a netCDF file containing many groups, use the ``:py:meth::DataTree.to_netcdf()`` method. |
| 23 | + |
| 24 | + |
| 25 | +.. _netcdf.group.warning: |
| 26 | + |
| 27 | +.. warning:: |
| 28 | + ``DataTree`` objects do not follow the exact same data model as netCDF files, which means that perfect round-tripping |
| 29 | + is not always possible. |
| 30 | + |
| 31 | + In particular in the netCDF data model dimensions are entities that can exist regardless of whether any variable possesses them. |
| 32 | + This is in contrast to `xarray's data model <https://docs.xarray.dev/en/stable/user-guide/data-structures.html>`_ |
| 33 | + (and hence :ref:`datatree's data model<data structures>`) in which the dimensions of a (Dataset/Tree) |
| 34 | + object are simply the set of dimensions present across all variables in that dataset. |
| 35 | + |
| 36 | + This means that if a netCDF file contains dimensions but no variables which possess those dimensions, |
| 37 | + these dimensions will not be present when that file is opened as a DataTree object. |
| 38 | + Saving this DataTree object to file will therefore not preserve these "unused" dimensions. |
| 39 | + |
| 40 | +Zarr |
| 41 | +---- |
| 42 | + |
| 43 | +Groups |
| 44 | +~~~~~~ |
| 45 | + |
| 46 | +Nested groups in zarr stores can be represented by loading the store as a ``:py:class::DataTree`` object, similarly to netCDF. |
| 47 | +To open a whole zarr store as a tree of groups use the ``:py:func::open_datatree()`` function. |
| 48 | +To save a DataTree object as a zarr store containing many groups, use the ``:py:meth::DataTree.to_zarr()`` method. |
| 49 | + |
| 50 | +.. note:: |
| 51 | + Note that perfect round-tripping should always be possible with a zarr store (:ref:`unlike for netCDF files<netcdf.group.warning>`), |
| 52 | + as zarr does not support "unused" dimensions. |
0 commit comments