@@ -65,7 +65,7 @@ def __getitem__(self, key):
65
65
# could possibly have a work-around for 0d data here
66
66
67
67
68
- def _determine_zarr_chunks (enc_chunks , var_chunks , ndim ):
68
+ def _determine_zarr_chunks (enc_chunks , var_chunks , ndim , name ):
69
69
"""
70
70
Given encoding chunks (possibly None) and variable chunks (possibly None)
71
71
"""
@@ -88,15 +88,16 @@ def _determine_zarr_chunks(enc_chunks, var_chunks, ndim):
88
88
if var_chunks and enc_chunks is None :
89
89
if any (len (set (chunks [:- 1 ])) > 1 for chunks in var_chunks ):
90
90
raise ValueError (
91
- "Zarr requires uniform chunk sizes except for final chunk."
92
- " Variable dask chunks %r are incompatible. Consider "
93
- "rechunking using `chunk()`." % ( var_chunks ,)
91
+ "Zarr requires uniform chunk sizes except for final chunk. "
92
+ f" Variable named { name !r } has incompatible dask chunks: { var_chunks !r } . "
93
+ "Consider rechunking using `chunk()`."
94
94
)
95
95
if any ((chunks [0 ] < chunks [- 1 ]) for chunks in var_chunks ):
96
96
raise ValueError (
97
97
"Final chunk of Zarr array must be the same size or smaller "
98
- "than the first. Variable Dask chunks %r are incompatible. "
99
- "Consider rechunking using `chunk()`." % var_chunks
98
+ f"than the first. Variable named { name !r} has incompatible Dask chunks { var_chunks !r} ."
99
+ "Consider either rechunking using `chunk()` or instead deleting "
100
+ "or modifying `encoding['chunks']`."
100
101
)
101
102
# return the first chunk for each dimension
102
103
return tuple (chunk [0 ] for chunk in var_chunks )
@@ -114,13 +115,15 @@ def _determine_zarr_chunks(enc_chunks, var_chunks, ndim):
114
115
115
116
if len (enc_chunks_tuple ) != ndim :
116
117
# throw away encoding chunks, start over
117
- return _determine_zarr_chunks (None , var_chunks , ndim )
118
+ return _determine_zarr_chunks (None , var_chunks , ndim , name )
118
119
119
120
for x in enc_chunks_tuple :
120
121
if not isinstance (x , int ):
121
122
raise TypeError (
122
- "zarr chunks must be an int or a tuple of ints. "
123
- "Instead found %r" % (enc_chunks_tuple ,)
123
+ "zarr chunk sizes specified in `encoding['chunks']` "
124
+ "must be an int or a tuple of ints. "
125
+ f"Instead found encoding['chunks']={ enc_chunks_tuple !r} "
126
+ f"for variable named { name !r} ."
124
127
)
125
128
126
129
# if there are chunks in encoding and the variable data is a numpy array,
@@ -142,19 +145,22 @@ def _determine_zarr_chunks(enc_chunks, var_chunks, ndim):
142
145
for dchunk in dchunks [:- 1 ]:
143
146
if dchunk % zchunk :
144
147
raise NotImplementedError (
145
- "Specified zarr chunks %r would overlap multiple dask "
146
- "chunks %r. This is not implemented in xarray yet . "
147
- " Consider rechunking the data using "
148
- "`chunk()` or specifying different chunks in encoding. "
149
- % ( enc_chunks_tuple , var_chunks )
148
+ f "Specified zarr chunks encoding['chunks']= { enc_chunks_tuple !r } for "
149
+ f"variable named { name !r } would overlap multiple dask chunks { var_chunks !r } . "
150
+ "This is not implemented in xarray yet. "
151
+ "Consider either rechunking using `chunk()` or instead deleting "
152
+ "or modifying `encoding['chunks']`."
150
153
)
151
154
if dchunks [- 1 ] > zchunk :
152
155
raise ValueError (
153
156
"Final chunk of Zarr array must be the same size or "
154
- "smaller than the first. The specified Zarr chunk "
155
- "encoding is %r, but %r in variable Dask chunks %r is "
156
- "incompatible. Consider rechunking using `chunk()`."
157
- % (enc_chunks_tuple , dchunks , var_chunks )
157
+ "smaller than the first. "
158
+ f"Specified Zarr chunk encoding['chunks']={ enc_chunks_tuple } , "
159
+ f"for variable named { name !r} "
160
+ f"but { dchunks } in the variable's Dask chunks { var_chunks } is "
161
+ "incompatible with this encoding. "
162
+ "Consider either rechunking using `chunk()` or instead deleting "
163
+ "or modifying `encoding['chunks']`."
158
164
)
159
165
return enc_chunks_tuple
160
166
@@ -177,7 +183,7 @@ def _get_zarr_dims_and_attrs(zarr_obj, dimension_key):
177
183
return dimensions , attributes
178
184
179
185
180
- def extract_zarr_variable_encoding (variable , raise_on_invalid = False ):
186
+ def extract_zarr_variable_encoding (variable , raise_on_invalid = False , name = None ):
181
187
"""
182
188
Extract zarr encoding dictionary from xarray Variable
183
189
@@ -207,7 +213,7 @@ def extract_zarr_variable_encoding(variable, raise_on_invalid=False):
207
213
del encoding [k ]
208
214
209
215
chunks = _determine_zarr_chunks (
210
- encoding .get ("chunks" ), variable .chunks , variable .ndim
216
+ encoding .get ("chunks" ), variable .chunks , variable .ndim , name
211
217
)
212
218
encoding ["chunks" ] = chunks
213
219
return encoding
@@ -453,7 +459,9 @@ def set_variables(self, variables, check_encoding_set, writer, unlimited_dims=No
453
459
writer .add (v .data , zarr_array , region = tuple (new_region ))
454
460
else :
455
461
# new variable
456
- encoding = extract_zarr_variable_encoding (v , raise_on_invalid = check )
462
+ encoding = extract_zarr_variable_encoding (
463
+ v , raise_on_invalid = check , name = vn
464
+ )
457
465
encoded_attrs = {}
458
466
# the magic for storing the hidden dimension data
459
467
encoded_attrs [DIMENSION_KEY ] = dims
0 commit comments