Fix build break on Windows due to name _expand

This commit is contained in:
Shahram Najm 2015-11-23 11:28:48 +00:00
parent b59e8f8b39
commit 982becb85e
3 changed files with 523 additions and 516 deletions

View File

@ -9,10 +9,10 @@
*/ */
/* Sequences and replication (not delayed) are resolved in this class. /* Sequences and replication (not delayed) are resolved in this class.
Number of elements to which a delayed replication applies are recomputed to Number of elements to which a delayed replication applies are recomputed to
take account of the expansion. take account of the expansion.
expanded descriptors cannot contain sequences and only delayed replication Expanded descriptors cannot contain sequences and only delayed replication
can appear can appear
*/ */
#include "grib_api_internal.h" #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 ) static void init(grib_accessor* a, const long len , grib_arguments* args )
{ {
grib_accessor_bufr_string_values* self = (grib_accessor_bufr_string_values*)a; grib_accessor_bufr_string_values* self = (grib_accessor_bufr_string_values*)a;
int n=0; int n=0;
self->dataAccessorName=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++); self->dataAccessorName=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++);
self->dataAccessor=NULL; self->dataAccessor=NULL;
a->length = 0; a->length = 0;
a->flags |= GRIB_ACCESSOR_FLAG_READ_ONLY; a->flags |= GRIB_ACCESSOR_FLAG_READ_ONLY;
} }
static void dump(grib_accessor* a, grib_dumper* dumper) 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) { static grib_accessor* get_accessor(grib_accessor* a)
grib_accessor_bufr_string_values* self = (grib_accessor_bufr_string_values*)a; {
if (!self->dataAccessor) { grib_accessor_bufr_string_values* self = (grib_accessor_bufr_string_values*)a;
self->dataAccessor=grib_find_accessor(grib_handle_of_accessor(a),self->dataAccessorName); if (!self->dataAccessor) {
} self->dataAccessor=grib_find_accessor(grib_handle_of_accessor(a),self->dataAccessorName);
return self->dataAccessor; }
return self->dataAccessor;
} }
static int unpack_string_array (grib_accessor* a, char** buffer, size_t *len) static int unpack_string_array (grib_accessor* a, char** buffer, size_t *len)
{ {
grib_accessor* data=0; grib_accessor* data=0;
grib_context* c=a->context; grib_context* c=a->context;
grib_vsarray* stringValues=NULL; grib_vsarray* stringValues=NULL;
long l=0,n=0,tl; long l=0,n=0,tl;
size_t size,i,j; size_t size,i,j;
char buf[25]={0,}; char buf[25]={0,};
long* v=0; long* v=0;
char** b=buffer; char** b=buffer;
data=get_accessor(a); data=get_accessor(a);
if (!data) return GRIB_NOT_FOUND; 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; tl=0;
for (j=0;j<n;j++) { for (j=0;j<n;j++) {
l=grib_sarray_used_size(stringValues->v[j]); l=grib_sarray_used_size(stringValues->v[j]);
tl+=l; tl+=l;
if (tl>*len) return GRIB_ARRAY_TOO_SMALL; if (tl>*len) return GRIB_ARRAY_TOO_SMALL;
for (i=0;i<l;i++) { for (i=0;i<l;i++) {
*(b++)=grib_context_strdup(c,stringValues->v[j]->v[i]); *(b++)=grib_context_strdup(c,stringValues->v[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){ static int pack_string_array(grib_accessor*a , const char** v, size_t *len)
return GRIB_NOT_IMPLEMENTED; {
return GRIB_NOT_IMPLEMENTED;
} }
static int unpack_string(grib_accessor* a, char* val, size_t *len) 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) 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) 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)
{
} }

View File

@ -9,10 +9,10 @@
*/ */
/* Sequences and replication (not delayed) are resolved in this class. /* Sequences and replication (not delayed) are resolved in this class.
Number of elements to which a delayed replication applies are recomputed to Number of elements to which a delayed replication applies are recomputed to
take account of the expansion. take account of the expansion.
expanded descriptors cannot contain sequences and only delayed replication Expanded descriptors cannot contain sequences and only delayed replication
can appear can appear
*/ */
#include "grib_api_internal.h" #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 ) static void init(grib_accessor* a, const long len , grib_arguments* args )
{ {
grib_accessor_bufrdc_expanded_descriptors* self = (grib_accessor_bufrdc_expanded_descriptors*)a; grib_accessor_bufrdc_expanded_descriptors* self = (grib_accessor_bufrdc_expanded_descriptors*)a;
int n=0; int n=0;
self->expandedDescriptors=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++); self->expandedDescriptors=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++);
self->expandedDescriptorsAccessor=0; self->expandedDescriptorsAccessor=0;
a->length = 0; a->length = 0;
a->flags |= GRIB_ACCESSOR_FLAG_READ_ONLY; a->flags |= GRIB_ACCESSOR_FLAG_READ_ONLY;
} }
static void dump(grib_accessor* a, grib_dumper* dumper) 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) { 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)
{ {
grib_accessor* descriptors=0; grib_accessor_bufrdc_expanded_descriptors* self = (grib_accessor_bufrdc_expanded_descriptors*)a;
size_t rlen=0,l; if (!self->expandedDescriptorsAccessor) {
long lenall=0; self->expandedDescriptorsAccessor=grib_find_accessor(grib_handle_of_accessor(a),self->expandedDescriptors);
size_t i; }
long* v=0; return self->expandedDescriptorsAccessor;
grib_context* c=a->context; }
descriptors=get_accessor(a); static int unpack_long (grib_accessor* a, long* val, size_t *len)
if (!descriptors) return GRIB_NOT_FOUND; {
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); descriptors=get_accessor(a);
v=(long*)grib_context_malloc_clear(c,sizeof(long)*lenall); if (!descriptors) return GRIB_NOT_FOUND;
l=lenall;
grib_unpack_long(descriptors,v,&l);
rlen=0; grib_value_count(a,&lenall);
for (i=0;i<l;i++) { v=(long*)grib_context_malloc_clear(c,sizeof(long)*lenall);
if ((v[i] < 100000 || v[i] > 221999 ) ) l=lenall;
val[rlen++]=v[i]; grib_unpack_long(descriptors,v,&l);
}
*len=rlen;
return GRIB_SUCCESS; rlen=0;
for (i=0;i<l;i++) {
if ((v[i] < 100000 || v[i] > 221999 ) )
val[rlen++]=v[i];
}
*len=rlen;
return GRIB_SUCCESS;
} }
static int unpack_string_array (grib_accessor* a, char** buffer, size_t *len) static int unpack_string_array (grib_accessor* a, char** buffer, size_t *len)
{ {
grib_accessor* descriptors=0; grib_accessor* descriptors=0;
grib_context* c=a->context; grib_context* c=a->context;
long l=0; long l=0;
size_t size,i; size_t size,i;
char buf[25]={0,}; char buf[25]={0,};
long* v=0; long* v=0;
descriptors=get_accessor(a); descriptors=get_accessor(a);
if (!descriptors) return GRIB_NOT_FOUND; if (!descriptors) return GRIB_NOT_FOUND;
value_count(descriptors,&l); value_count(descriptors,&l);
if (l>*len) return GRIB_ARRAY_TOO_SMALL; if (l>*len) return GRIB_ARRAY_TOO_SMALL;
v=(long*)grib_context_malloc_clear(c,sizeof(long)*l); v=(long*)grib_context_malloc_clear(c,sizeof(long)*l);
size=l; size=l;
unpack_long(a,v,&size); unpack_long(a,v,&size);
for (i=0;i<size;i++) { for (i=0;i<size;i++) {
sprintf(buf,"%06ld",v[i]); sprintf(buf,"%06ld",v[i]);
buffer[i]=grib_context_strdup(c,buf); buffer[i]=grib_context_strdup(c,buf);
} }
*len=l; *len=l;
return GRIB_SUCCESS; return GRIB_SUCCESS;
} }
static int value_count(grib_accessor* a,long* rlen) 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)
{
} }

View File

@ -9,10 +9,10 @@
*/ */
/* Sequences and replication (not delayed) are resolved in this class. /* Sequences and replication (not delayed) are resolved in this class.
Number of elements to which a delayed replication applies are recomputed to Number of elements to which a delayed replication applies are recomputed to
take account of the expansion. take account of the expansion.
expanded descriptors cannot contain sequences and only delayed replication Expanded descriptors cannot contain sequences and only delayed replication
can appear can appear
*/ */
#include "grib_api_internal.h" #include "grib_api_internal.h"
@ -160,295 +160,296 @@ static void init_class(grib_accessor_class* c)
#define MYDEBUG 0 #define MYDEBUG 0
typedef struct change_coding_params { typedef struct change_coding_params {
int associatedFieldWidth; int associatedFieldWidth;
int localDescriptorWidth; int localDescriptorWidth;
int extraWidth; int extraWidth;
int extraScale; int extraScale;
int newStringWidth; int newStringWidth;
double referenceFactor; double referenceFactor;
} change_coding_params ; } change_coding_params ;
static void init(grib_accessor* a, const long len , grib_arguments* args ) static void init(grib_accessor* a, const long len , grib_arguments* args )
{ {
grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a;
int n=0; int n=0;
self->tablesAccessorName=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++); 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->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++); self->rank=grib_arguments_get_long(grib_handle_of_accessor(a),args,n++);
if (self->rank!=0) { if (self->rank!=0) {
self->expandedAccessor=grib_find_accessor(grib_handle_of_accessor(a),self->expandedName); self->expandedAccessor=grib_find_accessor(grib_handle_of_accessor(a),self->expandedName);
} else { } else {
self->expandedAccessor=0; self->expandedAccessor=0;
} }
self->unexpandedDescriptors=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++); 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->sequence=grib_arguments_get_name(grib_handle_of_accessor(a),args,n++);
self->do_expand=1; self->do_expand=1;
self->expanded=0; self->expanded=0;
a->length = 0; a->length = 0;
} }
static void dump(grib_accessor* a, grib_dumper* dumper) 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 int depth=-1;
static size_t __expand(grib_accessor* a,bufr_descriptors_array* unexpanded,bufr_descriptors_array* expanded, static size_t __expand(grib_accessor* a, bufr_descriptors_array* unexpanded, bufr_descriptors_array* expanded,
change_coding_params* ccp,int* err) { change_coding_params* ccp, int* err)
int k,j,i; {
grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; int k,j,i;
size_t size=0; grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a;
long* v=NULL; size_t size=0;
bufr_descriptor* u=NULL; long* v=NULL;
bufr_descriptor* vv=NULL; bufr_descriptor* u=NULL;
bufr_descriptor** ur=NULL; bufr_descriptor* vv=NULL;
bufr_descriptor* urc=NULL; bufr_descriptor** ur=NULL;
int idx; bufr_descriptor* urc=NULL;
bufr_descriptor* u0=NULL; int idx;
grib_context* c=a->context; bufr_descriptor* u0=NULL;
bufr_descriptor* us=NULL; grib_context* c=a->context;
bufr_descriptors_array* inner_expanded=NULL; bufr_descriptor* us=NULL;
bufr_descriptors_array* inner_unexpanded=NULL; bufr_descriptors_array* inner_expanded=NULL;
bufr_descriptors_array* inner_unexpanded=NULL;
#if MYDEBUG #if MYDEBUG
int idepth; int idepth;
#endif #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 #if MYDEBUG
for (idepth=0;idepth<depth;idepth++) printf("\t"); for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("expanding ==> %d-%02d-%03d\n",us->F,us->X,us->Y); printf("expanding ==> %d-%02d-%03d\n",us->F,us->X,us->Y);
#endif #endif
switch (us->F) { switch (us->F) {
case 3: case 3:
/* sequence */ /* sequence */
u=grib_bufr_descriptors_array_pop_front(unexpanded);
#if MYDEBUG
for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ pop %06ld\n",u->code);
#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;i<size;i++) {
vv=grib_bufr_descriptor_new(self->tablesAccessor,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;i<inner_expanded->n;i++) {
for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ push %06ld\n",inner_expanded->v[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;idepth<depth;idepth++) printf("\t");
printf("+++ pop %06ld\n",u->code);
for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ push %06ld\n",u->code);
#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;j<us->X+1;j++) {
u0=grib_bufr_descriptors_array_pop_front(unexpanded);
grib_bufr_descriptors_array_push(inner_unexpanded,u0);
#if MYDEBUG
for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ pop %06ld\n",u0->code);
#endif
}
inner_expanded=_expand(a,inner_unexpanded,ccp,err);
size=grib_bufr_descriptors_array_used_size(inner_expanded);
#if MYDEBUG
for (i=0;i<inner_expanded->n;i++) {
for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ push %06ld\n",inner_expanded->v[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); u=grib_bufr_descriptors_array_pop_front(unexpanded);
#if MYDEBUG #if MYDEBUG
for (idepth=0;idepth<depth;idepth++) printf("\t"); for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ pop %06ld\n",u->code); printf("+++ pop %06ld\n",u->code);
#endif #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); grib_bufr_descriptor_delete(u);
size=us->X*us->Y; if (*err) return 0;
ur=(bufr_descriptor**)grib_context_malloc_clear(c,us->X*sizeof(bufr_descriptor)); v=(long*)grib_context_malloc_clear(c,sizeof(long)*size);
for (j=0;j<us->X;j++) { *err=grib_get_long_array(grib_handle_of_accessor(a),self->sequence,v,&size);
ur[j]=grib_bufr_descriptors_array_pop_front(unexpanded); if (*err) return 0;
#if MYDEBUG
for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ pop %06ld\n",ur[j]->code);
#endif
}
inner_unexpanded=grib_bufr_descriptors_array_new(c,100,100); inner_unexpanded=grib_bufr_descriptors_array_new(c,100,100);
for (j=0;j<us->X;j++) { for (i=0;i<size;i++) {
urc=grib_bufr_descriptor_clone(ur[j]); vv=grib_bufr_descriptor_new(self->tablesAccessor,v[i],err);
grib_bufr_descriptors_array_push(inner_unexpanded,urc); inner_unexpanded=grib_bufr_descriptors_array_push(inner_unexpanded,vv);
} }
for (k=1;k<us->Y;k++) { grib_context_free(c,v);
for (j=0;j<us->X;j++) { inner_expanded=do_expand(a,inner_unexpanded,ccp,err);
urc=grib_bufr_descriptor_clone(ur[j]);
grib_bufr_descriptors_array_push(inner_unexpanded,urc);
}
}
for (i=0;i<us->X;i++) grib_bufr_descriptor_delete(ur[i]);
grib_context_free(c,ur);
inner_expanded=_expand(a,inner_unexpanded,ccp,err);
grib_bufr_descriptors_array_delete(inner_unexpanded); grib_bufr_descriptors_array_delete(inner_unexpanded);
#if MYDEBUG #if MYDEBUG
for (i=0;i<inner_expanded->n;i++) { for (i=0;i<inner_expanded->n;i++) {
for (idepth=0;idepth<depth;idepth++) printf("\t"); for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ push %06ld\n",inner_expanded->v[i]->code); printf("+++ push %06ld\n",inner_expanded->v[i]->code);
} }
#endif #endif
size=grib_bufr_descriptors_array_used_size(inner_expanded); size=grib_bufr_descriptors_array_used_size(inner_expanded);
expanded=grib_bufr_descriptors_array_append(expanded,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;idepth<depth;idepth++) printf("\t");
printf("+++ pop %06ld\n",u->code);
for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ push %06ld\n",u->code);
#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;j<us->X+1;j++) {
u0=grib_bufr_descriptors_array_pop_front(unexpanded);
grib_bufr_descriptors_array_push(inner_unexpanded,u0);
#if MYDEBUG
for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ pop %06ld\n",u0->code);
#endif
}
inner_expanded=do_expand(a,inner_unexpanded,ccp,err);
size=grib_bufr_descriptors_array_used_size(inner_expanded);
#if MYDEBUG
for (i=0;i<inner_expanded->n;i++) {
for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ push %06ld\n",inner_expanded->v[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;idepth<depth;idepth++) printf("\t");
printf("+++ pop %06ld\n",u->code);
#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;j<us->X;j++) {
ur[j]=grib_bufr_descriptors_array_pop_front(unexpanded);
#if MYDEBUG
for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ pop %06ld\n",ur[j]->code);
#endif
}
inner_unexpanded=grib_bufr_descriptors_array_new(c,100,100);
for (j=0;j<us->X;j++) {
urc=grib_bufr_descriptor_clone(ur[j]);
grib_bufr_descriptors_array_push(inner_unexpanded,urc);
}
for (k=1;k<us->Y;k++) {
for (j=0;j<us->X;j++) {
urc=grib_bufr_descriptor_clone(ur[j]);
grib_bufr_descriptors_array_push(inner_unexpanded,urc);
}
}
for (i=0;i<us->X;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;i<inner_expanded->n;i++) {
for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ push %06ld\n",inner_expanded->v[i]->code);
}
#endif
size=grib_bufr_descriptors_array_used_size(inner_expanded);
expanded=grib_bufr_descriptors_array_append(expanded,inner_expanded);
}
break;
case 0: case 0:
u=grib_bufr_descriptors_array_pop_front(unexpanded); u=grib_bufr_descriptors_array_pop_front(unexpanded);
size=1; size=1;
if (ccp->associatedFieldWidth && u->X!=31) { if (ccp->associatedFieldWidth && u->X!=31) {
bufr_descriptor* au=grib_bufr_descriptor_new(self->tablesAccessor,999999,err); bufr_descriptor* au=grib_bufr_descriptor_new(self->tablesAccessor,999999,err);
au->width=ccp->associatedFieldWidth; au->width=ccp->associatedFieldWidth;
grib_bufr_descriptor_set_scale(au,0); grib_bufr_descriptor_set_scale(au,0);
au->shortName=grib_context_strdup(c,"associatedField"); au->shortName=grib_context_strdup(c,"associatedField");
au->name=grib_context_strdup(c,"associated field"); au->name=grib_context_strdup(c,"associated field");
au->units=grib_context_strdup(c,"associated units"); au->units=grib_context_strdup(c,"associated units");
#if MYDEBUG #if MYDEBUG
for (idepth=0;idepth<depth;idepth++) printf("\t"); for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ push %06ld (%ld %g %ld)",au->code,au->scale,au->reference,au->width); printf("+++ push %06ld (%ld %g %ld)",au->code,au->scale,au->reference,au->width);
#endif #endif
grib_bufr_descriptors_array_push(expanded,au); grib_bufr_descriptors_array_push(expanded,au);
size++; size++;
}
#if MYDEBUG
for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ pop %06ld\n",u->code);
for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ push %06ld [type=%d] (%ld %g %ld)",u->code,
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);
} }
} else if (u->type==BUFR_DESCRIPTOR_TYPE_STRING && ccp->newStringWidth!=0) {
u->width=ccp->newStringWidth;
}
#if MYDEBUG #if MYDEBUG
printf("->(%ld %g %ld)\n",u->scale,u->reference,u->width); for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ pop %06ld\n",u->code);
for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ push %06ld [type=%d] (%ld %g %ld)",u->code,
u->type,u->scale,u->reference,u->width);
#endif #endif
grib_bufr_descriptors_array_push(expanded,u); if ( u->type!=BUFR_DESCRIPTOR_TYPE_FLAG &&
break; 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: case 2:
u=grib_bufr_descriptors_array_pop_front(unexpanded); u=grib_bufr_descriptors_array_pop_front(unexpanded);
#if MYDEBUG #if MYDEBUG
for (idepth=0;idepth<depth;idepth++) printf("\t"); for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ pop %06ld\n",u->code); printf("+++ pop %06ld\n",u->code);
#endif #endif
switch(us->X) { switch(us->X) {
case 1: case 1:
ccp->extraWidth = us->Y ? us->Y-128 : 0; ccp->extraWidth = us->Y ? us->Y-128 : 0;
size=0; size=0;
grib_bufr_descriptor_delete(u); grib_bufr_descriptor_delete(u);
break; break;
case 2: case 2:
ccp->extraScale = us->Y ? us->Y-128 : 0; ccp->extraScale = us->Y ? us->Y-128 : 0;
size=0; size=0;
grib_bufr_descriptor_delete(u); grib_bufr_descriptor_delete(u);
break; break;
case 4: case 4:
/* associated field*/ /* associated field*/
ccp->associatedFieldWidth=us->Y; ccp->associatedFieldWidth=us->Y;
grib_bufr_descriptor_delete(u); grib_bufr_descriptor_delete(u);
break; break;
case 6: case 6:
/*signify data width*/ /*signify data width*/
ccp->localDescriptorWidth=us->Y; ccp->localDescriptorWidth=us->Y;
size=0; size=0;
grib_bufr_descriptor_delete(u); grib_bufr_descriptor_delete(u);
break; break;
case 7: case 7:
if (us->Y) { if (us->Y) {
ccp->extraScale = us->Y; ccp->extraScale = us->Y;
ccp->referenceFactor=grib_power(us->Y,10); ccp->referenceFactor=grib_power(us->Y,10);
ccp->extraWidth=((10*us->Y)+2)/3; ccp->extraWidth=((10*us->Y)+2)/3;
} else { } else {
ccp->extraWidth=0; ccp->extraWidth=0;
ccp->extraScale=0; ccp->extraScale=0;
ccp->referenceFactor=1; ccp->referenceFactor=1;
} }
size=0; size=0;
grib_bufr_descriptor_delete(u); grib_bufr_descriptor_delete(u);
break; break;
case 8: case 8:
ccp->newStringWidth = us->Y * 8 ; ccp->newStringWidth = us->Y * 8 ;
break; break;
default: default:
#if MYDEBUG #if MYDEBUG
for (idepth=0;idepth<depth;idepth++) printf("\t"); for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ push %06ld\n",u->code); printf("+++ push %06ld\n",u->code);
#endif #endif
grib_bufr_descriptors_array_push(expanded,u); grib_bufr_descriptors_array_push(expanded,u);
size=1; size=1;
} }
break; break;
default: default:
u=grib_bufr_descriptors_array_pop_front(unexpanded); u=grib_bufr_descriptors_array_pop_front(unexpanded);
#if MYDEBUG #if MYDEBUG
for (idepth=0;idepth<depth;idepth++) printf("\t"); for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ pop %06ld\n",u->code); printf("+++ pop %06ld\n",u->code);
for (idepth=0;idepth<depth;idepth++) printf("\t"); for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("+++ push %06ld\n",u->code); printf("+++ push %06ld\n",u->code);
#endif #endif
grib_bufr_descriptors_array_push(expanded,u); grib_bufr_descriptors_array_push(expanded,u);
size=1; size=1;
} }
#if MYDEBUG #if MYDEBUG
for (idepth=0;idepth<depth;idepth++) printf("\t"); for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("expanding <== %d-%.2d-%.3d (size=%ld)\n\n",us->F,us->X,us->Y,size); printf("expanding <== %d-%.2d-%.3d (size=%ld)\n\n",us->F,us->X,us->Y,size);
@ -457,230 +458,232 @@ static size_t __expand(grib_accessor* a,bufr_descriptors_array* unexpanded,bufr_
return size; 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; bufr_descriptors_array* expanded=NULL;
grib_context* c=a->context; grib_context* c=a->context;
#if MYDEBUG #if MYDEBUG
int idepth; int idepth;
#endif #endif
depth++; depth++;
expanded=grib_bufr_descriptors_array_new(c,100,100); expanded=grib_bufr_descriptors_array_new(c,100,100);
#if MYDEBUG #if MYDEBUG
{ {
int i; int i;
for (idepth=0;idepth<depth;idepth++) printf("\t"); for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("to be expanded ==> \n"); printf("to be expanded ==> \n");
for (i=0;i<unexpanded->n;i++) { for (i=0;i<unexpanded->n;i++) {
bufr_descriptor* xx=grib_bufr_descriptors_array_get(unexpanded,i); bufr_descriptor* xx=grib_bufr_descriptors_array_get(unexpanded,i);
for (idepth=0;idepth<depth;idepth++) printf("\t"); for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("%06ld\n",xx->code); printf("%06ld\n",xx->code);
} }
for (idepth=0;idepth<depth;idepth++) printf("\t"); for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("to be expanded <== \n\n"); printf("to be expanded <== \n\n");
} }
#endif #endif
while (unexpanded->n) { while (unexpanded->n) {
__expand(a,unexpanded,expanded,ccp,err); __expand(a,unexpanded,expanded,ccp,err);
} }
#if MYDEBUG #if MYDEBUG
{ {
int i; int i;
for (idepth=0;idepth<depth;idepth++) printf("\t"); for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("expanded ==> \n"); printf("expanded ==> \n");
for (i=0;i<expanded->n;i++) { for (i=0;i<expanded->n;i++) {
bufr_descriptor* xx=grib_bufr_descriptors_array_get(expanded,i); bufr_descriptor* xx=grib_bufr_descriptors_array_get(expanded,i);
for (idepth=0;idepth<depth;idepth++) printf("\t"); for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("== %-6d== %06ld ",i,xx->code); printf("== %-6d== %06ld ",i,xx->code);
printf("%ld %g %ld",xx->scale,xx->reference,xx->width); printf("%ld %g %ld",xx->scale,xx->reference,xx->width);
printf("\n"); printf("\n");
} }
for (idepth=0;idepth<depth;idepth++) printf("\t"); for (idepth=0;idepth<depth;idepth++) printf("\t");
printf("expanded <== \n\n"); printf("expanded <== \n\n");
} }
#endif #endif
depth--; depth--;
return expanded; return expanded;
} }
static int expand(grib_accessor* a) static int expand(grib_accessor* a)
{ {
grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a;
int err=0; int err=0;
size_t unexpandedSize=0; size_t unexpandedSize=0;
int i; int i;
long* u=0; long* u=0;
change_coding_params ccp; change_coding_params ccp;
bufr_descriptors_array* unexpanded=NULL; bufr_descriptors_array* unexpanded=NULL;
grib_context* c=a->context; grib_context* c=a->context;
if (!self->do_expand) { if (!self->do_expand) {
return err; 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;i<unexpandedSize;i++) {
grib_bufr_descriptors_array_push(unexpanded,grib_bufr_descriptor_new(self->tablesAccessor,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;
} }
*len = expandedSize; self->do_expand=0;
for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->reference; if (!self->tablesAccessor) {
} self->tablesAccessor=grib_find_accessor(grib_handle_of_accessor(a),self->tablesAccessorName);
return ret; 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;i<unexpandedSize;i++) {
grib_bufr_descriptors_array_push(unexpanded,grib_bufr_descriptor_new(self->tablesAccessor,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) static int unpack_long (grib_accessor* a, long* val, size_t *len)
{ {
grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a;
int ret=0; int ret=0;
size_t rlen=0; size_t rlen=0;
size_t i; size_t i;
ret=expand(a); ret=expand(a);
rlen=grib_bufr_descriptors_array_used_size(self->expanded); rlen=grib_bufr_descriptors_array_used_size(self->expanded);
if (ret) return ret; if (ret) return ret;
if(*len < rlen) if(*len < rlen)
{ {
grib_context_log(a->context, GRIB_LOG_ERROR, grib_context_log(a->context, GRIB_LOG_ERROR,
" wrong size (%ld) for %s it contains %d values ",*len, a->name , rlen); " wrong size (%ld) for %s it contains %d values ",*len, a->name , rlen);
*len = 0; *len = 0;
return GRIB_ARRAY_TOO_SMALL; return GRIB_ARRAY_TOO_SMALL;
} }
*len = rlen; *len = rlen;
switch (self->rank) { switch (self->rank) {
case 0: case 0:
for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->code; for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->code;
break; break;
case 1: case 1:
for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->scale; for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->scale;
break; break;
case 2: case 2:
return GRIB_INVALID_TYPE; return GRIB_INVALID_TYPE;
break; break;
case 3: case 3:
for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->width; for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->width;
break; break;
case 4: case 4:
for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->type; for (i=0;i<*len;i++) val[i]=self->expanded->v[i]->type;
break; break;
} }
return GRIB_SUCCESS; return GRIB_SUCCESS;
} }
static int pack_long (grib_accessor* a, const long* val, size_t *len) static int pack_long (grib_accessor* a, const long* val, size_t *len)
{ {
grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a;
self->do_expand=1; self->do_expand=1;
return GRIB_NOT_IMPLEMENTED; return GRIB_NOT_IMPLEMENTED;
} }
static int value_count(grib_accessor* a,long* rlen) static int value_count(grib_accessor* a,long* rlen)
{ {
grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a;
int ret=0; int ret=0;
grib_context* c=a->context; grib_context* c=a->context;
*rlen=0; *rlen=0;
ret=expand(a); ret=expand(a);
*rlen=grib_bufr_descriptors_array_used_size(self->expanded); *rlen=grib_bufr_descriptors_array_used_size(self->expanded);
if (ret) { if (ret) {
grib_context_log(c,GRIB_LOG_ERROR,"%s unable to compute size",a->name); grib_context_log(c,GRIB_LOG_ERROR,"%s unable to compute size",a->name);
return ret; return ret;
} }
return ret; return ret;
} }
static void destroy(grib_context* c,grib_accessor* a) { 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_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a;
grib_bufr_descriptors_array_delete(self->expanded); if (self->rank==0 && self->expanded) {
} grib_bufr_descriptors_array_delete(self->expanded);
}
} }
static int get_native_type(grib_accessor* a) static int get_native_type(grib_accessor* a)
{ {
grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a; grib_accessor_expanded_descriptors* self = (grib_accessor_expanded_descriptors*)a;
if (self->rank==2) return GRIB_TYPE_DOUBLE; if (self->rank==2) return GRIB_TYPE_DOUBLE;
else return GRIB_TYPE_LONG; else return GRIB_TYPE_LONG;
} }