Skip to content

Commit

Permalink
Add clone with headers_only option
Browse files Browse the repository at this point in the history
  • Loading branch information
shahramn committed Jan 9, 2024
1 parent 56cdb7c commit 55a01ff
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog for eccodes-python
============================

1.7.0 (2024-MM-DD)
--------------------

- Add clone with headers only

1.6.1 (2023-10-02)
--------------------

Expand Down
2 changes: 1 addition & 1 deletion gribapi/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import cffi

__version__ = "1.6.1"
__version__ = "1.7.0"

LOG = logging.getLogger(__name__)

Expand Down
7 changes: 4 additions & 3 deletions gribapi/grib_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct grib_values {
int has_value;
int equal;
grib_values* next;
} ;
};

typedef struct grib_handle grib_handle;
typedef struct grib_multi_handle grib_multi_handle;
Expand Down Expand Up @@ -74,7 +74,8 @@ int grib_count_in_file(grib_context* c, FILE* f,int* n);
grib_handle* grib_handle_new_from_file(grib_context* c, FILE* f, int* error);
grib_handle* grib_handle_new_from_message_copy(grib_context* c, const void* data, size_t data_len);
grib_handle* grib_handle_new_from_samples (grib_context* c, const char* sample_name);
grib_handle* grib_handle_clone(const grib_handle* h) ;
grib_handle* grib_handle_clone(const grib_handle* h);
grib_handle* grib_handle_clone_headers_only(const grib_handle* h);
int grib_handle_delete(grib_handle* h);
grib_multi_handle* grib_multi_handle_new(grib_context* c);
int grib_multi_handle_append(grib_handle* h,int start_section,grib_multi_handle* mh);
Expand Down Expand Up @@ -125,7 +126,7 @@ void grib_dump_content(const grib_handle* h, FILE* out, const char* mode, unsign
grib_context* grib_context_get_default(void);
void grib_context_delete(grib_context* c);

void grib_gts_header_on(grib_context* c) ;
void grib_gts_header_on(grib_context* c);
void grib_gts_header_off(grib_context* c);
void grib_gribex_mode_on(grib_context* c);
void grib_gribex_mode_off(grib_context* c);
Expand Down
12 changes: 8 additions & 4 deletions gribapi/gribapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ def codes_bufr_copy_data(msgid_src, msgid_dst):


@require(msgid_src=int)
def grib_clone(msgid_src):
def grib_clone(msgid_src, headers_only=False):
r"""
@brief Create a copy of a message.
Expand All @@ -1130,12 +1130,16 @@ def grib_clone(msgid_src):
\b Examples: \ref grib_clone.py "grib_clone.py"
@param msgid_src id of message to be cloned
@return id of clone
@param msgid_src id of message to be cloned
@param headers_only whether or not to load the message with the headers only
@return id of clone
@exception CodesInternalError
"""
h_src = get_handle(msgid_src)
h_dest = lib.grib_handle_clone(h_src)
if headers_only:
h_dest = lib.grib_handle_clone_headers_only(h_src)
else:
h_dest = lib.grib_handle_clone(h_src)
if h_dest == ffi.NULL:
raise errors.MessageInvalidError("clone failed")
return put_handle(h_dest)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_eccodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,18 @@ def test_grib_clone():
eccodes.codes_release(clone)


def test_grib_clone_headers_only():
with open(TEST_GRIB_ERA5_DATA, "rb") as f:
msgid1 = eccodes.codes_grib_new_from_file(f)
msgid2 = eccodes.codes_clone(msgid1, headers_only=True)
assert eccodes.codes_get(msgid1, "totalLength") == 14752
assert eccodes.codes_get(msgid2, "totalLength") == 112
assert eccodes.codes_get(msgid1, "bitsPerValue") == 16
assert eccodes.codes_get(msgid2, "bitsPerValue") == 0
eccodes.codes_release(msgid1)
eccodes.codes_release(msgid2)


def test_grib_keys_iterator():
gid = eccodes.codes_grib_new_from_samples("reduced_gg_pl_1280_grib1")
iterid = eccodes.codes_keys_iterator_new(gid, "ls")
Expand Down

0 comments on commit 55a01ff

Please sign in to comment.