Repository: dlr-eoc/ukis-pysat

Issue:
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.

Discussion:
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).


Patch:
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()


Test patch:
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)