Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ reduced_data_folder = Path(reduced_data_folder)
reduced_data_files = [f for f in reduced_data_folder.iterdir() if f.is_file()]

test_file_name = next((s for s in reduced_data_files if s.name.startswith("ASCA_SZO")))
test_file_path = reduced_data_folder / test_file_name
ds = metop_reader.open_dataset(file_path=str(test_file_path), maskingvalue = float("nan"))
test_file_path = str(reduced_data_folder / test_file_name)
ds = metop_reader.open_dataset(file_path=test_file_path, maskingvalue = float("nan"))
```

2. Check keys
Expand Down Expand Up @@ -175,6 +175,26 @@ print(longitude_slice_np)
metop_reader.close_dataset(ds)
```

8. It is recommended to use the `with` syntax to automaticlly close the file

```python
latitude = None

with metop_reader.dataset(test_file_path, maskingvalue = float("nan")) as ds:
latitude = metop_reader.as_array(ds['latitude'])
latitude = np.array(latitude, copy = None)

print(latitude.shape)
```
<details>

<summary>Output of the print </summary>

```
(42, 10)
```

</details>
## Development

Pre-requisite: Install podman or docker in your machine.
Expand Down
2 changes: 1 addition & 1 deletion metoppy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"""Metoppy package initialization."""

# Optional: version info
__version__ = "0.1.0"
__version__ = "0.1.1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

according to standard https://semver.org/ this should be 0.2.0 if we are introducing new funcionality.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have to do it like that since we are still in version 0.y.z

  1. Major version zero (0.y.z) is for initial development. Anything MAY change at any time. The public API SHOULD NOT be considered stable.

The julia package manager uses a slightly different approach to 0.y.z versions where z is incremented for non-breaking changes and y is incremented for breaking changes.
https://pkgdocs.julialang.org/v1/compatibility/#compat-pre-1.0

So if you think 0.2.0 is more pythonic then I will change it but we don't have to do it according to semver.


# Import main classes/functions
from .metopreader import MetopReader
Expand Down
22 changes: 21 additions & 1 deletion metoppy/metopreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"""MetopDatasets.jl Python wrapper class: MetopReader."""

from juliacall import Main

from contextlib import contextmanager

class MetopReader:
"""Python wrapper class for MetopDatasets.jl Julia package."""
Expand Down Expand Up @@ -129,3 +129,23 @@ def get_test_data_artifact(self):
A MetopDataset object containing test data for validation or demo purposes.
"""
return self._get_test_data_artifact()

@contextmanager
def dataset(self, file_path: str, **kwargs):
"""
Open a dataset using the python `with` syntax. The method opens the file using `open_dataset()` and
then finally closes the file using `close_dataset()` once the excution of the `with` block is completed.

Example
----------
::
metop_reader = MetopReader()
with metop_reader.dataset("path to a file") as ds:
# read some data

"""
ds = self.open_dataset(file_path, **kwargs)
try:
yield ds
finally:
self.close_dataset(ds)
45 changes: 35 additions & 10 deletions metoppy/tests/test_basic_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_file(request, metop_reader):
test_file_name = next((f for f in reduced_data_files if f.name.startswith(product_type)), None)
test_file_path = reduced_data_folder / test_file_name

return test_file_path
return str(test_file_path)


@pytest.mark.parametrize("test_file", ["ASCA_SZO"], indirect=True)
Expand All @@ -45,7 +45,7 @@ def test_get_keys(metop_reader, test_file):
Simple test for metop_reader.get_key
"""
# arrange
ds = metop_reader.open_dataset(file_path=str(test_file))
ds = metop_reader.open_dataset(test_file)

# act
keys = metop_reader.get_keys(ds)
Expand All @@ -69,7 +69,7 @@ def test_close_dataset(metop_reader, test_file):
"""
# arrange
import juliacall
ds = metop_reader.open_dataset(file_path=str(test_file))
ds = metop_reader.open_dataset(test_file)

# act
metop_reader.close_dataset(ds)
Expand All @@ -85,7 +85,7 @@ def test_shape(metop_reader, test_file):
Simple test for metop_reader.shape.
"""
# arrange
ds = metop_reader.open_dataset(file_path=str(test_file))
ds = metop_reader.open_dataset(test_file)

# act
latitude = ds['latitude']
Expand All @@ -110,7 +110,7 @@ def test_read_single_value(metop_reader, test_file):
"""
# arrange
import datetime
ds = metop_reader.open_dataset(file_path=str(test_file))
ds = metop_reader.open_dataset(test_file)

# act
CO2_radiance = ds["gs1cspect"][91, 0, 0, 0]
Expand All @@ -137,7 +137,7 @@ def test_read_array(metop_reader, test_file):
"""
# arrange
import numpy as np
ds = metop_reader.open_dataset(file_path=str(test_file))
ds = metop_reader.open_dataset(test_file)

# act
latitude_julia = metop_reader.as_array(ds['latitude'])
Expand Down Expand Up @@ -165,7 +165,7 @@ def test_type_stable_array(metop_reader, test_file):
"""
# arrange
import numpy as np
ds = metop_reader.open_dataset(file_path=str(test_file), maskingvalue = float("nan"))
ds = metop_reader.open_dataset(test_file, maskingvalue = float("nan"))

# act
latitude = np.array(metop_reader.as_array(ds['latitude']), copy = None)
Expand All @@ -186,7 +186,7 @@ def test_different_file_types(metop_reader, test_file):
Test that different types of test files can be opened.
"""
# act
ds = metop_reader.open_dataset(file_path=str(test_file))
ds = metop_reader.open_dataset(test_file)

# assert
assert ds is not None
Expand All @@ -204,7 +204,7 @@ def test_no_auto_convert(metop_reader, test_file):
"""
# arrange
from juliacall import Main as jl
ds = metop_reader.open_dataset(file_path=str(test_file), auto_convert=False)
ds = metop_reader.open_dataset(test_file, auto_convert=False)

# act
start_time = ds["record_start_time"][2]
Expand All @@ -216,4 +216,29 @@ def test_no_auto_convert(metop_reader, test_file):
assert start_time.millisecond == 73275381

# clean
metop_reader.close_dataset(ds)
metop_reader.close_dataset(ds)


@pytest.mark.parametrize("test_file", ["ASCA_SZO"], indirect=True)
def test_with_dataset(metop_reader, test_file):
"""
Test using `with` syntax to read data from a data set.
"""
# arrange
import numpy as np
import juliacall
ds_place_holder = None
latitude = None

# act
with metop_reader.dataset(test_file, maskingvalue = float("nan")) as ds:
latitude_julia = metop_reader.as_array(ds['latitude'])
latitude = np.array(latitude_julia, copy = None)
ds_place_holder = ds

# assert
assert np.all((-90 < latitude)&(latitude < 90)) # check values
assert latitude.shape == (42,10) # check size
with pytest.raises(juliacall.JuliaError): # check that file is closed.
ds_place_holder['longitude'][0,0]

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "metoppy"
version = "0.1.0"
version = "0.1.1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

according to standard https://semver.org/ this should be 0.2.0 if we are introducing new functionality.

description = "Python package wrapper of MetopDatasets.jl Julia package for reading products from the METOP satellites."
authors = [
{ name = "Simon Kok Lupemba", email = "simon.koklupemba@eumetsat.int" },
Expand Down