From bd423aa4990e04052cda2a6c9ebc1ad6ef7c0030 Mon Sep 17 00:00:00 2001 From: Sandor Kertesz Date: Thu, 22 Jan 2015 10:05:13 +0000 Subject: [PATCH] BUFR: added test for reading bufr header in C ECC-24 --- data/bufr/CMakeLists.txt | 1 + tests/CMakeLists.txt | 2 ++ tests/bufr_read_header.c | 73 +++++++++++++++++++++++++++++++++++++++ tests/bufr_read_header.sh | 41 ++++++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 tests/bufr_read_header.c create mode 100755 tests/bufr_read_header.sh diff --git a/data/bufr/CMakeLists.txt b/data/bufr/CMakeLists.txt index 5ea004b1e..d9cf4b001 100644 --- a/data/bufr/CMakeLists.txt +++ b/data/bufr/CMakeLists.txt @@ -403,6 +403,7 @@ set(bufr_refs_to_download wavb_134.bufr.desc.ref aaen_55.bufr.ls.ref + syno_multi.bufr.header.ref ) foreach( f ${bufr_files_to_download} ${bufr_refs_to_download} ) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 48f2bea5b..b0f987332 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -13,6 +13,7 @@ list( APPEND test_bins read_index unit_tests gauss_sub + bufr_read_header ) foreach( tool ${test_bins} ) @@ -34,6 +35,7 @@ list( APPEND tests bufrdc_ref bufr_json bufr_ls + bufr_read_header ieee grib1to2 grib2to1 diff --git a/tests/bufr_read_header.c b/tests/bufr_read_header.c new file mode 100644 index 000000000..17181f102 --- /dev/null +++ b/tests/bufr_read_header.c @@ -0,0 +1,73 @@ +/* + * Copyright 2005-2015 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 "eccodes.h" + +void usage(char* prog) { + printf("usage: %s infile\n",prog); + exit(1); +} + +int main(int argc,char* argv[]) { + + char* filename; + FILE* f; + codes_handle* h=NULL; + long longVal; + double doubleVal; + int count; + int err=0; + int header_only=1; + + if (argc!=2) usage(argv[0]); + filename=argv[1]; + + f=fopen(filename,"r"); + if (!f) { + perror(filename); + exit(1); + } + + count=1; + + while((h=bufr_new_from_file(NULL,f,header_only,&err)) != NULL) + { + /* Check of errors after reading a message. */ + if (err != CODES_SUCCESS) CODES_CHECK(err,0); + + printf("=======================================\n"); + printf("message no: %d\n",count); + + /*From section 0 */ + CODES_CHECK(codes_get_long(h,"totalLength",&longVal),0); + printf("\ttotalLength: %d\n",longVal); + + /*From section 1 */ + CODES_CHECK(codes_get_long(h,"centre",&longVal),0); + printf("\tcentre: %d\n",longVal); + + /*From section 2 */ + CODES_CHECK(codes_get_long(h,"rdbtimeDay",&longVal),0); + printf("\trdbtimeDay: %d\n",longVal); + + /*From section 3 */ + /*CODES_CHECK(codes_get_long(h,"numberOfSubsets",&longVal),0); + printf("\tnumberOfSubsets: %d\n",longVal);*/ + + count++; + + codes_handle_delete(h); + } + + fclose(f); + + return 0; + +} diff --git a/tests/bufr_read_header.sh b/tests/bufr_read_header.sh new file mode 100755 index 000000000..c64a909ff --- /dev/null +++ b/tests/bufr_read_header.sh @@ -0,0 +1,41 @@ +#!/bin/sh +# Copyright 2005-2015 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 -x + +#Remember work dir +dWork=`pwd` + +#Enter data dir +cd ${data_dir}/bufr + +fTmp="tmp.txt" +rm -f $fTmp | true + +#----------------------------------------------------- +# Test reading the header only from a +# file with multiple messages +#---------------------------------------------------- + +f="syno_multi.bufr" +fRef=$f".header.ref" +fRes=$f".header.test" +REDIRECT=/dev/null + +${test_dir}/bufr_read_header ${data_dir}/bufr/$f 2> $REDIRECT > $fRes + +#Write the values into a file and compare to ref +diff $fRef $fRes >$REDIRECT 2> $REDIRECT +[ $? -eq 0 ] + +#Go back to workdir +cd $dWork