ECC-424: codes_bufr_keys_iterator rework (part 1)

This commit is contained in:
Shahram Najm 2017-02-03 15:44:03 +00:00
parent 804e9ac571
commit bce6d80376
8 changed files with 60 additions and 55 deletions

View File

@ -53,7 +53,7 @@ int main(int argc,char* argv[])
/* 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;
codes_bufr_keys_iterator* kiter=NULL;
if (h == NULL) {
printf("Error: unable to create handle for message %d\n",cnt);
cnt++;

View File

@ -10,17 +10,18 @@
#include "grib_api_internal.h"
grib_keys_iterator* codes_bufr_keys_iterator_new(grib_handle* h)
bufr_keys_iterator* codes_bufr_keys_iterator_new(grib_handle* h)
{
grib_keys_iterator* ki=NULL;
bufr_keys_iterator* ki=NULL;
if (!h) return NULL;
ki= (grib_keys_iterator*)grib_context_malloc_clear(h->context,sizeof(grib_keys_iterator));
ki= (bufr_keys_iterator*)grib_context_malloc_clear(h->context,sizeof(bufr_keys_iterator));
if (!ki) return NULL;
ki->filter_flags = 0;
ki->handle = h;
Assert(h->product_kind==PRODUCT_BUFR);
ki->names = NULL;
ki->i_curr_attribute=0;
ki->accessor_flags_only= GRIB_ACCESSOR_FLAG_DUMP;
ki->accessor_flags_skip= GRIB_ACCESSOR_FLAG_HIDDEN | GRIB_ACCESSOR_FLAG_READ_ONLY;
@ -33,17 +34,17 @@ grib_keys_iterator* codes_bufr_keys_iterator_new(grib_handle* h)
return ki;
}
grib_keys_iterator* codes_bufr_data_section_keys_iterator_new(grib_handle* h)
bufr_keys_iterator* codes_bufr_data_section_keys_iterator_new(grib_handle* h)
{
grib_keys_iterator* ki=NULL;
bufr_keys_iterator* ki=NULL;
if (!h) return NULL;
ki= (grib_keys_iterator*)grib_context_malloc_clear(h->context,sizeof(grib_keys_iterator));
ki= (bufr_keys_iterator*)grib_context_malloc_clear(h->context,sizeof(bufr_keys_iterator));
if (!ki) return NULL;
ki->filter_flags = 0;
ki->handle = h;
Assert(h->product_kind==PRODUCT_BUFR);
ki->i_curr_attribute=0;
ki->accessor_flags_only= GRIB_ACCESSOR_FLAG_BUFR_DATA | GRIB_ACCESSOR_FLAG_DUMP;
ki->accessor_flags_skip= GRIB_ACCESSOR_FLAG_HIDDEN | GRIB_ACCESSOR_FLAG_READ_ONLY;
@ -56,7 +57,7 @@ grib_keys_iterator* codes_bufr_data_section_keys_iterator_new(grib_handle* h)
return ki;
}
static void mark_seen(grib_keys_iterator* ki,const char* name)
static void mark_seen(bufr_keys_iterator* ki,const char* name)
{
int* r=(int*)grib_trie_get(ki->seen,name);
@ -68,20 +69,13 @@ static void mark_seen(grib_keys_iterator* ki,const char* name)
}
}
/*
static int was_seen(grib_keys_iterator* ki,const char* name)
{
return grib_trie_get(ki->seen,name) != NULL;
}
*/
int codes_bufr_keys_iterator_rewind(grib_keys_iterator* ki)
int codes_bufr_keys_iterator_rewind(bufr_keys_iterator* ki)
{
ki->at_start = 1;
return GRIB_SUCCESS;
}
static int skip(grib_keys_iterator* kiter)
static int skip(bufr_keys_iterator* kiter)
{
if(kiter->current->sub_section)
return 1;
@ -101,7 +95,7 @@ static int skip(grib_keys_iterator* kiter)
return 0;
}
static int next_attribute(grib_keys_iterator* kiter)
static int next_attribute(bufr_keys_iterator* kiter)
{
int *r=0;
int i_curr_attribute;
@ -142,10 +136,9 @@ static int next_attribute(grib_keys_iterator* kiter)
kiter->i_curr_attribute=0;
return next_attribute(kiter);
}
}
int codes_bufr_keys_iterator_next(grib_keys_iterator* kiter)
int codes_bufr_keys_iterator_next(bufr_keys_iterator* kiter)
{
if(kiter->at_start)
{
@ -176,7 +169,7 @@ int codes_bufr_keys_iterator_next(grib_keys_iterator* kiter)
return kiter->current != NULL;
}
char* codes_bufr_keys_iterator_get_name(grib_keys_iterator* kiter)
char* codes_bufr_keys_iterator_get_name(bufr_keys_iterator* kiter)
{
int *r=0;
char* ret=0;
@ -199,18 +192,18 @@ char* codes_bufr_keys_iterator_get_name(grib_keys_iterator* kiter)
return ret;
}
grib_accessor* codes_bufr_keys_iterator_get_accessor(grib_keys_iterator* kiter)
grib_accessor* codes_bufr_keys_iterator_get_accessor(bufr_keys_iterator* kiter)
{
return kiter->current;
}
int codes_bufr_keys_iterator_delete( grib_keys_iterator* kiter)
int codes_bufr_keys_iterator_delete(bufr_keys_iterator* kiter)
{
if (kiter) {
if(kiter->seen)
grib_trie_delete(kiter->seen);
if (kiter->name_space)
grib_context_free(kiter->handle->context,kiter->name_space);
/*if (kiter->name_space)
grib_context_free(kiter->handle->context,kiter->name_space);*/
grib_context_free(kiter->handle->context,kiter);
}
return 0;

View File

@ -53,7 +53,7 @@ int compute_bufr_key_rank(grib_handle* h, grib_string_list* keys, const char* ke
char** codes_bufr_copy_data_return_copied_keys(grib_handle* hin,grib_handle* hout, size_t* nkeys, int* err)
{
grib_keys_iterator* kiter=NULL;
bufr_keys_iterator* kiter=NULL;
char* name=0;
char** keys=NULL;
grib_sarray* k=0;
@ -85,13 +85,13 @@ char** codes_bufr_copy_data_return_copied_keys(grib_handle* hin,grib_handle* hou
/* Do the pack if something was copied */
*err=grib_set_long(hout, "pack", 1);
}
grib_keys_iterator_delete(kiter);
codes_bufr_keys_iterator_delete(kiter);
return keys;
}
int codes_bufr_copy_data(grib_handle* hin, grib_handle* hout)
{
grib_keys_iterator* kiter=NULL;
bufr_keys_iterator* kiter=NULL;
char* name=0;
int err=0;
int nkeys=0;
@ -121,6 +121,6 @@ int codes_bufr_copy_data(grib_handle* hin, grib_handle* hout)
err=grib_set_long(hout, "pack", 1);
}
grib_keys_iterator_delete(kiter);
codes_bufr_keys_iterator_delete(kiter);
return err;
}

View File

@ -164,6 +164,7 @@ typedef struct grib_points codes_points;
\struct codes_keys_iterator
*/
typedef struct grib_keys_iterator codes_keys_iterator;
typedef struct bufr_keys_iterator codes_bufr_keys_iterator;
typedef struct grib_fieldset codes_fieldset;
typedef struct grib_order_by codes_order_by;
@ -1098,14 +1099,12 @@ attributes or by the namespace they belong to.
/*! Create a new iterator from a valid and initialised handle.
* @param h : the handle whose keys you want to iterate
* @param filter_flags : flags to filter out some of the keys through their attributes
* @param name_space : if not null the iteration is carried out only on
* @param name_space : if not null the iteration is carried out only on
* keys belonging to the namespace passed. (NULL for all the keys)
* @return keys iterator ready to iterate through keys according to filter_flags
* and namespace
* and namespace
*/
codes_keys_iterator* codes_keys_iterator_new(codes_handle* h,unsigned long filter_flags, const char* name_space);
codes_keys_iterator* codes_bufr_keys_iterator_new(codes_handle* h);
codes_keys_iterator* codes_bufr_data_section_keys_iterator_new(codes_handle* h);
/* codes_bufr_copy_data copies all the values in the data section that are present in the same position in the data tree
* and with the same number of values to the output handle. Should not exit with error if the output handle has a different
@ -1121,7 +1120,6 @@ int codes_bufr_copy_data(grib_handle* hin, grib_handle* hout);
* @return 1 if next iterator exists, 0 if no more elements to iterate on
*/
int codes_keys_iterator_next(codes_keys_iterator *kiter);
int codes_bufr_keys_iterator_next(codes_keys_iterator *kiter);
/*! get the key name from the keys iterator
@ -1135,19 +1133,15 @@ const char* codes_keys_iterator_get_name(codes_keys_iterator *kiter);
* @return 0 if OK, integer value on error
*/
int codes_keys_iterator_delete( codes_keys_iterator* kiter);
int codes_bufr_keys_iterator_delete( codes_keys_iterator* kiter);
/*! Rewind the keys iterator.
* @param kiter : valid codes_keys_iterator
* @return 0 if OK, integer value on error
*/
int codes_keys_iterator_rewind(codes_keys_iterator* kiter);
int codes_bufr_keys_iterator_rewind(codes_keys_iterator* kiter);
char* codes_bufr_keys_iterator_get_name(codes_keys_iterator* kiter);
int codes_keys_iterator_set_flags(codes_keys_iterator *kiter,unsigned long flags);
int codes_keys_iterator_get_long(codes_keys_iterator *kiter, long *v, size_t *len);
int codes_keys_iterator_get_double(codes_keys_iterator *kiter, double *v, size_t *len);
int codes_keys_iterator_get_string(codes_keys_iterator *kiter, char *v, size_t *len);

View File

@ -209,7 +209,7 @@ typedef struct grib_points grib_points;
\ingroup keys_iterator
*/
typedef struct grib_keys_iterator grib_keys_iterator;
typedef struct bufr_keys_iterator bufr_keys_iterator;
typedef struct grib_fieldset grib_fieldset;
@ -1254,31 +1254,36 @@ attributes or by the namespace they belong to.
* and namespace
*/
grib_keys_iterator* grib_keys_iterator_new(grib_handle* h,unsigned long filter_flags, const char* name_space);
bufr_keys_iterator* codes_bufr_keys_iterator_new(grib_handle* h);
bufr_keys_iterator* codes_bufr_data_section_keys_iterator_new(grib_handle* h);
/*! Step to the next iterator.
* @param kiter : valid grib_keys_iterator
* @return 1 if next iterator exists, 0 if no more elements to iterate on
*/
int grib_keys_iterator_next(grib_keys_iterator *kiter);
int grib_keys_iterator_next(grib_keys_iterator* kiter);
int codes_bufr_keys_iterator_next(bufr_keys_iterator* kiter);
/*! get the key name from the iterator
* @param kiter : valid grib_keys_iterator
* @return key name
*/
const char* grib_keys_iterator_get_name(grib_keys_iterator *kiter);
char* codes_bufr_keys_iterator_get_name(bufr_keys_iterator* kiter);
/*! Delete the iterator.
* @param kiter : valid grib_keys_iterator
* @return 0 if OK, integer value on error
*/
int grib_keys_iterator_delete( grib_keys_iterator* kiter);
int grib_keys_iterator_delete(grib_keys_iterator* kiter);
int codes_bufr_keys_iterator_delete(bufr_keys_iterator* kiter);
/*! Rewind the iterator.
* @param kiter : valid grib_keys_iterator
* @return 0 if OK, integer value on error
*/
int grib_keys_iterator_rewind(grib_keys_iterator* kiter);
int codes_bufr_keys_iterator_rewind(bufr_keys_iterator* kiter);
int grib_keys_iterator_set_flags(grib_keys_iterator *kiter,unsigned long flags);

View File

@ -1348,7 +1348,7 @@ struct grib_action_file_list
grib_action_file * last ;
};
/* keys iterator common to grib and bufr*/
/* Common keys iterator */
struct grib_keys_iterator{
grib_handle *handle;
unsigned long filter_flags; /** flags to filter out accessors */
@ -1364,6 +1364,20 @@ struct grib_keys_iterator{
grib_trie *seen;
};
/* BUFR-specific keys iterator */
struct bufr_keys_iterator{
grib_handle* handle;
unsigned long accessor_flags_skip;
unsigned long accessor_flags_only;
grib_accessor* current;
grib_string_list* names;
int at_start;
int match;
int i_curr_attribute;
grib_accessor** attributes;
char* prefix;
grib_trie* seen;
};
/* ----------*/
/* md5 */

View File

@ -1144,13 +1144,13 @@ int grib_keys_iterator_get_bytes(grib_keys_iterator *kiter, unsigned char *v, si
int grib_keys_iterator_get_native_type(grib_keys_iterator *kiter);
/* bufr_keys_iterator.c */
grib_keys_iterator *codes_bufr_keys_iterator_new(grib_handle *h);
grib_keys_iterator *codes_bufr_data_section_keys_iterator_new(grib_handle *h);
int codes_bufr_keys_iterator_rewind(grib_keys_iterator *ki);
int codes_bufr_keys_iterator_next(grib_keys_iterator *kiter);
char *codes_bufr_keys_iterator_get_name(grib_keys_iterator *kiter);
grib_accessor *codes_bufr_keys_iterator_get_accessor(grib_keys_iterator *kiter);
int codes_bufr_keys_iterator_delete(grib_keys_iterator *kiter);
bufr_keys_iterator *codes_bufr_keys_iterator_new(grib_handle *h);
bufr_keys_iterator *codes_bufr_data_section_keys_iterator_new(grib_handle *h);
int codes_bufr_keys_iterator_rewind(bufr_keys_iterator *ki);
int codes_bufr_keys_iterator_next(bufr_keys_iterator *kiter);
char *codes_bufr_keys_iterator_get_name(bufr_keys_iterator *kiter);
grib_accessor *codes_bufr_keys_iterator_get_accessor(bufr_keys_iterator *kiter);
int codes_bufr_keys_iterator_delete(bufr_keys_iterator *kiter);
/* grib_parse_utils.c */
int grib_recompose_name(grib_handle *h, grib_accessor *observer, const char *uname, char *fname, int fail);

View File

@ -21,10 +21,10 @@ int main(int argc,char* argv[])
{
int err = 0;
codes_handle* h = NULL;
grib_keys_iterator* kiter = NULL;
codes_bufr_keys_iterator* kiter = NULL;
char* input_filename = NULL;
FILE* f = NULL;
grib_context* c = grib_context_get_default();
/*grib_context* c = grib_context_get_default();*/
if (argc!=2) usage(argv[0]);
input_filename = argv[1];
@ -41,7 +41,6 @@ int main(int argc,char* argv[])
{
char* kname = codes_bufr_keys_iterator_get_name(kiter);
printf("%s\n", kname);
grib_context_free(c, kname);
}
codes_bufr_keys_iterator_delete(kiter);
codes_handle_delete(h);