From f50d2c5884710844494013df5d01c8d8ae61996f Mon Sep 17 00:00:00 2001 From: shahramn Date: Thu, 8 Aug 2024 13:37:07 +0100 Subject: [PATCH 1/3] ECC-1858: Allow user to control encoding via optimizeScaleFactor (by default 0) --- src/accessor/grib_accessor_class_data_g22order_packing.cc | 7 +++++-- tests/grib_bitmap.sh | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/accessor/grib_accessor_class_data_g22order_packing.cc b/src/accessor/grib_accessor_class_data_g22order_packing.cc index aefef16c8..c4949e89e 100644 --- a/src/accessor/grib_accessor_class_data_g22order_packing.cc +++ b/src/accessor/grib_accessor_class_data_g22order_packing.cc @@ -786,7 +786,7 @@ int grib_accessor_class_data_g22order_packing_t::pack_double(grib_accessor* a, c // long nvals_per_group = 0; // long nbits_per_group_val = 0; - long binary_scale_factor, decimal_scale_factor, typeOfOriginalFieldValues; + long binary_scale_factor, decimal_scale_factor, typeOfOriginalFieldValues, optimize_scale_factor; // long groupSplittingMethodUsed, numberOfGroupsOfDataValues, referenceForGroupWidths; long missingValueManagementUsed, primaryMissingValueSubstitute, secondaryMissingValueSubstitute; long numberOfBitsUsedForTheGroupWidths, numberOfBitsUsedForTheScaledGroupLengths, orderOfSpatialDifferencing; @@ -815,7 +815,10 @@ int grib_accessor_class_data_g22order_packing_t::pack_double(grib_accessor* a, c if ((err = grib_get_long_internal(gh, self->decimal_scale_factor, &decimal_scale_factor)) != GRIB_SUCCESS) return err; - int use_scale = 1; + if ((err = grib_get_long_internal(gh, self->optimize_scale_factor, &optimize_scale_factor)) != GRIB_SUCCESS) + return err; + + int use_scale = !optimize_scale_factor; if ((err = grib_get_long_internal(gh, self->binary_scale_factor, &binary_scale_factor)) != GRIB_SUCCESS) return err; diff --git a/tests/grib_bitmap.sh b/tests/grib_bitmap.sh index 36ba2cf4d..e78ccc897 100755 --- a/tests/grib_bitmap.sh +++ b/tests/grib_bitmap.sh @@ -127,6 +127,8 @@ ${tools_dir}/grib_copy -r $infile $temp2 ${tools_dir}/grib_compare -R all=0.3 -c data:n $infile $temp2 grib_check_key_equals $temp2 bitsPerValue 9 # Note: The input file has bpv=9 +${tools_dir}/grib_set -rs optimizeScaleFactor=1 $infile $temp2 +${tools_dir}/grib_compare $infile $temp2 # Simple to grid_complex tempComplex=temp.grib_bitmap.complex.grib From f5b8a9b7f13310e52cb2c50e4b943a840ae60c7c Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Thu, 8 Aug 2024 16:14:03 +0000 Subject: [PATCH 2/3] ECC-1894: GRIB2: Transfer encoding from 240016 to 26 --- definitions/grib2/localConcepts/ecmf/name.def | 6 ------ definitions/grib2/localConcepts/ecmf/paramId.def | 6 ------ definitions/grib2/localConcepts/ecmf/shortName.def | 6 ------ definitions/grib2/localConcepts/ecmf/units.def | 6 ------ definitions/grib2/name.def | 12 ++++++------ definitions/grib2/paramId.def | 12 ++++++------ definitions/grib2/shortName.def | 12 ++++++------ definitions/grib2/units.def | 12 ++++++------ 8 files changed, 24 insertions(+), 48 deletions(-) diff --git a/definitions/grib2/localConcepts/ecmf/name.def b/definitions/grib2/localConcepts/ecmf/name.def index f2d776220..1e390741a 100644 --- a/definitions/grib2/localConcepts/ecmf/name.def +++ b/definitions/grib2/localConcepts/ecmf/name.def @@ -115,12 +115,6 @@ parameterCategory = 128 ; parameterNumber = 25 ; } -#Lake cover -'Lake cover' = { - discipline = 192 ; - parameterCategory = 128 ; - parameterNumber = 26 ; - } #Snow albedo 'Snow albedo' = { localTablesVersion = 1 ; diff --git a/definitions/grib2/localConcepts/ecmf/paramId.def b/definitions/grib2/localConcepts/ecmf/paramId.def index 705e33a26..ab184ae4d 100644 --- a/definitions/grib2/localConcepts/ecmf/paramId.def +++ b/definitions/grib2/localConcepts/ecmf/paramId.def @@ -115,12 +115,6 @@ parameterCategory = 128 ; parameterNumber = 25 ; } -#Lake cover -'26' = { - discipline = 192 ; - parameterCategory = 128 ; - parameterNumber = 26 ; - } #Snow albedo '32' = { localTablesVersion = 1 ; diff --git a/definitions/grib2/localConcepts/ecmf/shortName.def b/definitions/grib2/localConcepts/ecmf/shortName.def index 78f8f2fe9..e5fa627d0 100644 --- a/definitions/grib2/localConcepts/ecmf/shortName.def +++ b/definitions/grib2/localConcepts/ecmf/shortName.def @@ -115,12 +115,6 @@ parameterCategory = 128 ; parameterNumber = 25 ; } -#Lake cover -'cl' = { - discipline = 192 ; - parameterCategory = 128 ; - parameterNumber = 26 ; - } #Snow albedo 'asn' = { localTablesVersion = 1 ; diff --git a/definitions/grib2/localConcepts/ecmf/units.def b/definitions/grib2/localConcepts/ecmf/units.def index 4f611fb61..7931fee09 100644 --- a/definitions/grib2/localConcepts/ecmf/units.def +++ b/definitions/grib2/localConcepts/ecmf/units.def @@ -115,12 +115,6 @@ parameterCategory = 128 ; parameterNumber = 25 ; } -#Lake cover -'(0 - 1)' = { - discipline = 192 ; - parameterCategory = 128 ; - parameterNumber = 26 ; - } #Snow albedo '(0 - 1)' = { localTablesVersion = 1 ; diff --git a/definitions/grib2/name.def b/definitions/grib2/name.def index e7ff266d0..9db74ef7c 100644 --- a/definitions/grib2/name.def +++ b/definitions/grib2/name.def @@ -181,6 +181,12 @@ parameterCategory = 2 ; parameterNumber = 45 ; } +#Lake cover +'Lake cover' = { + discipline = 1 ; + parameterCategory = 2 ; + parameterNumber = 2 ; + } #Low vegetation cover 'Low vegetation cover' = { discipline = 2 ; @@ -5416,12 +5422,6 @@ parameterCategory = 0 ; parameterNumber = 12 ; } -#Water fraction -'Water fraction' = { - discipline = 1 ; - parameterCategory = 2 ; - parameterNumber = 2 ; - } #Days since last observation 'Days since last observation' = { discipline = 0 ; diff --git a/definitions/grib2/paramId.def b/definitions/grib2/paramId.def index 9fa1fe993..670e477af 100644 --- a/definitions/grib2/paramId.def +++ b/definitions/grib2/paramId.def @@ -181,6 +181,12 @@ parameterCategory = 2 ; parameterNumber = 45 ; } +#Lake cover +'26' = { + discipline = 1 ; + parameterCategory = 2 ; + parameterNumber = 2 ; + } #Low vegetation cover '27' = { discipline = 2 ; @@ -5416,12 +5422,6 @@ parameterCategory = 0 ; parameterNumber = 12 ; } -#Water fraction -'240016' = { - discipline = 1 ; - parameterCategory = 2 ; - parameterNumber = 2 ; - } #Days since last observation '240017' = { discipline = 0 ; diff --git a/definitions/grib2/shortName.def b/definitions/grib2/shortName.def index 51823f5a7..81e6cd53a 100644 --- a/definitions/grib2/shortName.def +++ b/definitions/grib2/shortName.def @@ -181,6 +181,12 @@ parameterCategory = 2 ; parameterNumber = 45 ; } +#Lake cover +'cl' = { + discipline = 1 ; + parameterCategory = 2 ; + parameterNumber = 2 ; + } #Low vegetation cover 'cvl' = { discipline = 2 ; @@ -5416,12 +5422,6 @@ parameterCategory = 0 ; parameterNumber = 12 ; } -#Water fraction -'fldfrc' = { - discipline = 1 ; - parameterCategory = 2 ; - parameterNumber = 2 ; - } #Days since last observation 'dslr' = { discipline = 0 ; diff --git a/definitions/grib2/units.def b/definitions/grib2/units.def index 6ed54fc31..b6ebdbf3f 100644 --- a/definitions/grib2/units.def +++ b/definitions/grib2/units.def @@ -181,6 +181,12 @@ parameterCategory = 2 ; parameterNumber = 45 ; } +#Lake cover +'(0 - 1)' = { + discipline = 1 ; + parameterCategory = 2 ; + parameterNumber = 2 ; + } #Low vegetation cover '(0 - 1)' = { discipline = 2 ; @@ -5416,12 +5422,6 @@ parameterCategory = 0 ; parameterNumber = 12 ; } -#Water fraction -'(0 - 1)' = { - discipline = 1 ; - parameterCategory = 2 ; - parameterNumber = 2 ; - } #Days since last observation 'Integer' = { discipline = 0 ; From eac06e88532f0bf8ef6d0562e2a23a90ca08e19c Mon Sep 17 00:00:00 2001 From: shahramn Date: Fri, 9 Aug 2024 14:27:16 +0100 Subject: [PATCH 3/3] ECC-1899: API function to allow setting debug level --- fortran/eccodes_f90_tail.f90 | 6 ++++++ fortran/grib_api_externals.h | 2 +- fortran/grib_f90_tail.f90 | 7 +++++++ fortran/grib_fortran.cc | 15 ++++++++++++--- fortran/grib_fortran_prototypes.h | 1 + src/eccodes.cc | 5 +++++ src/eccodes.h | 2 ++ src/grib_api.h | 2 ++ tests/unit_tests.cc | 26 ++++++++++++++++++++++++++ 9 files changed, 62 insertions(+), 4 deletions(-) diff --git a/fortran/eccodes_f90_tail.f90 b/fortran/eccodes_f90_tail.f90 index fcc0c860b..05d59beb5 100644 --- a/fortran/eccodes_f90_tail.f90 +++ b/fortran/eccodes_f90_tail.f90 @@ -2765,6 +2765,12 @@ call grib_skip_read_only(iterid, status) end subroutine codes_skip_read_only + !> Set debug mode + subroutine codes_set_debug(mode) + integer(kind=kindOfInt), intent(in) :: mode + call grib_set_debug(mode) + end subroutine codes_set_debug + !> Set the definition path !> !> In case of error, if the status parameter (optional) is not given, the program will diff --git a/fortran/grib_api_externals.h b/fortran/grib_api_externals.h index f789fb92e..fd3d6c10a 100644 --- a/fortran/grib_api_externals.h +++ b/fortran/grib_api_externals.h @@ -79,7 +79,7 @@ integer, external :: grib_f_set_int, grib_f_set_int_array, & integer, external :: grib_f_get_message_size, grib_f_copy_message, grib_f_count_in_file integer, external :: grib_f_write, grib_f_multi_write, grib_f_multi_append integer, external :: grib_f_clone, grib_f_copy_namespace -external :: grib_f_check +external :: grib_f_check , grib_f_set_debug integer, external :: grib_f_util_sections_copy integer, external :: grib_f_set_definitions_path, grib_f_set_samples_path integer, external :: grib_f_julian_to_datetime, grib_f_datetime_to_julian, grib_f_copy_key diff --git a/fortran/grib_f90_tail.f90 b/fortran/grib_f90_tail.f90 index c178eedbe..be1a574ff 100644 --- a/fortran/grib_f90_tail.f90 +++ b/fortran/grib_f90_tail.f90 @@ -3177,6 +3177,13 @@ end if end subroutine grib_skip_read_only + !> Set debug mode + subroutine grib_set_debug(mode) + integer(kind=kindOfInt), intent(in) :: mode + call grib_f_set_debug(mode) + end subroutine grib_set_debug + + !> Set the definition path !> !> In case of error, if the status parameter (optional) is not given, the program will diff --git a/fortran/grib_fortran.cc b/fortran/grib_fortran.cc index 972118384..1539bc9db 100644 --- a/fortran/grib_fortran.cc +++ b/fortran/grib_fortran.cc @@ -975,8 +975,9 @@ int grib_f_close_file_(int* fid) /*****************************************************************************/ static int file_count=0; -void grib_f_write_on_fail(int* gid) { - grib_context* c=grib_context_get_default(); +void grib_f_write_on_fail(int* gid) +{ + grib_context* c = grib_context_get_default(); if (c->write_on_fail) { char filename[100]={0,}; grib_handle* h=NULL; @@ -993,7 +994,8 @@ void grib_f_write_on_fail(int* gid) { if (h) grib_write_message(h,filename,"w"); } } -void grib_f_write_on_fail_(int* gid) { +void grib_f_write_on_fail_(int* gid) +{ grib_f_write_on_fail(gid); } /*****************************************************************************/ @@ -2890,6 +2892,13 @@ int codes_f_bufr_multi_element_constant_arrays_off_(void) return GRIB_SUCCESS; } +/*****************************************************************************/ +void grib_f_set_debug_(int mode) +{ + grib_context* c = grib_context_get_default(); + grib_context_set_debug(c, mode); +} + /*****************************************************************************/ int grib_f_set_definitions_path_(char* path, int len) { diff --git a/fortran/grib_fortran_prototypes.h b/fortran/grib_fortran_prototypes.h index 4eb0a313d..7af6b2101 100644 --- a/fortran/grib_fortran_prototypes.h +++ b/fortran/grib_fortran_prototypes.h @@ -309,6 +309,7 @@ int grib_f_multi_append__(int *ingid, int *sec, int *mgid); int codes_f_bufr_keys_iterator_new_(int* gid,int* iterid); int grib_f_read_file_(int* fid, void* buffer, size_t* nbytes); int codes_f_bufr_keys_iterator_delete_(int* iterid); +void grib_f_set_debug_(int mode); int grib_f_set_definitions_path_(char* path, int len); int grib_f_read_any_from_file_(int* fid, void* buffer, size_t* nbytes); int any_f_new_from_file_(int* fid, int* gid); diff --git a/src/eccodes.cc b/src/eccodes.cc index 9ec6d8073..779690790 100644 --- a/src/eccodes.cc +++ b/src/eccodes.cc @@ -585,6 +585,11 @@ void codes_context_set_samples_path(grib_context* c, const char* path) grib_context_set_samples_path(c, path); } +void codes_context_set_debug(grib_context* c, int mode) +{ + grib_context_set_debug(c, mode); +} + void codes_context_set_memory_proc(grib_context* c, grib_malloc_proc p_malloc, grib_free_proc p_free, grib_realloc_proc p_realloc) { grib_context_set_memory_proc(c, p_malloc, p_free, p_realloc); diff --git a/src/eccodes.h b/src/eccodes.h index f0b110b86..0c8f7193c 100644 --- a/src/eccodes.h +++ b/src/eccodes.h @@ -1174,6 +1174,8 @@ void codes_context_set_definitions_path(codes_context* c, const char* path); */ void codes_context_set_samples_path(codes_context* c, const char* path); +void codes_context_set_debug(grib_context* c, int mode); + /** * Sets memory procedures of the context * diff --git a/src/grib_api.h b/src/grib_api.h index 84566b108..09601899f 100644 --- a/src/grib_api.h +++ b/src/grib_api.h @@ -1169,6 +1169,8 @@ void grib_context_set_definitions_path(grib_context* c, const char* path); */ void grib_context_set_samples_path(grib_context* c, const char* path); +void grib_context_set_debug(grib_context* c, int mode); + /** * Sets memory procedures of the context * diff --git a/tests/unit_tests.cc b/tests/unit_tests.cc index 22a394548..d1575301a 100644 --- a/tests/unit_tests.cc +++ b/tests/unit_tests.cc @@ -742,10 +742,36 @@ void test_codes_get_error_message() Assert( STR_EQUAL(errmsg, "Unknown error -6666")); } +void test_codes_context_set_debug() +{ + printf("Running %s ...\n", __func__); + grib_context* context = NULL; + int err = 0; + + printf("\tEnable debugging...\n"); + grib_context_set_debug(context, -1); + + grib_handle* h = grib_handle_new_from_samples(context, "GRIB2"); + err = grib_set_long(h, "paramId", 167); + Assert(!err); + + printf("\tDisable debugging...\n"); + grib_context_set_debug(context, 0); + + err = grib_set_long(h, "edition", 1); + Assert(!err); + printf("\tEnable debugging again (verbose)...\n"); + grib_context_set_debug(context, 1); + grib_handle_delete(h); + + grib_context_set_debug(context, 0); +} + int main(int argc, char** argv) { printf("Doing unit tests. ecCodes version = %ld\n", grib_get_api_version()); + test_codes_context_set_debug(); test_codes_get_error_message(); test_iarray();