diff --git a/README.md b/README.md index fc4d195..de3c47d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) +``` +
+ +Output of the print + +``` +(42, 10) +``` + +
## Development Pre-requisite: Install podman or docker in your machine. diff --git a/metoppy/__init__.py b/metoppy/__init__.py index c239af3..73f4223 100644 --- a/metoppy/__init__.py +++ b/metoppy/__init__.py @@ -11,7 +11,7 @@ """Metoppy package initialization.""" # Optional: version info -__version__ = "0.1.0" +__version__ = "0.1.1" # Import main classes/functions from .metopreader import MetopReader diff --git a/metoppy/metopreader.py b/metoppy/metopreader.py index 841bfc4..49f3255 100644 --- a/metoppy/metopreader.py +++ b/metoppy/metopreader.py @@ -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.""" @@ -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) diff --git a/metoppy/tests/test_basic_interface.py b/metoppy/tests/test_basic_interface.py index 2428967..8ecd08b 100644 --- a/metoppy/tests/test_basic_interface.py +++ b/metoppy/tests/test_basic_interface.py @@ -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) @@ -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) @@ -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) @@ -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'] @@ -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] @@ -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']) @@ -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) @@ -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 @@ -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] @@ -216,4 +216,29 @@ def test_no_auto_convert(metop_reader, test_file): assert start_time.millisecond == 73275381 # clean - metop_reader.close_dataset(ds) \ No newline at end of file + 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] + \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index bf77137..1e537ca 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "metoppy" -version = "0.1.0" +version = "0.1.1" 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" },