Add C, F90 and python examples ECC-15

This commit is contained in:
Sandor Kertesz 2015-02-09 15:41:15 +00:00
parent f69af1564c
commit 4836cc1039
35 changed files with 898 additions and 85 deletions

View File

@ -9,9 +9,9 @@
*/
/*
* C Implementation: clone
* C implementation: bufr_clone
*
* Description: How to create a new BUFR message by cloning
* Description: how to create new BUFR messages by cloning
* an existing message.
*
*/
@ -27,10 +27,13 @@ int main(int argc, char *argv[])
{
FILE *in = NULL;
FILE *out = NULL;
/* message handle. Required in all the eccodes calls acting on a message.*/
codes_handle *source_handle = NULL;
const void *buffer = NULL;
size_t size = 0;
int err = 0;
int i, err = 0;
if (argc != 3) {
usage(argv[0]);
@ -40,16 +43,27 @@ int main(int argc, char *argv[])
in = fopen(argv[1],"r");
out = fopen(argv[2],"w");
/* open input and output */
if (!in || !out) {
perror("ERROR: unable to open files");
fclose(out);
fclose(in);
return 1;
}
/* create a handle for the first message */
source_handle = bufr_new_from_file(0,in,&err);
if (source_handle == NULL) {
perror("ERROR: could not create handle for message");
return 1;
}
/* create several clones of this message and alter them
in different ways */
for(i=0; i < 3; i++) {
/* loop over the messages in the source bufr and clone them */
while ((source_handle = bufr_new_from_file(0,in,&err))!=NULL)
{
/* clone the current handle */
codes_handle *clone_handle = codes_handle_clone(source_handle);
@ -58,26 +72,26 @@ int main(int argc, char *argv[])
return 1;
}
/* This is the place where you may wish to modify the clone */
/*E.g. we change the centre */
/* This is the place where you may wish to modify the clone
E.g. we change the centre */
CODES_CHECK(codes_set_long(clone_handle, "centre", 250),0);
CODES_CHECK(codes_set_long(clone_handle, "centre", 222),0);
/* get the coded message in a buffer */
CODES_CHECK(codes_get_message(clone_handle,&buffer,&size),0);
/* write the buffer to a file */
if(fwrite(buffer,1,size,out) != size) {
perror(argv[1]);
perror("ERROR: could not write message to file");
return 1;
}
/* relase the clone's handle */
codes_handle_delete(clone_handle);
/* release the source's handle */
codes_handle_delete(source_handle);
}
/* release the source's handle */
codes_handle_delete(source_handle);
fclose(out);
fclose(in);

View File

@ -16,8 +16,8 @@ label="bufr_clone_test_c"
fBufrTmp=${label}.cloned.bufr
rm -f $fBufrTmp | true
#We clone a bufr file with multiple messages
f=${data_dir}/bufr/syno_multi.bufr
#We clone this bufr file
f=${data_dir}/bufr/syno_1.bufr
REDIRECT=/dev/null
@ -36,9 +36,5 @@ fi
set -e
#Check if clone has the same number of messages
[ `${tools_dir}/bufr_count $f` = `${tools_dir}/bufr_count $fBufrTmp` ]
#Clean up
rm -f ${fBufrTmp} | true

View File

@ -39,6 +39,7 @@ int main(int argc,char* argv[])
filename=argv[1];
/* open bufr file */
in=fopen(filename,"r");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
@ -53,12 +54,10 @@ int main(int argc,char* argv[])
cnt++;
continue;
}
/* we need to instruct ecCodes to unpack the data values */
CODES_CHECK(codes_set_long(h,"unpack",1),0);
/* get the size of the values array*/
CODES_CHECK(codes_get_size(h,"numericValues",&values_len),0);
CODES_CHECK(codes_get_size(h,"numericValues",&values_len),0);
printf(" number of expanded values: %ld\n",values_len);
/* allocate array for data values */
values = malloc(values_len*sizeof(double));
@ -68,7 +67,7 @@ int main(int argc,char* argv[])
for(i = 0; i < values_len; i++)
{
printf("%.10e\n",values[i]);
printf(" %.10e\n",values[i]);
}
free(values);

View File

@ -73,6 +73,16 @@ int main(int argc,char* argv[])
/* double value */
CODES_CHECK(codes_get_double(h,"airTemperatureAt2M",&doubleVal),0);
printf(" airTemperatureAt2M: %f\n",doubleVal);
/* ---- string value -----------------*/
/* get the size and allocate memory*/
CODES_CHECK(codes_get_length(h, "typicalDate", &len), 0);
typicalDate = (char*)malloc(len*sizeof(char));
/* get the values*/
grib_get_string(h, "typicalDate", typicalDate, &len);
printf(" typicalDate: %s\n", typicalDate);
/* ---- array of long ----------------*/
@ -101,16 +111,6 @@ int main(int argc,char* argv[])
{
printf(" %.10e\n",values[i]);
}
/* ---- string value -----------------*/
/* get the size and allocate memory*/
CODES_CHECK(codes_get_length(h, "typicalDate", &len), 0);
typicalDate = (char*)malloc(len*sizeof(char));
/* get the values*/
grib_get_string(h, "typicalDate", typicalDate, &len);
printf(" typicalDate: %s\n", typicalDate);
/* free allocated arrays */
free(descriptors);

View File

@ -11,7 +11,7 @@
/*
* C Implementation: bufr_keys_iterator
*
* Description: Example on how to use keys_iterator functions and the
* Description: how to use keys_iterator functions and the
* codes_keys_iterator structure to get all the available
* keys in a BUFR message.
*
@ -37,17 +37,24 @@ int main(int argc,char* argv[])
long longVal;
int err=0, cnt=0;
/* To skip read only and not coded keys
unsigned long key_iterator_filter_flags=CODES_KEYS_ITERATOR_SKIP_READ_ONLY ||
CODES_KEYS_ITERATOR_SKIP_COMPUTED;
*/
/* To skip certain keys use the combination of these flags:
unsigned long key_iterator_filter_flags=
CODES_KEYS_ITERATOR_SKIP_READ_ONLY ||
CODES_KEYS_ITERATOR_SKIP_COMPUTED ||
CODES_KEYS_ITERATOR_SKIP_CODED ||
CODES_KEYS_ITERATOR_SKIP_EDITION_SPECIFIC ||
CODES_KEYS_ITERATOR_SKIP_DUPLICATES ||
CODES_KEYS_ITERATOR_SKIP_FUNCTION; */
unsigned long key_iterator_filter_flags=CODES_KEYS_ITERATOR_ALL_KEYS;
/* name_space=NULL to get all the keys */
/* name_space=NULL to get all the keys. Other namespaces are e.g. "ls" */
char* name_space=0;
char value[MAX_VAL_LEN];
size_t vlen=MAX_VAL_LEN;
size_t klen=0;
if (argc!=2) usage(argv[0]);
@ -81,8 +88,9 @@ int main(int argc,char* argv[])
/* loop over the keys */
while(codes_keys_iterator_next(kiter))
{
/* print key name*/
/* get key name*/
const char* name = codes_keys_iterator_get_name(kiter);
printf(" %s\n",name);
}

View File

@ -11,7 +11,7 @@
/*
* C Implementation: bufr_print_data
*
* Description: how to read/print data values from synop BUFR messages.
* Description: how to read data values from BUFR messages.
*
*/
@ -55,7 +55,8 @@ int main(int argc,char* argv[])
/* we need to instruct ecCodes to unpack the data values */
CODES_CHECK(codes_set_long(h,"unpack",1),0);
/* read and print some data values */
/* read and print some data values. This example was written for a SYNOP
BUFR file!! */
CODES_CHECK(codes_get_long(h,"blockNumber",&longVal),0);
printf(" blockNumber: %ld\n",longVal);

View File

@ -11,7 +11,7 @@
/*
* C Implementation: bufr_print_header
*
* Description: how to read/print the header of BUFR messages.
* Description: how to read the header of BUFR messages.
*
*/

View File

@ -11,7 +11,7 @@
/*
* C Implementation: bufr_set_keys
*
* Description: how to set different type of keys from BUFR messages.
* Description: how to set different type of keys in BUFR messages.
*
*/
@ -31,7 +31,7 @@ int main(int argc,char* argv[])
codes_handle* h=NULL;
long longVal;
double doubleVal;
/*double doubleVal;*/
int i, err=0;
int cnt=0;
size_t size = 0;

View File

@ -33,7 +33,7 @@ ${tools_dir}/bufr_compare $f $fBufrTmp >$REDIRECT 2> $REDIRECT
#Check if modified is different
if [ $? -eq 0 ]; then
echo "cloning produced identical files " >&2
echo "setting keys produced identical files " >&2
exit 1
fi

View File

@ -6,7 +6,7 @@
! 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.
!
!
! FORTRAN 90 implementation: bufr_clone
!
! Description: how to create a new BUFR message by cloning
! an existing message.
@ -34,29 +34,28 @@ program bufr_clone
! ibufr is the bufr id to be used in subsequent calls
call codes_bufr_new_from_file(infile,ibufr_in,iret)
do while (iret/=CODES_END_OF_FILE)
! create several clones of this message and alter them
! in different ways
do i=1,3
! clone the current handle
call codes_clone(ibufr_in, ibufr_out)
! This is the place where you may wish to modify the clone
! E.g. we change the centre
call codes_set(ibufr_out,'centre',250)
call codes_set(ibufr_out,'centre',222)
! write cloned messages to a file
call codes_write(ibufr_out,outfile)
! relase the clone's handle
call codes_release(ibufr_out)
end do
! relase the sources 's handle
call codes_release(ibufr_in)
! relase the sources 's handle
call codes_release(ibufr_in)
! next message from source
call codes_bufr_new_from_file(infile,ibufr_in,iret)
end do
call codes_close_file(infile)
call codes_close_file(outfile)

View File

@ -21,7 +21,7 @@ f=${data_dir}/bufr/syno_multi.bufr
REDIRECT=/dev/null
#The input ($f) and output ($fTmp) are hardcoded in the f90 example!!!
#The input ($f) and output ($fBufrTmp) are hardcoded in the f90 example!!!
${examples_dir}/f_bufr_clone >$REDIRECT 2> $REDIRECT
#Compare clone to the original
@ -36,8 +36,5 @@ fi
set -e
#Check if clone has the same number of messages
[ `${tools_dir}/bufr_count $f` = `${tools_dir}/bufr_count $fBufrTmp` ]
#Clean up
rm -f ${fBufrTmp} | true

View File

@ -8,7 +8,7 @@
! virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
!
!
! FOTRAN 90 Implementation: bufr_expanded
! FORTRAN 90 Implementation: bufr_expanded
!
! Description: how to read all the exapanded data values from BUFR messages.
!

View File

@ -8,10 +8,10 @@
! virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
!
!
! FOTRAN 90 Implementation: bufr_keys_iterator
! FORTRAN 90 implementation: bufr_keys_iterator
!
!
! Description: Example on how to use keys_iterator functions and the
! Description: how to use keys_iterator functions and the
! codes_keys_iterator structure to get all the available
! keys in a BUFR message.
!

View File

@ -10,7 +10,7 @@
!
! FOTRAN 90 Implementation: bufr_print_data
!
! Description: how to read/print data values from synop BUFR messages.
! Description: how to read data values from BUFR messages.
!
!
program bufr_print_data
@ -30,14 +30,16 @@ real(kind=8) :: t2m
call codes_bufr_new_from_file(ifile,ibufr,iret)
do while (iret/=CODES_END_OF_FILE)
! get and print some keys form the BUFR header
write(*,*) 'message: ',count
! we need to instruct ecCodes to expand all the descriptors
! i.e. unpack the data values
call codes_set(ibufr,"unpack",1);
!read and print some data values. This example was written
! for a SYNOP BUFR file!
! get wmo block number
call codes_get(ibufr,'blockNumber',blockNumber);
write(*,*) ' blockNumber:',blockNumber

View File

@ -24,7 +24,7 @@ rm -f $fTmp | true
REDIRECT=/dev/null
#Write the values into a file and compare with reference
${examples_dir}/f_bufr_print_data #2> $REDIRECT > $fTmp
${examples_dir}/f_bufr_print_data 2> $REDIRECT > $fTmp
#TODO: check the output

View File

@ -8,9 +8,9 @@
! virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
!
!
! FOTRAN 90 Implementation: bufr_print_header
! FORTRAN 90 implementation: bufr_print_header
!
! Description: how to read/print the header of BUFR messages.
! Description: how to read the header of BUFR messages.
!
!
program bufr_print_header

View File

@ -6,9 +6,9 @@
! 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.
!
! FOTRAN 90 Implementation: bufr_set_keys
! FORTRAN 90 implementation: bufr_set_keys
!
! Description: Description: how to set different type of keys from BUFR messages.
! Description: how to set different type of keys in BUFR messages.
!
!
program bufr_set_keys

View File

@ -16,7 +16,7 @@ label="bufr_set_keys_test_f"
fBufrTmp=${label}.tmp.bufr
rm -f $fBufrTmp | true
#We clone a bufr file with multiple messages.
#The bufr file to change
f=${data_dir}/bufr/syno_multi.bufr
REDIRECT=/dev/null
@ -30,7 +30,7 @@ ${tools_dir}/bufr_compare $f $fBufrTmp >$REDIRECT 2> $REDIRECT
#Check if they are different
if [ $? -eq 0 ]; then
echo "cloning produced identical files " >&2
echo "setting keys produced identical files " >&2
exit 1
fi

View File

@ -8,7 +8,7 @@
! virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
!
!
! FOTRAN 90 Implementation: bufr_subset
! FORTRAN 90 implementation: bufr_subset
!
! Description: how to read data values from a given subset of a BUFR message.
!

View File

@ -39,8 +39,14 @@ list( APPEND tests
set_missing
binary_message
set_bitmap
bufr_clone
bufr_expanded
bufr_get_keys
bufr_keys_iterator
bufr_print_header
bufr_print_data
bufr_print_data
bufr_set_keys
bufr_subset
)
foreach( test ${tests} )
ecbuild_add_test( TARGET p_${test}_test

View File

@ -2,7 +2,8 @@ if WITH_PYTHON
AM_CFLAGS = @WARN_PEDANTIC@ @WERROR@
TESTS = clone.sh count_messages.sh get.sh index.sh iterator.sh keys_iterator.sh multi_write.sh nearest.sh print_data.sh \
samples.sh set.sh set_missing.sh binary_message.sh set_bitmap.sh bufr_print_header.sh bufr_print_data.sh
samples.sh set.sh set_missing.sh binary_message.sh set_bitmap.sh bufr_print_header.sh bufr_print_data.sh \
bufr_clone.sh bufr_get_keys.sh bufr_set_keys.sh bufr_expanded.sh bufr_keys_iterator.sh bufr_subset.sh
TESTS_ENVIRONMENT = TOPBUILDDIR=$(top_builddir) PYTHON=$(PYTHON)
noinst_PROGRAMS = p_keys_iterator p_print_data p_iterator p_count_messages
@ -16,6 +17,7 @@ DEPENDENCIES = $(LDADD)
EXTRA_DIST = $(TESTS) include.sh clone.py count_messages.py get.py index.py iterator.py keys_iterator.py multi_write.py \
nearest.py print_data.py samples.py set.py set_missing.py binary_message.py set_pv.py set_bitmap.py \
bufr_print_header.py bufr_print_data.py \
bufr_print_header.py bufr_print_data.py bufr_clone.py bufr_get_keys.py bufr_set_keys.py \
bufr_expanded.py bufr_keys_iterator.py bufr_subset.py \
CMakeLists.txt include.ctest.sh.in
endif

View File

@ -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.
#
#
# Python implementation: bufr_clone
#
# Description: how to create a new BUFR message by cloning
# an existing message.
#
#
import traceback
import sys
from eccodes import *
INPUT='../../data/bufr/syno_1.bufr'
OUTPUT='bufr_clone_test_p.clone.bufr'
VERBOSE=1 # verbose error reporting
def example():
# open bufr file
fin = open(INPUT)
# open otput bufr file
fout = open(OUTPUT,'w')
# get handle for message
gid = codes_bufr_new_from_file(fin)
# create several clones of this message and alter them
# in different ways
for centre in range(0,3):
# clone the message
clone_id = codes_clone(gid)
# this is the place where you may wish to modify the clone
codes_set(clone_id,'centre',centre)
# write the cloned message to a file
codes_write(clone_id,fout)
# relase the clone's handle
codes_release(clone_id)
# release the source's handle
codes_release(gid)
fin.close()
fout.close()
def main():
try:
example()
except CodesInternalError,err:
if VERBOSE:
traceback.print_exc(file=sys.stderr)
else:
print >>sys.stderr,err.msg
return 1
if __name__ == "__main__":
sys.exit(main())

44
examples/python/bufr_clone.sh Executable file
View File

@ -0,0 +1,44 @@
#!/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_clone_test_p"
#Prepare tmp file
fBufrTmp=${label}.cloned.bufr
rm -f $fBufrTmp | true
#We clone a bufr file with multiple messages
#f=${data_dir}/bufr/syno_1.bufr
REDIRECT=/dev/null
#The input ($f) and output ($fBufrTmp) are hardcoded in the example!!!
$PYTHON bufr_clone.py >$REDIRECT 2> $REDIRECT
#TODO: add more checks
#Compare clone to the original
set +e
${tools_dir}/bufr_compare $f $fBufrTmp >$REDIRECT 2> $REDIRECT
#Check if clone is different
if [ $? -eq 0 ]; then
echo "cloning produced identical files " >&2
exit 1
fi
set -e
#Clean up
rm -f ${fBufrTmp} | true

View File

@ -0,0 +1,77 @@
# 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.
#
# Python implementation: bufr_get_keys
#
# Description: how to read values of different type of keys from BUFR messages.
#
#
import traceback
import sys
from eccodes import *
INPUT='../../data/bufr/syno_1.bufr'
VERBOSE=1 # verbose error reporting
def example():
# open bufr file
f = open(INPUT)
cnt=0
# loop for the messages in the file
while 1:
# get handle for message
gid = codes_bufr_new_from_file(f)
if gid is None: break
print "message: %s" % cnt
# we need to instruct ecCodes to expand all the descriptors
# i.e. unpack the data values
codes_set(gid,'unpack',1);
#-----------------------------------
# get all the expanded data values
#-----------------------------------
key='numericValues'
# get size
num=codes_get_size(gid,key)
print ' size of %s is: %s' % (key,num)
# get values
values=codes_get_array(gid,key)
for i in xrange(len(values)):
print " %d %.10e" % (i+1,values[i])
cnt+=1
# delete handle
codes_release(gid)
# close the file
f.close()
def main():
try:
example()
except CodesInternalError,err:
if VERBOSE:
traceback.print_exc(file=sys.stderr)
else:
print >>sys.stderr,err.msg
return 1
if __name__ == "__main__":
sys.exit(main())

View File

@ -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
#Define a common label for all the tmp files
label="bufr_expanded_test_p"
#Prepare tmp file
fTmp=${label}.tmp.txt
rm -f $fTmp | true
#-----------------------------------------------------
# Test reading the expanded values
#----------------------------------------------------
#f=${data_dir}/bufr/syno_1.bufr
REDIRECT=/dev/null
#We check "syno_1.bufr". The path is
#hardcoded in the example
#Write the values into a file
$PYTHON bufr_expanded.py >$fTmp 2> $REDIRECT
#TODO: add a better check. It could be compared against the bufrdc
# references.
#Check if there is any output
[ -s ${fTmp} ]
#cat $fTmp
#Clean up
rm -f ${fTmp}

View File

@ -0,0 +1,120 @@
# 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.
#
# Python implementation: bufr_get_keys
#
# Description: how to read values of different type of keys from BUFR messages.
#
#
import traceback
import sys
from eccodes import *
INPUT='../../data/bufr/syno_multi.bufr'
VERBOSE=1 # verbose error reporting
def example():
# open bufr file
f = open(INPUT)
cnt=0
# loop for the messages in the file
while 1:
# get handle for message
gid = codes_bufr_new_from_file(f)
if gid is None: break
print "message: %s" % cnt
# we need to instruct ecCodes to expand all the descriptors
# i.e. unpack the data values
codes_set(gid,'unpack',1);
#----------------------------------------------
# get values for keys holding a single value
#----------------------------------------------
#Native type integer
key='blockNumber'
if not codes_is_defined(gid,key):
raise Exception("Key: " + key + " was not defined")
print ' %s: %s' % (key,codes_get(gid,key))
#Native type integer
key='stationNumber'
if not codes_is_defined(gid,key):
raise Exception("Key: " + key + " was not defined")
print ' %s: %s' % (key,codes_get(gid,key))
#Native type float
key='airTemperatureAt2M'
if not codes_is_defined(gid,key):
raise Exception("Key: " + key + " was not defined")
print ' %s: %s' % (key,codes_get(gid,key))
#Native type string
key='typicalDate'
if not codes_is_defined(gid,key):
raise Exception("Key: " + key + " was not defined")
print ' %s: %s' % (key,codes_get(gid,key))
#---------------------------------
# get values for an array
#---------------------------------
# Native type integer
key='bufrdcExpandedDescriptors'
# get size
num=codes_get_size(gid,key)
print ' size of %s is: %s' % (key,num)
# get values
values=codes_get_array(gid,key)
for i in xrange(len(values)):
print " %d %06d" % (i+1,values[i])
# Native type float
key='numericValues'
# get size
num=codes_get_size(gid,key)
print ' size of %s is: %s' % (key,num)
# get values
values=codes_get_array(gid,key)
for i in xrange(len(values)):
print " %d %.10e" % (i+1,values[i])
cnt+=1
# delete handle
codes_release(gid)
# close the file
f.close()
def main():
try:
example()
except CodesInternalError,err:
if VERBOSE:
traceback.print_exc(file=sys.stderr)
else:
print >>sys.stderr,err.msg
return 1
if __name__ == "__main__":
sys.exit(main())

View File

@ -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_get_keys_test_p"
#Define tmp file
fTmp=${label}.tmp.txt
rm -f $fTmp | true
#We check "syno_multi.bufr". The path is
#hardcoded in the example
REDIRECT=/dev/null
#Write the key values into a file
$PYTHON bufr_get_keys.py 2> $REDIRECT > $fTmp
#TODO: check the results
#cat $fTmp
#Clean up
rm -f $fTmp | true

View File

@ -0,0 +1,87 @@
# 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.
#
# Python implementation: bufr_keys_iterator
#
# Description: Example on how to use keys_iterator functions and the
# codes_keys_iterator structure to get all the available
# keys in a BUFR message.
import traceback
import sys
from eccodes import *
INPUT='../../data/bufr/syno_1.bufr'
VERBOSE=1 # verbose error reporting
def example():
# open bufr file
f = open(INPUT)
cnt=0
# loop for the messages in the file
while 1:
# get handle for message
gid = codes_bufr_new_from_file(f)
if gid is None: break
print "message: %s" % cnt
# we need to instruct ecCodes to expand all the descriptors
# i.e. unpack the data values
#codes_set(gid,'unpack',1);
# get key iterator for a given namespace
iterid = codes_keys_iterator_new(gid,'ls')
# Different types of keys can be skipped
# codes_skip_computed(iterid)
# codes_skip_coded(iterid)
# codes_skip_edition_specific(iterid)
# codes_skip_duplicates(iterid)
# codes_skip_read_only(iterid)
# codes_skip_function(iterid)
#loop over the keys
while codes_keys_iterator_next(iterid):
# print key name
keyname = codes_keys_iterator_get_name(iterid)
#keyval = codes_get_string(iterid,keyname)
#print "%s = %s" % (keyname,keyval)
print " %s" % keyname
# delete the key iterator
codes_keys_iterator_delete(iterid)
cnt+=1
# delete handle
codes_release(gid)
# close the file
f.close()
def main():
try:
example()
except CodesInternalError,err:
if VERBOSE:
traceback.print_exc(file=sys.stderr)
else:
print >>sys.stderr,err.msg
return 1
if __name__ == "__main__":
sys.exit(main())

View File

@ -0,0 +1,33 @@
#!/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_keys_iterator_test_p"
#Define tmp file
fTmp=${label}".tmp.txt"
rm -f $fTmp | true
REDIRECT=/dev/null
#The bufr file to use
f=${data_dir}/bufr/syno_1.bufr
#The input ($f) is hardcoded in the example!!!
$PYTHON bufr_keys_iterator.py 2> $REDIRECT > $fTmp
#TODO: check the output
#cat $fTmp
#Clean up
rm -f $fTmp | true

View File

@ -6,6 +6,13 @@
# 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_print_data
#
# Description: how to read data values from BUFR messages.
#
#
import traceback
import sys

View File

@ -6,6 +6,14 @@
# 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_print_header
#
# Description: how to read the header from BUFR messages.
#
#
import traceback
import sys
@ -15,8 +23,11 @@ INPUT='../../data/bufr/syno_multi.bufr'
VERBOSE=1 # verbose error reporting
def example():
# open bufr file
f = open(INPUT)
# define the keys to be printed
keys = [
'dataCategory',
'dataSubCategory',
@ -29,20 +40,26 @@ def example():
]
cnt=0
# loop for the messages in the file
while 1:
# get handle for message
gid = codes_bufr_new_from_file(f)
if gid is None: break
print "message: %s" % cnt
# print the values for the selected keys from the message
for key in keys:
if not codes_is_defined(gid,key): raise Exception("Key was not defined")
if not codes_is_defined(gid,key): raise Exception("Key " + key + " was not defined")
print ' %s: %s' % (key,codes_get(gid,key))
cnt+=1
# delete handle
codes_release(gid)
# close the file
f.close()
def main():

View File

@ -0,0 +1,87 @@
# 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.
#
# Python implementation: bufr_set_keys
#
# Description: how to set different type of keys in BUFR messages.
#
#
import traceback
import sys
from eccodes import *
INPUT='../../data/bufr/syno_multi.bufr'
OUTPUT='bufr_set_keys_test_p.tmp.bufr'
VERBOSE=1 # verbose error reporting
def example():
# open bufr file
fin = open(INPUT)
# open otput bufr file
fout = open(OUTPUT,'w')
cnt=0
# loop for the messages in the file
while 1:
# get handle for message
gid = codes_bufr_new_from_file(fin)
if gid is None: break
print "message: %s" % cnt
# we need to instruct ecCodes to expand all the descriptors
# i.e. unpack the data values
#codes_set(gid,'unpack',1);
# This is the place where you may wish to modify the message
# E.g. we change the centre and 2m temperature
# set centre
val=222
print ' set centre to: %d' % val
key='centre'
if not codes_is_defined(gid,key):
raise Exception("Key: " + key + " was not defined")
codes_set(gid,key,val)
#check centre's value
print ' %s''s new value is: %d' % (key,codes_get(gid,key))
# write modified message to output
codes_write(gid,fout)
cnt+=1
# delete handle
codes_release(gid)
fin.close()
fout.close()
def main():
try:
example()
except CodesInternalError,err:
if VERBOSE:
traceback.print_exc(file=sys.stderr)
else:
print >>sys.stderr,err.msg
return 1
if __name__ == "__main__":
sys.exit(main())

View File

@ -0,0 +1,46 @@
#!/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_set_keys_test_p"
#Define tmp file
fBufrTmp=${label}.tmp.bufr
rm -f $fBufrTmp | true
#The bufr file to change
f=${data_dir}/bufr/syno_multi.bufr
REDIRECT=/dev/null
#The input ($f) and output ($fBufrTmp) are hardcoded in the f90 example!!!
$PYTHON bufr_set_keys.py 2> $REDIRECT > $REDIRECT
#Compare modified to the original
set +e
${tools_dir}/bufr_compare $f $fBufrTmp >$REDIRECT 2> $REDIRECT
#Check if modified is different
if [ $? -eq 0 ]; then
echo "setting keys produced identical files " >&2
exit 1
fi
set -e
#Check if modified has the same number of messages
[ `${tools_dir}bufr_count $f` -eq `${tools_dir}/bufr_count ${fBufrTmp}` ]
#Clean up
rm -f $fBufrTmp | true

View File

@ -0,0 +1,89 @@
# 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.
#
# Python implementation: bufr_subset
#
# Description: how to read data values from a given subset of a BUFR message.
#
#
import traceback
import sys
from eccodes import *
INPUT='../../data/bufr/synop_multi_subset.bufr'
VERBOSE=1 # verbose error reporting
def example():
# open bufr file
f = open(INPUT)
cnt=0
# loop for the messages in the file
while 1:
# get handle for message
gid = codes_bufr_new_from_file(f)
if gid is None: break
print "message: %s" % cnt
# we need to instruct ecCodes to expand all the descriptors
# i.e. unpack the data values
codes_set(gid,'unpack',1);
# find out the number of subsets
key='numberOfSubsets'
numberOfSubsets=codes_get(gid,'numberOfSubsets')
print ' %s: %d' % (key,numberOfSubsets)
# loop over the subsets
for i in range(numberOfSubsets) :
#specify the subset number
codes_set(gid,'subsetNumber',0)
# read and print some data values
key='blockNumber'
val=codes_get(gid,key)
print ' %s: %d' % (key,val)
key='stationNumber'
val=codes_get(gid,key)
print ' %s: %d' % (key,val)
#key='airTemperatureAt2M'
#val=codes_get(gid,key)
#print ' %d: %d' % (key,val)
cnt+=1
# delete handle
codes_release(gid)
# close the file
f.close()
def main():
try:
example()
except CodesInternalError,err:
if VERBOSE:
traceback.print_exc(file=sys.stderr)
else:
print >>sys.stderr,err.msg
return 1
if __name__ == "__main__":
sys.exit(main())

34
examples/python/bufr_subset.sh Executable file
View File

@ -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_subset_test_p"
#Prepare tmp file
fTmp=${label}.tmp.txt
rm -f $fTmp | true
#We check "synop_multi_subset.bufr". The path is
#hardcoded in the example
REDIRECT=/dev/null
#
$PYTHON bufr_subset.py 2> $REDIRECT > $fTmp
#TODO: add a proper check when subsets are properly implemented
#cat $fTmp
#Clean up
rm -f $fTmp