Skip to content

Commit 99dc235

Browse files
committed
more debugging
1 parent 6ea661a commit 99dc235

File tree

1 file changed

+55
-16
lines changed

1 file changed

+55
-16
lines changed

xarray/backends/zarr.py

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -844,13 +844,18 @@ def ds(self):
844844
return self.zarr_group
845845

846846
def open_store_variable(self, name):
847+
print(f"DEBUG OPEN: Reading variable '{name}'")
847848
zarr_array = self.members[name]
849+
print(
850+
f"DEBUG OPEN: zarr_array.dtype={zarr_array.dtype}, fill_value={zarr_array.fill_value}"
851+
)
848852
data = indexing.LazilyIndexedArray(ZarrArrayWrapper(zarr_array))
849853
try_nczarr = self._mode == "r"
850854
dimensions, attributes = _get_zarr_dims_and_attrs(
851855
zarr_array, DIMENSION_KEY, try_nczarr
852856
)
853857
attributes = dict(attributes)
858+
print(f"DEBUG OPEN: Original attributes: {attributes}")
854859

855860
encoding = {
856861
"chunks": zarr_array.chunks,
@@ -875,32 +880,60 @@ def open_store_variable(self, name):
875880
}
876881
)
877882

883+
print(
884+
f"DEBUG OPEN: _use_zarr_fill_value_as_mask={self._use_zarr_fill_value_as_mask}"
885+
)
886+
print(f"DEBUG OPEN: '_FillValue' in attributes: {'_FillValue' in attributes}")
887+
878888
if self._use_zarr_fill_value_as_mask:
879889
# Setting this attribute triggers CF decoding for missing values
880890
# by interpreting Zarr's fill_value to mean the same as netCDF's _FillValue
891+
print(f"DEBUG OPEN: Using zarr fill_value as mask for '{name}'")
892+
print("===========")
893+
print(f"DEBUG DTYPE: zarr array fill value: {zarr_array.fill_value.dtype}")
881894
if zarr_array.fill_value is not None:
882895
attributes["_FillValue"] = zarr_array.fill_value
896+
print(
897+
f"\t DEBUG OPEN: Set _FillValue to {zarr_array.fill_value} for '{name}'"
898+
)
899+
print("===========")
883900
elif "_FillValue" in attributes:
884901
# TODO update version check for the released version with dtypes
885902
# probably be 3.1
886903
import zarr
887904

888-
#
905+
print(f"DEBUG: Processing _FillValue for {name}")
906+
print(f"DEBUG: Original _FillValue: {attributes['_FillValue']}")
907+
print(
908+
f"DEBUG: zarr_array.metadata.data_type: {zarr_array.metadata.data_type}"
909+
)
910+
889911
if Version(zarr.__version__) >= Version("3.0.6"):
912+
native_dtype = zarr_array.metadata.data_type.to_native_dtype()
913+
print(f"DEBUG: native_dtype: {native_dtype}")
890914
attributes["_FillValue"] = (
891915
# Use the new dtype infrastructure instead of doing xarray
892916
# specific fill value decoding
893917
FillValueCoder.decode(
894918
attributes["_FillValue"],
895-
zarr_array.metadata.data_type.to_native_dtype(),
919+
native_dtype,
896920
)
897921
)
898922
else:
923+
dtype_value = zarr_array.metadata.data_type.value
924+
print(f"DEBUG: dtype_value: {dtype_value}")
899925
attributes["_FillValue"] = FillValueCoder.decode(
900-
attributes["_FillValue"], zarr_array.metadata.data_type.value
926+
attributes["_FillValue"], dtype_value
901927
)
902928

903-
return Variable(dimensions, data, attributes, encoding)
929+
print(f"DEBUG: Decoded _FillValue: {attributes['_FillValue']}")
930+
print(f"DEBUG: Decoded _FillValue type: {type(attributes['_FillValue'])}")
931+
932+
print(f"DEBUG OPEN: Final attributes for '{name}': {attributes}")
933+
variable = Variable(dimensions, data, attributes, encoding)
934+
print(f"DEBUG OPEN: Created variable '{name}' with dtype: {variable.dtype}")
935+
936+
return variable
904937

905938
def get_variables(self):
906939
return FrozenDict((k, self.open_store_variable(k)) for k in self.array_keys())
@@ -1015,22 +1048,36 @@ def store(
10151048
variables_encoded, attributes = self.encode(
10161049
{vn: variables[vn] for vn in new_variable_names}, attributes
10171050
)
1018-
print(f"{variables_encoded=}")
1051+
print("HERE")
10191052

10201053
if existing_variable_names:
10211054
# We make sure that values to be appended are encoded *exactly*
10221055
# as the current values in the store.
10231056
# To do so, we decode variables directly to access the proper encoding,
10241057
# without going via xarray.Dataset to avoid needing to load
10251058
# index variables into memory.
1059+
print(
1060+
f"DEBUG APPEND: Processing existing variables: {existing_variable_names}"
1061+
)
1062+
store_vars = {
1063+
k: self.open_store_variable(name=k) for k in existing_variable_names
1064+
}
1065+
print(
1066+
f"DEBUG APPEND: Store vars dtypes: {[(name, var.dtype) for name, var in store_vars.items()]}"
1067+
)
1068+
10261069
existing_vars, _, _ = conventions.decode_cf_variables(
1027-
variables={
1028-
k: self.open_store_variable(name=k) for k in existing_variable_names
1029-
},
1070+
variables=store_vars,
10301071
# attributes = {} since we don't care about parsing the global
10311072
# "coordinates" attribute
10321073
attributes={},
10331074
)
1075+
print(
1076+
f"DEBUG APPEND: After CF decode dtypes: {[(name, var.dtype) for name, var in existing_vars.items()]}"
1077+
)
1078+
print(
1079+
f"DEBUG APPEND: Variables to append dtypes: {[(name, var.dtype) for name, var in variables_encoded.items() if name in existing_variable_names]}"
1080+
)
10341081
# Modified variables must use the same encoding as the store.
10351082
vars_with_encoding = {}
10361083
for vn in existing_variable_names:
@@ -1068,7 +1115,6 @@ def store(
10681115
else:
10691116
variables_to_set = variables_encoded
10701117

1071-
print(f"{variables_to_set=}")
10721118
self.set_variables(
10731119
variables_to_set, check_encoding_set, writer, unlimited_dims=unlimited_dims
10741120
)
@@ -1077,7 +1123,6 @@ def store(
10771123
if _zarr_v3():
10781124
kwargs["zarr_format"] = self.zarr_group.metadata.zarr_format
10791125
zarr.consolidate_metadata(self.zarr_group.store, **kwargs)
1080-
print("DONE STORE.STORE")
10811126

10821127
def sync(self):
10831128
pass
@@ -1151,8 +1196,6 @@ def _create_new_array(
11511196
if c in encoding:
11521197
encoding["config"][c] = encoding.pop(c)
11531198

1154-
print("create")
1155-
print(dtype)
11561199
zarr_array = self.zarr_group.create(
11571200
name,
11581201
shape=shape,
@@ -1259,8 +1302,6 @@ def set_variables(self, variables, check_encoding_set, writer, unlimited_dims=No
12591302

12601303
encoding["overwrite"] = True if self._mode == "w" else False
12611304

1262-
print(dtype)
1263-
print(";sdf")
12641305
zarr_array = self._create_new_array(
12651306
name=name,
12661307
dtype=dtype,
@@ -1269,8 +1310,6 @@ def set_variables(self, variables, check_encoding_set, writer, unlimited_dims=No
12691310
encoding=encoding,
12701311
attrs=encoded_attrs,
12711312
)
1272-
print(zarr_array)
1273-
print(type(zarr_array))
12741313

12751314
writer.add(v.data, zarr_array, region)
12761315

0 commit comments

Comments
 (0)