File tree Expand file tree Collapse file tree 2 files changed +68
-0
lines changed Expand file tree Collapse file tree 2 files changed +68
-0
lines changed Original file line number Diff line number Diff line change @@ -162,11 +162,77 @@ To do so, pass a ``group`` keyword argument to the
162
162
:py:func: `open_dataset ` function. The group can be specified as a path-like
163
163
string, e.g., to access subgroup ``'bar' `` within group ``'foo' `` pass
164
164
``'/foo/bar' `` as the ``group `` argument.
165
+
165
166
In a similar way, the ``group `` keyword argument can be given to the
166
167
:py:meth: `Dataset.to_netcdf ` method to write to a group
167
168
in a netCDF file.
168
169
When writing multiple groups in one file, pass ``mode='a' `` to
169
170
:py:meth: `Dataset.to_netcdf ` to ensure that each call does not delete the file.
171
+ For example:
172
+
173
+ .. ipython ::
174
+ :verbatim:
175
+
176
+ In [1]: ds1 = xr.Dataset({"a": 0})
177
+
178
+ In [2]: ds2 = xr.Dataset({"b": 1})
179
+
180
+ In [3]: ds1.to_netcdf("file.nc", group="A")
181
+
182
+ In [4]: ds2.to_netcdf("file.nc", group="B", mode="a")
183
+
184
+ We can verify that two groups have been saved using the ncdump command-line utility.
185
+
186
+ .. code :: bash
187
+
188
+ $ ncdump file.nc
189
+ netcdf file {
190
+
191
+ group: A {
192
+ variables:
193
+ int64 a ;
194
+ data:
195
+
196
+ a = 0 ;
197
+ } // group A
198
+
199
+ group: B {
200
+ variables:
201
+ int64 b ;
202
+ data:
203
+
204
+ b = 1 ;
205
+ } // group B
206
+ }
207
+
208
+ Either of these groups can be loaded from the file as an independent :py:class: `Dataset ` object:
209
+
210
+ .. ipython ::
211
+ :verbatim:
212
+
213
+ In [1]: group1 = xr.open_dataset("file.nc", group="A")
214
+
215
+ In [2]: group1
216
+ Out[2]:
217
+ <xarray.Dataset>
218
+ Dimensions: ()
219
+ Data variables:
220
+ a int64 ...
221
+
222
+ In [3]: group2 = xr.open_dataset("file.nc", group="B")
223
+
224
+ In [4]: group2
225
+ Out[4]:
226
+ <xarray.Dataset>
227
+ Dimensions: ()
228
+ Data variables:
229
+ b int64 ...
230
+
231
+ .. note ::
232
+
233
+ For native handling of multiple groups with xarray, including I/O, you might be interested in the experimental
234
+ `xarray-datatree <https://github.com/xarray-contrib/datatree >`_ package.
235
+
170
236
171
237
.. _io.encoding :
172
238
Original file line number Diff line number Diff line change @@ -69,6 +69,8 @@ Bug fixes
69
69
Documentation
70
70
~~~~~~~~~~~~~
71
71
72
+ - Add example of reading and writing individual groups to a single netCDF file to I/O docs page. (:pull: `7338 `)
73
+ By `Tom Nicholas <https://github.com/TomNicholas >`_.
72
74
73
75
Internal Changes
74
76
~~~~~~~~~~~~~~~~
You can’t perform that action at this time.
0 commit comments