From e235318c6ebb049293bf9f46ccc5f35bd3af049d Mon Sep 17 00:00:00 2001 From: Sandor Kertesz Date: Fri, 27 Feb 2015 12:50:03 +0000 Subject: [PATCH] Make C, Fortran and Python examples ECC-15 --- examples/C/CMakeLists.txt | 2 + examples/C/Makefile.am | 5 ++- examples/C/bufr_missing.c | 81 ++++++++++++++++++++++++++++++++++++++ examples/C/bufr_missing.sh | 34 ++++++++++++++++ 4 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 examples/C/bufr_missing.c create mode 100755 examples/C/bufr_missing.sh diff --git a/examples/C/CMakeLists.txt b/examples/C/CMakeLists.txt index e5323bd63..c025433f0 100644 --- a/examples/C/CMakeLists.txt +++ b/examples/C/CMakeLists.txt @@ -37,6 +37,7 @@ list( APPEND test_bins bufr_expanded bufr_get_keys bufr_keys_iterator + bufr_missing bufr_print_header bufr_print_data bufr_set_keys @@ -74,6 +75,7 @@ list( APPEND tests bufr_expanded bufr_get_keys bufr_keys_iterator + bufr_missing bufr_print_header bufr_print_data bufr_set_keys diff --git a/examples/C/Makefile.am b/examples/C/Makefile.am index e0c9ec383..1d2654d64 100644 --- a/examples/C/Makefile.am +++ b/examples/C/Makefile.am @@ -5,14 +5,14 @@ TESTS = iterator.sh get.sh print_data.sh set.sh keys_iterator.sh multi.sh multi_ precision.sh list.sh large_grib1.sh get_data.sh sections_copy.sh set_missing.sh clone.sh set_pv.sh \ check_gaussian_grids.sh \ bufr_attributes.sh bufr_clone.sh bufr_expanded.sh bufr_get_keys.sh bufr_print_header.sh bufr_print_data.sh \ - bufr_set_keys.sh bufr_subset.sh bufr_keys_iterator.sh + bufr_set_keys.sh bufr_subset.sh bufr_keys_iterator.sh bufr_missing.sh noinst_PROGRAMS = nearest set_bitmap iterator get print_data set set_missing keys_iterator \ set_data mars_param values_check box multi multi2 multi_write precision \ set_pv list sections_copy large_grib1 get_data iterator_bitmap clone new_sample \ check_gaussian_grid ensemble_index points \ bufr_attributes bufr_clone bufr_expanded bufr_get_keys bufr_print_header bufr_print_data \ - bufr_get_keys bufr_subset bufr_keys_iterator bufr_set_keys + bufr_get_keys bufr_subset bufr_keys_iterator bufr_set_keys bufr_missing #bin_PROGRAMS = points box_SOURCES = box.c @@ -46,6 +46,7 @@ bufr_clone_SOURCES = bufr_clone.c bufr_expanded_SOURCES = bufr_expanded.c bufr_get_keys_SOURCES = bufr_get_keys.c bufr_keys_iterator = bufr_keys_iterator.c +bufr_missing = bufr_missing.c bufr_print_header_SOURCES = bufr_print_header.c bufr_print_data_SOURCES = bufr_print_data.c bufr_set_keys_SOURCES = bufr_set_keys.c diff --git a/examples/C/bufr_missing.c b/examples/C/bufr_missing.c new file mode 100644 index 000000000..06cd9c6f5 --- /dev/null +++ b/examples/C/bufr_missing.c @@ -0,0 +1,81 @@ +/* + * 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. + */ + +/* + * C Implementation: bufr_missing + * + * Description: how to handle missing values in BUFR messages. + * + */ + +#include "eccodes.h" + + +int main(int argc,char* argv[]) +{ + FILE* in = NULL; + + /* message handle. Required in all the eccodes calls acting on a message.*/ + codes_handle* h=NULL; + + char* units= NULL; + char* unitsPercent= NULL; + long longVal; + double doubleVal; + size_t values_len=0, desc_len=0, len=0; + int i, err=0; + int cnt=0; + char* infile = "../../data/bufr/syno_1.bufr"; + + in=fopen(infile,"r"); + if (!in) { + printf("ERROR: unable to open file %s\n", infile); + return 1; + } + + /* loop over the messages in the bufr file */ + while ((h = codes_handle_new_from_file(NULL,in,PRODUCT_BUFR,&err)) != NULL || err != CODES_SUCCESS) + { + if (h == NULL) { + printf("Error: unable to create handle for message %d\n",cnt); + cnt++; + continue; + } + + printf("message: %d\n",cnt); + + /* we need to instruct ecCodes to expand the descriptors + i.e. unpack the data values */ + CODES_CHECK(codes_set_long(h,"unpack",1),0); + + /* This value is missing */ + CODES_CHECK(codes_get_double(h,"relativeHumidity",&doubleVal),&err); + printf(" relativeHumidity: %.2f %ld\n",doubleVal,err); + + /* This value exists */ + CODES_CHECK(codes_get_double(h,"airTemperatureAt2M",&doubleVal),&err); + printf(" airTemperatureAt2M: %.2f %ld\n",doubleVal,err); + + /*longVal=10; + err=codes_is_missing(h,"relativeHumidity",&longVal); + printf(" missing: %ld %ld\n",longVal,err); + + err=codes_is_missing(h,"airTemperatureAt2M",&longVal); + printf(" missing: %ld %ld\n",longVal,err);*/ + + /* delete handle */ + codes_handle_delete(h); + + cnt++; + } + + fclose(in); + return 0; +} diff --git a/examples/C/bufr_missing.sh b/examples/C/bufr_missing.sh new file mode 100755 index 000000000..39737ffa5 --- /dev/null +++ b/examples/C/bufr_missing.sh @@ -0,0 +1,34 @@ +#!/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 + + +#Define a common label for all the tmp files +label="bufr_missing_test_c" + +#Define tmp file +fTmp=${label}.tmp.txt +rm -f $fTmp | true + +#We check "syno_1.bufr". The path is +#hardcoded in the example + +REDIRECT=/dev/null + +#Write the key values into a file +${examples_dir}/bufr_missing #2> $REDIRECT > $fTmp + +#TODO: check the results + +#cat $fTmp + +#Clean up +rm -f $fTmp | true