|
12 | 12 | from __future__ import annotations |
13 | 13 |
|
14 | 14 | import importlib.util |
15 | | -import os |
16 | | -import tempfile |
17 | 15 |
|
18 | 16 | import numpy as np |
19 | 17 | import pytest |
@@ -49,14 +47,18 @@ def _gpu_available() -> bool: |
49 | 47 |
|
50 | 48 |
|
51 | 49 | @pytest.fixture |
52 | | -def uint16_neg_nodata_tif(): |
| 50 | +def uint16_neg_nodata_tif(tmp_path): |
| 51 | + """Write a uint16 TIFF with an out-of-range negative nodata sentinel. |
| 52 | +
|
| 53 | + Uses pytest's ``tmp_path`` rather than ``tempfile.NamedTemporaryFile`` |
| 54 | + so the mmap cache in ``_reader.py`` does not block teardown on Windows |
| 55 | + (open mmaps cannot be unlinked there). |
| 56 | + """ |
53 | 57 | arr = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint16) |
54 | 58 | da = xr.DataArray(arr, dims=['y', 'x']) |
55 | | - with tempfile.NamedTemporaryFile(suffix='.tif', delete=False) as f: |
56 | | - path = f.name |
| 59 | + path = str(tmp_path / 'uint16_neg_nodata.tif') |
57 | 60 | to_geotiff(da, path, crs=4326, nodata=-9999) |
58 | 61 | yield path, arr |
59 | | - os.unlink(path) |
60 | 62 |
|
61 | 63 |
|
62 | 64 | def test_int_nodata_in_range_helper(): |
@@ -118,23 +120,19 @@ def test_read_geotiff_dask_uint16_negative_nodata_compute( |
118 | 120 | np.testing.assert_array_equal(result.values, expected) |
119 | 121 |
|
120 | 122 |
|
121 | | -def test_open_geotiff_uint16_in_range_nodata_still_masks(): |
| 123 | +def test_open_geotiff_uint16_in_range_nodata_still_masks(tmp_path): |
122 | 124 | """The fix doesn't regress the in-range case: uint16 + nodata=65535 |
123 | 125 | still promotes to float64 and masks to NaN.""" |
124 | 126 | arr = np.array([[1, 2, 3], [4, 5, 65535]], dtype=np.uint16) |
125 | 127 | da = xr.DataArray(arr, dims=['y', 'x']) |
126 | | - with tempfile.NamedTemporaryFile(suffix='.tif', delete=False) as f: |
127 | | - path = f.name |
128 | | - try: |
129 | | - to_geotiff(da, path, crs=4326, nodata=65535) |
130 | | - result = open_geotiff(path) |
131 | | - assert result.dtype == np.float64 |
132 | | - # The 65535 pixel should be NaN; the rest unchanged. |
133 | | - assert np.isnan(result.values[1, 2]) |
134 | | - assert result.values[0, 0] == 1 |
135 | | - assert result.attrs.get('nodata') == 65535.0 |
136 | | - finally: |
137 | | - os.unlink(path) |
| 128 | + path = str(tmp_path / 'uint16_in_range_nodata.tif') |
| 129 | + to_geotiff(da, path, crs=4326, nodata=65535) |
| 130 | + result = open_geotiff(path) |
| 131 | + assert result.dtype == np.float64 |
| 132 | + # The 65535 pixel should be NaN; the rest unchanged. |
| 133 | + assert np.isnan(result.values[1, 2]) |
| 134 | + assert result.values[0, 0] == 1 |
| 135 | + assert result.attrs.get('nodata') == 65535.0 |
138 | 136 |
|
139 | 137 |
|
140 | 138 | @_gpu_only |
|
0 commit comments