From bce6d803766f8d76ef84d1fa47aaccd5ef8bfbcc Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Fri, 3 Feb 2017 15:44:03 +0000 Subject: [PATCH] ECC-424: codes_bufr_keys_iterator rework (part 1) --- examples/C/bufr_keys_iterator.c | 2 +- src/bufr_keys_iterator.c | 45 ++++++++++++++------------------- src/bufr_util.c | 8 +++--- src/eccodes.h | 12 +++------ src/grib_api.h | 13 +++++++--- src/grib_api_internal.h | 16 +++++++++++- src/grib_api_prototypes.h | 14 +++++----- tests/bufr_keys_iter.c | 5 ++-- 8 files changed, 60 insertions(+), 55 deletions(-) diff --git a/examples/C/bufr_keys_iterator.c b/examples/C/bufr_keys_iterator.c index f9fd5be5c..9abf8e4a2 100644 --- a/examples/C/bufr_keys_iterator.c +++ b/examples/C/bufr_keys_iterator.c @@ -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++; diff --git a/src/bufr_keys_iterator.c b/src/bufr_keys_iterator.c index 2c4177943..8faa8cb31 100644 --- a/src/bufr_keys_iterator.c +++ b/src/bufr_keys_iterator.c @@ -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; diff --git a/src/bufr_util.c b/src/bufr_util.c index a0403a938..11929f403 100644 --- a/src/bufr_util.c +++ b/src/bufr_util.c @@ -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; } diff --git a/src/eccodes.h b/src/eccodes.h index 90c4ac276..2e5d1e407 100644 --- a/src/eccodes.h +++ b/src/eccodes.h @@ -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); diff --git a/src/grib_api.h b/src/grib_api.h index fc3e4e15a..f0f18a155 100644 --- a/src/grib_api.h +++ b/src/grib_api.h @@ -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); diff --git a/src/grib_api_internal.h b/src/grib_api_internal.h index e5dfa4f63..c53f186f5 100644 --- a/src/grib_api_internal.h +++ b/src/grib_api_internal.h @@ -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 */ diff --git a/src/grib_api_prototypes.h b/src/grib_api_prototypes.h index 56944a04e..648fa8bad 100644 --- a/src/grib_api_prototypes.h +++ b/src/grib_api_prototypes.h @@ -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); diff --git a/tests/bufr_keys_iter.c b/tests/bufr_keys_iter.c index 89c317f51..b8e962d85 100644 --- a/tests/bufr_keys_iter.c +++ b/tests/bufr_keys_iter.c @@ -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);