Skip to content

Commit 4ec18e4

Browse files
authored
35 enforce documentation (#40)
* ci: enforce correct documentation * docs: fix code documentation * fix: remove erroneous import
1 parent ee29723 commit 4ec18e4

13 files changed

Lines changed: 210 additions & 21 deletions

.flake8

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[flake8]
2+
docstring-convention = numpy
3+
max-line-length = 120
4+
exclude = tests, .tox

.github/workflows/docstyle.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Check Docstrings
2+
on: [push, pull_request]
3+
4+
jobs:
5+
docstyle:
6+
runs-on: ubuntu-latest
7+
8+
steps:
9+
- uses: actions/checkout@v4
10+
with:
11+
fetch-depth: 0
12+
13+
- name: Set up Python
14+
uses: actions/setup-python@v5
15+
with:
16+
python-version: '3.12'
17+
18+
- name: Install dependencies
19+
run: |
20+
python -m pip install --upgrade pip
21+
pip install flake8 flake8-docstrings
22+
23+
- name: Run flake8
24+
run: flake8 .

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,7 @@ pyrightconfig.json
296296
/.idea/sonarlint.xml
297297

298298
# Large test resources
299-
tests/resources/ASA_IMS_1PNESA20040109_194924_000000182023_00157_09730_0000.N1
299+
tests/resources/ASA_IMS_1PNESA20040109_194924_000000182023_00157_09730_0000.N1
300+
301+
# Pylint
302+
pylint.xml

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ The project uses git lfs for tracking satellite imagery. In order to use git lfs
6161

6262
If you need to track any other large files, execute `git lfs track "*.${extension}"`.
6363

64+
### flak8 documentation
65+
66+
The project actively uses flake8 for documentation and code style checking. Before pushing the changes,
67+
it is highly recommended to run:
68+
```bash
69+
70+
pip install flake8 flake8-docstrings
71+
flake8 .
72+
```
73+
The repository is configured to run flake8 checks on every commit. If the code style is not correct, the commit will be rejected.
74+
75+
If using Pycharm for development, use of _flake8-for-pycharm_ plugin is recommended. Installation instructions can be
76+
found
77+
[here](https://pypi.org/project/flake8-for-pycharm/).
78+
6479
### Issue tracking
6580
When the development on the issue begins, tag _In progress_ should be added to it.
6681

asar_xarray/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""asar_xarray: A library for mapping ASAR/ENVISAT Level-1 products into xarray format."""
2+
3+
__all__: list[str] = [
4+
]

asar_xarray/asar.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""ASAR Xarray Dataset Reader."""
2+
13
import os
24
from typing import Dict, Any
35

@@ -26,30 +28,54 @@ def get_attributes(gdal_dataset: gdal.Dataset) -> Dict[str, Any]:
2628
process_records_metadata(gdal_dataset, attributes)
2729
process_derived_subdatasets_metadata(gdal_dataset, attributes)
2830

29-
3031
attributes['chirp_parameters'] = get_chirp_parameters(gdal_dataset)
3132

3233
return attributes
3334

3435

3536
def open_asar_dataset(filepath: str | os.PathLike[Any] | ReadBuffer[Any] | AbstractDataStore) -> xr.Dataset:
37+
"""
38+
Open an ASAR dataset and converts it into an xarray Dataset.
39+
40+
This function reads the metadata and pixel data from the given ASAR dataset file,
41+
processes the metadata into attributes, and constructs an xarray Dataset.
42+
43+
:param filepath: The path to the ASAR dataset file. It can be a string,
44+
a PathLike object, a ReadBuffer, or an AbstractDataStore.
45+
:return: An xarray Dataset containing the pixel data and metadata attributes.
46+
:raises NotImplementedError: If the filepath is not a string.
47+
"""
3648
if not isinstance(filepath, str):
3749
raise NotImplementedError(f'Filepath type {type(filepath)} is not supported')
50+
3851
logger.debug('Opening ASAR dataset {}', filepath)
52+
53+
# Open the dataset using a custom reader
3954
gdal_dataset: gdal.Dataset = reader.get_gdal_dataset(filepath)
55+
56+
# Extract metadata attributes
4057
attributes = get_attributes(gdal_dataset)
58+
59+
# Read pixel data from the dataset
4160
data = gdal_dataset.ReadAsArray()
42-
dataset: xr.Dataset = xr.Dataset(data_vars={'pixel_values': (('y', 'x'), data)},
43-
coords={
44-
'x': np.arange(data.shape[1]),
45-
'y': np.arange(data.shape[0])
46-
}, attrs=attributes)
61+
62+
# Create an xarray Dataset with pixel data and metadata attributes
63+
dataset: xr.Dataset = xr.Dataset(
64+
data_vars={'pixel_values': (('y', 'x'), data)},
65+
coords={
66+
'x': np.arange(data.shape[1]),
67+
'y': np.arange(data.shape[0])
68+
},
69+
attrs=attributes
70+
)
71+
4772
return dataset
4873

4974

5075
def get_chirp_parameters(dataset: gdal.Dataset) -> dict[str, Any]:
5176
"""
52-
Parses dataset metadata for chirp parameters.
77+
Parse dataset metadata for chirp parameters.
78+
5379
:param dataset: ASAR dataset.
5480
:return: dictionary with chirp parameters.
5581
"""

asar_xarray/derived_subdatasets_metadata.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Process derived subdatasets metadata from GDAL dataset."""
2+
13
from typing import Any
24

35
from osgeo import gdal

asar_xarray/general_metadata.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""General metadata processing for ASAR datasets."""
2+
13
from datetime import datetime
24
import re
35
from typing import Any
@@ -9,6 +11,18 @@
911

1012

1113
def process_general_metadata(dataset: gdal.Dataset, attributes: dict[str, Any]) -> None:
14+
"""
15+
Process general metadata from a GDAL dataset and update the attributes dictionary.
16+
17+
This function retrieves metadata from the provided GDAL dataset and processes it
18+
using three helper functions: `process_mph_metadata`, `process_sph_metadata`, and
19+
`process_ds_metadata`. The processed metadata is then added to the `attributes`
20+
dictionary.
21+
22+
:param dataset: A GDAL dataset object from which metadata is retrieved.
23+
:param attributes: A dictionary to be updated with processed metadata.
24+
:return: None
25+
"""
1226
metadata = dataset.GetMetadata(domain='')
1327
attributes.update(process_mph_metadata(metadata))
1428
attributes.update(process_sph_metadata(metadata))
@@ -18,6 +32,7 @@ def process_general_metadata(dataset: gdal.Dataset, attributes: dict[str, Any])
1832
def process_mph_metadata(metadata: dict[str, str]) -> dict[str, Any]:
1933
"""
2034
Process MPH metadata by removing 'MPH_' prefix and converting values to appropriate types.
35+
2136
Uses numpy.datetime64 for datetime values.
2237
2338
:param metadata: Dictionary with MPH metadata
@@ -67,6 +82,7 @@ def process_mph_metadata(metadata: dict[str, str]) -> dict[str, Any]:
6782
def process_sph_metadata(metadata: dict[str, str]) -> dict[str, Any]:
6883
"""
6984
Process SPH metadata by removing 'SPH_' prefix and converting values to appropriate types.
85+
7086
Uses numpy.datetime64 for datetime values.
7187
7288
:param metadata: Dictionary with SPH metadata

asar_xarray/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
"""Shows how to use the asar xarray plugin."""
2+
13
import xarray as xr
24

35
if __name__ == "__main__":
46
# Example usage
57
test_file = ('../tests/resources'
68
'/ASA_IMS_1PNESA20040109_194924_000000182023_00157_09730_0000.N1')
79
dataset = xr.open_dataset(test_file, engine='asar')
8-
# print(dataset)
9-
print(dataset.attrs)
10+
print(dataset)

asar_xarray/reader.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
from osgeo import gdal, ogr
1+
"""Reading GDAL dataset."""
2+
3+
from osgeo import gdal
24

35
gdal.UseExceptions()
46

57

68
def get_gdal_dataset(filepath: str) -> gdal.Dataset:
79
"""
8-
Reads file into gdal dataset.
10+
Read file into gdal dataset.
911
1012
:param filepath: File to be read
1113
:return: Opened dataset
1214
"""
13-
1415
dataset: gdal.Dataset = gdal.Open(filepath)
1516
if not dataset:
1617
raise RuntimeError(f'Could not open file: {filepath}')

0 commit comments

Comments
 (0)