static int pack_long(grib_accessor* a, const long* val, size_t *len) { double* values=NULL; size_t size=0; int ret=0; grib_accessor_bits_per_value* self= (grib_accessor_bits_per_value*)a; grib_context* c=a->context; grib_handle* h=grib_handle_of_accessor(a); if ( (ret=grib_get_size(h,self->values,&size)) != GRIB_SUCCESS) return ret; values=(double*)grib_context_malloc(c,size*sizeof(double)); if (!values) return GRIB_OUT_OF_MEMORY; if((ret = grib_get_double_array_internal(h,self->values,values,&size)) != GRIB_SUCCESS) { grib_context_free(c,values); return ret; } if((ret = grib_set_long_internal(h, self->bits_per_value,*val)) != GRIB_SUCCESS) return ret; if((ret = grib_set_double_array_internal(h, self->values,values,size)) != GRIB_SUCCESS) return ret; grib_context_free(c,values); return GRIB_SUCCESS; }
static int compare(grib_accessor* a, grib_accessor* b) { int retval=0; double *aval=0; double *bval=0; size_t alen = (size_t)grib_value_count(a); size_t blen = (size_t)grib_value_count(b); if (alen != blen) return GRIB_COUNT_MISMATCH; aval=grib_context_malloc(a->parent->h->context,alen*sizeof(double)); bval=grib_context_malloc(b->parent->h->context,blen*sizeof(double)); b->dirty=1; a->dirty=1; grib_unpack_double(a,aval,&alen); grib_unpack_double(b,bval,&blen); retval = GRIB_SUCCESS; while (alen != 0) { if (*bval != *aval) retval = GRIB_DOUBLE_VALUE_MISMATCH; alen--; } grib_context_free(a->parent->h->context,aval); grib_context_free(b->parent->h->context,bval); return GRIB_SUCCESS; }
void grib_oarray_delete(grib_context* c,grib_oarray* v) { if (!v) return; if (!c) grib_context_get_default(); if (v->v) grib_context_free(c,v->v); grib_context_free(c,v); }
static int compare(grib_accessor* a,grib_accessor* b) { int retval=0; char *aval=0; char *bval=0; size_t alen = 0; size_t blen = 0; int err=0; long count=0; err=grib_value_count(a,&count); if (err) return err; alen=count; err=grib_value_count(b,&count); if (err) return err; blen=count; if (alen != blen) return GRIB_COUNT_MISMATCH; aval=(char*)grib_context_malloc(a->parent->h->context,alen*sizeof(char)); bval=(char*)grib_context_malloc(b->parent->h->context,blen*sizeof(char)); grib_unpack_string(a,aval,&alen); grib_unpack_string(b,bval,&blen); retval = GRIB_SUCCESS; if (!aval || !bval || grib_inline_strcmp(aval,bval)) retval = GRIB_STRING_VALUE_MISMATCH; grib_context_free(a->parent->h->context,aval); grib_context_free(b->parent->h->context,bval); return retval; }
static int destroy(grib_iterator* i){ grib_iterator_regular* self = (grib_iterator_regular*)i; const grib_context *c = i->h->context; grib_context_free(c,self->las); grib_context_free(c,self->los); return GRIB_SUCCESS; }
void grib_free_second_order_groups(grib_context *c,second_order_packed* sp) { if(!sp) return; grib_context_free(c,sp->array_of_group_size); grib_context_free(c,sp->array_of_group_refs); grib_context_free(c,sp->array_of_group_width); grib_context_free(c,sp); }
void grib_buffer_delete(const grib_context *c, grib_buffer *b) { if(b->property == GRIB_MY_BUFFER) grib_context_free(c,b->data); b->length = 0; b->ulength = 0; grib_context_free(c,b); }
static int destroy(grib_iterator* i) { grib_iterator_lambert_azimuthal_equal_area* self = (grib_iterator_lambert_azimuthal_equal_area*)i; const grib_context *c = i->h->context; grib_context_free(c,self->lats); grib_context_free(c,self->lons); return 1; }
static int destroy(grib_iterator* i) { grib_iterator_gaussian_reduced* self = (grib_iterator_gaussian_reduced*)i; const grib_context *c = i->h->context; grib_context_free(c,self->las); grib_context_free(c,self->los); return 1; }
static int destroy(grib_iterator* i) { grib_iterator_lambert_conformal* self = (grib_iterator_lambert_conformal*)i; const grib_context *c = i->h->context; grib_context_free(c,self->lats); grib_context_free(c,self->lons); return 1; }
static int destroy(grib_iterator* i) { grib_iterator_polar_stereographic* self = (grib_iterator_polar_stereographic*)i; const grib_context *c = i->h->context; grib_context_free(c, self->lats); grib_context_free(c, self->lons); return 1; }
static void index_entry_delete(grib_context* c,grib_concept_index_entry* entry) { grib_concept_index_entry* e; while (entry) { e=entry; entry=entry->next; grib_context_free(c,e->name); grib_context_free(c,e->value); grib_context_free(c,e); } }
static int destroy(grib_nearest* nearest) { grib_nearest_latlon_reduced* self = (grib_nearest_latlon_reduced*) nearest; if (self->lats) grib_context_free(nearest->context,self->lats); if (self->lons) grib_context_free(nearest->context,self->lons); if (self->j) grib_context_free(nearest->context,self->j); if (self->k) grib_context_free(nearest->context,self->k); if (self->distances) grib_context_free(nearest->context,self->distances); return GRIB_SUCCESS; }
static void dump_bytes(grib_dumper* d,grib_accessor* a,const char* comment) { grib_dumper_c_code *self = (grib_dumper_c_code*)d; int err =0; size_t size = a->length; unsigned char* buf; if (a->flags & GRIB_ACCESSOR_FLAG_READ_ONLY) return; if(size == 0) return; buf = grib_context_malloc(d->handle->context,size); if(!buf) { fprintf(self->dumper.out,"/* %s: cannot malloc(%ld) */\n",a->name,(long)size); return; } err = grib_unpack_bytes(a,buf,&size); if(err){ grib_context_free(d->handle->context,buf); fprintf(self->dumper.out," *** ERR=%d (%s) \n}",err,grib_get_error_message(err)); return ; } #if 0 if(size > 100) { more = size - 100; size = 100; } k = 0; /* if(size > 100) size = 100; */ while(k < size) { int j; for(i = 0; i < d->depth + 3 ; i++) fprintf(self->dumper.out," "); for(j = 0; j < 16 && k < size; j++, k++) { fprintf(self->dumper.out,"%02x",buf[k]); if(k != size-1) fprintf(self->dumper.out,", "); } fprintf(self->dumper.out,"\n"); } #endif grib_context_free(d->handle->context,buf); }
static int unpack_string(grib_accessor*a , char* v, size_t *len){ grib_accessor_md5* self = (grib_accessor_md5*)a; unsigned mess_len; unsigned char* mess; unsigned char* p; long offset,length; grib_string_list* blacklist=NULL; grib_accessor* b=NULL; int ret=0; int i=0; struct grib_md5_state md5c; if (*len <32 ) { grib_context_log(a->context,GRIB_LOG_ERROR,"md5: array too small"); return GRIB_ARRAY_TOO_SMALL; } if((ret = grib_get_long_internal(grib_handle_of_accessor(a),self->offset,&offset)) != GRIB_SUCCESS) return ret; if((ret = grib_expression_evaluate_long(grib_handle_of_accessor(a),self->length,&length)) != GRIB_SUCCESS) return ret; mess=(unsigned char*)grib_context_malloc(a->context,length); memcpy(mess,grib_handle_of_accessor(a)->buffer->data+offset,length); mess_len=length; blacklist=a->context->blacklist; /* passed blacklist overrides context blacklist. Consider to modify following line to extend context blacklist. */ if (self->blacklist) blacklist=self->blacklist; while (blacklist && blacklist->value) { b=grib_find_accessor(grib_handle_of_accessor(a),blacklist->value); if (!b) { grib_context_free(a->context,mess); return GRIB_NOT_FOUND; } p=mess+b->offset-offset; for (i=0;i<b->length;i++) *(p++)=0; blacklist=blacklist->next; } grib_md5_init(&md5c); grib_md5_add(&md5c,mess,mess_len); grib_md5_end(&md5c,v); grib_context_free(a->context,mess); return ret; }
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; }
static void destroy(grib_context* context,grib_accessor* a) { if (a->vvalue != NULL) grib_context_free(context, a->vvalue); a->vvalue=NULL; }
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 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 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; }
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 spatial_difference (grib_context *c, unsigned long* vals, long len, long order, long* bias) { long j = 3; long *v = (long*)grib_context_malloc(c,(len)*sizeof(long)); for(j = 0; j< len;j++) v[j] = vals[j]; Assert(order == 2); *bias=v[order]; for(j = order; j< len;j++){ v[j] -= vals [j-1]; v[j] -= vals [j-1] - vals[j-2]; if(*bias>v[j]) *bias = v[j]; } for(j = order; j< len;j++){ Assert(v[j]-*bias >=0); vals[j] = v[j]-*bias; } grib_context_free(c,v); return 0; }
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; }
/* --------------- 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; }
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 long value_count(grib_accessor* a) { grib_accessor_latitudes* self = (grib_accessor_latitudes*)a; grib_handle* h=a->parent->h; grib_context* c=a->parent->h->context; double* val=NULL; int ret; long len; size_t size; if ((ret=grib_get_size(h,self->values,&size))!=GRIB_SUCCESS) { grib_context_log(h->context,GRIB_LOG_ERROR,"unable to get size of %s",self->values); return 0; } len=(long)size; if (self->distinct) { ret=get_distinct(a,&val,&len); if (ret!=GRIB_SUCCESS) return 0; if (self->save) { self->lats=val; self->size=len; } else { grib_context_free(c,val); } } return len; }
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) { double* values=NULL; size_t size=0; double missingValue=0; long missingValuesPresent = 0; int ret=0,i=0; grib_accessor_offset_values* self= (grib_accessor_offset_values*)a; grib_context* c=a->context; grib_handle* h=grib_handle_of_accessor(a); if (*val==0) return GRIB_SUCCESS; if((ret = grib_get_double_internal(h,self->missingValue,&missingValue)) != GRIB_SUCCESS) { return ret; } if((ret=grib_get_long_internal(h,"missingValuesPresent",&missingValuesPresent)) != GRIB_SUCCESS) { return ret; } if ( (ret=grib_get_size(h,self->values,&size)) != GRIB_SUCCESS) return ret; values=(double*)grib_context_malloc(c,size*sizeof(double)); if (!values) return GRIB_OUT_OF_MEMORY; if((ret = grib_get_double_array_internal(h,self->values,values,&size)) != GRIB_SUCCESS) { grib_context_free(c,values); return ret; } for (i=0;i<size;i++) { if (missingValuesPresent) { if (values[i]!=missingValue) values[i]+=*val; } else { values[i]+=*val; } } if((ret = grib_set_double_array_internal(h, self->values,values,size)) != GRIB_SUCCESS) return ret; grib_context_free(c,values); return GRIB_SUCCESS; }
static int unpack_double(grib_accessor* a, double* val,size_t *len){ size_t rlen = 0; long count=0; unsigned long i = 0; long *values = NULL; long oneval = 0; int ret = GRIB_SUCCESS; ret=grib_value_count(a,&count); if (ret) return ret; rlen=count; if(*len < rlen) { grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, " wrong size for %s it contains %d values ", a->name , rlen); *len = 0; return GRIB_ARRAY_TOO_SMALL; } if(rlen == 1){ ret = grib_unpack_long(a,&oneval,&rlen); if(ret != GRIB_SUCCESS) return ret; *val = oneval; *len = 1; return GRIB_SUCCESS; } values = (long*)grib_context_malloc(a->parent->h->context,rlen*sizeof(long)); if(!values) return GRIB_INTERNAL_ERROR; ret = grib_unpack_long(a,values,&rlen); if(ret != GRIB_SUCCESS){ grib_context_free(a->parent->h->context,values); return ret; } for(i=0; i< rlen;i++) val[i] = values[i]; grib_context_free(a->parent->h->context,values); *len = rlen; return GRIB_SUCCESS; }
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; }