diff --git a/examples/python/CMakeLists.txt b/examples/python/CMakeLists.txt index 9585d5169..1ac352207 100644 --- a/examples/python/CMakeLists.txt +++ b/examples/python/CMakeLists.txt @@ -29,6 +29,7 @@ endforeach() ################################################# list( APPEND tests_basic grib_set_pv + bufr_read_sample ) list( APPEND tests_extra grib_clone @@ -54,7 +55,7 @@ list( APPEND tests_extra bufr_read_header bufr_read_scatterometer bufr_read_tropical_cyclone - bufr_read_synop + bufr_read_synop bufr_read_temp bufr_set_keys bufr_subset diff --git a/examples/python/bufr_read_sample.py b/examples/python/bufr_read_sample.py new file mode 100644 index 000000000..f754254e6 --- /dev/null +++ b/examples/python/bufr_read_sample.py @@ -0,0 +1,79 @@ +# Copyright 2005-2018 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. + +# +# Python implementation: bufr_read_sample +# +# Description: how to read data values from a BUFR sample message. +# + +from __future__ import print_function +import traceback +import sys + +from eccodes import * + +VERBOSE = 1 # verbose error reporting + + +def example(INPUT): + # open bufr file + f = open(INPUT) + + # These keys should be in the sample files + keys = [ + 'blockNumber', + 'stationNumber', + 'latitude', + 'longitude', + '24HourPressureChange', + 'horizontalVisibility', + '#1#cloudAmount', # cloud amount (low and mid level) + '#1#heightOfBaseOfCloud', + '#1#cloudType', # cloud type (low clouds) + '#2#cloudType', # cloud type (middle clouds) + '#3#cloudType' # cloud type (highclouds) + ] + + cnt = 0 + + # Loop for the messages in the file + while 1: + bufr = codes_bufr_new_from_file(f) + if bufr is None: + break + + print("message: %s" % cnt) + + codes_set(bufr, 'unpack', 1) + + for key in keys: + print(' %s: %s' % (key, codes_get(bufr, key))) + + cnt += 1 + + codes_release(bufr) + + f.close() + + +def main(): + try: + example(sys.argv[1]) + except CodesInternalError as err: + if VERBOSE: + traceback.print_exc(file=sys.stderr) + else: + sys.stderr.write(err.msg + '\n') + + return 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/examples/python/bufr_read_sample.sh b/examples/python/bufr_read_sample.sh new file mode 100755 index 000000000..706c60df1 --- /dev/null +++ b/examples/python/bufr_read_sample.sh @@ -0,0 +1,26 @@ +#!/bin/sh +# Copyright 2005-2018 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_read_sample_test_p" + +# Define tmp file +fTmp=${label}".tmp.txt" + +REDIRECT=/dev/null + +$PYTHON $examples_src/bufr_read_sample.py ${ECCODES_SAMPLES_PATH}/BUFR3.tmpl 2> $REDIRECT +$PYTHON $examples_src/bufr_read_sample.py ${ECCODES_SAMPLES_PATH}/BUFR4.tmpl 2> $REDIRECT + +# Clean up +rm -f $fTmp