Skip to content

Commit

Permalink
Use tmp_path instead of tempfile library
Browse files Browse the repository at this point in the history
  • Loading branch information
lahtinep committed Mar 7, 2024
1 parent f6594cf commit e1a6f37
Showing 1 changed file with 26 additions and 34 deletions.
60 changes: 26 additions & 34 deletions satpy/tests/reader_tests/test_netcdf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,53 +300,45 @@ def test_use_h5netcdf_for_file_not_accessible_locally(self):
"scale_factor": 0.01,
"add_offset": 0}

def test_get_data_as_xarray_netcdf4():
def test_get_data_as_xarray_netcdf4(tmp_path):
"""Test getting xr.DataArray from netcdf4 variable."""
import tempfile

import netCDF4 as nc
import numpy as np

from satpy.readers.netcdf_utils import get_data_as_xarray

data = np.array([1, 2, 3])

with tempfile.TemporaryDirectory() as tmpdir:
# Create an empty HDF5
fname = os.path.join(tmpdir, "test.nc")
dset = nc.Dataset(fname, "w")
dset.createDimension("y", None)
var = dset.createVariable("test_data", "uint8", ("y",))
var[:] = data
var.setncatts(NC_ATTRS)
# Turn off automatic scale factor and offset handling
dset.set_auto_maskandscale(False)
res = get_data_as_xarray(var)
np.testing.assert_equal(res.data, data)
assert res.attrs == NC_ATTRS
dset.close()


def test_get_data_as_xarray_h5netcdf():
fname = tmp_path / "test.nc"
dset = nc.Dataset(fname, "w")
dset.createDimension("y", None)
var = dset.createVariable("test_data", "uint8", ("y",))
var[:] = data
var.setncatts(NC_ATTRS)
# Turn off automatic scale factor and offset handling
dset.set_auto_maskandscale(False)
res = get_data_as_xarray(var)
np.testing.assert_equal(res.data, data)
assert res.attrs == NC_ATTRS
dset.close()


def test_get_data_as_xarray_h5netcdf(tmp_path):
"""Test getting xr.DataArray from h5netcdf variable."""
import tempfile

import h5netcdf
import numpy as np

from satpy.readers.netcdf_utils import get_data_as_xarray

data = np.array([1, 2, 3])

with tempfile.TemporaryDirectory() as tmpdir:
# Create an empty HDF5
fname = os.path.join(tmpdir, "test.nc")
with h5netcdf.File(fname, "w") as fid:
fid.dimensions = {"y": data.size}
var = fid.create_variable("test_data", ("y",), "uint8")
var[:] = data
for key in NC_ATTRS:
var.attrs[key] = NC_ATTRS[key]
res = get_data_as_xarray(var)
np.testing.assert_equal(res.data, data)
assert res.attrs == NC_ATTRS
fname = tmp_path / "test.nc"
with h5netcdf.File(fname, "w") as fid:
fid.dimensions = {"y": data.size}
var = fid.create_variable("test_data", ("y",), "uint8")
var[:] = data
for key in NC_ATTRS:
var.attrs[key] = NC_ATTRS[key]
res = get_data_as_xarray(var)
np.testing.assert_equal(res.data, data)
assert res.attrs == NC_ATTRS

0 comments on commit e1a6f37

Please sign in to comment.