ECC-418: Fortran and Python codes_bufr_keys_iterator (Part 3: C example)

This commit is contained in:
Shahram Najm 2017-01-26 17:45:46 +00:00
parent c0165af67b
commit 940cee8c37
2 changed files with 27 additions and 41 deletions

View File

@ -12,8 +12,8 @@
* C Implementation: bufr_keys_iterator
*
* Description: how to use keys_iterator functions and the
* codes_keys_iterator structure to get all the available
* keys in a BUFR message.
* codes_bufr_keys_iterator structure to get all the available
* keys in a BUFR message.
*
*/
@ -34,22 +34,7 @@ int main(int argc,char* argv[])
/* message handle. Required in all the eccodes calls acting on a message.*/
codes_handle* h=NULL;
int err=0, cnt=0;
int keyType;
/* 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. Other namespaces are e.g. "ls" */
char* name_space=0;
int keyType = 0;
char value[MAX_VAL_LEN];
size_t vlen=MAX_VAL_LEN;
@ -65,7 +50,7 @@ int main(int argc,char* argv[])
return 1;
}
/* loop over the messages in the bufr file */
/* loop over the messages in the BUFR file */
while ((h = codes_handle_new_from_file(NULL,in,PRODUCT_BUFR,&err)) != NULL || err != CODES_SUCCESS)
{
codes_keys_iterator* kiter=NULL;
@ -77,21 +62,21 @@ int main(int argc,char* argv[])
printf("message: %d\n",cnt);
/* get key iterator */
kiter=codes_keys_iterator_new(h,key_iterator_filter_flags,name_space);
if (!kiter) {
printf("ERROR: Unable to create keys iterator\n");
exit(1);
}
/* we need to instruct ecCodes to unpack the data values */
CODES_CHECK(codes_set_long(h,"unpack",1),0);
/* get BUFR key iterator */
kiter=codes_bufr_keys_iterator_new(h);
if (!kiter) {
printf("ERROR: Unable to create BUFR keys iterator\n");
exit(1);
}
/* loop over the keys */
while(codes_keys_iterator_next(kiter))
while(codes_bufr_keys_iterator_next(kiter))
{
/* get key name*/
const char* name = codes_keys_iterator_get_name(kiter);
/* get key name. This needs to be free'd later */
char* name = codes_bufr_keys_iterator_get_name(kiter);
printf(" %s=",name);
CODES_CHECK(codes_get_native_type(h,name,&keyType),0);
@ -99,22 +84,23 @@ int main(int argc,char* argv[])
/* get key size to see if it is an array */
CODES_CHECK(codes_get_size(h,name,&klen),0);
/* not array */
if(klen ==1)
{
if (klen ==1)
{
/* not array */
vlen=MAX_VAL_LEN;
bzero(value,vlen);
codes_get_string(h,name,value,&vlen);
printf("%s\n",value);
printf("%s\n",value);
}
/* for arrays */
else
else {
/* for arrays */
printf("(array of %ld)\n",klen);
}
free(name);
}
/* delete key iterator */
codes_keys_iterator_delete(kiter);
codes_bufr_keys_iterator_delete(kiter);
/* delete handle */
codes_handle_delete(h);

View File

@ -17,7 +17,7 @@ label="bufr_keys_iterator_test_c"
#Define tmp file
fTmp=${label}".tmp.txt"
rm -f $fTmp | true
rm -f $fTmp
REDIRECT=/dev/null
@ -27,8 +27,8 @@ f=${data_dir}/bufr/syno_1.bufr
${examples_dir}c_bufr_keys_iterator $f 2> $REDIRECT > $fTmp
#TODO: check the output
#cat $fTmp
grep -q '#5#cloudAmount->percentConfidence=70' $fTmp
grep -q 'unexpandedDescriptors=(array of 10)' $fTmp
#Clean up
rm -f $fTmp | true
rm -f $fTmp