/* --------------- grib_column functions ------------------*/ int grib_fieldset_new_column(grib_fieldset* set,int id,char* key,int type) { grib_column* column=0; grib_context* c; int err=0; if (!set) return GRIB_INVALID_ARGUMENT; c=set->context; set->columns[id].errors=(int*)grib_context_malloc_clear(c, sizeof(int)*GRIB_START_ARRAY_SIZE); switch (type) { case GRIB_TYPE_LONG: set->columns[id].long_values=(long*)grib_context_malloc_clear(c, sizeof(long)*GRIB_START_ARRAY_SIZE); if (!set->columns[id].long_values) { grib_context_log(c, GRIB_LOG_ERROR, "grib_fieldset_new_column : Cannot malloc %d bytes", sizeof(long)*GRIB_START_ARRAY_SIZE); err=GRIB_OUT_OF_MEMORY; return err; } break; case GRIB_TYPE_DOUBLE: set->columns[id].double_values=(double*)grib_context_malloc_clear(c, sizeof(double)*GRIB_START_ARRAY_SIZE); if (!set->columns[id].double_values) { grib_context_log(c, GRIB_LOG_ERROR, "grib_fieldset_new_column : Cannot malloc %d bytes", sizeof(double)*GRIB_START_ARRAY_SIZE); err=GRIB_OUT_OF_MEMORY; return err; } break; case GRIB_TYPE_STRING: set->columns[id].string_values=(char**)grib_context_malloc_clear(c, sizeof(char*)*GRIB_START_ARRAY_SIZE); if (!set->columns[id].string_values) { grib_context_log(c, GRIB_LOG_ERROR, "grib_fieldset_new_column : Cannot malloc %d bytes", sizeof(char*)*GRIB_START_ARRAY_SIZE); err=GRIB_OUT_OF_MEMORY; return err; } break; default: grib_context_log(c, GRIB_LOG_ERROR, "grib_fieldset_new_column : unknown column type %d",type); grib_context_free(c,column); return err; } set->columns[id].context=c; set->columns[id].name=grib_context_strdup(c,key); set->columns[id].type=type; set->columns[id].values_array_size=GRIB_START_ARRAY_SIZE; set->columns[id].size=0; return err; }
grib_buffer* grib_create_growable_buffer(const grib_context* c) { grib_buffer *b = (grib_buffer*)grib_context_malloc_clear(c,sizeof(grib_buffer)); if(b == NULL) { grib_context_log(c,GRIB_LOG_ERROR,"grib_new_buffer: cannot allocate buffer"); return NULL; } b->property = GRIB_MY_BUFFER; b->length = 10240; b->ulength = 0; b->data = (unsigned char*)grib_context_malloc_clear(c,b->length); b->growable = 1; if(!b->data) { grib_context_log(c,GRIB_LOG_ERROR,"grib_new_buffer: cannot allocate buffer"); grib_context_free(c,b); return NULL; } return b; }
static void init(grib_accessor* a, const long len , grib_arguments* arg ) { grib_accessor_md5* self = (grib_accessor_md5*)a; char* b=0; int n=0; grib_string_list* current=0; grib_context* context=a->context; self->offset = grib_arguments_get_name(grib_handle_of_accessor(a),arg,n++); self->length = grib_arguments_get_expression(grib_handle_of_accessor(a),arg,n++); self->blacklist=0; while ( (b=(char*)grib_arguments_get_name(grib_handle_of_accessor(a),arg,n++)) !=NULL) { if (! self->blacklist) { self->blacklist=(grib_string_list*)grib_context_malloc_clear(context,sizeof(grib_string_list)); self->blacklist->value=grib_context_strdup(context,b); current=self->blacklist; } else { current->next=(grib_string_list*)grib_context_malloc_clear(context,sizeof(grib_string_list)); current->next->value=grib_context_strdup(context,b); current=current->next; } } a->length = 0; a->flags |= GRIB_ACCESSOR_FLAG_READ_ONLY; a->flags |= GRIB_ACCESSOR_FLAG_EDITION_SPECIFIC; }
static grib_concept_index_entry* index_entry_new(grib_context* c,grib_concept_index_keys* keys) { grib_concept_index_entry* entry=NULL; Assert(keys); if (!c) c=grib_context_get_default(); entry=grib_context_malloc_clear(c,sizeof(grib_concept_index_entry)); if (!entry) grib_context_log(c,GRIB_LOG_FATAL,"grib_concept_index_entry unable to allocate"); e=entry; while (keys && keys->name) { e->name=grib_context_strdup(c,keys->name); e->type=keys->type; e->next=grib_context_malloc_clear(c,sizeof(grib_concept_index_entry)); if (!e->next) grib_context_log(c,GRIB_LOG_FATAL,"grib_concept_index_entry unable to allocate"); e=e->next; keys=keys->next; } return entry; }
grib_section* grib_section_create ( grib_handle* h,grib_accessor* owner ) { grib_section* s = ( grib_section* ) grib_context_malloc_clear ( h->context,sizeof ( grib_section ) ); s->owner = owner; s->aclength = NULL; s->h = h; s->block = ( grib_block_of_accessors* ) grib_context_malloc_clear ( h->context, sizeof ( grib_block_of_accessors ) ); return s; }
static int get_distinct(grib_accessor* a,double** val,long* len) { long count=0; double prev; double *v=NULL; double *v1=NULL; double dummy; int ret=0; int i; size_t size=*len; grib_context* c=a->parent->h->context; grib_iterator* iter=grib_iterator_new(a->parent->h,0,&ret); if (ret!=GRIB_SUCCESS) { if (iter) grib_iterator_delete(iter); grib_context_log(c,GRIB_LOG_ERROR,"unable to create iterator"); return ret; } v=(double*)grib_context_malloc_clear(c,size*sizeof(double)); if (!v) { grib_context_log(c,GRIB_LOG_ERROR, "unable to allocate %ld bytes",(long)size*sizeof(double)); return GRIB_OUT_OF_MEMORY; } *val=v; while(grib_iterator_next(iter,v++,&dummy,&dummy)) {} grib_iterator_delete(iter); v=*val; qsort(v,*len,sizeof(double),&compare_doubles); v1=(double*)grib_context_malloc_clear(c,size*sizeof(double)); if (!v1) { grib_context_log(c,GRIB_LOG_ERROR, "unable to allocate %ld bytes",(long)size*sizeof(double)); return GRIB_OUT_OF_MEMORY; } prev=v[0]; v1[0]=prev; count=1; for (i=1;i<*len;i++) { if (v[i]!=prev) { prev=v[i]; v1[count]=prev; count++; } } grib_context_free(c,v); *val=v1; *len=count; return GRIB_SUCCESS; }
static double grib_invtrans(grib_context* context,int L,double latdeg,double londeg,double* values) { double val; double *c,*s,*TR,*TI; double sinlat,deg2rad,lonrad; int Lp1=L+1; deg2rad=acos(0.0)/90.0; sinlat=sin(latdeg*deg2rad); lonrad=londeg*deg2rad; c=(double*)grib_context_malloc_clear(context,sizeof(double)*Lp1); if (!c) { grib_context_log(context,GRIB_LOG_ERROR, "nearest_sh: unable to allocate %d bytes",sizeof(double)*Lp1); return GRIB_OUT_OF_MEMORY; } s=(double*)grib_context_malloc_clear(context,sizeof(double)*Lp1); if (!s) { grib_context_log(context,GRIB_LOG_ERROR, "nearest_sh: unable to allocate %d bytes",sizeof(double)*Lp1); return GRIB_OUT_OF_MEMORY; } grib_trigs(L,lonrad,c,s); TR=(double*)grib_context_malloc_clear(context,sizeof(double)*Lp1); if (!TR) { grib_context_log(context,GRIB_LOG_ERROR, "nearest_sh: unable to allocate %d bytes",sizeof(double)*Lp1); return GRIB_OUT_OF_MEMORY; } TI=(double*)grib_context_malloc_clear(context,sizeof(double)*Lp1); if (!TI) { grib_context_log(context,GRIB_LOG_ERROR, "nearest_sh: unable to allocate %d bytes",sizeof(double)*Lp1); return GRIB_OUT_OF_MEMORY; } grib_invtrans_legendre(L,sinlat,values,TR,TI); val=grib_invtrans_trig(L,TR,TI,c,s); grib_context_free(context,c); grib_context_free(context,s); grib_context_free(context,TR); grib_context_free(context,TI); return val; }
grib_iarray* grib_iarray_resize_to(grib_iarray* v,size_t newsize) { long* newv; size_t i; grib_context* c=v->context; if (newsize<v->size) return v; if (!c) c=grib_context_get_default(); newv=(long*)grib_context_malloc_clear(c,newsize*sizeof(long)); if (!newv) { grib_context_log(c,GRIB_LOG_ERROR, "grib_iarray_resize unable to allocate %d bytes\n",sizeof(long)*newsize); return NULL; } for (i=0;i<v->n;i++) newv[i]=v->v[i]; v->v-=v->number_of_pop_front; grib_context_free(c,v->v); v->v=newv; v->size=newsize; v->number_of_pop_front=0; return v; }
static int unpack_long (grib_accessor* a, long* val, size_t *len) { grib_accessor_sum* self = (grib_accessor_sum*)a; int ret = 0; size_t size=0; long* values=0; long i; size=value_count(a); if (size==0) { *val=0; return ret; } values=grib_context_malloc_clear(a->parent->h->context,sizeof(long)*size); if (!values) return GRIB_OUT_OF_MEMORY; grib_get_long_array(a->parent->h,self->values,values,&size); *val=0; for (i=0;i<size;i++) *val+=values[i]; grib_context_free(a->parent->h->context,values); return ret; }
static int pack_long(grib_accessor* a, const long* val, size_t *len) { grib_accessor_element* self = (grib_accessor_element*)a; int ret = 0; size_t size=0; long* ar=NULL; grib_context* c=a->parent->h->context; if(*len < 1){ ret = GRIB_ARRAY_TOO_SMALL; return ret; } if((ret = grib_get_size(a->parent->h, self->array,&size)) != GRIB_SUCCESS) return ret; ar=grib_context_malloc_clear(c,size*sizeof(long)); if (!ar) { grib_context_log(c,GRIB_LOG_ERROR,"unable to allocate %d bytes",size*sizeof(long)); return GRIB_OUT_OF_MEMORY; } if((ret = grib_get_long_array_internal(a->parent->h, self->array,ar,&size)) != GRIB_SUCCESS) return ret; ar[self->element]=*val; if((ret = grib_set_long_array_internal(a->parent->h, self->array,ar,size)) != GRIB_SUCCESS) return ret; grib_context_free(c,ar); return ret; }
static int unpack_double (grib_accessor* a, double* val, size_t *len) { grib_accessor_sum* self = (grib_accessor_sum*)a; int ret = 0; size_t size=0; double* values=0; long i; long count=0; ret=value_count(a,&count); if (ret) return ret; size=count; if (size==0) { *val=0; return ret; } values=(double*)grib_context_malloc_clear(a->parent->h->context,sizeof(double)*size); if (!values) return GRIB_OUT_OF_MEMORY; grib_get_double_array(a->parent->h,self->values,values,&size); *val=0; for (i=0;i<size;i++) *val+=values[i]; grib_context_free(a->parent->h->context,values); return ret; }
static int pack_long (grib_accessor* a, const long* val, size_t *len) { grib_accessor_signed_bits* self = (grib_accessor_signed_bits*)a; int ret = 0; long off = 0; long numberOfBits=0; size_t buflen = 0; unsigned char *buf = NULL; unsigned long i = 0; unsigned long rlen = value_count(a); if(*len != rlen) { ret=grib_set_long(a->parent->h,self->numberOfElements,rlen); } ret=grib_get_long(a->parent->h,self->numberOfBits,&numberOfBits); if (ret) return ret; buflen = compute_byte_count(a); buf = grib_context_malloc_clear(a->parent->h->context,buflen+sizeof(long)); for(i=0; i < rlen;i++) grib_encode_signed_longb(buf, val[i] , &off, numberOfBits); grib_buffer_replace(a, buf, buflen,1,1); grib_context_free(a->parent->h->context,buf); return ret; }
static void dump_string(grib_dumper* d,grib_accessor* a,const char* comment) { grib_dumper_default *self = (grib_dumper_default*)d; char *value=NULL; char *p = NULL; size_t size = 0; grib_context* c=NULL; int err = grib_get_string_length(a->parent->h,a->name,&size); c=a->parent->h->context; if (size==0) return; value=(char*)grib_context_malloc_clear(c,size); if (!value) { grib_context_log(c,GRIB_LOG_FATAL,"unable to allocate %d bytes",(int)size); return; } err = grib_unpack_string(a,value,&size); p=value; if ( (a->flags & GRIB_ACCESSOR_FLAG_DUMP) == 0) return; while(*p) { if(!isprint(*p)) *p = '.'; p++; } print_offset(self->dumper.out,d,a); if ((d->option_flags & GRIB_DUMP_FLAG_TYPE) != 0) { fprintf(self->dumper.out," "); fprintf(self->dumper.out,"# type %s \n",a->creator->op); } aliases(d,a); if(comment) { fprintf(self->dumper.out," "); fprintf(self->dumper.out,"# %s \n",comment); } if (a->flags & GRIB_ACCESSOR_FLAG_READ_ONLY) { fprintf(self->dumper.out," "); fprintf(self->dumper.out,"#-READ ONLY- "); } else fprintf(self->dumper.out," "); if( ((a->flags & GRIB_ACCESSOR_FLAG_CAN_BE_MISSING) != 0) && grib_is_missing_internal(a) ) fprintf(self->dumper.out,"%s = MISSING;",a->name); else fprintf(self->dumper.out,"%s = %s;",a->name,value); if(err) { fprintf(self->dumper.out," "); fprintf(self->dumper.out,"# *** ERR=%d (%s) [grib_dumper_default::dump_string]",err,grib_get_error_message(err)); } fprintf(self->dumper.out,"\n"); grib_context_free(c,value); }
static int pack_double(grib_accessor* a, const double* val, size_t *len) { grib_accessor_data_apply_bitmap* self = (grib_accessor_data_apply_bitmap*)a; int err = 0; size_t bmaplen = *len; long coded_n_vals = 0; double* coded_vals = NULL; long i = 0; long j = 0; double missing_value = 0; if (*len ==0) return GRIB_NO_VALUES; if(!grib_find_accessor(a->parent->h,self->bitmap)){ err = grib_set_double_array_internal(a->parent->h,self->coded_values,val,*len); /*printf("SETTING TOTAL number_of_data_points %s %ld\n",self->number_of_data_points,*len);*/ if(self->number_of_data_points) grib_set_long_internal(a->parent->h,self->number_of_data_points,*len); return err; } if((err = grib_get_double_internal(a->parent->h,self->missing_value,&missing_value)) != GRIB_SUCCESS) return err; if((err = grib_set_double_array_internal(a->parent->h,self->bitmap,val,bmaplen)) != GRIB_SUCCESS) return err; coded_n_vals = *len; if(coded_n_vals < 1){ err = grib_set_double_array_internal(a->parent->h,self->coded_values,NULL,0); return err; } coded_vals = (double*)grib_context_malloc_clear(a->parent->h->context,coded_n_vals*sizeof(double)); if(!coded_vals) return GRIB_OUT_OF_MEMORY; for(i=0; i<*len ; i++) { if(val[i] != missing_value) { coded_vals[j++] = val[i]; } } err = grib_set_double_array_internal(a->parent->h,self->coded_values,coded_vals,j); if (j==0) { if (self->number_of_values) err=grib_set_long_internal(a->parent->h,self->number_of_values,0); if (self->binary_scale_factor) err=grib_set_long_internal(a->parent->h,self->binary_scale_factor,0); } grib_context_free(a->parent->h->context,coded_vals); return err; }
static void resize(grib_accessor* a,size_t new_size) { void* zero = grib_context_malloc_clear(a->parent->h->context,new_size); grib_buffer_replace(a,(const unsigned char*)zero,new_size,1,0); grib_context_free(a->parent->h->context,zero); grib_context_log(a->parent->h->context,GRIB_LOG_DEBUG,"resize: grib_accessor_class_padding.c %ld %ld %s %s\n",(long)new_size,(long)a->length,a->cclass->name,a->name); Assert(new_size == a->length); }
second_order_packed* grib_get_second_order_groups(grib_context *c, const unsigned long* vals, size_t len) { second_order_packed* s = grib_context_malloc_clear(c,sizeof(second_order_packed)); const unsigned long* group_val = vals; size_t nv = len; size_t i = 0; unsigned long nbit_per_group; unsigned long size_of_group; long ref_of_group; s->packed_byte_count = 0; s->nbits_per_group_size = 6; s->nbits_per_widths = 4; s->size_of_group_array = 0; while(find_next_group(group_val, nv, calc_pow_2(s->nbits_per_widths), calc_pow_2(s->nbits_per_group_size ), &nbit_per_group, &size_of_group, &ref_of_group) == GRIB_SUCCESS) { s->size_of_group_array += 1; nv -= size_of_group; group_val += size_of_group; s->packed_byte_count += size_of_group*nbit_per_group; } s->packed_byte_count = ((s->packed_byte_count+7)/8); s->array_of_group_size = grib_context_malloc_clear(c,sizeof(unsigned long)*s->size_of_group_array); s->array_of_group_width = grib_context_malloc_clear(c,sizeof(unsigned long)*s->size_of_group_array); s->array_of_group_refs = grib_context_malloc_clear(c,sizeof( long)*s->size_of_group_array); group_val = vals; nv = len; while(find_next_group(group_val, nv, calc_pow_2(s->nbits_per_widths), calc_pow_2(s->nbits_per_group_size ), &nbit_per_group, &size_of_group, &ref_of_group) == GRIB_SUCCESS) { nv -= size_of_group; group_val += size_of_group; Assert(i<s->size_of_group_array); s->array_of_group_size[i] = size_of_group; s->array_of_group_width[i] = nbit_per_group; s->array_of_group_refs[i] = ref_of_group; i++; } return s; }
static void init(grib_accessor* a, const long len, grib_arguments* params) { int n=0; grib_accessor_codetable* self = (grib_accessor_codetable*)a; grib_action* act=(grib_action*)(a->creator); self->tablename = grib_arguments_get_string(a->parent->h,params,n++); self->masterDir = grib_arguments_get_name(a->parent->h,params,n++); self->localDir = grib_arguments_get_name(a->parent->h,params,n++); /*if (a->flags & GRIB_ACCESSOR_FLAG_STRING_TYPE) printf("-------- %s type string (%ld)\n",a->name,a->flags);*/ if (a->flags & GRIB_ACCESSOR_FLAG_TRANSIENT) { a->length = 0; if (!a->vvalue) a->vvalue = grib_context_malloc_clear(a->parent->h->context,sizeof(grib_virtual_value)); a->vvalue->type=grib_accessor_get_native_type(a); a->vvalue->length=len; if (act->default_value!=NULL) { const char* p = 0; size_t len = 1; long l; int ret=0; double d; char tmp[1024]; grib_expression* expression=grib_arguments_get_expression(a->parent->h,act->default_value,0); int type = grib_expression_native_type(a->parent->h,expression); switch(type) { case GRIB_TYPE_DOUBLE: grib_expression_evaluate_double(a->parent->h,expression,&d); grib_pack_double(a,&d,&len); break; case GRIB_TYPE_LONG: grib_expression_evaluate_long(a->parent->h,expression,&l); grib_pack_long(a,&l,&len); break; default: len = sizeof(tmp); p = grib_expression_evaluate_string(a->parent->h,expression,tmp,&len,&ret); if (ret != GRIB_SUCCESS) { grib_context_log(a->parent->h->context,GRIB_LOG_FATAL, "unable to evaluate %s as string",a->name); } len = strlen(p)+1; pack_string(a,p,&len); break; } } } else a->length = len; }
long* grib_iarray_get_array(grib_iarray* v) { long* vv; size_t i; grib_context* c=grib_context_get_default(); vv=(long*)grib_context_malloc_clear(c,sizeof(long)*v->n); for (i=0;i<v->n;i++) vv[i]=v->v[i]; return vv; }
static int unpack_long(grib_accessor* a, long* val, size_t *len) { int ret=GRIB_SUCCESS; size_t size=0; size_t i; double max,min,d,b; double *values=0; long binaryScaleFactor,decimalScaleFactor; grib_accessor_second_order_bits_per_value* self = (grib_accessor_second_order_bits_per_value*)a; if (self->bitsPerValue) { *val=self->bitsPerValue; return GRIB_SUCCESS; } if((ret = grib_get_size(a->parent->h, self->values,&size)) != GRIB_SUCCESS) { *val=self->bitsPerValue; return GRIB_SUCCESS; } if((ret = grib_get_long(a->parent->h, self->binaryScaleFactor,&binaryScaleFactor)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h, self->decimalScaleFactor,&decimalScaleFactor)) != GRIB_SUCCESS) return ret; values=grib_context_malloc_clear(a->parent->h->context,sizeof(double)*size); if (!values) { grib_context_log(a->parent->h->context,GRIB_LOG_FATAL,"%s unable to allocate %ld bytes", a->name,(long)size); return GRIB_OUT_OF_MEMORY; } if((ret = grib_get_double_array_internal(a->parent->h, self->values,values,&size)) != GRIB_SUCCESS) return ret; max=values[0]; min=max; for (i=1;i<size;i++) { if (max<values[i]) max=values[i]; if (min>values[i]) min=values[i]; } d=grib_power(-decimalScaleFactor,10); b=grib_power(-binaryScaleFactor,2); /* self->bitsPerValue=(long)ceil(log((double)((max-min)*d+1))/log(2.0))-binaryScaleFactor; */ self->bitsPerValue=number_of_bits((unsigned long)(fabs(max-min)*b*d)); *val=self->bitsPerValue; grib_context_free(a->parent->h->context,values); return ret; }
grib_oarray* grib_oarray_new(grib_context* c,size_t size,size_t incsize) { grib_oarray* v=NULL; if (!c) c=grib_context_get_default(); v=(grib_oarray*)grib_context_malloc_clear(c,sizeof(grib_oarray)); if (!v) { grib_context_log(c,GRIB_LOG_ERROR, "grib_oarray_new unable to allocate %d bytes\n",sizeof(grib_oarray)); return NULL; } v->size=size; v->n=0; v->incsize=incsize; v->v=(void**)grib_context_malloc_clear(c,sizeof(char*)*size); if (!v->v) { grib_context_log(c,GRIB_LOG_ERROR, "grib_oarray_new unable to allocate %d bytes\n",sizeof(char*)*size); return NULL; } return v; }
static void grib_grow_buffer_to(const grib_context *c, grib_buffer *b, size_t ns) { unsigned char* newdata; if(ns>b->length) { grib_get_buffer_ownership(c, b); newdata = (unsigned char*)grib_context_malloc_clear(c, ns); memcpy(newdata, b->data, b->length); grib_context_free(c,b->data); b->data = newdata; b->length = ns; } }
static int find(grib_nearest* nearest, grib_handle* h, double inlat, double inlon,unsigned long flags, double* outlats,double* outlons, double *outvalues,double *distances,int* indexes, size_t *len) { grib_nearest_sh* self = (grib_nearest_sh*) nearest; long J,K,M; double *values; int size,i,ret; size_t vsize=0; double val; if( (ret = grib_get_long(h,self->J,&J))!= GRIB_SUCCESS) return ret; if( (ret = grib_get_long(h,self->K,&K))!= GRIB_SUCCESS) return ret; if( (ret = grib_get_long(h,self->M,&M))!= GRIB_SUCCESS) return ret; size=2*LEGENDRE_SIZE(J); vsize=size; values=(double*)grib_context_malloc_clear(h->context,sizeof(double)*size); if (!values) { grib_context_log(h->context,GRIB_LOG_ERROR, "nearest_sh: unable to allocate %d bytes", sizeof(double)*size); return GRIB_OUT_OF_MEMORY; } if( (ret = grib_get_double_array(h,self->values_key,values,&vsize)) != GRIB_SUCCESS) return ret; Assert(vsize==size); val=grib_invtrans(h->context,J,inlat,inlon,values); grib_context_free(h->context,values); for (i=0;i<4;i++) { outlats[i]=inlat; outlons[i]=inlon; outvalues[i]=val; indexes[i]=-1; } return GRIB_SUCCESS; }
grib_buffer* grib_new_buffer(const grib_context* c, const unsigned char* data, size_t buflen) { grib_buffer *b = (grib_buffer*)grib_context_malloc_clear(c,sizeof(grib_buffer)); if(b == NULL) { grib_context_log(c,GRIB_LOG_ERROR,"grib_new_buffer: cannot allocate buffer"); return NULL; } b->property = GRIB_USER_BUFFER; b->length = buflen; b->ulength = buflen; b->ulength_bits = buflen*8; b->data = (unsigned char*)data; return b; }
static int unpack_double_element(grib_accessor* a, size_t idx,double* val) { size_t size; double* values; int err = 0; /* GRIB-564: The index idx relates to codedValues NOT values! */ err=grib_get_size(a->parent->h,"codedValues",&size); if (err) return err; if (idx > size) return GRIB_INVALID_NEAREST; values=(double*)grib_context_malloc_clear(a->parent->h->context,size*sizeof(double)); err=grib_get_double_array(a->parent->h,"codedValues",values,&size); if (err) return err; *val=values[idx]; grib_context_free(a->parent->h->context,values); return err; }
static int pack_long (grib_accessor* a, const long* val, size_t *len) { grib_accessor_unsigned_bits* self = (grib_accessor_unsigned_bits*)a; int ret = 0; long off = 0; long numberOfBits=0; size_t buflen = 0; unsigned char *buf = NULL; unsigned long i = 0; unsigned long rlen = value_count(a); /* if(*len < rlen) { grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, "Wrong size for %s it contains %d values ", a->name , rlen ); return GRIB_ARRAY_TOO_SMALL; } */ if (*len!=rlen) ret=grib_set_long(a->parent->h,self->numberOfElements,*len); ret=grib_get_long(a->parent->h,self->numberOfBits,&numberOfBits); if (ret) return ret; if (numberOfBits==0) { grib_buffer_replace(a, NULL, 0,1,1); return GRIB_SUCCESS; } buflen = compute_byte_count(a); buf = grib_context_malloc_clear(a->parent->h->context,buflen+sizeof(long)); for(i=0; i < *len; i++) grib_encode_unsigned_longb(buf, val[i] , &off, numberOfBits); grib_buffer_replace(a, buf, buflen,1,1); grib_context_free(a->parent->h->context,buf); return ret; }
static void dump_string(grib_dumper* d,grib_accessor* a,const char* comment) { grib_dumper_wmo *self = (grib_dumper_wmo*)d; size_t size=0; char *value=NULL; char *p=NULL; int err = grib_get_string_length(a->parent->h,a->name,&size); value=(char *)grib_context_malloc_clear(a->parent->h->context,size); if (!value) { grib_context_log(a->parent->h->context,GRIB_LOG_FATAL,"unable to allocate %d bytes",(int)size); return; } err=grib_unpack_string(a,value,&size); p=value; if( a->length == 0 && (d->option_flags & GRIB_DUMP_FLAG_CODED) != 0) { grib_context_free(a->parent->h->context,value); return; } set_begin_end(d,a); while(*p) { if(!isprint(*p)) *p = '.'; p++; } /*for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");*/ print_offset(self->dumper.out,self->begin,self->theEnd); if ((d->option_flags & GRIB_DUMP_FLAG_TYPE) != 0) fprintf(self->dumper.out,"%s ",a->creator->op); fprintf(self->dumper.out,"%s = %s",a->name,value); if (err==0) print_hexadecimal(self->dumper.out,d->option_flags,a); /*if(comment) fprintf(self->dumper.out," [%s]",comment);*/ if(err) fprintf(self->dumper.out," *** ERR=%d (%s) [grib_dumper_wmo::dump_string]",err,grib_get_error_message(err)); aliases(d,a); fprintf(self->dumper.out,"\n"); if (value) grib_context_free(a->parent->h->context,value); }
static int pack_double(grib_accessor* a, const double* val,size_t *len){ grib_accessor_g1bitmap* self = (grib_accessor_g1bitmap*)a; size_t tlen; unsigned char* buf = NULL; long i; int err = 0; long pos = 0; long bmaplen = 0; const int bit_padding = 16; double miss_values = 0; tlen = ((*len+bit_padding-1)/bit_padding*bit_padding)/8; if((err = grib_get_double_internal(a->parent->h, self->missing_value, &miss_values)) != GRIB_SUCCESS) return err; buf = grib_context_malloc_clear(a->parent->h->context,tlen); if(!buf) return GRIB_OUT_OF_MEMORY; pos=0; for(i=0;i<*len;i++) { if (val[i] == miss_values) pos++; else{ bmaplen++; grib_set_bit_on(buf, &pos); } } if((err = grib_set_long_internal(a->parent->h, self->unusedBits,tlen*8 - *len )) != GRIB_SUCCESS) return err; grib_buffer_replace(a, buf, tlen,1,1); grib_context_free(a->parent->h->context,buf); return GRIB_SUCCESS; }
static void dump_string(grib_dumper* d,grib_accessor* a,const char* comment) { grib_dumper_debug *self = (grib_dumper_debug*)d; int err=0; int i; size_t size=0; char *value=NULL; char *p=NULL ; err = grib_get_string_length(a->parent->h, a->name, &size); if ( (size < 2) && grib_is_missing_internal(a) ) { /* GRIB-302: transients and missing keys. Need to re-adjust the size */ size = 10; /* big enough to hold the string "missing" */ } value=(char*)grib_context_malloc_clear(a->parent->h->context,size); if (!value) return; err=grib_unpack_string(a,value,&size); if(err) strcpy(value,"<error>"); p=value; if( a->length == 0 && (d->option_flags & GRIB_DUMP_FLAG_CODED) != 0) return; set_begin_end(d,a); while(*p) { if(!isprint(*p)) *p = '.'; p++; } for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," "); fprintf(self->dumper.out,"%ld-%ld %s %s = %s",self->begin,self->theEnd,a->creator->op, a->name,value); if(comment) fprintf(self->dumper.out," [%s]",comment); if(err) fprintf(self->dumper.out," *** ERR=%d (%s) [grib_dumper_debug::dump_string]",err,grib_get_error_message(err)); aliases(d,a); fprintf(self->dumper.out,"\n"); if (value) grib_context_free(a->parent->h->context,value); }
static grib_fieldset* grib_fieldset_create_from_order_by(grib_context* c,grib_order_by* ob, int* err) { char** keys=NULL; size_t nkeys=0; int i=0; grib_fieldset* set=NULL; grib_order_by* next=ob; while(next) {nkeys++;next=next->next;} keys=(char**)grib_context_malloc_clear( c,nkeys*sizeof(char*)); next=ob; i=0; while(next) {keys[i++]=next->key;next=next->next;} set=grib_fieldset_create_from_keys(c,keys,nkeys,err); grib_context_free(c,keys); return set; }
static void init(grib_accessor* a, const long len , grib_arguments* arg ) { grib_accessor_unsigned* self = (grib_accessor_unsigned*)a; self->arg = NULL; self->arg = arg; self->nbytes = len; if (a->flags & GRIB_ACCESSOR_FLAG_TRANSIENT) { a->length=0; if (!a->vvalue) a->vvalue=(grib_virtual_value*)grib_context_malloc_clear(a->parent->h->context,sizeof(grib_virtual_value)); a->vvalue->type=GRIB_TYPE_LONG; a->vvalue->length=len; } else { long count=0; grib_value_count(a,&count); a->length = len*count; a->vvalue=NULL; } }