Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/pymetkit #56

Merged
merged 23 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c73ded1
Create README.md
jinmannwong Jan 25, 2024
d5dd9c0
Initial Python interface implementation
jinmannwong Jan 24, 2024
7fecdbc
Modify api to accept string or file-like object
jinmannwong Jan 25, 2024
681cdc7
Add README and comments
jinmannwong Jan 25, 2024
1a713d9
Add required python version to pyproject.toml
jinmannwong Jan 25, 2024
396c50f
Refactor and add functionality to create request from scratch
jinmannwong Feb 9, 2024
5486cd8
Add method to validate request
jinmannwong Feb 20, 2024
cdb8728
Add method to merge requests and related tests
jinmannwong Feb 20, 2024
92bb89c
Review comments
jinmannwong Feb 21, 2024
1ef7c05
Add check for equality and tests
jinmannwong Mar 19, 2024
2968806
Add 'pymetkit/' from commit '1ef7c05d9b7f94a17918cbc29eba84e3731f209a'
ChrisspyB Dec 9, 2024
47681bb
Merge from bitbucket branch 'feature/metkit-python' into feature/pyme…
ChrisspyB Dec 9, 2024
43b41d3
Merge branch 'feature/c-api' into feature/pymetkit
ChrisspyB Feb 24, 2025
20100c4
Update pymetkit to reflect C API changes
ChrisspyB Feb 24, 2025
95da9f5
Merge branch 'feature/c-api' into feature/pymetkit
ChrisspyB Mar 4, 2025
e62548f
Restructure python project
ChrisspyB Mar 12, 2025
56d2d4e
Small change
ChrisspyB Mar 19, 2025
8ae0b12
Merge branch 'feature/c-api' into feature/pymetkit
ChrisspyB Mar 19, 2025
5f4c960
Rename Request to MarsRequest
ChrisspyB Mar 19, 2025
24800af
Merge branch 'feature/c-api' into feature/pymetkit
ChrisspyB Mar 25, 2025
ddf611d
Temporarily disable some strict expansion tests
ChrisspyB Mar 25, 2025
ea8ba6d
Merge branch 'develop' into feature/pymetkit
ChrisspyB Mar 26, 2025
232fd48
Merge branch 'develop' into feature/pymetkit
ChrisspyB Mar 27, 2025
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
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,14 @@ doc/latex
.*.sw*
.*un~
*.ccls-cache
*.py[cod]
*.swp
**/__pycache__
.vscode
*.egg-info
.pytest_cache
*.prof
*.idx
.mypy_cache
build/
Testing/
28 changes: 28 additions & 0 deletions pymetkit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# pymetkit

This repository contains an Python interface to the MetKit library for parsing MARS requests.

## Example

The function for parsing a MARS request is `metkit.parse_mars_request` which accepts a string or file-like object
as inputs. A list of `metkit.mars.Request` instances are returned, which is a dictionary containing the keys and
values in the MARS request and the attribute `verb` for the verb in the MARS request.

### From String
```
from metkit import parse_mars_request

request_str = "retrieve,class=od,date=20240124,time=12,param=129,step=12,target=test.grib"
requests = parse_mars_request(requests)

print(requests[0])
# verb: retrieve, request: {'class': ['od'], 'date': ['20240124'], 'time': ['1200'], 'param': ['129'], 'step': ['12'], 'target': ['test.grib'], 'domain': ['g'], 'expver': ['0001'], 'levelist': ['1000', '850', '700', '500', '400', '300'], 'levtype': ['pl'], 'stream': ['oper'], 'type': ['an']}
```

### From File
If the MARS request is contained inside a file, e.g. test_requests.txt:
```
from metkit import parse_mars_request

requests = parse_mars_request(open("test_requests.txt", "r"))
```
1 change: 1 addition & 0 deletions pymetkit/src/pymetkit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .pymetkit import *
5 changes: 5 additions & 0 deletions pymetkit/src/pymetkit/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pathlib import Path
from .pymetkit import *
import importlib.metadata

__version__ = importlib.metadata.version("pymetkit")
51 changes: 51 additions & 0 deletions pymetkit/src/pymetkit/metkit_c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

struct metkit_marsrequest_t;
typedef struct metkit_marsrequest_t metkit_marsrequest_t;
struct metkit_requestiterator_t;
typedef struct metkit_requestiterator_t metkit_requestiterator_t;
struct metkit_paramiterator_t;
typedef struct metkit_paramiterator_t metkit_paramiterator_t;

typedef enum metkit_error_values_t
{
METKIT_SUCCESS = 0, /* Operation succeded. */
METKIT_ERROR = 1, /* Operation failed. */
METKIT_ERROR_UNKNOWN = 2, /* Failed with an unknown error. */
METKIT_ERROR_USER = 3, /* Failed with an user error. */
METKIT_ERROR_ASSERT = 4 /* Failed with an assert() */
} metkit_error_t;

typedef enum metkit_iterator_status_t
{
METKIT_ITERATOR_SUCCESS = 0, /* Operation succeded. */
METKIT_ITERATOR_COMPLETE = 1, /* All elements have been returned */
METKIT_ITERATOR_ERROR = 2 /* Operation failed. */
} metkit_iterator_status_t;


const char* metkit_get_error_string(enum metkit_error_values_t err);
const char* metkit_version();
const char* metkit_git_sha1();
metkit_error_t metkit_initialise();

metkit_error_t metkit_parse_marsrequests(const char* str, metkit_requestiterator_t** requests, bool strict);
metkit_error_t metkit_marsrequest_new(metkit_marsrequest_t** request);
metkit_error_t metkit_marsrequest_delete(const metkit_marsrequest_t* request);
metkit_error_t metkit_marsrequest_set(metkit_marsrequest_t* request, const char* param, const char* values[], int numValues);
metkit_error_t metkit_marsrequest_set_one(metkit_marsrequest_t* request, const char* param, const char* value);
metkit_error_t metkit_marsrequest_set_verb(metkit_marsrequest_t* request, const char* verb);
metkit_error_t metkit_marsrequest_verb(const metkit_marsrequest_t* request, const char** verb);
metkit_error_t metkit_marsrequest_has_param(const metkit_marsrequest_t* request, const char* param, bool* has);
metkit_error_t metkit_marsrequest_params(const metkit_marsrequest_t* request, metkit_paramiterator_t** params);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not convinced you need a params iterator, as opposed to just returning them by count/index in the same way as you do for the values?

Copy link
Member Author

@ChrisspyB ChrisspyB Mar 24, 2025

Choose a reason for hiding this comment

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

With the existing C++ implementation, it is not possible to return the params without making a copy (they internally stored in a linked list), unlike the values, so they are less suitable for accessing by index and count

metkit_error_t metkit_marsrequest_count_values(const metkit_marsrequest_t* request, const char* param, size_t* count);
metkit_error_t metkit_marsrequest_value(const metkit_marsrequest_t* request, const char* param, int index, const char** value);
metkit_error_t metkit_marsrequest_expand(const metkit_marsrequest_t* request, bool inherit, bool strict, metkit_marsrequest_t* expandedRequest);
metkit_error_t metkit_marsrequest_merge(metkit_marsrequest_t* request, const metkit_marsrequest_t* otherRequest);

metkit_error_t metkit_requestiterator_delete(const metkit_requestiterator_t* it);
metkit_iterator_status_t metkit_requestiterator_next(metkit_requestiterator_t* it);
metkit_iterator_status_t metkit_requestiterator_current(metkit_requestiterator_t* it, metkit_marsrequest_t* request);

metkit_error_t metkit_paramiterator_delete(const metkit_paramiterator_t* it);
metkit_iterator_status_t metkit_paramiterator_next(metkit_paramiterator_t* it);
metkit_iterator_status_t metkit_paramiterator_current(const metkit_paramiterator_t* it, const char** param);
Copy link
Contributor

Choose a reason for hiding this comment

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

If you're going to have a parameter iterator, at least make it able to return the number of values and the values as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

See other comment

Loading
Loading