2015-12-31 12:44:51 +00:00
|
|
|
# Copyright 2005-2016 ECMWF.
|
2015-02-09 15:41:15 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2015-11-12 15:15:50 +00:00
|
|
|
# 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.
|
2015-02-09 15:41:15 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
#
|
|
|
|
# Python implementation: bufr_clone
|
|
|
|
#
|
|
|
|
# Description: how to create a new BUFR message by cloning
|
|
|
|
# an existing message.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import traceback
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from eccodes import *
|
|
|
|
|
2015-11-11 18:20:31 +00:00
|
|
|
INPUT = '../../data/bufr/syno_1.bufr'
|
|
|
|
OUTPUT = 'bufr_clone_test_p.clone.bufr'
|
|
|
|
VERBOSE = 1 # verbose error reporting
|
|
|
|
|
2015-03-18 10:36:56 +00:00
|
|
|
|
2015-02-09 15:41:15 +00:00
|
|
|
def example():
|
2015-03-18 10:36:56 +00:00
|
|
|
|
2015-02-09 15:41:15 +00:00
|
|
|
# open bufr file
|
|
|
|
fin = open(INPUT)
|
|
|
|
|
|
|
|
# open otput bufr file
|
2015-11-11 18:20:31 +00:00
|
|
|
fout = open(OUTPUT, 'w')
|
2015-02-09 15:41:15 +00:00
|
|
|
|
|
|
|
# get handle for message
|
|
|
|
gid = codes_bufr_new_from_file(fin)
|
|
|
|
|
|
|
|
# create several clones of this message and alter them
|
|
|
|
# in different ways
|
|
|
|
|
2015-11-11 18:20:31 +00:00
|
|
|
for centre in range(0, 3):
|
2015-03-18 10:36:56 +00:00
|
|
|
|
2015-02-09 15:41:15 +00:00
|
|
|
# clone the message
|
|
|
|
clone_id = codes_clone(gid)
|
2015-03-18 10:36:56 +00:00
|
|
|
|
2015-02-09 15:41:15 +00:00
|
|
|
# this is the place where you may wish to modify the clone
|
2015-11-11 18:20:31 +00:00
|
|
|
codes_set(clone_id, 'bufrHeaderCentre', centre)
|
2015-03-18 10:36:56 +00:00
|
|
|
|
2015-02-09 15:41:15 +00:00
|
|
|
# write the cloned message to a file
|
2015-11-11 18:20:31 +00:00
|
|
|
codes_write(clone_id, fout)
|
2015-03-18 10:36:56 +00:00
|
|
|
|
2015-02-09 15:41:15 +00:00
|
|
|
# relase the clone's handle
|
|
|
|
codes_release(clone_id)
|
2015-03-18 10:36:56 +00:00
|
|
|
|
2015-02-09 15:41:15 +00:00
|
|
|
# release the source's handle
|
|
|
|
codes_release(gid)
|
2015-03-18 10:36:56 +00:00
|
|
|
|
2015-02-09 15:41:15 +00:00
|
|
|
fin.close()
|
|
|
|
fout.close()
|
|
|
|
|
2015-11-11 18:20:31 +00:00
|
|
|
|
2015-02-09 15:41:15 +00:00
|
|
|
def main():
|
|
|
|
try:
|
|
|
|
example()
|
2015-11-12 15:15:50 +00:00
|
|
|
except CodesInternalError as err:
|
2015-02-09 15:41:15 +00:00
|
|
|
if VERBOSE:
|
|
|
|
traceback.print_exc(file=sys.stderr)
|
|
|
|
else:
|
2016-06-14 10:55:48 +00:00
|
|
|
sys.stderr.write(err.msg + '\n')
|
2015-02-09 15:41:15 +00:00
|
|
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main())
|