diff --git a/src/grib_accessor_class_bufr_string_values.c b/src/grib_accessor_class_bufr_string_values.c index e664fbe45..47f952153 100644 --- a/src/grib_accessor_class_bufr_string_values.c +++ b/src/grib_accessor_class_bufr_string_values.c @@ -9,10 +9,10 @@ */ /* Sequences and replication (not delayed) are resolved in this class. -Number of elements to which a delayed replication applies are recomputed to -take account of the expansion. -expanded descriptors cannot contain sequences and only delayed replication -can appear + Number of elements to which a delayed replication applies are recomputed to + take account of the expansion. + Expanded descriptors cannot contain sequences and only delayed replication + can appear */ #include "grib_api_internal.h" @@ -145,81 +145,84 @@ static void init_class(grib_accessor_class* c) static void init(grib_accessor* a, const long len , grib_arguments* args ) { - grib_accessor_bufr_string_values* self = (grib_accessor_bufr_string_values*)a; - int n=0; - self->dataAccessorName=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++); - self->dataAccessor=NULL; - a->length = 0; - a->flags |= GRIB_ACCESSOR_FLAG_READ_ONLY; + grib_accessor_bufr_string_values* self = (grib_accessor_bufr_string_values*)a; + int n=0; + self->dataAccessorName=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++); + self->dataAccessor=NULL; + a->length = 0; + a->flags |= GRIB_ACCESSOR_FLAG_READ_ONLY; } static void dump(grib_accessor* a, grib_dumper* dumper) { - grib_dump_string_array(dumper,a,NULL); + grib_dump_string_array(dumper,a,NULL); } -static grib_accessor* get_accessor(grib_accessor* a) { - grib_accessor_bufr_string_values* self = (grib_accessor_bufr_string_values*)a; - if (!self->dataAccessor) { - self->dataAccessor=grib_find_accessor(grib_handle_of_accessor(a),self->dataAccessorName); - } - return self->dataAccessor; +static grib_accessor* get_accessor(grib_accessor* a) +{ + grib_accessor_bufr_string_values* self = (grib_accessor_bufr_string_values*)a; + if (!self->dataAccessor) { + self->dataAccessor=grib_find_accessor(grib_handle_of_accessor(a),self->dataAccessorName); + } + return self->dataAccessor; } static int unpack_string_array (grib_accessor* a, char** buffer, size_t *len) { - grib_accessor* data=0; - grib_context* c=a->context; - grib_vsarray* stringValues=NULL; - long l=0,n=0,tl; - size_t size,i,j; - char buf[25]={0,}; - long* v=0; - char** b=buffer; + grib_accessor* data=0; + grib_context* c=a->context; + grib_vsarray* stringValues=NULL; + long l=0,n=0,tl; + size_t size,i,j; + char buf[25]={0,}; + long* v=0; + char** b=buffer; - data=get_accessor(a); - if (!data) return GRIB_NOT_FOUND; + data=get_accessor(a); + if (!data) return GRIB_NOT_FOUND; - stringValues=accessor_bufr_data_array_get_stringValues(data); + stringValues=accessor_bufr_data_array_get_stringValues(data); - n=grib_vsarray_used_size(stringValues); + n=grib_vsarray_used_size(stringValues); - tl=0; - for (j=0;jv[j]); - tl+=l; + tl=0; + for (j=0;jv[j]); + tl+=l; - if (tl>*len) return GRIB_ARRAY_TOO_SMALL; + if (tl>*len) return GRIB_ARRAY_TOO_SMALL; - for (i=0;iv[j]->v[i]); + for (i=0;iv[j]->v[i]); + } } - } - *len=tl; + *len=tl; - return GRIB_SUCCESS; + return GRIB_SUCCESS; } -static int pack_string_array(grib_accessor*a , const char** v, size_t *len){ - return GRIB_NOT_IMPLEMENTED; +static int pack_string_array(grib_accessor*a , const char** v, size_t *len) +{ + return GRIB_NOT_IMPLEMENTED; } static int unpack_string(grib_accessor* a, char* val, size_t *len) { - return GRIB_NOT_IMPLEMENTED; + return GRIB_NOT_IMPLEMENTED; } static int pack_string(grib_accessor* a, const char* val, size_t *len) { - return GRIB_NOT_IMPLEMENTED; + return GRIB_NOT_IMPLEMENTED; } static int value_count(grib_accessor* a,long* rlen) { - grib_accessor* descriptors=get_accessor(a); + grib_accessor* descriptors=get_accessor(a); - return grib_value_count(descriptors,rlen); + return grib_value_count(descriptors,rlen); } -static void destroy(grib_context* c,grib_accessor* a) { +static void destroy(grib_context* c,grib_accessor* a) +{ } diff --git a/src/grib_accessor_class_bufrdc_expanded_descriptors.c b/src/grib_accessor_class_bufrdc_expanded_descriptors.c index f78b8f2c5..dfc5be1a0 100644 --- a/src/grib_accessor_class_bufrdc_expanded_descriptors.c +++ b/src/grib_accessor_class_bufrdc_expanded_descriptors.c @@ -9,10 +9,10 @@ */ /* Sequences and replication (not delayed) are resolved in this class. -Number of elements to which a delayed replication applies are recomputed to -take account of the expansion. -expanded descriptors cannot contain sequences and only delayed replication -can appear + Number of elements to which a delayed replication applies are recomputed to + take account of the expansion. + Expanded descriptors cannot contain sequences and only delayed replication + can appear */ #include "grib_api_internal.h" @@ -145,89 +145,90 @@ static void init_class(grib_accessor_class* c) static void init(grib_accessor* a, const long len , grib_arguments* args ) { - grib_accessor_bufrdc_expanded_descriptors* self = (grib_accessor_bufrdc_expanded_descriptors*)a; - int n=0; - self->expandedDescriptors=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++); - self->expandedDescriptorsAccessor=0; - a->length = 0; - a->flags |= GRIB_ACCESSOR_FLAG_READ_ONLY; + grib_accessor_bufrdc_expanded_descriptors* self = (grib_accessor_bufrdc_expanded_descriptors*)a; + int n=0; + self->expandedDescriptors=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++); + self->expandedDescriptorsAccessor=0; + a->length = 0; + a->flags |= GRIB_ACCESSOR_FLAG_READ_ONLY; } static void dump(grib_accessor* a, grib_dumper* dumper) { - grib_dump_long(dumper,a,NULL); + grib_dump_long(dumper,a,NULL); } -static grib_accessor* get_accessor(grib_accessor* a) { - grib_accessor_bufrdc_expanded_descriptors* self = (grib_accessor_bufrdc_expanded_descriptors*)a; - if (!self->expandedDescriptorsAccessor) { - self->expandedDescriptorsAccessor=grib_find_accessor(grib_handle_of_accessor(a),self->expandedDescriptors); - } - return self->expandedDescriptorsAccessor; -} - -static int unpack_long (grib_accessor* a, long* val, size_t *len) +static grib_accessor* get_accessor(grib_accessor* a) { - grib_accessor* descriptors=0; - size_t rlen=0,l; - long lenall=0; - size_t i; - long* v=0; - grib_context* c=a->context; + grib_accessor_bufrdc_expanded_descriptors* self = (grib_accessor_bufrdc_expanded_descriptors*)a; + if (!self->expandedDescriptorsAccessor) { + self->expandedDescriptorsAccessor=grib_find_accessor(grib_handle_of_accessor(a),self->expandedDescriptors); + } + return self->expandedDescriptorsAccessor; +} - descriptors=get_accessor(a); - if (!descriptors) return GRIB_NOT_FOUND; +static int unpack_long (grib_accessor* a, long* val, size_t *len) +{ + grib_accessor* descriptors=0; + size_t rlen=0,l; + long lenall=0; + size_t i; + long* v=0; + grib_context* c=a->context; - grib_value_count(a,&lenall); - v=(long*)grib_context_malloc_clear(c,sizeof(long)*lenall); - l=lenall; - grib_unpack_long(descriptors,v,&l); + descriptors=get_accessor(a); + if (!descriptors) return GRIB_NOT_FOUND; - rlen=0; - for (i=0;i 221999 ) ) - val[rlen++]=v[i]; - } - *len=rlen; + grib_value_count(a,&lenall); + v=(long*)grib_context_malloc_clear(c,sizeof(long)*lenall); + l=lenall; + grib_unpack_long(descriptors,v,&l); - return GRIB_SUCCESS; + rlen=0; + for (i=0;i 221999 ) ) + val[rlen++]=v[i]; + } + *len=rlen; + + return GRIB_SUCCESS; } static int unpack_string_array (grib_accessor* a, char** buffer, size_t *len) { - grib_accessor* descriptors=0; - grib_context* c=a->context; - long l=0; - size_t size,i; - char buf[25]={0,}; - long* v=0; + grib_accessor* descriptors=0; + grib_context* c=a->context; + long l=0; + size_t size,i; + char buf[25]={0,}; + long* v=0; - descriptors=get_accessor(a); - if (!descriptors) return GRIB_NOT_FOUND; + descriptors=get_accessor(a); + if (!descriptors) return GRIB_NOT_FOUND; - value_count(descriptors,&l); - if (l>*len) return GRIB_ARRAY_TOO_SMALL; + value_count(descriptors,&l); + if (l>*len) return GRIB_ARRAY_TOO_SMALL; - v=(long*)grib_context_malloc_clear(c,sizeof(long)*l); - size=l; - unpack_long(a,v,&size); + v=(long*)grib_context_malloc_clear(c,sizeof(long)*l); + size=l; + unpack_long(a,v,&size); - for (i=0;itablesAccessorName=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++); - self->expandedName=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++); - self->rank=grib_arguments_get_long(grib_handle_of_accessor(a),args,n++); - if (self->rank!=0) { - self->expandedAccessor=grib_find_accessor(grib_handle_of_accessor(a),self->expandedName); - } else { - self->expandedAccessor=0; - } - self->unexpandedDescriptors=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++); - self->sequence=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++); - self->do_expand=1; - self->expanded=0; - a->length = 0; + grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; + int n=0; + self->tablesAccessorName=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++); + self->expandedName=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++); + self->rank=grib_arguments_get_long(grib_handle_of_accessor(a),args,n++); + if (self->rank!=0) { + self->expandedAccessor=grib_find_accessor(grib_handle_of_accessor(a),self->expandedName); + } else { + self->expandedAccessor=0; + } + self->unexpandedDescriptors=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++); + self->sequence=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++); + self->do_expand=1; + self->expanded=0; + a->length = 0; } static void dump(grib_accessor* a, grib_dumper* dumper) { - grib_dump_long(dumper,a,NULL); + grib_dump_long(dumper,a,NULL); } -static bufr_descriptors_array* _expand(grib_accessor* a,bufr_descriptors_array* unexpanded,change_coding_params* ccp,int *err); +static bufr_descriptors_array* do_expand(grib_accessor* a,bufr_descriptors_array* unexpanded,change_coding_params* ccp,int *err); static int depth=-1; -static size_t __expand(grib_accessor* a,bufr_descriptors_array* unexpanded,bufr_descriptors_array* expanded, - change_coding_params* ccp,int* err) { - int k,j,i; - grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; - size_t size=0; - long* v=NULL; - bufr_descriptor* u=NULL; - bufr_descriptor* vv=NULL; - bufr_descriptor** ur=NULL; - bufr_descriptor* urc=NULL; - int idx; - bufr_descriptor* u0=NULL; - grib_context* c=a->context; - bufr_descriptor* us=NULL; - bufr_descriptors_array* inner_expanded=NULL; - bufr_descriptors_array* inner_unexpanded=NULL; +static size_t __expand(grib_accessor* a, bufr_descriptors_array* unexpanded, bufr_descriptors_array* expanded, + change_coding_params* ccp, int* err) +{ + int k,j,i; + grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; + size_t size=0; + long* v=NULL; + bufr_descriptor* u=NULL; + bufr_descriptor* vv=NULL; + bufr_descriptor** ur=NULL; + bufr_descriptor* urc=NULL; + int idx; + bufr_descriptor* u0=NULL; + grib_context* c=a->context; + bufr_descriptor* us=NULL; + bufr_descriptors_array* inner_expanded=NULL; + bufr_descriptors_array* inner_unexpanded=NULL; #if MYDEBUG - int idepth; + int idepth; #endif - if (grib_bufr_descriptors_array_used_size(unexpanded)==0) return 0; + if (grib_bufr_descriptors_array_used_size(unexpanded)==0) return 0; - us=grib_bufr_descriptor_clone(grib_bufr_descriptors_array_get(unexpanded,0)); + us=grib_bufr_descriptor_clone(grib_bufr_descriptors_array_get(unexpanded,0)); - *err=0; + *err=0; #if MYDEBUG - for (idepth=0;idepth %d-%02d-%03d\n",us->F,us->X,us->Y); + for (idepth=0;idepth %d-%02d-%03d\n",us->F,us->X,us->Y); #endif - switch (us->F) { + switch (us->F) { case 3: - /* sequence */ - u=grib_bufr_descriptors_array_pop_front(unexpanded); -#if MYDEBUG - for (idepth=0;idepthcode); -#endif - /*this is to get the sequence elements of the sequence unexpanded[i] */ - *err=grib_set_long(grib_handle_of_accessor(a),self->sequence,u->code); - *err=grib_get_size(grib_handle_of_accessor(a),self->sequence,&size); - grib_bufr_descriptor_delete(u); - if (*err) return 0; - v=(long*)grib_context_malloc_clear(c,sizeof(long)*size); - *err=grib_get_long_array(grib_handle_of_accessor(a),self->sequence,v,&size); - if (*err) return 0; - inner_unexpanded=grib_bufr_descriptors_array_new(c,100,100); - for (i=0;itablesAccessor,v[i],err); - inner_unexpanded=grib_bufr_descriptors_array_push(inner_unexpanded,vv); - } - grib_context_free(c,v); - inner_expanded=_expand(a,inner_unexpanded,ccp,err); - grib_bufr_descriptors_array_delete(inner_unexpanded); -#if MYDEBUG - for (i=0;in;i++) { - for (idepth=0;idepthv[i]->code); - } -#endif - size=grib_bufr_descriptors_array_used_size(inner_expanded); - expanded=grib_bufr_descriptors_array_append(expanded,inner_expanded); - break; - - case 1: - if (us->Y==0) { - /* delayed replication */ - bufr_descriptor* uidx=0; - u=grib_bufr_descriptors_array_pop_front(unexpanded); -#if MYDEBUG - for (idepth=0;idepthcode); - for (idepth=0;idepthcode); -#endif - grib_bufr_descriptors_array_push(expanded,u); - idx=expanded->n-1; - size=0; - inner_unexpanded=grib_bufr_descriptors_array_new(c,100,100); - inner_expanded=grib_bufr_descriptors_array_new(c,100,100); - for (j=0;jX+1;j++) { - u0=grib_bufr_descriptors_array_pop_front(unexpanded); - grib_bufr_descriptors_array_push(inner_unexpanded,u0); -#if MYDEBUG - for (idepth=0;idepthcode); -#endif - } - inner_expanded=_expand(a,inner_unexpanded,ccp,err); - size=grib_bufr_descriptors_array_used_size(inner_expanded); -#if MYDEBUG - for (i=0;in;i++) { - for (idepth=0;idepthv[i]->code); - } -#endif - expanded=grib_bufr_descriptors_array_append(expanded,inner_expanded); - uidx=grib_bufr_descriptors_array_get(expanded,idx); - grib_bufr_descriptor_set_code(0,(size-1)*1000+100000,uidx); - size++; - } else { + /* sequence */ u=grib_bufr_descriptors_array_pop_front(unexpanded); #if MYDEBUG for (idepth=0;idepthcode); #endif + /*this is to get the sequence elements of the sequence unexpanded[i] */ + *err=grib_set_long(grib_handle_of_accessor(a),self->sequence,u->code); + *err=grib_get_size(grib_handle_of_accessor(a),self->sequence,&size); grib_bufr_descriptor_delete(u); - size=us->X*us->Y; - ur=(bufr_descriptor**)grib_context_malloc_clear(c,us->X*sizeof(bufr_descriptor)); - for (j=0;jX;j++) { - ur[j]=grib_bufr_descriptors_array_pop_front(unexpanded); -#if MYDEBUG - for (idepth=0;idepthcode); -#endif - } + if (*err) return 0; + v=(long*)grib_context_malloc_clear(c,sizeof(long)*size); + *err=grib_get_long_array(grib_handle_of_accessor(a),self->sequence,v,&size); + if (*err) return 0; inner_unexpanded=grib_bufr_descriptors_array_new(c,100,100); - for (j=0;jX;j++) { - urc=grib_bufr_descriptor_clone(ur[j]); - grib_bufr_descriptors_array_push(inner_unexpanded,urc); + for (i=0;itablesAccessor,v[i],err); + inner_unexpanded=grib_bufr_descriptors_array_push(inner_unexpanded,vv); } - for (k=1;kY;k++) { - for (j=0;jX;j++) { - urc=grib_bufr_descriptor_clone(ur[j]); - grib_bufr_descriptors_array_push(inner_unexpanded,urc); - } - } - for (i=0;iX;i++) grib_bufr_descriptor_delete(ur[i]); - grib_context_free(c,ur); - inner_expanded=_expand(a,inner_unexpanded,ccp,err); + grib_context_free(c,v); + inner_expanded=do_expand(a,inner_unexpanded,ccp,err); grib_bufr_descriptors_array_delete(inner_unexpanded); #if MYDEBUG for (i=0;in;i++) { - for (idepth=0;idepthv[i]->code); + for (idepth=0;idepthv[i]->code); } #endif size=grib_bufr_descriptors_array_used_size(inner_expanded); expanded=grib_bufr_descriptors_array_append(expanded,inner_expanded); - } - break; + break; + + case 1: + if (us->Y==0) { + /* delayed replication */ + bufr_descriptor* uidx=0; + u=grib_bufr_descriptors_array_pop_front(unexpanded); +#if MYDEBUG + for (idepth=0;idepthcode); + for (idepth=0;idepthcode); +#endif + grib_bufr_descriptors_array_push(expanded,u); + idx=expanded->n-1; + size=0; + inner_unexpanded=grib_bufr_descriptors_array_new(c,100,100); + inner_expanded=grib_bufr_descriptors_array_new(c,100,100); + for (j=0;jX+1;j++) { + u0=grib_bufr_descriptors_array_pop_front(unexpanded); + grib_bufr_descriptors_array_push(inner_unexpanded,u0); +#if MYDEBUG + for (idepth=0;idepthcode); +#endif + } + inner_expanded=do_expand(a,inner_unexpanded,ccp,err); + size=grib_bufr_descriptors_array_used_size(inner_expanded); +#if MYDEBUG + for (i=0;in;i++) { + for (idepth=0;idepthv[i]->code); + } +#endif + expanded=grib_bufr_descriptors_array_append(expanded,inner_expanded); + uidx=grib_bufr_descriptors_array_get(expanded,idx); + grib_bufr_descriptor_set_code(0,(size-1)*1000+100000,uidx); + size++; + } else { + u=grib_bufr_descriptors_array_pop_front(unexpanded); +#if MYDEBUG + for (idepth=0;idepthcode); +#endif + grib_bufr_descriptor_delete(u); + size=us->X*us->Y; + ur=(bufr_descriptor**)grib_context_malloc_clear(c,us->X*sizeof(bufr_descriptor)); + for (j=0;jX;j++) { + ur[j]=grib_bufr_descriptors_array_pop_front(unexpanded); +#if MYDEBUG + for (idepth=0;idepthcode); +#endif + } + inner_unexpanded=grib_bufr_descriptors_array_new(c,100,100); + for (j=0;jX;j++) { + urc=grib_bufr_descriptor_clone(ur[j]); + grib_bufr_descriptors_array_push(inner_unexpanded,urc); + } + for (k=1;kY;k++) { + for (j=0;jX;j++) { + urc=grib_bufr_descriptor_clone(ur[j]); + grib_bufr_descriptors_array_push(inner_unexpanded,urc); + } + } + for (i=0;iX;i++) grib_bufr_descriptor_delete(ur[i]); + grib_context_free(c,ur); + inner_expanded=do_expand(a,inner_unexpanded,ccp,err); + grib_bufr_descriptors_array_delete(inner_unexpanded); +#if MYDEBUG + for (i=0;in;i++) { + for (idepth=0;idepthv[i]->code); + } +#endif + size=grib_bufr_descriptors_array_used_size(inner_expanded); + expanded=grib_bufr_descriptors_array_append(expanded,inner_expanded); + } + break; case 0: - u=grib_bufr_descriptors_array_pop_front(unexpanded); - size=1; - if (ccp->associatedFieldWidth && u->X!=31) { - bufr_descriptor* au=grib_bufr_descriptor_new(self->tablesAccessor,999999,err); - au->width=ccp->associatedFieldWidth; - grib_bufr_descriptor_set_scale(au,0); - au->shortName=grib_context_strdup(c,"associatedField"); - au->name=grib_context_strdup(c,"associated field"); - au->units=grib_context_strdup(c,"associated units"); + u=grib_bufr_descriptors_array_pop_front(unexpanded); + size=1; + if (ccp->associatedFieldWidth && u->X!=31) { + bufr_descriptor* au=grib_bufr_descriptor_new(self->tablesAccessor,999999,err); + au->width=ccp->associatedFieldWidth; + grib_bufr_descriptor_set_scale(au,0); + au->shortName=grib_context_strdup(c,"associatedField"); + au->name=grib_context_strdup(c,"associated field"); + au->units=grib_context_strdup(c,"associated units"); #if MYDEBUG - for (idepth=0;idepthcode,au->scale,au->reference,au->width); + for (idepth=0;idepthcode,au->scale,au->reference,au->width); #endif - grib_bufr_descriptors_array_push(expanded,au); - size++; - } -#if MYDEBUG - for (idepth=0;idepthcode); - for (idepth=0;idepthcode, - u->type,u->scale,u->reference,u->width); -#endif - if ( u->type!=BUFR_DESCRIPTOR_TYPE_FLAG && - u->type!=BUFR_DESCRIPTOR_TYPE_TABLE && - u->type!=BUFR_DESCRIPTOR_TYPE_STRING ) { - if (ccp->localDescriptorWidth>0) { - u->width=ccp->localDescriptorWidth; - u->reference=0; - grib_bufr_descriptor_set_scale(u,0); - ccp->localDescriptorWidth=0; - } else { - u->width += ccp->extraWidth; - u->reference *= ccp->referenceFactor; - grib_bufr_descriptor_set_scale(u,u->scale+ccp->extraScale); + grib_bufr_descriptors_array_push(expanded,au); + size++; } - } else if (u->type==BUFR_DESCRIPTOR_TYPE_STRING && ccp->newStringWidth!=0) { - u->width=ccp->newStringWidth; - } #if MYDEBUG - printf("->(%ld %g %ld)\n",u->scale,u->reference,u->width); + for (idepth=0;idepthcode); + for (idepth=0;idepthcode, + u->type,u->scale,u->reference,u->width); #endif - grib_bufr_descriptors_array_push(expanded,u); - break; + if ( u->type!=BUFR_DESCRIPTOR_TYPE_FLAG && + u->type!=BUFR_DESCRIPTOR_TYPE_TABLE && + u->type!=BUFR_DESCRIPTOR_TYPE_STRING ) { + if (ccp->localDescriptorWidth>0) { + u->width=ccp->localDescriptorWidth; + u->reference=0; + grib_bufr_descriptor_set_scale(u,0); + ccp->localDescriptorWidth=0; + } else { + u->width += ccp->extraWidth; + u->reference *= ccp->referenceFactor; + grib_bufr_descriptor_set_scale(u,u->scale+ccp->extraScale); + } + } else if (u->type==BUFR_DESCRIPTOR_TYPE_STRING && ccp->newStringWidth!=0) { + u->width=ccp->newStringWidth; + } +#if MYDEBUG + printf("->(%ld %g %ld)\n",u->scale,u->reference,u->width); +#endif + grib_bufr_descriptors_array_push(expanded,u); + break; case 2: - u=grib_bufr_descriptors_array_pop_front(unexpanded); + u=grib_bufr_descriptors_array_pop_front(unexpanded); #if MYDEBUG - for (idepth=0;idepthcode); + for (idepth=0;idepthcode); #endif - switch(us->X) { + switch(us->X) { case 1: - ccp->extraWidth = us->Y ? us->Y-128 : 0; - size=0; - grib_bufr_descriptor_delete(u); - break; + ccp->extraWidth = us->Y ? us->Y-128 : 0; + size=0; + grib_bufr_descriptor_delete(u); + break; case 2: - ccp->extraScale = us->Y ? us->Y-128 : 0; - size=0; - grib_bufr_descriptor_delete(u); - break; + ccp->extraScale = us->Y ? us->Y-128 : 0; + size=0; + grib_bufr_descriptor_delete(u); + break; case 4: - /* associated field*/ - ccp->associatedFieldWidth=us->Y; - grib_bufr_descriptor_delete(u); - break; + /* associated field*/ + ccp->associatedFieldWidth=us->Y; + grib_bufr_descriptor_delete(u); + break; case 6: - /*signify data width*/ - ccp->localDescriptorWidth=us->Y; - size=0; - grib_bufr_descriptor_delete(u); - break; + /*signify data width*/ + ccp->localDescriptorWidth=us->Y; + size=0; + grib_bufr_descriptor_delete(u); + break; case 7: - if (us->Y) { - ccp->extraScale = us->Y; - ccp->referenceFactor=grib_power(us->Y,10); - ccp->extraWidth=((10*us->Y)+2)/3; - } else { - ccp->extraWidth=0; - ccp->extraScale=0; - ccp->referenceFactor=1; - } - size=0; - grib_bufr_descriptor_delete(u); - break; + if (us->Y) { + ccp->extraScale = us->Y; + ccp->referenceFactor=grib_power(us->Y,10); + ccp->extraWidth=((10*us->Y)+2)/3; + } else { + ccp->extraWidth=0; + ccp->extraScale=0; + ccp->referenceFactor=1; + } + size=0; + grib_bufr_descriptor_delete(u); + break; case 8: - ccp->newStringWidth = us->Y * 8 ; - break; + ccp->newStringWidth = us->Y * 8 ; + break; default: #if MYDEBUG - for (idepth=0;idepthcode); + for (idepth=0;idepthcode); #endif - grib_bufr_descriptors_array_push(expanded,u); - size=1; - } - break; + grib_bufr_descriptors_array_push(expanded,u); + size=1; + } + break; - default: - u=grib_bufr_descriptors_array_pop_front(unexpanded); + default: + u=grib_bufr_descriptors_array_pop_front(unexpanded); #if MYDEBUG - for (idepth=0;idepthcode); - for (idepth=0;idepthcode); + for (idepth=0;idepthcode); + for (idepth=0;idepthcode); #endif - grib_bufr_descriptors_array_push(expanded,u); - size=1; - } + grib_bufr_descriptors_array_push(expanded,u); + size=1; + } #if MYDEBUG for (idepth=0;idepthF,us->X,us->Y,size); @@ -457,230 +458,232 @@ static size_t __expand(grib_accessor* a,bufr_descriptors_array* unexpanded,bufr_ return size; } -static bufr_descriptors_array* _expand(grib_accessor* a,bufr_descriptors_array* unexpanded,change_coding_params* ccp,int *err) +bufr_descriptors_array* do_expand(grib_accessor* a,bufr_descriptors_array* unexpanded,change_coding_params* ccp,int *err) { - bufr_descriptors_array* expanded=NULL; - grib_context* c=a->context; + bufr_descriptors_array* expanded=NULL; + grib_context* c=a->context; #if MYDEBUG - int idepth; + int idepth; #endif - depth++; + depth++; - expanded=grib_bufr_descriptors_array_new(c,100,100); + expanded=grib_bufr_descriptors_array_new(c,100,100); #if MYDEBUG { - int i; - for (idepth=0;idepth \n"); - for (i=0;in;i++) { - bufr_descriptor* xx=grib_bufr_descriptors_array_get(unexpanded,i); - for (idepth=0;idepthcode); - } - for (idepth=0;idepth \n"); + for (i=0;in;i++) { + bufr_descriptor* xx=grib_bufr_descriptors_array_get(unexpanded,i); + for (idepth=0;idepthcode); + } + for (idepth=0;idepthn) { - __expand(a,unexpanded,expanded,ccp,err); - } + while (unexpanded->n) { + __expand(a,unexpanded,expanded,ccp,err); + } #if MYDEBUG { - int i; - for (idepth=0;idepth \n"); - for (i=0;in;i++) { - bufr_descriptor* xx=grib_bufr_descriptors_array_get(expanded,i); - for (idepth=0;idepthcode); - printf("%ld %g %ld",xx->scale,xx->reference,xx->width); - printf("\n"); - } - for (idepth=0;idepth \n"); + for (i=0;in;i++) { + bufr_descriptor* xx=grib_bufr_descriptors_array_get(expanded,i); + for (idepth=0;idepthcode); + printf("%ld %g %ld",xx->scale,xx->reference,xx->width); + printf("\n"); + } + for (idepth=0;idepthcontext; + grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; + int err=0; + size_t unexpandedSize=0; + int i; + long* u=0; + change_coding_params ccp; + bufr_descriptors_array* unexpanded=NULL; + grib_context* c=a->context; - if (!self->do_expand) { - return err; - } - self->do_expand=0; - if (!self->tablesAccessor) { - self->tablesAccessor=grib_find_accessor(grib_handle_of_accessor(a),self->tablesAccessorName); - Assert(self->tablesAccessor); - } - - if (self->rank!=0) { - err=expand(self->expandedAccessor); - self->expanded=((grib_accessor_expanded_descriptors*)self->expandedAccessor)->expanded; - return err; - } - - grib_bufr_descriptors_array_delete(self->expanded); - err=grib_get_size(grib_handle_of_accessor(a),self->unexpandedDescriptors,&unexpandedSize); - if (err) return err; - u=(long*)grib_context_malloc_clear(c,sizeof(long)*unexpandedSize); - if (!u) {err=GRIB_OUT_OF_MEMORY; return err;} - err=grib_get_long_array(grib_handle_of_accessor(a),self->unexpandedDescriptors,u,&unexpandedSize); - if (err) return err; - - unexpanded=grib_bufr_descriptors_array_new(c,unexpandedSize,100); - for (i=0;itablesAccessor,u[i],&err)); - } - - grib_context_free(c,u); - - ccp.extraWidth=0; - ccp.localDescriptorWidth=-1; - ccp.extraScale=0; - ccp.referenceFactor=1; - ccp.associatedFieldWidth=0; - ccp.newStringWidth=0; - self->expanded=_expand(a,unexpanded,&ccp,&err); - grib_bufr_descriptors_array_delete(unexpanded); - - return err; - -} - -int grib_accessor_class_expanded_descriptors_set_do_expand(grib_accessor* a,long do_expand) { - grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; - self->do_expand=do_expand; - return 0; -} - -bufr_descriptors_array* grib_accessor_class_expanded_descriptors_get_expanded(grib_accessor* a,int* err) { - grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; - *err=expand(a); - return self->expanded; -} - -static int unpack_double (grib_accessor* a, double* val, size_t *len) { - grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; - int ret=0; - int i; - size_t expandedSize; - - if (self->rank!=2) { - long* lval=(long*)grib_context_malloc_clear(a->context,*len*sizeof(long)); - ret=unpack_long(a,lval,len); - if (ret) return ret; - for (i=0;i<*len;i++) val[i]=(double)lval[i]; - grib_context_free(a->context,lval); - } else { - ret=expand(a); - if (ret) return ret; - - expandedSize=grib_bufr_descriptors_array_used_size(self->expanded); - if(*len < expandedSize) - { - grib_context_log(a->context, GRIB_LOG_ERROR, - " wrong size (%ld) for %s it contains %d values ",*len, a->name , expandedSize); - *len = 0; - return GRIB_ARRAY_TOO_SMALL; + if (!self->do_expand) { + return err; } - *len = expandedSize; - for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->reference; - } - return ret; + self->do_expand=0; + if (!self->tablesAccessor) { + self->tablesAccessor=grib_find_accessor(grib_handle_of_accessor(a),self->tablesAccessorName); + Assert(self->tablesAccessor); + } + + if (self->rank!=0) { + err=expand(self->expandedAccessor); + self->expanded=((grib_accessor_expanded_descriptors*)self->expandedAccessor)->expanded; + return err; + } + + grib_bufr_descriptors_array_delete(self->expanded); + err=grib_get_size(grib_handle_of_accessor(a),self->unexpandedDescriptors,&unexpandedSize); + if (err) return err; + u=(long*)grib_context_malloc_clear(c,sizeof(long)*unexpandedSize); + if (!u) {err=GRIB_OUT_OF_MEMORY; return err;} + err=grib_get_long_array(grib_handle_of_accessor(a),self->unexpandedDescriptors,u,&unexpandedSize); + if (err) return err; + + unexpanded=grib_bufr_descriptors_array_new(c,unexpandedSize,100); + for (i=0;itablesAccessor,u[i],&err)); + } + + grib_context_free(c,u); + + ccp.extraWidth=0; + ccp.localDescriptorWidth=-1; + ccp.extraScale=0; + ccp.referenceFactor=1; + ccp.associatedFieldWidth=0; + ccp.newStringWidth=0; + self->expanded=do_expand(a,unexpanded,&ccp,&err); + grib_bufr_descriptors_array_delete(unexpanded); + + return err; + +} + +int grib_accessor_class_expanded_descriptors_set_do_expand(grib_accessor* a,long do_expand) +{ + grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; + self->do_expand=do_expand; + return 0; +} + +bufr_descriptors_array* grib_accessor_class_expanded_descriptors_get_expanded(grib_accessor* a,int* err) +{ + grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; + *err=expand(a); + return self->expanded; +} + +static int unpack_double (grib_accessor* a, double* val, size_t *len) +{ + grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; + int ret=0; + int i; + size_t expandedSize; + + if (self->rank!=2) { + long* lval=(long*)grib_context_malloc_clear(a->context,*len*sizeof(long)); + ret=unpack_long(a,lval,len); + if (ret) return ret; + for (i=0;i<*len;i++) val[i]=(double)lval[i]; + grib_context_free(a->context,lval); + } else { + ret=expand(a); + if (ret) return ret; + + expandedSize=grib_bufr_descriptors_array_used_size(self->expanded); + if(*len < expandedSize) + { + grib_context_log(a->context, GRIB_LOG_ERROR, + " wrong size (%ld) for %s it contains %d values ",*len, a->name , expandedSize); + *len = 0; + return GRIB_ARRAY_TOO_SMALL; + } + *len = expandedSize; + for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->reference; + } + return ret; } static int unpack_long (grib_accessor* a, long* val, size_t *len) { - grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; - int ret=0; - size_t rlen=0; - size_t i; + grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; + int ret=0; + size_t rlen=0; + size_t i; - ret=expand(a); - rlen=grib_bufr_descriptors_array_used_size(self->expanded); - if (ret) return ret; + ret=expand(a); + rlen=grib_bufr_descriptors_array_used_size(self->expanded); + if (ret) return ret; - if(*len < rlen) - { - grib_context_log(a->context, GRIB_LOG_ERROR, - " wrong size (%ld) for %s it contains %d values ",*len, a->name , rlen); - *len = 0; - return GRIB_ARRAY_TOO_SMALL; - } + if(*len < rlen) + { + grib_context_log(a->context, GRIB_LOG_ERROR, + " wrong size (%ld) for %s it contains %d values ",*len, a->name , rlen); + *len = 0; + return GRIB_ARRAY_TOO_SMALL; + } - *len = rlen; - switch (self->rank) { + *len = rlen; + switch (self->rank) { case 0: - for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->code; - break; + for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->code; + break; case 1: - for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->scale; - break; + for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->scale; + break; case 2: - return GRIB_INVALID_TYPE; - break; + return GRIB_INVALID_TYPE; + break; case 3: - for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->width; - break; + for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->width; + break; case 4: - for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->type; - break; - } + for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->type; + break; + } - return GRIB_SUCCESS; + return GRIB_SUCCESS; } static int pack_long (grib_accessor* a, const long* val, size_t *len) { - grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; - self->do_expand=1; - return GRIB_NOT_IMPLEMENTED; + grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; + self->do_expand=1; + return GRIB_NOT_IMPLEMENTED; } - static int value_count(grib_accessor* a,long* rlen) { - grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; - int ret=0; - grib_context* c=a->context; - *rlen=0; + grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; + int ret=0; + grib_context* c=a->context; + *rlen=0; - ret=expand(a); - *rlen=grib_bufr_descriptors_array_used_size(self->expanded); - if (ret) { - grib_context_log(c,GRIB_LOG_ERROR,"%s unable to compute size",a->name); - return ret; - } + ret=expand(a); + *rlen=grib_bufr_descriptors_array_used_size(self->expanded); + if (ret) { + grib_context_log(c,GRIB_LOG_ERROR,"%s unable to compute size",a->name); + return ret; + } - return ret; + return ret; } -static void destroy(grib_context* c,grib_accessor* a) { - grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; - if (self->rank==0 && self->expanded) { - grib_bufr_descriptors_array_delete(self->expanded); - } +static void destroy(grib_context* c,grib_accessor* a) +{ + grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; + if (self->rank==0 && self->expanded) { + grib_bufr_descriptors_array_delete(self->expanded); + } } static int get_native_type(grib_accessor* a) { - grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; - if (self->rank==2) return GRIB_TYPE_DOUBLE; - else return GRIB_TYPE_LONG; + grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; + if (self->rank==2) return GRIB_TYPE_DOUBLE; + else return GRIB_TYPE_LONG; } -