diff --git a/CHANGELOG.rst b/CHANGELOG.rst index eea584f..26b37f2 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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) -------------------- diff --git a/gribapi/bindings.py b/gribapi/bindings.py index 0d1f98e..32fdc34 100644 --- a/gribapi/bindings.py +++ b/gribapi/bindings.py @@ -21,7 +21,7 @@ import cffi -__version__ = "1.6.1" +__version__ = "1.7.0" LOG = logging.getLogger(__name__) diff --git a/gribapi/grib_api.h b/gribapi/grib_api.h index 947788d..e0df7b9 100644 --- a/gribapi/grib_api.h +++ b/gribapi/grib_api.h @@ -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; @@ -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); @@ -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); diff --git a/gribapi/gribapi.py b/gribapi/gribapi.py index 257a45a..ab1cffe 100644 --- a/gribapi/gribapi.py +++ b/gribapi/gribapi.py @@ -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. @@ -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) diff --git a/tests/test_eccodes.py b/tests/test_eccodes.py index 7443ada..e959277 100644 --- a/tests/test_eccodes.py +++ b/tests/test_eccodes.py @@ -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")