TNO Intern

Commit 9ae52602 authored by Arjo Segers's avatar Arjo Segers
Browse files

Improved check on arrays being numeric. Avoid warnings from packing in case of all-nan values.

parent 46c9e3af
Loading
Loading
Loading
Loading
+13 −10
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@
#
# 2025-04, Arjo Segers
#   Enable zlib compression only for numerical data.
#   Avoid warnings from packing in case of all-nan values.
#

########################################################################
@@ -188,9 +189,16 @@ def Pack_DataArray(da, dtype="i2"):

    # only floats ...
    if da.dtype in [numpy.float32, numpy.float64]:
        # should have some values ..
        if numpy.any( ~ numpy.isnan(da.values) ):
            # value range, ignore nan's:
            vmin = numpy.nanmin(da.values)
            vmax = numpy.nanmax(da.values)
        else:
            # dummy range:
            vmin = 0.0
            vmax = 0.0
        #end if
        # target data type could be integer or float:
        if dtype.startswith("i"):
            # use absolute minimum to represent nans:
@@ -610,13 +618,8 @@ class CSO_File(object):
            # loop over variables:
            for vname in self.ds.keys():
                # only numerical values:
                if self.ds[vname].dtype in [
                    numpy.int16,
                    numpy.int32,
                    numpy.int64,
                    numpy.float32,
                    numpy.float64,
                ]:
                dtype = self.ds[vname].dtype
                if numpy.issubdtype(dtype,numpy.integer) or numpy.issubdtype(dtype,numpy.floating):
                    # enable zlib compression and set level:
                    self.ds[vname].encoding["zlib"] = True
                    self.ds[vname].encoding["complevel"] = complevel