codecov-commenter: # [Codecov](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/54?src=pr&el=h1) Report
> Merging [#54](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/54?src=pr&el=desc) into [master](https://codecov.io/gh/dlr-eoc/ukis-pysat/commit/9a5d5f3f649996de5a2b4456ff8341f54b181155&el=desc) will **increase** coverage by `0.04%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/54/graphs/tree.svg?width=650&height=150&src=pr&token=6LYPGRGMXV)](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/54?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master      #54      +/-   ##
==========================================
+ Coverage   93.83%   93.88%   +0.04%     
==========================================
  Files           8        8              
  Lines         714      719       +5     
==========================================
+ Hits          670      675       +5     
  Misses         44       44              
```

| Flag | Coverage Δ | |
|---|---|---|
| #unittests | `93.88% <100.00%> (+0.04%)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/54?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [tests/test\_raster.py](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/54/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9yYXN0ZXIucHk=) | `99.23% <100.00%> (+0.01%)` | :arrow_up: |
| [ukis\_pysat/data.py](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/54/diff?src=pr&el=tree#diff-dWtpc19weXNhdC9kYXRhLnB5) | `87.73% <100.00%> (-0.12%)` | :arrow_down: |
| [ukis\_pysat/raster.py](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/54/diff?src=pr&el=tree#diff-dWtpc19weXNhdC9yYXN0ZXIucHk=) | `94.07% <100.00%> (+0.16%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/54?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/54?src=pr&el=footer). Last update [9a5d5f3...ee0b09e](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/54?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index ac8641f..80f41f0 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -15,6 +15,9 @@ something was changed
 
 [master]  (2020-**-**)
 ----------------------
+Added
+*****
+- `raster.Image()``: With Statement Context Manager for Image #45
 
 [0.5.0]  (2020-07-03)
 ----------------------
diff --git a/ukis_pysat/data.py b/ukis_pysat/data.py
index 25cd42c..60c591b 100644
--- a/ukis_pysat/data.py
+++ b/ukis_pysat/data.py
@@ -127,11 +127,9 @@ class Source:
                         m_cloud_cover = 0
                     if (
                         m_platform == platform.value
-                        and m_date >= start_date
-                        and m_date < end_date
+                        and start_date <= m_date < end_date
                         and m_geom.intersects(geom)
-                        and m_cloud_cover >= min_cloud_cover
-                        and m_cloud_cover < max_cloud_cover
+                        and min_cloud_cover <= m_cloud_cover < max_cloud_cover
                     ):
                         meta_src.append(m)
 
@@ -358,7 +356,7 @@ class Source:
         else:
             pass
 
-    def __exit__(self, exc_type, exc_val, exc_tb):
+    def __exit__(self, exc_type, exc_value, traceback):
         self.close()
 
 
@@ -515,7 +513,3 @@ class MetadataCollection:
 class _GeoInterface(object):
     def __init__(self, d):
         self.__geo_interface__ = d
-
-
-if __name__ == "__main__":
-    pass
diff --git a/ukis_pysat/raster.py b/ukis_pysat/raster.py
index 554151d..89d4476 100644
--- a/ukis_pysat/raster.py
+++ b/ukis_pysat/raster.py
@@ -62,6 +62,9 @@ class Image:
         else:
             raise TypeError("dataset must be of type rasterio.io.DatasetReader, str or np.ndarray")
 
+    def __enter__(self):
+        return self
+
     @property
     def arr(self):
         """array property"""
@@ -414,3 +417,6 @@ class Image:
     def close(self):
         """closes Image"""
         self.dataset.close()
+
+    def __exit__(self, exc_type, exc_value, traceback):
+        self.close()

Dangling rasterio.dataset
We don't enforce closing of the dataset, and we're not even very clear about that you should do this in the documentation (even though we offer the possibility), nor do we close `img` when we're testing.

We could offer using ukis-pysat as context manager or at least document this better and use it ourselves.
diff --git a/tests/test_raster.py b/tests/test_raster.py
index dc4c233..aa80145 100644
--- a/tests/test_raster.py
+++ b/tests/test_raster.py
@@ -26,6 +26,10 @@ class DataTest(unittest.TestCase):
         self.assertTrue(np.array_equal(self.img.arr, img.arr))
         img.close()
 
+    def test_context(self):
+        with Image(TEST_FILE) as raster_file:
+            self.assertTrue(np.array_equal(self.img.arr, raster_file.arr))
+
     def test_init_fail_invalid_path(self):
         with self.assertRaises(TypeError):
             Image(1)

pip install -e .[complete]
parse_log_pytest
requirements.txt
pytest
apt-get update apt-get install -y gcc
3.7
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
affine==2.4.0
appdirs==1.4.4
attrs==24.2.0
certifi @ file:///croot/certifi_1671487769961/work/certifi
chardet==3.0.4
click==7.1.2
click-plugins==1.1.1
cligj==0.7.2
dask==2.14.0
decopatch==1.4.10
exceptiongroup==1.2.2
Fiona==1.8.22
geographiclib==2.0
geojson==3.2.0
geomet==1.1.0
geopy==2.4.1
html2text==2020.1.16
idna==2.10
importlib-metadata==6.7.0
iniconfig==2.0.0
landsatxplore==0.6
makefun==1.15.6
munch==4.0.0
numpy==1.18.5
packaging==24.0
pandas==1.0.5
Pillow==7.1.2
pluggy==1.2.0
pyfields==1.1.5
pylandsat==0.4
pyparsing==3.1.4
pyproj==2.6.1.post1
pytest==7.4.4
python-dateutil==2.8.2
pytz==2025.2
rasterio==1.1.8
requests==2.23.0
rio-mucho==1.0.0
rio-toa==0.3.0
sentinelsat==0.14
Shapely==1.7.1
six==1.17.0
snuggs==1.4.7
tomli==2.0.1
toolz==0.12.1
tqdm==4.67.1
typing_extensions==4.7.1
-e git+https://github.com/dlr-eoc/ukis-pysat.git@9a5d5f3f649996de5a2b4456ff8341f54b181155#egg=ukis_pysat
urllib3==1.25.11
utm==0.5.0
valid8==5.1.2
zipp==3.15.0

name: ukis-pysat
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=22.3.1=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - affine==2.4.0
      - appdirs==1.4.4
      - attrs==24.2.0
      - chardet==3.0.4
      - click==7.1.2
      - click-plugins==1.1.1
      - cligj==0.7.2
      - dask==2.14.0
      - decopatch==1.4.10
      - exceptiongroup==1.2.2
      - fiona==1.8.22
      - geographiclib==2.0
      - geojson==3.2.0
      - geomet==1.1.0
      - geopy==2.4.1
      - html2text==2020.1.16
      - idna==2.10
      - importlib-metadata==6.7.0
      - iniconfig==2.0.0
      - landsatxplore==0.6
      - makefun==1.15.6
      - munch==4.0.0
      - numpy==1.18.5
      - packaging==24.0
      - pandas==1.0.5
      - pillow==7.1.2
      - pluggy==1.2.0
      - pyfields==1.1.5
      - pylandsat==0.4
      - pyparsing==3.1.4
      - pyproj==2.6.1.post1
      - pytest==7.4.4
      - python-dateutil==2.8.2
      - pytz==2025.2
      - rasterio==1.1.8
      - requests==2.23.0
      - rio-mucho==1.0.0
      - rio-toa==0.3.0
      - sentinelsat==0.14
      - shapely==1.7.1
      - six==1.17.0
      - snuggs==1.4.7
      - tomli==2.0.1
      - toolz==0.12.1
      - tqdm==4.67.1
      - typing-extensions==4.7.1
      - urllib3==1.25.11
      - utm==0.5.0
      - valid8==5.1.2
      - zipp==3.15.0
prefix: /opt/conda/envs/ukis-pysat

tests/test_raster.py::DataTest::test_context
tests/test_raster.py::DataTest::test__lookup_bands tests/test_raster.py::DataTest::test_arr tests/test_raster.py::DataTest::test_dimorder_error tests/test_raster.py::DataTest::test_dn2toa tests/test_raster.py::DataTest::test_get_dask_array tests/test_raster.py::DataTest::test_get_subset tests/test_raster.py::DataTest::test_get_tiles tests/test_raster.py::DataTest::test_get_valid_data_bbox tests/test_raster.py::DataTest::test_init tests/test_raster.py::DataTest::test_init_fail_invalid_dataset tests/test_raster.py::DataTest::test_init_fail_invalid_path tests/test_raster.py::DataTest::test_init_with_array tests/test_raster.py::DataTest::test_init_with_arry_fail_missing_crs tests/test_raster.py::DataTest::test_init_with_arry_fail_missing_crs_and_transform tests/test_raster.py::DataTest::test_init_with_arry_fail_missing_transform tests/test_raster.py::DataTest::test_mask_image tests/test_raster.py::DataTest::test_warp tests/test_raster.py::DataTest::test_write_to_file
CHANGELOG.rst ukis_pysat/raster.py ukis_pysat/data.py
CHANGELOG.rst ukis_pysat/raster.py ukis_pysat/data.py