Set longdouble=False
in cftime.date2num
within the date encoding context
#7171
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, the default behavior of
cftime.date2num
is to return integer values when possible (i.e. when the encoding units allow), and fall back to returning float64 values when that is not possible. Recently, cftime added the option to use float128 as the fallback dtype, which enables greater potential roundtrip precision. This is through thelongdouble
flag tocftime.date2num
, which currently defaults toFalse
. It was intentionally set toFalse
by default, because netCDF does not support storing float128 values in files, and so, without any changes, would otherwise break xarray's encoding procedure.The desire in cftime, however, is to eventually set this flag to
True
by default (Unidata/cftime#297). This PR makes the necessary changes in xarray to adapt to this eventual new default. Essentially if thelongdouble
argument is allowed in the user's version ofcftime.date2num
, we explicitly set it toFalse
to preserve the current float64 fallback behavior within the context of encoding times. There are a few more places wheredate2num
is used (some additional places in the tests, and incalendar_ops.py
), but in those places using float128 values would not present a problem.At some point we might consider relaxing this behavior in xarray, since it is possible to store float128 values in zarr stores for example, but for the time being the simplest approach seems to be to stick with float64 for all backends (it would be complicated to have backend-specific defaults).
cc: @jswhit