Skip to content

Commit 55a01ff

Browse files
committed
Add clone with headers_only option
1 parent 56cdb7c commit 55a01ff

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

CHANGELOG.rst

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

5+
1.7.0 (2024-MM-DD)
6+
--------------------
7+
8+
- Add clone with headers only
9+
510
1.6.1 (2023-10-02)
611
--------------------
712

gribapi/bindings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import cffi
2323

24-
__version__ = "1.6.1"
24+
__version__ = "1.7.0"
2525

2626
LOG = logging.getLogger(__name__)
2727

gribapi/grib_api.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct grib_values {
3838
int has_value;
3939
int equal;
4040
grib_values* next;
41-
} ;
41+
};
4242

4343
typedef struct grib_handle grib_handle;
4444
typedef struct grib_multi_handle grib_multi_handle;
@@ -74,7 +74,8 @@ int grib_count_in_file(grib_context* c, FILE* f,int* n);
7474
grib_handle* grib_handle_new_from_file(grib_context* c, FILE* f, int* error);
7575
grib_handle* grib_handle_new_from_message_copy(grib_context* c, const void* data, size_t data_len);
7676
grib_handle* grib_handle_new_from_samples (grib_context* c, const char* sample_name);
77-
grib_handle* grib_handle_clone(const grib_handle* h) ;
77+
grib_handle* grib_handle_clone(const grib_handle* h);
78+
grib_handle* grib_handle_clone_headers_only(const grib_handle* h);
7879
int grib_handle_delete(grib_handle* h);
7980
grib_multi_handle* grib_multi_handle_new(grib_context* c);
8081
int grib_multi_handle_append(grib_handle* h,int start_section,grib_multi_handle* mh);
@@ -125,7 +126,7 @@ void grib_dump_content(const grib_handle* h, FILE* out, const char* mode, unsign
125126
grib_context* grib_context_get_default(void);
126127
void grib_context_delete(grib_context* c);
127128

128-
void grib_gts_header_on(grib_context* c) ;
129+
void grib_gts_header_on(grib_context* c);
129130
void grib_gts_header_off(grib_context* c);
130131
void grib_gribex_mode_on(grib_context* c);
131132
void grib_gribex_mode_off(grib_context* c);

gribapi/gribapi.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ def codes_bufr_copy_data(msgid_src, msgid_dst):
11211121

11221122

11231123
@require(msgid_src=int)
1124-
def grib_clone(msgid_src):
1124+
def grib_clone(msgid_src, headers_only=False):
11251125
r"""
11261126
@brief Create a copy of a message.
11271127
@@ -1130,12 +1130,16 @@ def grib_clone(msgid_src):
11301130
11311131
\b Examples: \ref grib_clone.py "grib_clone.py"
11321132
1133-
@param msgid_src id of message to be cloned
1134-
@return id of clone
1133+
@param msgid_src id of message to be cloned
1134+
@param headers_only whether or not to load the message with the headers only
1135+
@return id of clone
11351136
@exception CodesInternalError
11361137
"""
11371138
h_src = get_handle(msgid_src)
1138-
h_dest = lib.grib_handle_clone(h_src)
1139+
if headers_only:
1140+
h_dest = lib.grib_handle_clone_headers_only(h_src)
1141+
else:
1142+
h_dest = lib.grib_handle_clone(h_src)
11391143
if h_dest == ffi.NULL:
11401144
raise errors.MessageInvalidError("clone failed")
11411145
return put_handle(h_dest)

tests/test_eccodes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,18 @@ def test_grib_clone():
350350
eccodes.codes_release(clone)
351351

352352

353+
def test_grib_clone_headers_only():
354+
with open(TEST_GRIB_ERA5_DATA, "rb") as f:
355+
msgid1 = eccodes.codes_grib_new_from_file(f)
356+
msgid2 = eccodes.codes_clone(msgid1, headers_only=True)
357+
assert eccodes.codes_get(msgid1, "totalLength") == 14752
358+
assert eccodes.codes_get(msgid2, "totalLength") == 112
359+
assert eccodes.codes_get(msgid1, "bitsPerValue") == 16
360+
assert eccodes.codes_get(msgid2, "bitsPerValue") == 0
361+
eccodes.codes_release(msgid1)
362+
eccodes.codes_release(msgid2)
363+
364+
353365
def test_grib_keys_iterator():
354366
gid = eccodes.codes_grib_new_from_samples("reduced_gg_pl_1280_grib1")
355367
iterid = eccodes.codes_keys_iterator_new(gid, "ls")

0 commit comments

Comments
 (0)