@@ -132,6 +132,11 @@ def create_multiscale_from_datatree(
132132 for data_var in dataset .data_vars :
133133 if dataset [data_var ].dtype in (np .dtype ("<f8" ), np .dtype (">f8" )):
134134 dataset [data_var ] = dataset [data_var ].astype ("float32" )
135+ # Clear _FillValue from the DataArray's own encoding to prevent
136+ # xarray from raising "Zarr does not support _FillValue in encoding".
137+ if not keep_scale_offset :
138+ for data_var in dataset .data_vars :
139+ dataset [data_var ].encoding .pop ("_FillValue" , None )
135140 else :
136141 # Non-measurement groups: preserve original encoding
137142 encoding = create_original_encoding (dataset )
@@ -185,6 +190,11 @@ def create_multiscale_from_datatree(
185190 keep_scale_offset = keep_scale_offset ,
186191 )
187192
193+ # Strip _FillValue from DataArray encoding for downsampled levels too
194+ if not keep_scale_offset :
195+ for data_var in downsampled_dataset .data_vars :
196+ downsampled_dataset [data_var ].encoding .pop ("_FillValue" , None )
197+
188198 # Write dataset
189199 ds_out = stream_write_dataset (
190200 downsampled_dataset ,
@@ -269,7 +279,15 @@ def create_measurements_encoding(
269279 keep_keys = XARRAY_ENCODING_KEYS - {"compressors" , "shards" , "chunks" }
270280
271281 if not keep_scale_offset :
272- keep_keys = keep_keys - CF_SCALE_OFFSET_KEYS
282+ # When stripping scale/offset, also strip _FillValue since the original
283+ # _FillValue is in raw integer units and meaningless for decoded float data.
284+ keep_keys = keep_keys - CF_SCALE_OFFSET_KEYS - {"_FillValue" }
285+ # Set zarr fill_value to NaN so nodata regions are correctly identified
286+ # as transparent by zarr-aware viewers (e.g. OpenLayers GeoZarr source).
287+ # xarray's zarr backend uses "fill_value" (no underscore) in encoding
288+ # to set the zarr-level fill value, distinct from "_FillValue" which
289+ # controls CF-convention attribute masking.
290+ var_encoding ["fill_value" ] = float ("nan" )
273291
274292 for key in keep_keys :
275293 if key in var_data .encoding :
0 commit comments