Skip to content

Commit f8ed356

Browse files
committed
ECC-1790: Python3 bindings: Add codes_get_offset
1 parent f281dee commit f8ed356

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
Changelog for eccodes-python
33
============================
44

5+
1.7.1 (2024-MM-DD)
6+
--------------------
7+
8+
- Add codes_get_offset
9+
510
1.7.0 (2024-02-26)
611
--------------------
12+
713
- ECC-1761: Add function to extract message offsets and sizes
814
- ECC-1742: Add function to clone only the meta-data of a message
915

eccodes/eccodes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
from gribapi import grib_get_message_offset as codes_get_message_offset
7474
from gribapi import grib_get_message_size as codes_get_message_size
7575
from gribapi import grib_get_native_type as codes_get_native_type
76+
from gribapi import grib_get_offset as codes_get_offset
7677
from gribapi import grib_get_size as codes_get_size
7778
from gribapi import grib_get_string as codes_get_string
7879
from gribapi import grib_get_string_array as codes_get_string_array
@@ -263,6 +264,7 @@
263264
"codes_get_message_size",
264265
"codes_get_message",
265266
"codes_get_native_type",
267+
"codes_get_offset",
266268
"codes_get_size",
267269
"codes_get_string_array",
268270
"codes_get_string_length",

gribapi/grib_api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ int grib_nearest_find_multiple(const grib_handle* h, int is_lsm,
100100
double* outlats, double* outlons,
101101
double* values, double* distances, int* indexes);
102102

103+
int grib_get_offset(const grib_handle* h, const char* key, size_t* offset);
103104
int grib_get_size(const grib_handle* h, const char* key,size_t *size);
104105

105106
int grib_get_length(const grib_handle* h, const char* key,size_t *length);

gribapi/gribapi.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,27 @@ def grib_multi_append(ingribid, startsection, multigribid):
578578
GRIB_CHECK(lib.grib_multi_handle_append(h, startsection, mh))
579579

580580

581+
@require(msgid=int, key=str)
582+
def grib_get_offset(msgid, key):
583+
"""
584+
@brief Get the byte offset of a key. If several keys of the same name
585+
are present, the offset of the last one is returned
586+
587+
@param msgid id of the message loaded in memory
588+
@param key name of the key
589+
@exception CodesInternalError
590+
"""
591+
h = get_handle(msgid)
592+
offset_p = ffi.new("size_t*")
593+
err = lib.grib_get_offset(h, key.encode(ENC), offset_p)
594+
GRIB_CHECK(err)
595+
return offset_p[0]
596+
597+
581598
@require(msgid=int, key=str)
582599
def grib_get_size(msgid, key):
583600
"""
584-
@brief Get the size of an array key.
601+
@brief Get the size of a key. Return 1 for scalar keys and >1 for array keys
585602
586603
\b Examples: \ref grib_get_keys.py "grib_get_keys.py",\ref count_messages.py "count_messages.py"
587604

tests/test_eccodes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,14 @@ def test_grib_get_message_offset():
358358
assert eccodes.codes_get_message_offset(gid) == 0
359359

360360

361+
def test_grib_get_key_offset():
362+
gid = eccodes.codes_grib_new_from_samples("GRIB2")
363+
assert eccodes.codes_get_offset(gid, "identifier") == 0
364+
assert eccodes.codes_get_offset(gid, "discipline") == 6
365+
assert eccodes.codes_get_offset(gid, "offsetSection1") == 16
366+
assert eccodes.codes_get_offset(gid, "7777") == 175
367+
368+
361369
def test_grib_clone():
362370
gid = eccodes.codes_grib_new_from_samples("GRIB2")
363371
clone = eccodes.codes_clone(gid)

0 commit comments

Comments
 (0)