mirror of https://github.com/ecmwf/eccodes.git
ECC-418: Fortran and Python codes_bufr_keys_iterator (Part 3: C example)
This commit is contained in:
parent
c0165af67b
commit
940cee8c37
|
@ -12,8 +12,8 @@
|
||||||
* C Implementation: bufr_keys_iterator
|
* C Implementation: bufr_keys_iterator
|
||||||
*
|
*
|
||||||
* Description: 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
|
* codes_bufr_keys_iterator structure to get all the available
|
||||||
* keys in a BUFR message.
|
* 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.*/
|
/* message handle. Required in all the eccodes calls acting on a message.*/
|
||||||
codes_handle* h=NULL;
|
codes_handle* h=NULL;
|
||||||
int err=0, cnt=0;
|
int err=0, cnt=0;
|
||||||
int keyType;
|
int keyType = 0;
|
||||||
|
|
||||||
/* 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;
|
|
||||||
|
|
||||||
char value[MAX_VAL_LEN];
|
char value[MAX_VAL_LEN];
|
||||||
size_t vlen=MAX_VAL_LEN;
|
size_t vlen=MAX_VAL_LEN;
|
||||||
|
@ -65,7 +50,7 @@ int main(int argc,char* argv[])
|
||||||
return 1;
|
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)
|
while ((h = codes_handle_new_from_file(NULL,in,PRODUCT_BUFR,&err)) != NULL || err != CODES_SUCCESS)
|
||||||
{
|
{
|
||||||
codes_keys_iterator* kiter=NULL;
|
codes_keys_iterator* kiter=NULL;
|
||||||
|
@ -77,21 +62,21 @@ int main(int argc,char* argv[])
|
||||||
|
|
||||||
printf("message: %d\n",cnt);
|
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 */
|
/* we need to instruct ecCodes to unpack the data values */
|
||||||
CODES_CHECK(codes_set_long(h,"unpack",1),0);
|
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 */
|
/* loop over the keys */
|
||||||
while(codes_keys_iterator_next(kiter))
|
while(codes_bufr_keys_iterator_next(kiter))
|
||||||
{
|
{
|
||||||
/* get key name*/
|
/* get key name. This needs to be free'd later */
|
||||||
const char* name = codes_keys_iterator_get_name(kiter);
|
char* name = codes_bufr_keys_iterator_get_name(kiter);
|
||||||
printf(" %s=",name);
|
printf(" %s=",name);
|
||||||
|
|
||||||
CODES_CHECK(codes_get_native_type(h,name,&keyType),0);
|
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 */
|
/* get key size to see if it is an array */
|
||||||
CODES_CHECK(codes_get_size(h,name,&klen),0);
|
CODES_CHECK(codes_get_size(h,name,&klen),0);
|
||||||
|
|
||||||
/* not array */
|
if (klen ==1)
|
||||||
if(klen ==1)
|
{
|
||||||
{
|
/* not array */
|
||||||
vlen=MAX_VAL_LEN;
|
vlen=MAX_VAL_LEN;
|
||||||
bzero(value,vlen);
|
bzero(value,vlen);
|
||||||
|
|
||||||
codes_get_string(h,name,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);
|
printf("(array of %ld)\n",klen);
|
||||||
|
}
|
||||||
|
free(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* delete key iterator */
|
/* delete key iterator */
|
||||||
codes_keys_iterator_delete(kiter);
|
codes_bufr_keys_iterator_delete(kiter);
|
||||||
|
|
||||||
/* delete handle */
|
/* delete handle */
|
||||||
codes_handle_delete(h);
|
codes_handle_delete(h);
|
||||||
|
|
|
@ -17,7 +17,7 @@ label="bufr_keys_iterator_test_c"
|
||||||
|
|
||||||
#Define tmp file
|
#Define tmp file
|
||||||
fTmp=${label}".tmp.txt"
|
fTmp=${label}".tmp.txt"
|
||||||
rm -f $fTmp | true
|
rm -f $fTmp
|
||||||
|
|
||||||
REDIRECT=/dev/null
|
REDIRECT=/dev/null
|
||||||
|
|
||||||
|
@ -27,8 +27,8 @@ f=${data_dir}/bufr/syno_1.bufr
|
||||||
${examples_dir}c_bufr_keys_iterator $f 2> $REDIRECT > $fTmp
|
${examples_dir}c_bufr_keys_iterator $f 2> $REDIRECT > $fTmp
|
||||||
|
|
||||||
#TODO: check the output
|
#TODO: check the output
|
||||||
|
grep -q '#5#cloudAmount->percentConfidence=70' $fTmp
|
||||||
#cat $fTmp
|
grep -q 'unexpandedDescriptors=(array of 10)' $fTmp
|
||||||
|
|
||||||
#Clean up
|
#Clean up
|
||||||
rm -f $fTmp | true
|
rm -f $fTmp
|
||||||
|
|
Loading…
Reference in New Issue