Skip to content

Commit 9bd3014

Browse files
committed
fix: Skip previous encoding workaround for fixed xarray versions
see reported issue pydata/xarray#7691 and pr pydata/xarray#8713 which was included into xarray v2024.03.0
1 parent 5d974d1 commit 9bd3014

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

atlite/datasets/era5.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import os
1313
import warnings
1414
import weakref
15+
from importlib.metadata import version
1516
from tempfile import mkstemp
1617

1718
import cdsapi
@@ -21,6 +22,7 @@
2122
from dask import compute, delayed
2223
from dask.array import arctan2, sqrt
2324
from numpy import atleast_1d
25+
from packaging.version import parse
2426

2527
from atlite.gis import maybe_swap_spatial_dims
2628
from atlite.pv.solar_position import SolarPosition
@@ -365,10 +367,12 @@ def retrieve_data(product, chunks=None, tmpdir=None, lock=None, **updates):
365367
# saving due to how xarray handles netcdf compression (only float encoded as short int seem affected)
366368
# Fixes issue by keeping "float32" encoded as "float32" instead of internally saving as "short int", see:
367369
# https://stackoverflow.com/questions/75755441/why-does-saving-to-netcdf-without-encoding-change-some-values-to-nan
368-
# and hopefully fixed soon (could then remove), see https://github.com/pydata/xarray/issues/7691
369-
for v in ds.data_vars:
370-
if ds[v].encoding["dtype"] == "int16":
371-
ds[v].encoding.clear()
370+
# see https://github.com/pydata/xarray/issues/7691 and https://github.com/pydata/xarray/pull/8713
371+
# Fix was included in v2024.03.0
372+
if parse(version("xarray")) < parse("2024.03.0"):
373+
for v in ds.data_vars:
374+
if ds[v].encoding["dtype"] == "int16":
375+
ds[v].encoding.clear()
372376

373377
return ds
374378

0 commit comments

Comments
 (0)