Skip to content

Commit 511da36

Browse files
committed
Merge pull request #529 from petercable/f64
Issue #526 F8 values are incorrectly coerced to F4 when writing NETCD…
2 parents 32365b3 + 2934036 commit 511da36

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

doc/whats-new.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ What's New
99
import xray
1010
np.random.seed(123456)
1111
12+
v0.6.0 (unreleased)
13+
-------------------
14+
15+
Bug fixes
16+
~~~~~~~~~
17+
18+
- Fixed unnecessary coercion of float64 to float32 when using netcdf3 and
19+
netcdf4_classic formats (:issue:`526`).
20+
1221
v0.5.3 (unreleased)
1322
-------------------
1423

xray/backends/netcdf3.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919

2020
# These data-types aren't supported by netCDF3, so they are automatically
2121
# coerced instead as indicated by the "coerce_nc3_dtype" function
22-
_nc3_dtype_coercions = {'int64': 'int32', 'float64': 'float32',
23-
'bool': 'int8'}
22+
_nc3_dtype_coercions = {'int64': 'int32', 'bool': 'int8'}
2423

2524

2625
def coerce_nc3_dtype(arr):

xray/test/test_backends.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ def test_roundtrip_timedelta_data(self):
196196
with self.roundtrip(expected) as actual:
197197
self.assertDatasetIdentical(expected, actual)
198198

199+
def test_roundtrip_float64_data(self):
200+
expected = Dataset({'x': ('y', np.array([1.0, 2.0, np.pi], dtype='float64'))})
201+
with self.roundtrip(expected) as actual:
202+
self.assertDatasetIdentical(expected, actual)
203+
199204
def test_roundtrip_example_1_netcdf(self):
200205
expected = open_example_dataset('example_1.nc')
201206
with self.roundtrip(expected) as actual:
@@ -266,7 +271,7 @@ def test_roundtrip_strings_with_fill_value(self):
266271
# netCDF4 can't keep track of an empty _FillValue for VLEN
267272
# variables
268273
expected['x'][-1] = ''
269-
elif (type(self) is NetCDF3ViaNetCDF4DataTest
274+
elif (isinstance(self, (NetCDF3ViaNetCDF4DataTest, NetCDF4ClassicViaNetCDF4DataTest))
270275
or (has_netCDF4 and type(self) is GenericNetCDFDataTest)):
271276
# netCDF4 can't keep track of an empty _FillValue for nc3, either:
272277
# https://github.com/Unidata/netcdf4-python/issues/273
@@ -651,6 +656,24 @@ def roundtrip(self, data, **kwargs):
651656
yield ds
652657

653658

659+
@requires_netCDF4
660+
class NetCDF4ClassicViaNetCDF4DataTest(CFEncodedDataTest, Only32BitTypes, TestCase):
661+
@contextlib.contextmanager
662+
def create_store(self):
663+
with create_tmp_file() as tmp_file:
664+
with backends.NetCDF4DataStore(tmp_file, mode='w',
665+
format='NETCDF4_CLASSIC') as store:
666+
yield store
667+
668+
@contextlib.contextmanager
669+
def roundtrip(self, data, **kwargs):
670+
with create_tmp_file() as tmp_file:
671+
data.to_netcdf(tmp_file, format='NETCDF4_CLASSIC',
672+
engine='netcdf4')
673+
with open_dataset(tmp_file, engine='netcdf4', **kwargs) as ds:
674+
yield ds
675+
676+
654677
@requires_scipy_or_netCDF4
655678
class GenericNetCDFDataTest(CFEncodedDataTest, Only32BitTypes, TestCase):
656679
# verify that we can read and write netCDF3 files as long as we have scipy

0 commit comments

Comments
 (0)