From 98c687ef15beb8babebffddfcdcc7adc71c583ca Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Thu, 21 Jan 2021 21:18:59 +0000 Subject: [PATCH] CCSDS packing: add other cases --- src/grib_accessor_class_data_ccsds_packing.c | 4 -- tests/CMakeLists.txt | 1 + tests/grib_packing_order.c | 45 +++++++++-------- tests/grib_packing_order.sh | 51 ++++++++++++++++++++ 4 files changed, 76 insertions(+), 25 deletions(-) create mode 100755 tests/grib_packing_order.sh diff --git a/src/grib_accessor_class_data_ccsds_packing.c b/src/grib_accessor_class_data_ccsds_packing.c index fa013e8ed..37b59c7dc 100644 --- a/src/grib_accessor_class_data_ccsds_packing.c +++ b/src/grib_accessor_class_data_ccsds_packing.c @@ -330,7 +330,6 @@ static int pack_double(grib_accessor* a, const double* val, size_t* len) unsigned char* buf = NULL; unsigned char* encoded = NULL; size_t n_vals = 0; - //long nn = 0; long binary_scale_factor = 0; long decimal_scale_factor = 0; @@ -350,9 +349,6 @@ static int pack_double(grib_accessor* a, const double* val, size_t* len) self->dirty = 1; - //if ((err = grib_value_count(a, &nn)) != GRIB_SUCCESS) - // return err; - //n_vals = nn; n_vals = *len; if ((err = grib_get_long_internal(hand, self->bits_per_value, &bits_per_value)) != GRIB_SUCCESS) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d3bed0f04..66f455556 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -65,6 +65,7 @@ if( HAVE_BUILD_TOOLS ) grib_calendar grib_md5 grib_cfNames + grib_packing_order filter_substr grib_uerra grib_2nd_order_numValues diff --git a/tests/grib_packing_order.c b/tests/grib_packing_order.c index 480421ab6..d501681b9 100644 --- a/tests/grib_packing_order.c +++ b/tests/grib_packing_order.c @@ -1,8 +1,8 @@ -#include -#include #include #include "eccodes.h" + +/* Values taken from an actual IFS forecast run for paramId=133 (Specific humidity) */ const double values[] = { 0.0000026822,0.0000026803,0.0000026786,0.0000026773,0.0000026768, 0.0000026771,0.0000026783,0.0000026801,0.0000026820,0.0000026840, @@ -130888,41 +130888,44 @@ const double values[] = { static void usage(const char* prog) { - printf("usage: %s packingType packingStage outfile\n", prog); + printf("Usage: %s packingType packingStage outfile\n", prog); + printf("packingStage: either packing_type_before_values or values_before_packing_type\n"); exit(1); } -// SIMPLE -// max min avg sd skew kurt const -// 2.97473e-06 1.26532e-06 2.17382e-06 3.84169e-07 0.32796 -1.03089 0 -// -// CCSDS -// 2.8052e-06 2.6129e-06 2.61477e-06 1.36989e-08 7.77715 62.686 0 + +typedef enum { + PACKING_TYPE_BEFORE_VALUES, + VALUES_BEFORE_PACKING_TYPE +} PackingStage; int main(int argc, char** argv) { - size_t values_len = sizeof(values)/sizeof(values[0]); // 654400; - const char* sample_filename = "gg_sfc_grib2"; // GRIB2 ?? - //const char* sample_filename = "GRIB2"; //constant field + size_t values_len = sizeof(values)/sizeof(values[0]); /* 654400 */ + const char* sample_filename = "gg_sfc_grib2"; codes_handle* h = NULL; size_t str_len = 0; - int packing_stage; + PackingStage packing_stage; char* packing_type; char* outfile_name; if (argc != 4) usage(argv[0]); packing_type = argv[1]; - packing_stage = atoi(argv[2]); - outfile_name = argv[3]; - assert(packing_stage == 1 || packing_stage == 2); - + if (strcmp(argv[2], "packing_type_before_values")==0) + packing_stage = PACKING_TYPE_BEFORE_VALUES; + else if (strcmp(argv[2], "values_before_packing_type")==0) + packing_stage = VALUES_BEFORE_PACKING_TYPE; + else + exit(1); + + outfile_name = argv[3]; + printf("Using sample_filename = %s\n", sample_filename); h = codes_grib_handle_new_from_samples(0, sample_filename); assert(h); - assert( 654400==values_len ); CODES_CHECK(codes_set_long(h, "bitsPerValue", 16), 0); - if (packing_stage == 1) { + if (packing_stage == PACKING_TYPE_BEFORE_VALUES) { printf("Set packingType to %s\n", packing_type); CODES_CHECK(codes_set_string(h, "packingType", packing_type, &str_len), 0); } @@ -130930,9 +130933,9 @@ int main(int argc, char** argv) printf("Set values\n"); CODES_CHECK(codes_set_double_array(h, "values", values, values_len), 0); - if (packing_stage == 2) { + if (packing_stage == VALUES_BEFORE_PACKING_TYPE) { printf("Set packingType to %s\n", packing_type); - CODES_CHECK(codes_set_string(h, "packingType", packing_type, &str_len), 0); //WORKS + CODES_CHECK(codes_set_string(h, "packingType", packing_type, &str_len), 0); } CODES_CHECK(codes_write_message(h, outfile_name, "w"), 0); diff --git a/tests/grib_packing_order.sh b/tests/grib_packing_order.sh new file mode 100755 index 000000000..0cde5130d --- /dev/null +++ b/tests/grib_packing_order.sh @@ -0,0 +1,51 @@ +#!/bin/sh +# (C) Copyright 2005- ECMWF. +# +# This software is licensed under the terms of the Apache Licence Version 2.0 +# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. +# +# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by +# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. +# + +. ./include.sh +set -u +REDIRECT=/dev/null +label="grib_packing_order" +temp=${label}".grib.tmp" +temp_simple1=${label}".simple1.tmp" +temp_simple2=${label}".simple2.tmp" +temp_ccsds1=${label}".ccsds1.tmp" +temp_ccsds2=${label}".ccsds2.tmp" +temp_jpeg1=${label}".jpeg1.tmp" +temp_jpeg2=${label}".jpeg2.tmp" + +# Simple Packing +$EXEC ${test_dir}/grib_packing_order grid_simple packing_type_before_values $temp_simple1 +$EXEC ${test_dir}/grib_packing_order grid_simple values_before_packing_type $temp_simple2 +grib_check_key_equals $temp_simple1 packingType grid_simple +${tools_dir}/grib_compare $temp_simple1 $temp_simple2 + +if [ $HAVE_AEC -eq 1 ]; then + # CCSDS (AEC) Packing + $EXEC ${test_dir}/grib_packing_order grid_ccsds packing_type_before_values $temp_ccsds1 + $EXEC ${test_dir}/grib_packing_order grid_ccsds values_before_packing_type $temp_ccsds2 + grib_check_key_equals $temp_ccsds1 packingType grid_ccsds + ${tools_dir}/grib_compare $temp_ccsds1 $temp_ccsds2 + + ${tools_dir}/grib_compare -c data:n $temp_simple1 $temp_ccsds1 +fi + +if [ $HAVE_JPEG -eq 1 ]; then + # JPEG Packing + $EXEC ${test_dir}/grib_packing_order grid_jpeg packing_type_before_values $temp_jpeg1 + $EXEC ${test_dir}/grib_packing_order grid_jpeg values_before_packing_type $temp_jpeg2 + grib_check_key_equals $temp_jpeg1 packingType grid_jpeg + ${tools_dir}/grib_compare $temp_jpeg1 $temp_jpeg2 + + ${tools_dir}/grib_compare -c data:n $temp_simple1 $temp_jpeg1 +fi + + +# Clean up +rm -f $temp_simple1 $temp_simple2 $temp_ccsds1 $temp_ccsds2 $temp_jpeg1 $temp_jpeg2