Add C,F90 and python examples ECC-15

This commit is contained in:
Sandor Kertesz 2015-02-09 12:30:11 +00:00
parent 8d86117cdb
commit 968e15d806
13 changed files with 349 additions and 12 deletions

View File

@ -86,6 +86,7 @@ int main(int argc,char* argv[])
printf(" %s\n",name);
}
/* delete key iterator */
codes_keys_iterator_delete(kiter);
/* delete handle */

View File

@ -18,15 +18,11 @@ label="bufr_keys_iterator_test_c"
fTmp=${label}".tmp.txt"
rm -f $fTmp | true
REDIRECT=/dev/null
f=${data_dir}/bufr/syno_1.bufr
#We check "syno_multi.bufr". The path is
#hardcoded in the example
REDIRECT=/dev/null
#Write the values into a file and compare with reference
#Dump the keys
${examples_dir}/bufr_keys_iterator $f 2> $REDIRECT > $fTmp
#TODO: check the output

View File

@ -31,6 +31,7 @@ int main(int argc,char* argv[])
long numberOfSubsets=0;
long longVal;
/*double doubleVal;*/
int i,err=0;
int cnt=0;
char* infile = "../../data/bufr/synop_multi_subset.bufr";
@ -60,7 +61,7 @@ int main(int argc,char* argv[])
printf(" numberOfSubsets: %ld\n",numberOfSubsets);
/* loop over the subsets */
for(i=0; i < numberOfSubsets; i++)
for(i=1; i <= numberOfSubsets; i++)
{
/* specify the subset number */
CODES_CHECK(codes_set_long(h,"subsetNumber",0),0);
@ -72,7 +73,7 @@ int main(int argc,char* argv[])
CODES_CHECK(codes_get_long(h,"stationNumber",&longVal),0);
printf(" stationNumber: %ld\n",longVal);
/* CODES_CHECK(codes_get_double(h,"airTemperatureAt2M",&doubleVal),0);
/*CODES_CHECK(codes_get_double(h,"airTemperatureAt2M",&doubleVal),0);
printf(" airTemperatureAt2M %f\n",doubleVal);*/
}

View File

@ -32,9 +32,11 @@ list( APPEND tests
bufr_clone
bufr_expanded
bufr_get_keys
bufr_keys_iterator
bufr_print_header
bufr_print_data
bufr_set_keys
bufr_subset
)
# Simplify tests: no need to build executable and then test. All in one

View File

@ -4,13 +4,14 @@ AM_CFLAGS = @WARN_PEDANTIC@ @WERROR@ @FORCE_32_CFLAGS@
TESTS = copy_message.sh get.sh get_data.sh get_pl.sh get_pv.sh keys_iterator.sh \
nearest.sh precision.sh multi_write.sh multi.sh print_data.sh set.sh set_bitmap.sh set_missing.sh \
set_pv.sh samples.sh count_messages.sh read_message.sh read_from_file.sh index.sh get_set_uuid.sh \
bufr_clone.sh bufr_expanded.sh bufr_get_keys.sh bufr_print_header.sh bufr_print_data.sh f_bufr_set_keys.sh
bufr_clone.sh bufr_expanded.sh bufr_get_keys.sh bufr_print_header.sh bufr_print_data.sh bufr_set_keys.sh \
bufr_keys_iterator.sh bufr_subset.sh
noinst_PROGRAMS = f_index f_copy_message f_get f_get_data f_get_pl f_get_pv f_keys_iterator \
f_multi_write f_multi f_nearest f_precision f_print_data f_set f_set_bitmap f_set_missing \
f_set_pv f_samples f_count_messages f_read_message f_read_from_file f_new_from_file \
f_copy_namespace f_get_set_uuid f_set_gvc f_clone f_bufr_clone f_bufr_expanded f_bufr_get_keys \
f_bufr_print_header f_bufr_print_data f_bufr_set_keys
f_bufr_print_header f_bufr_print_data f_bufr_set_keys f_bufr_keys_iterator f_bufr_subset
f_index_SOURCES=index.f90
f_copy_message_SOURCES=copy_message.f90
@ -40,9 +41,11 @@ f_clone_SOURCES=clone.f90
f_bufr_clone_SOURCES=bufr_clone.f90
f_bufr_expanded_SOURCES=bufr_expanded.f90
f_bufr_get_keys_SOURCES=bufr_get_keys.f90
f_bufr_keys_iterator_SOURCES=bufr_keys_iterator.f90
f_bufr_print_header_SOURCES=bufr_print_header.f90
f_bufr_print_data_SOURCES=bufr_print_data.f90
f_bufr_set_keys_SOURCES=bufr_set_keys.f90
f_bufr_subset_SOURCES=bufr_subset.f90
INCLUDES = -I$(top_builddir)/src

View File

@ -0,0 +1,84 @@
!
!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.
!
!
! FOTRAN 90 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.
!
!
program bufr_keys_iterator
use eccodes
implicit none
integer :: ifile
integer :: iret
integer :: ibufr
integer :: count=0
character(len=20) :: name_space
character(len=256) :: key
integer :: kiter
call codes_open_file(ifile,'../../data/bufr/syno_1.bufr','r')
! name_space='' to get all the keys
name_space='ls'
! the first bufr message is loaded from file
! ibufr is the bufr id to be used in subsequent calls
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
! create key iterator
call codes_keys_iterator_new(ibufr,kiter,name_space,iret)
if (iret .ne. 0) then
write(*,*) 'ERROR: Unable to create keys iterator'
call exit(1)
end if
! get first key
call codes_keys_iterator_next(kiter, iret)
! loop over keys
do while (iret == 1)
! print key name
call codes_keys_iterator_get_name(kiter,key)
write(*,*) ' ',trim(key)
! get next key
call codes_keys_iterator_next(kiter, iret)
end do
! delete key iterator
call grib_keys_iterator_delete(kiter)
! release the bufr message
call codes_release(ibufr)
! load the next bufr message
call codes_bufr_new_from_file(ifile,ibufr,iret)
count=count+1
end do
! close file
call codes_close_file(ifile)
end program bufr_keys_iterator

View File

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

View File

@ -0,0 +1,79 @@
!
!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.
!
!
! FOTRAN 90 Implementation: bufr_subset
!
! Description: how to read data values from a given subset of a BUFR message.
!
!
program bufr_subset
use eccodes
implicit none
integer :: ifile
integer :: iret
integer :: ibufr
integer :: i, count=0
integer(kind=4) :: numberOfSubsets
integer(kind=4) :: blockNumber,stationNumber
!real(kind=8) :: t2m
call codes_open_file(ifile,'../../data/bufr/synop_multi_subset.bufr','r')
! the first bufr message is loaded from file
! ibufr is the bufr id to be used in subsequent calls
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);
! find out the number of subsets
call codes_get(ibufr,'numberOfSubsets',numberOfSubsets)
write(*,*) ' numberOfSubsets:',numberOfSubsets
! loop over the subsets
do i=1,numberOfSubsets
! specify the subset number
call codes_set(ibufr,'subsetNumber',0)
! read and print some data values
call codes_get(ibufr,'blockNumber',blockNumber);
write(*,*) ' blockNumber:',blockNumber
call codes_get(ibufr,'stationNumber',stationNumber);
write(*,*) ' stationNumber:',stationNumber
!call codes_get(ibufr,'airTemperatureAt2M',t2m);
!write(*,*) ' airTemperatureAt2M:',t2m
end do
! release the bufr message
call codes_release(ibufr)
! load the next bufr message
call codes_bufr_new_from_file(ifile,ibufr,iret)
count=count+1
end do
! close file
call codes_close_file(ifile)
end program bufr_subset

36
examples/F90/bufr_subset.sh Executable file
View File

@ -0,0 +1,36 @@
#!/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_f"
#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
#Write the values into a file and compare with reference
${examples_dir}/f_bufr_subset 2> $REDIRECT > $fTmp
#TODO: add a proper check when subsets are properly implemented
#We compare output to the reference by ignoring the whitespaces
#diff -w $fRef $fTmp >$REDIRECT 2> $REDIRECT
#cat $fTmp
#Clean up
rm -f $fTmp

View File

@ -40,6 +40,7 @@ list( APPEND tests
binary_message
set_bitmap
bufr_print_header
bufr_print_data
)
foreach( test ${tests} )
ecbuild_add_test( TARGET p_${test}_test

View File

@ -2,7 +2,7 @@ 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
samples.sh set.sh set_missing.sh binary_message.sh set_bitmap.sh bufr_print_header.sh bufr_print_data.sh
TESTS_ENVIRONMENT = TOPBUILDDIR=$(top_builddir) PYTHON=$(PYTHON)
noinst_PROGRAMS = p_keys_iterator p_print_data p_iterator p_count_messages
@ -16,6 +16,6 @@ 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_header.py bufr_print_data.py \
CMakeLists.txt include.ctest.sh.in
endif

View File

@ -0,0 +1,68 @@
# 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.
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)
# define the keys to be printed
keys = [
'blockNumber',
'stationNumber',
'airTemperatureAt2M',
]
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);
# print the values for the selected keys from the message
for key in keys:
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():
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_print_data_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 values into a file and compare with reference
$PYTHON bufr_print_data.py 2> $REDIRECT > $fTmp
#TODO: check the output
#cat $fTmp
#Clean up
rm -f $fTmp | true