Skip to content

Commit f3b8b06

Browse files
authored
Merge branch 'main' into zarr-append-fix
2 parents 4abcaa8 + 1aec2ab commit f3b8b06

File tree

19 files changed

+211
-50
lines changed

19 files changed

+211
-50
lines changed

.github/ISSUE_TEMPLATE/bugreport.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ body:
5454
attributes:
5555
label: Environment
5656
description: |
57-
Paste the output of `xr.show_versions()` here
57+
Paste the output of `xr.show_versions()` between the `<details>` tags, leaving an empty line following the opening tag.
58+
value: |
59+
<details>
60+
61+
62+
63+
</details>
5864
validations:
5965
required: true

.github/ISSUE_TEMPLATE/config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ contact_links:
66
Ask questions and discuss with other community members here.
77
If you have a question like "How do I concatenate a list of datasets?" then
88
please include a self-contained reproducible example if possible.
9+
- name: Raster analysis usage question
10+
url: https://github.com/corteva/rioxarray/discussions
11+
about: |
12+
If you are using the rioxarray extension (engine='rasterio'), or have questions about
13+
raster analysis such as geospatial formats, coordinate reprojection, etc.,
14+
please use the rioxarray discussion forum.

.github/stale.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ exemptLabels:
1414
- "[Status] Maybe Later"
1515

1616
# Set to true to ignore issues in a project (defaults to false)
17-
exemptProjects: false
17+
exemptProjects: true
1818

1919
# Set to true to ignore issues in a milestone (defaults to false)
20-
exemptMilestones: false
20+
exemptMilestones: true
2121

2222
# Set to true to ignore issues with an assignee (defaults to false)
2323
exemptAssignees: true

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# https://pre-commit.com/
22
repos:
33
- repo: https://github.com/pre-commit/pre-commit-hooks
4-
rev: v4.1.0
4+
rev: v4.2.0
55
hooks:
66
- id: trailing-whitespace
77
- id: end-of-file-fixer
@@ -19,7 +19,7 @@ repos:
1919
hooks:
2020
- id: isort
2121
- repo: https://github.com/asottile/pyupgrade
22-
rev: v2.31.1
22+
rev: v2.32.0
2323
hooks:
2424
- id: pyupgrade
2525
args:

README.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ powerful and concise interface. For example:
7171
Documentation
7272
-------------
7373

74-
Learn more about xarray in its official documentation at https://docs.xarray.dev/
74+
Learn more about xarray in its official documentation at https://docs.xarray.dev/.
75+
76+
Try out an `interactive Jupyter notebook <https://mybinder.org/v2/gh/pydata/xarray/main?urlpath=lab/tree/doc/examples/weather-data.ipynb>`_.
7577

7678
Contributing
7779
------------

doc/internals/zarr-encoding-spec.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ the variable dimension names and then removed from the attributes dictionary
3232
returned to the user.
3333

3434
Because of these choices, Xarray cannot read arbitrary array data, but only
35-
Zarr data with valid ``_ARRAY_DIMENSIONS`` attributes on each array.
35+
Zarr data with valid ``_ARRAY_DIMENSIONS`` or
36+
`NCZarr <https://docs.unidata.ucar.edu/nug/current/nczarr_head.html>`_ attributes
37+
on each array (NCZarr dimension names are defined in the ``.zarray`` file).
3638

37-
After decoding the ``_ARRAY_DIMENSIONS`` attribute and assigning the variable
39+
After decoding the ``_ARRAY_DIMENSIONS`` or NCZarr attribute and assigning the variable
3840
dimensions, Xarray proceeds to [optionally] decode each variable using its
3941
standard CF decoding machinery used for NetCDF data (see :py:func:`decode_cf`).
4042

doc/user-guide/io.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,11 @@ the ability to store and analyze datasets far too large fit onto disk
518518

519519
Xarray can't open just any zarr dataset, because xarray requires special
520520
metadata (attributes) describing the dataset dimensions and coordinates.
521-
At this time, xarray can only open zarr datasets that have been written by
522-
xarray. For implementation details, see :ref:`zarr_encoding`.
521+
At this time, xarray can only open zarr datasets with these special attributes,
522+
such as zarr datasets written by xarray,
523+
`netCDF <https://docs.unidata.ucar.edu/nug/current/nczarr_head.html>`_,
524+
or `GDAL <https://gdal.org/drivers/raster/zarr.html>`_.
525+
For implementation details, see :ref:`zarr_encoding`.
523526

524527
To write a dataset with zarr, we use the :py:meth:`Dataset.to_zarr` method.
525528

@@ -548,6 +551,11 @@ store is already present at that path, an error will be raised, preventing it
548551
from being overwritten. To override this behavior and overwrite an existing
549552
store, add ``mode='w'`` when invoking :py:meth:`~Dataset.to_zarr`.
550553

554+
.. note::
555+
556+
xarray does not write NCZarr attributes. Therefore, NCZarr data must be
557+
opened in read-only mode.
558+
551559
To store variable length strings, convert them to object arrays first with
552560
``dtype=object``.
553561

doc/whats-new.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ v2022.03.1 (unreleased)
2222
New Features
2323
~~~~~~~~~~~~
2424

25+
- The `zarr` backend is now able to read NCZarr.
26+
By `Mattia Almansi <https://github.com/malmans2>`_.
2527
- Add a weighted ``quantile`` method to :py:class:`~core.weighted.DatasetWeighted` and
2628
:py:class:`~core.weighted.DataArrayWeighted` (:pull:`6059`). By
2729
`Christian Jauvin <https://github.com/cjauvin>`_ and `David Huard <https://github.com/huard>`_.
@@ -36,7 +38,7 @@ New Features
3638
elements which trigger summarization rather than full repr in (numpy) array
3739
detailed views of the html repr (:pull:`6400`).
3840
By `Benoît Bovy <https://github.com/benbovy>`_.
39-
- Allow passing chunks in **kwargs form to :py:meth:`Dataset.chunk`, :py:meth:`DataArray.chunk`, and
41+
- Allow passing chunks in ``**kwargs`` form to :py:meth:`Dataset.chunk`, :py:meth:`DataArray.chunk`, and
4042
:py:meth:`Variable.chunk`. (:pull:`6471`)
4143
By `Tom Nicholas <https://github.com/TomNicholas>`_.
4244

@@ -76,6 +78,17 @@ Bug fixes
7678
- In the API for backends, support dimensions that express their preferred chunk sizes
7779
as a tuple of integers. (:issue:`6333`, :pull:`6334`)
7880
By `Stan West <https://github.com/stanwest>`_.
81+
- Fix bug in :py:func:`where` when passing non-xarray objects with ``keep_attrs=True``. (:issue:`6444`, :pull:`6461`)
82+
By `Sam Levang <https://github.com/slevang>`_.
83+
- Allow passing both ``other`` and ``drop=True`` arguments to ``xr.DataArray.where``
84+
and ``xr.Dataset.where`` (:pull:`6466`, :pull:`6467`).
85+
By `Michael Delgado <https://github.com/delgadom>`_.
86+
- Ensure dtype encoding attributes are not added or modified on variables that
87+
contain datetime-like values prior to being passed to
88+
:py:func:`xarray.conventions.decode_cf_variable` (:issue:`6453`,
89+
:pull:`6489`). By `Spencer Clark <https://github.com/spencerkclark>`_.
90+
- Dark themes are now properly detected in Furo-themed Sphinx documents (:issue:`6500`, :pull:`6501`).
91+
By `Kevin Paul <https://github.com/kmpaul>`_.
7992

8093
Documentation
8194
~~~~~~~~~~~~~

xarray/backends/scipy_.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ def _open_scipy_netcdf(filename, mode, mmap, version):
8686
)
8787
except TypeError as e:
8888
# TODO: gzipped loading only works with NetCDF3 files.
89-
if "is not a valid NetCDF 3 file" in e.message:
89+
errmsg = e.args[0]
90+
if "is not a valid NetCDF 3 file" in errmsg:
9091
raise ValueError("gzipped file loading only supports NetCDF 3 files.")
9192
else:
9293
raise

xarray/backends/zarr.py

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23
import warnings
34

@@ -178,19 +179,37 @@ def _determine_zarr_chunks(enc_chunks, var_chunks, ndim, name, safe_chunks):
178179
raise AssertionError("We should never get here. Function logic must be wrong.")
179180

180181

181-
def _get_zarr_dims_and_attrs(zarr_obj, dimension_key):
182+
def _get_zarr_dims_and_attrs(zarr_obj, dimension_key, try_nczarr):
182183
# Zarr arrays do not have dimensions. To get around this problem, we add
183184
# an attribute that specifies the dimension. We have to hide this attribute
184185
# when we send the attributes to the user.
185186
# zarr_obj can be either a zarr group or zarr array
186187
try:
188+
# Xarray-Zarr
187189
dimensions = zarr_obj.attrs[dimension_key]
188-
except KeyError:
189-
raise KeyError(
190-
f"Zarr object is missing the attribute `{dimension_key}`, which is "
191-
"required for xarray to determine variable dimensions."
192-
)
193-
attributes = HiddenKeyDict(zarr_obj.attrs, [dimension_key])
190+
except KeyError as e:
191+
if not try_nczarr:
192+
raise KeyError(
193+
f"Zarr object is missing the attribute `{dimension_key}`, which is "
194+
"required for xarray to determine variable dimensions."
195+
) from e
196+
197+
# NCZarr defines dimensions through metadata in .zarray
198+
zarray_path = os.path.join(zarr_obj.path, ".zarray")
199+
zarray = json.loads(zarr_obj.store[zarray_path])
200+
try:
201+
# NCZarr uses Fully Qualified Names
202+
dimensions = [
203+
os.path.basename(dim) for dim in zarray["_NCZARR_ARRAY"]["dimrefs"]
204+
]
205+
except KeyError as e:
206+
raise KeyError(
207+
f"Zarr object is missing the attribute `{dimension_key}` and the NCZarr metadata, "
208+
"which are required for xarray to determine variable dimensions."
209+
) from e
210+
211+
nc_attrs = [attr for attr in zarr_obj.attrs if attr.startswith("_NC")]
212+
attributes = HiddenKeyDict(zarr_obj.attrs, [dimension_key] + nc_attrs)
194213
return dimensions, attributes
195214

196215

@@ -409,7 +428,10 @@ def ds(self):
409428

410429
def open_store_variable(self, name, zarr_array):
411430
data = indexing.LazilyIndexedArray(ZarrArrayWrapper(name, self))
412-
dimensions, attributes = _get_zarr_dims_and_attrs(zarr_array, DIMENSION_KEY)
431+
try_nczarr = self._mode == "r"
432+
dimensions, attributes = _get_zarr_dims_and_attrs(
433+
zarr_array, DIMENSION_KEY, try_nczarr
434+
)
413435
attributes = dict(attributes)
414436
encoding = {
415437
"chunks": zarr_array.chunks,
@@ -430,26 +452,24 @@ def get_variables(self):
430452
)
431453

432454
def get_attrs(self):
433-
return dict(self.zarr_group.attrs.asdict())
455+
return {
456+
k: v
457+
for k, v in self.zarr_group.attrs.asdict().items()
458+
if not k.startswith("_NC")
459+
}
434460

435461
def get_dimensions(self):
462+
try_nczarr = self._mode == "r"
436463
dimensions = {}
437464
for k, v in self.zarr_group.arrays():
438-
try:
439-
for d, s in zip(v.attrs[DIMENSION_KEY], v.shape):
440-
if d in dimensions and dimensions[d] != s:
441-
raise ValueError(
442-
f"found conflicting lengths for dimension {d} "
443-
f"({s} != {dimensions[d]})"
444-
)
445-
dimensions[d] = s
446-
447-
except KeyError:
448-
raise KeyError(
449-
f"Zarr object is missing the attribute `{DIMENSION_KEY}`, "
450-
"which is required for xarray to determine "
451-
"variable dimensions."
452-
)
465+
dim_names, _ = _get_zarr_dims_and_attrs(v, DIMENSION_KEY, try_nczarr)
466+
for d, s in zip(dim_names, v.shape):
467+
if d in dimensions and dimensions[d] != s:
468+
raise ValueError(
469+
f"found conflicting lengths for dimension {d} "
470+
f"({s} != {dimensions[d]})"
471+
)
472+
dimensions[d] = s
453473
return dimensions
454474

455475
def set_dimensions(self, variables, unlimited_dims=None):
@@ -645,7 +665,7 @@ def open_zarr(
645665
646666
The `store` object should be a valid store for a Zarr group. `store`
647667
variables must contain dimension metadata encoded in the
648-
`_ARRAY_DIMENSIONS` attribute.
668+
`_ARRAY_DIMENSIONS` attribute or must have NCZarr format.
649669
650670
Parameters
651671
----------

0 commit comments

Comments
 (0)