Skip to content

Commit 50b0a69

Browse files
shoyerJoe Hamman
authored andcommitted
Switch (some) coding/encoding in conventions.py to use xarray.coding. (#1803)
* Switch (some) coding/encoding in conventions.py to use xarray.coding. The goal here is to eventually convert everything in xarray.conventions to using the new coding module, which is more modular and supports dask arrays. For now, I have switched over datetime, timedelta, unsigned integer, scaling and mask coding to use new coders. Integrating these into xarray.conventions lets us harness our existing test suite and delete a lot of redundant code. Most of the code/tests is simply reorganized. There should be no changes to public API (to keep this manageable for review). All of the original tests that are still relevant should still be present, though I have reorganized many of them into new locations to match the revised code. * Fix zarr and cmds export * add whats-new and small cleanup * Move constant to top of module * use _NS_PER_TIME_DELTA
1 parent 289f95a commit 50b0a69

File tree

9 files changed

+905
-1068
lines changed

9 files changed

+905
-1068
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ Bug fixes
7373
with size one in some dimension can now be plotted, which is good for
7474
exploring satellite imagery (:issue:`1780`).
7575
By `Zac Hatfield-Dodds <https://github.com/Zac-HD>`_.
76+
- Fixed ``UnboundLocalError`` when opening netCDF file `` (:issue:`1781`).
77+
By `Stephan Hoyer <https://github.com/shoyer>`_.
7678
- The ``variables``, ``attrs``, and ``dimensions`` properties have been
7779
deprecated as part of a bug fix addressing an issue where backends were
7880
unintentionally loading the datastores data and attributes repeatedly during

xarray/backends/zarr.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import numpy as np
88

9+
from .. import coding
910
from .. import Variable
1011
from ..core import indexing
1112
from ..core.utils import FrozenOrderedDict, HiddenKeyDict
@@ -259,13 +260,13 @@ def encode_zarr_variable(var, needs_copy=True, name=None):
259260
raise NotImplementedError("Variable `%s` is an object. Zarr "
260261
"store can't yet encode objects." % name)
261262

262-
var = conventions.maybe_encode_datetime(var, name=name)
263-
var = conventions.maybe_encode_timedelta(var, name=name)
264-
var, needs_copy = conventions.maybe_encode_offset_and_scale(var,
265-
needs_copy,
266-
name=name)
267-
var, needs_copy = conventions.maybe_encode_fill_value(var, needs_copy,
268-
name=name)
263+
for coder in [coding.times.CFDatetimeCoder(),
264+
coding.times.CFTimedeltaCoder(),
265+
coding.variables.CFScaleOffsetCoder(),
266+
coding.variables.CFMaskCoder(),
267+
coding.variables.UnsignedIntegerCoder()]:
268+
var = coder.encode(var, name=name)
269+
269270
var = conventions.maybe_encode_nonstring_dtype(var, name=name)
270271
var = conventions.maybe_default_fill_value(var)
271272
var = conventions.maybe_encode_bools(var)

0 commit comments

Comments
 (0)