Skip to content

Commit af137de

Browse files
committed
Delete the temporary NETCDF4 file if converting to CDF5
Test to make sure it's been deleted.
1 parent d0b649b commit af137de

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

conda_package/mpas_tools/io.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def write_netcdf(
122122

123123
if convert:
124124
basename, extension = os.path.splitext(fileName)
125-
out_filename = f'{basename}.netcdf4{extension}'
125+
out_filename = f'_tmp_{basename}.netcdf4{extension}'
126126
format = 'NETCDF4'
127127
if engine == 'scipy':
128128
# that's not going to work
@@ -151,6 +151,8 @@ def write_netcdf(
151151
)
152152
else:
153153
check_call(args, logger=logger)
154+
# delete the temporary NETCDF4 file
155+
os.remove(out_filename)
154156

155157

156158
def update_history(ds):

conda_package/tests/test_io.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4852
def 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

Comments
 (0)