@@ -43,6 +43,10 @@ def test_write_netcdf_cdf5_format(tmp_path):
4343 )
4444 # Should be cdf5 for NETCDF3_64BIT_DATA
4545 assert result .stdout .strip () == 'cdf5'
46+ # Check that the temporary file was deleted
47+ basename , extension = os .path .splitext (str (out_file ))
48+ tmp_file = f'_tmp_{ basename } .netcdf4{ extension } '
49+ assert not os .path .exists (tmp_file )
4650
4751
4852def test_write_netcdf_int64_conversion_and_attr (tmp_path ):
@@ -58,3 +62,40 @@ def test_write_netcdf_int64_conversion_and_attr(tmp_path):
5862 # Attribute should be preserved
5963 assert ds2 ['foo' ].attrs ['myattr' ] == 'testattr'
6064 ds2 .close ()
65+
66+
67+ def test_write_netcdf_fill_value (tmp_path ):
68+ # Test that NaN values are written with correct fill value
69+ arr = np .array ([1.0 , np .nan , 3.0 ], dtype = np .float32 )
70+ ds = xr .Dataset ({'bar' : (('x' ,), arr )})
71+ out_file = tmp_path / 'test_fill.nc'
72+ write_netcdf (ds , str (out_file ))
73+ ds2 = xr .open_dataset (out_file )
74+ # The second value should be the default fill value for float32
75+ fill_value = ds2 ['bar' ].encoding .get ('_FillValue' , None )
76+ assert fill_value is not None
77+ assert np .isnan (ds2 ['bar' ].values [1 ])
78+ ds2 .close ()
79+
80+
81+ def test_write_netcdf_string_dim_name (tmp_path ):
82+ # Test that custom char_dim_name is used in encoding
83+ arr = np .array ([b'abc' , b'def' ])
84+ ds = xr .Dataset ({'baz' : (('x' ,), arr )})
85+ out_file = tmp_path / 'test_strdim.nc'
86+ write_netcdf (ds , str (out_file ), char_dim_name = 'CustomStrLen' )
87+ ds2 = xr .open_dataset (out_file )
88+ # Should have the variable and correct shape
89+ assert 'baz' in ds2 .variables
90+ ds2 .close ()
91+ arr = np .array ([1 , 2 , 3 ], dtype = np .int64 )
92+ ds = xr .Dataset ({'foo' : (('x' ,), arr )})
93+ ds ['foo' ].attrs ['myattr' ] = 'testattr'
94+ out_file = tmp_path / 'test_int64.nc'
95+ write_netcdf (ds , str (out_file ))
96+ ds2 = xr .open_dataset (out_file )
97+ # Should be int32, not int64
98+ assert ds2 ['foo' ].dtype == np .int32
99+ # Attribute should be preserved
100+ assert ds2 ['foo' ].attrs ['myattr' ] == 'testattr'
101+ ds2 .close ()
0 commit comments