@@ -1584,6 +1584,46 @@ def test_mask_and_scale(self, dtype) -> None:
1584
1584
expected = create_masked_and_scaled_data (dtype )
1585
1585
assert_identical (expected , ds )
1586
1586
1587
+ @pytest .mark .parametrize ("dtype" , [np .float32 , np .float64 ])
1588
+ @pytest .mark .parametrize ("offset_cf_conforming" , [True , False ])
1589
+ def test_mask_and_scale_non_cf_conforming (self , dtype , offset_cf_conforming ) -> None :
1590
+ with create_tmp_file () as tmp_file :
1591
+ with nc4 .Dataset (tmp_file , mode = "w" ) as nc :
1592
+ nc .createDimension ("t" , 5 )
1593
+ nc .createVariable ("x" , "int16" , ("t" ,), fill_value = - 1 )
1594
+ v = nc .variables ["x" ]
1595
+ v .set_auto_maskandscale (False )
1596
+ if offset_cf_conforming :
1597
+ v .add_offset = dtype (10 )
1598
+ else :
1599
+ v .add_offset = 10
1600
+ v .scale_factor = dtype (0.1 )
1601
+ v [:] = np .array ([- 1 , - 1 , 0 , 1 , 2 ])
1602
+
1603
+ # first make sure netCDF4 reads the masked and scaled data
1604
+ # correctly
1605
+ with nc4 .Dataset (tmp_file , mode = "r" ) as nc :
1606
+ expected = np .ma .array (
1607
+ [- 1 , - 1 , 10 , 10.1 , 10.2 ],
1608
+ mask = [True , True , False , False , False ],
1609
+ dtype = dtype ,
1610
+ )
1611
+ actual = nc .variables ["x" ][:]
1612
+ assert_array_equal (expected , actual )
1613
+
1614
+ # now check xarray
1615
+ with open_dataset (tmp_file ) as ds :
1616
+ if not offset_cf_conforming :
1617
+ # if not conforming, this get's promoted to float64 in any case
1618
+ # if add_offset is available
1619
+ expected = create_masked_and_scaled_data (np .float64 )
1620
+ # here we have slight differences possibly
1621
+ # due to using float32 first and casting to float64 later
1622
+ assert_allclose (expected , ds )
1623
+ else :
1624
+ expected = create_masked_and_scaled_data (dtype )
1625
+ assert_identical (expected , ds )
1626
+
1587
1627
def test_0dimensional_variable (self ) -> None :
1588
1628
# This fix verifies our work-around to this netCDF4-python bug:
1589
1629
# https://github.com/Unidata/netcdf4-python/pull/220
0 commit comments