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;
}
Example #2
0
grib_iarray* grib_iarray_new(grib_context* c,size_t size,size_t incsize)
{
    grib_iarray* v=NULL;

    if (!c) c=grib_context_get_default();

    v=(grib_iarray*)grib_context_malloc(c,sizeof(grib_iarray));
    if (!v) {
        grib_context_log(c,GRIB_LOG_ERROR,
                "grib_iarray_new unable to allocate %d bytes\n",sizeof(grib_iarray));
        return NULL;
    }
    v->context=c;
    v->size=size;
    v->n=0;
    v->incsize=incsize;
    v->v=(long*)grib_context_malloc(c,sizeof(long)*size);
    v->number_of_pop_front=0;
    if (!v->v) {
        grib_context_log(c,GRIB_LOG_ERROR,
                "grib_iarray_new unable to allocate %d bytes\n",sizeof(long)*size);
        return NULL;
    }
    return v;
}
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;
}
static int init(grib_nearest* nearest,grib_handle* h,grib_arguments* args)
{
    grib_nearest_lambert_conformal* self = (grib_nearest_lambert_conformal*) nearest;
    self->Ni  = grib_arguments_get_name(h,args,self->cargs++);
    self->Nj  = grib_arguments_get_name(h,args,self->cargs++);
    self->i=(int*)grib_context_malloc(h->context,2*sizeof(int));
    self->j=(int*)grib_context_malloc(h->context,2*sizeof(int));
    return 0;
}
static int init(grib_nearest* nearest,grib_handle* h,grib_arguments* args)
{
  grib_nearest_reduced* self = (grib_nearest_reduced*) nearest;
  self->Nj  = grib_arguments_get_name(h,args,self->cargs++);
  self->pl  = grib_arguments_get_name(h,args,self->cargs++);
  self->j=(int*)grib_context_malloc(h->context,2*sizeof(int));
  if (!self->j) return GRIB_OUT_OF_MEMORY;
  self->k=(int*)grib_context_malloc( nearest->context,4*sizeof(int));
  if (!self->k) return GRIB_OUT_OF_MEMORY;

  return 0;

}
static int init(grib_iterator* i,grib_handle* h,grib_arguments* args)
{
  grib_iterator_regular* self = (grib_iterator_regular*)i;
  int ret = GRIB_SUCCESS;

  long nap;
  long nam;
  double idir;

  double lof,lol;
  long loi;
  
  const char* longoffirst = grib_arguments_get_name(h,args,self->carg++);
  const char* idirec      = grib_arguments_get_name(h,args,self->carg++);
  const char* nalpar      = grib_arguments_get_name(h,args,self->carg++);
  const char* nalmer      = grib_arguments_get_name(h,args,self->carg++);
  const char* iScansNegatively  = grib_arguments_get_name(h,args,self->carg++);

  
  if((ret = grib_get_double_internal(h,longoffirst,   &lof))) return ret;
  if((ret = grib_get_double_internal(h,"longitudeOfLastGridPointInDegrees",   &lol))) return ret;

  if((ret = grib_get_double_internal(h,idirec,        &idir))) return ret;

  if((ret = grib_get_long_internal(h,nalpar,          &nap))) return ret;
  if((ret = grib_get_long_internal(h,nalmer,          &nam))) return ret;
  if((ret = grib_get_long_internal(h,iScansNegatively,&self->iScansNegatively)))
     return ret;

  idir=fabs(lof-lol)/(nap-1);
  if (self->iScansNegatively) {
    idir=-idir;
  } else {
	if (lof+(nap-2)*idir>360) lof-=360;
    else if (lof+nap*idir>360) idir=360.0/(float)nap;
  }

  self->nap = nap;
  self->nam = nam;

  self->las = grib_context_malloc(h->context,nam*sizeof(double));
  self->los = grib_context_malloc(h->context,nap*sizeof(double));

  for( loi = 0; loi < nap; loi++ )  {
    self->los[loi] = lof;
    lof += idir ;
  }

  return ret;
}
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 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;
}
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;
  unsigned char digest[16];
  long offset,length;
  grib_string_list* blacklist=NULL;
  grib_accessor* b=NULL;
  int ret=0;
  char* s=NULL;
  int i=0;
  struct cvs_MD5Context md5c;

  if (*len <32 ) {
    grib_context_log(a->parent->h->context,GRIB_LOG_ERROR,"md5: array too small");
    return GRIB_ARRAY_TOO_SMALL;
  }
  
  if((ret = grib_get_long_internal(a->parent->h,self->offset,&offset))
      != GRIB_SUCCESS)
    return ret;
  if((ret = grib_get_long_internal(a->parent->h,self->length,&length))
      != GRIB_SUCCESS)
    return ret;

  
  mess=grib_context_malloc(a->parent->h->context,length);
  memcpy(mess,a->parent->h->buffer->data+offset,length);
  mess_len=length;

  blacklist=a->parent->h->context->blacklist;
  while (blacklist && blacklist->value) {
    
    b=grib_find_accessor(a->parent->h,blacklist->value);
    if (!b) return GRIB_NOT_FOUND;

    p=mess+b->offset-offset;
    for (i=0;i<b->length;i++) *(p++)=0;

    blacklist=blacklist->next;
  }

  cvs_MD5Init(&md5c);
  cvs_MD5Update(&md5c,mess,mess_len);
  cvs_MD5Final(digest,&md5c);

  s=v;
  for (i = 0; i < 16; i++)
  {
    sprintf (s,"%02x", (unsigned int) digest[i]);
    s+=2;
  }

  return ret;
}
Example #10
0
void grib_get_buffer_ownership(const grib_context *c, grib_buffer *b)
{
    unsigned char* newdata;
    if(b->property == GRIB_MY_BUFFER)
        return;

    newdata = (unsigned char*)grib_context_malloc(c, b->length);
    memcpy(newdata, b->data, b->length);
    b->data = newdata;
    b->property = GRIB_MY_BUFFER;
}
Example #11
0
grib_darray* grib_darray_new(grib_context* c,size_t size,size_t incsize) {
  grib_darray* v=NULL;
  if (!c) c=grib_context_get_default();
  v=(grib_darray*)grib_context_malloc(c,sizeof(grib_darray));
  if (!v) {
    grib_context_log(c,GRIB_LOG_ERROR,
          "grib_darray_new unable to allocate %d bytes\n",sizeof(grib_darray));
    return NULL;
  }
  v->size=size;
  v->n=0;
  v->incsize=incsize;
  v->v=(double*)grib_context_malloc(c,sizeof(double)*size);
  if (!v->v) {
    grib_context_log(c,GRIB_LOG_ERROR,
          "grib_darray_new unable to allocate %d bytes\n",sizeof(double)*size);
    return NULL;
  }
  return v;
}
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 int pack_double   (grib_accessor* a, const double* val, size_t *len)
{
    grib_accessor_ibmfloat* self = (grib_accessor_ibmfloat*)a;
    int ret = 0;
    unsigned long i = 0;
    unsigned long rlen = *len;
    size_t buflen  = 0;
    unsigned char *buf = NULL;
    long off = 0;

    if(*len < 1)
    {
        grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, " wrong size for %s it pack at least 1 values ", a->name , rlen );
        *len = 0;
        return GRIB_ARRAY_TOO_SMALL;
    }

    if (rlen == 1){
        /*
    double x = 0;
    grib_nearest_smaller_ibm_float(val[0],&x);
    double y = grib_long_to_ibm(grib_ibm_to_long(val[0]));
    printf("IBMFLOAT val=%.20f nearest_smaller_ibm_float=%.20f long_to_ibm=%.20f\n",val[0],x ,y);
         */
        off = byte_offset(a)*8;
        ret =  grib_encode_unsigned_long(a->parent->h->buffer->data,grib_ibm_to_long(val[0]), &off,  32);
        if (*len > 1)  grib_context_log(a->parent->h->context, GRIB_LOG_WARNING, "grib_accessor_unsigned : Trying to pack %d values in a scalar %s, packing first value",  *len, a->name  );
        if (ret == GRIB_SUCCESS) len[0] = 1;
        return ret;
    }

    buflen = rlen*4;

    buf = (unsigned char*)grib_context_malloc(a->parent->h->context,buflen);

    for(i=0; i < rlen;i++){
        grib_encode_unsigned_longb(buf,grib_ibm_to_long(val[i]), &off,  32);
    }
    ret = grib_set_long_internal(a->parent->h,grib_arguments_get_name(a->parent->h,self->arg,0),rlen);

    if(ret == GRIB_SUCCESS)
        grib_buffer_replace(a, buf, buflen,1,1);
    else
        *len = 0;

    grib_context_free(a->parent->h->context,buf);

    a->length = byte_count(a);

    return ret;
}
static int compare(grib_accessor* a,grib_accessor* b) {
  int retval=0;
  long *aval=0;
  long *bval=0;
  long count=0;

  size_t alen = 0;
  size_t blen = 0;
  int err=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=(long*)grib_context_malloc(a->parent->h->context,alen*sizeof(long));
  bval=(long*)grib_context_malloc(b->parent->h->context,blen*sizeof(long));

  grib_unpack_long(a,aval,&alen);
  grib_unpack_long(b,bval,&blen);

  retval = GRIB_SUCCESS;
  while (alen != 0) {
    if (*bval != *aval) retval = GRIB_LONG_VALUE_MISMATCH;
  alen--;
  }

  grib_context_free(a->parent->h->context,aval);
  grib_context_free(b->parent->h->context,bval);

  return retval;
}
static void init(grib_accessor* a,const long l, grib_arguments* c)
{
  grib_accessor_statistics* self = (grib_accessor_statistics*)a;
  int n = 0;

  self->missing_value = grib_arguments_get_name(a->parent->h,c,n++);
  self->values = grib_arguments_get_name(a->parent->h,c,n++);
  a->flags  |= GRIB_ACCESSOR_FLAG_READ_ONLY;
  a->flags |= GRIB_ACCESSOR_FLAG_FUNCTION;
  a->flags |= GRIB_ACCESSOR_FLAG_HIDDEN;

  self->number_of_elements=8;
  self->v=(double*)grib_context_malloc(a->parent->h->context,
                sizeof(double)*self->number_of_elements);

  a->length=0;
  a->dirty=1;
}
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)
{
    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    pack_long   (grib_accessor* a, const long* val, size_t *len)
{
  long missing=255;
  int ret=0;
  size_t size=0;
  double* values;
  grib_context* c=a->parent->h->context;
  grib_handle* h=a->parent->h;
  grib_accessor_gds_is_present* self = (grib_accessor_gds_is_present*)a;

  if (*val != 1) return GRIB_NOT_IMPLEMENTED;

  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->gds_present,*val))
      != GRIB_SUCCESS) return ret;

  if((ret = grib_set_long_internal(h, self->bitmap_present,*val))
      != GRIB_SUCCESS) return ret;



  if((ret = grib_set_long_internal(h, self->grid_definition,missing))
      != 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 unpack_long(grib_accessor* a, long* val, size_t *len) {
  size_t size=0;
  long* vector;
  grib_accessor_long_vector* self = (grib_accessor_long_vector*)a;
  grib_accessor* va=NULL;
  grib_accessor_abstract_long_vector* v =NULL;
  
  va=(grib_accessor*)grib_find_accessor(a->parent->h,self->vector);
  v=(grib_accessor_abstract_long_vector*)va;

  /*TODO implement the dirty mechanism to avoid to unpack every time */
  grib_get_size(a->parent->h,self->vector,&size);
  vector=(long*)grib_context_malloc(a->parent->h->context,sizeof(long)*size);
  grib_unpack_long(va,vector,&size);
  grib_context_free(a->parent->h->context,vector);
  

  *val = v->v[self->index];

  return GRIB_SUCCESS;
}
static int pack_double(grib_accessor* a, const double* val, size_t *len)
{
  double* values=NULL;
  double missingValue=0;
  size_t size=0;
  int ret=0,i=0;
  grib_accessor_scale_values* self= (grib_accessor_scale_values*)a;
  grib_context* c=a->parent->h->context;
  grib_handle* h=a->parent->h;

  if (*val==1) return GRIB_SUCCESS;

  if((ret = grib_get_double_internal(h,self->missingValue,&missingValue))
       != 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 (values[i]!=missingValue) 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_element(grib_accessor* a, size_t idx,double* val)
{
    grib_accessor_data_apply_boustrophedonic_bitmap* self =  (grib_accessor_data_apply_boustrophedonic_bitmap*)a;
    int err = 0,i=0;
    size_t cidx=0;
    double missing_value = 0;
    double* bvals=NULL;
    size_t n_vals = 0;
    long nn=0;

    err=grib_value_count(a,&nn);
    n_vals=nn;
    if (err) return err;

    if(!grib_find_accessor(a->parent->h,self->bitmap))
        return grib_get_double_element_internal(a->parent->h,self->coded_values,idx,val);

    if((err = grib_get_double_internal(a->parent->h,self->missing_value,&missing_value)) != GRIB_SUCCESS)
        return err;

    if((err = grib_get_double_element_internal(a->parent->h,self->bitmap,idx,val)) != GRIB_SUCCESS)
        return err;

    if (*val == 0) {*val=missing_value;return GRIB_SUCCESS;}

    bvals = (double*)grib_context_malloc(a->parent->h->context,n_vals*sizeof(double));
    if(bvals == NULL) return GRIB_OUT_OF_MEMORY;

    if((err = grib_get_double_array_internal(a->parent->h,self->bitmap,bvals,&n_vals)) != GRIB_SUCCESS)
        return err;

    cidx=0;
    for (i=0;i<idx;i++) {cidx+=bvals[i];}

    grib_context_free(a->parent->h->context,bvals);

    return grib_get_double_element_internal(a->parent->h,self->coded_values,cidx,val);
}
static int init(grib_iterator* i,grib_handle *h, grib_arguments* args)
{
  grib_iterator_gen* self = (grib_iterator_gen*) i;
  size_t dli=0;
  int ret = GRIB_SUCCESS;
  const char* rawdat  = NULL;
  const char* snumberOfPoints=NULL;
  long numberOfPoints=0;
  self->carg = 1;
  
  snumberOfPoints = grib_arguments_get_name(h,args,self->carg++);
  self->missingValue  = grib_arguments_get_name(h,args,self->carg++);
  rawdat      = grib_arguments_get_name(h,args,self->carg++);

  i->h    = h; /* We may not need to keep them */
  i->args = args;
  if( (ret =  grib_get_size(h,rawdat,&dli))!= GRIB_SUCCESS) return ret;

  if( (ret =  grib_get_long_internal(h,snumberOfPoints,&numberOfPoints))
       != GRIB_SUCCESS)
    return ret;

  if (numberOfPoints!=dli) {
    grib_context_log(h->context,GRIB_LOG_ERROR,"%s != size(%s) (%ld!=%ld)",
                     snumberOfPoints,rawdat,numberOfPoints,dli);
    return GRIB_WRONG_GRID;
  }
  i->nv = dli;
  i->data = (double*)grib_context_malloc(h->context,(i->nv)*sizeof(double));

  if( (ret = grib_get_double_array_internal(h,rawdat,i->data ,&(i->nv))))
    return ret;

  i->e = -1;

  return ret;
}
static int  unpack_long(grib_accessor* a, long* val, size_t *len)
{
  int ret=GRIB_SUCCESS;
  long ni=0,nj=0,plpresent=0;
  size_t plsize=0;
  long* pl;
  int i;
  grib_accessor_number_of_points* self = (grib_accessor_number_of_points*)a;
  grib_context* c=a->context;

  if((ret = grib_get_long_internal(grib_handle_of_accessor(a), self->ni,&ni)) != GRIB_SUCCESS)
    return ret;

  if((ret = grib_get_long_internal(grib_handle_of_accessor(a), self->nj,&nj)) != GRIB_SUCCESS)
    return ret;

  if(self->plpresent &&
     ((ret = grib_get_long_internal(grib_handle_of_accessor(a), self->plpresent,&plpresent)) != GRIB_SUCCESS) )
    return ret;

  if (nj == 0) return GRIB_GEOCALCULUS_PROBLEM;

  if (plpresent) {
    /*reduced*/
    plsize=nj;
    pl=(long*)grib_context_malloc(c,sizeof(long)*plsize);
    grib_get_long_array_internal(grib_handle_of_accessor(a),self->pl,pl, &plsize);
    *val=0;
    for (i=0;i<plsize;i++) *val+=pl[i];
	grib_context_free(c,pl);
  } else {
    /*regular*/
    *val=ni*nj;
  }

  return ret;
}
static void dump_values(grib_dumper* d,grib_accessor* a)
{
    grib_dumper_debug *self = (grib_dumper_debug*)d;
    int i,k,err =0;
    int more = 0;
    double*  buf = NULL;
    size_t size=0;
    long count=0;

    if( a->length == 0  &&
            (d->option_flags & GRIB_DUMP_FLAG_CODED) != 0)
        return;

    grib_value_count(a,&count);
    size=count;
    if(size == 1){
        dump_double(d,a,NULL);
        return ;
    }
    buf = (double*)grib_context_malloc(d->handle->context,size * sizeof(double));

    set_begin_end(d,a);

    for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");
    fprintf(self->dumper.out,"%ld-%ld %s %s = (%ld,%ld)",self->begin,self->theEnd,a->creator->op, a->name,(long)size,a->length);
    aliases(d,a);
    fprintf(self->dumper.out," {");

    if(!buf)
    {
        if(size == 0)
            fprintf(self->dumper.out,"}\n");
        else
            fprintf(self->dumper.out," *** ERR cannot malloc(%ld) }\n",(long)size);
        return;
    }

    fprintf(self->dumper.out,"\n");

    err =  grib_unpack_double(a,buf,&size);

    if(err){
        grib_context_free(d->handle->context,buf);
        fprintf(self->dumper.out," *** ERR=%d (%s) [grib_dumper_debug::dump_values]\n}",err,grib_get_error_message(err));
        return ;
    }

    if(size > 100) {
        more = size - 100;
        size = 100;
    }


    k = 0;
    while(k < size)
    {
#if 1
        int j;
        for(i = 0; i < d->depth + 3 ; i++) fprintf(self->dumper.out," ");
        for(j = 0; j < 8 && k < size; j++, k++)
        {
            fprintf(self->dumper.out,"%10g",buf[k]);
            if(k != size-1)
                fprintf(self->dumper.out,", ");
        }
        fprintf(self->dumper.out,"\n");
#else

        fprintf(self->dumper.out,"%d %g\n",k,buf[k]);

#endif

    }
    if(more)
    {
        for(i = 0; i < d->depth + 3 ; i++) fprintf(self->dumper.out," ");
        fprintf(self->dumper.out,"... %d more values\n",more);
    }

    for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");
    fprintf(self->dumper.out,"} # %s %s \n",a->creator->op, a->name);
    grib_context_free(d->handle->context,buf);
}
static void dump_bytes(grib_dumper* d,grib_accessor* a,const char* comment)
{
    grib_dumper_debug *self = (grib_dumper_debug*)d;
    int i,k,err =0;
    int more = 0;
    size_t size = a->length;
    unsigned char* buf = (unsigned char*)grib_context_malloc(d->handle->context,size);

    if( a->length == 0  &&
            (d->option_flags & GRIB_DUMP_FLAG_CODED) != 0)
        return;

    set_begin_end(d,a);

    for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");
    fprintf(self->dumper.out,"%ld-%ld %s %s = %ld",self->begin,self->theEnd,a->creator->op, a->name,a->length);
    aliases(d,a);
    fprintf(self->dumper.out," {");

    if(!buf)
    {
        if(size == 0)
            fprintf(self->dumper.out,"}\n");
        else
            fprintf(self->dumper.out," *** ERR cannot malloc(%ld) }\n",(long)size);
        return;
    }

    fprintf(self->dumper.out,"\n");

    err = grib_unpack_bytes(a,buf,&size);
    if(err){
        grib_context_free(d->handle->context,buf);
        fprintf(self->dumper.out," *** ERR=%d (%s) [grib_dumper_debug::dump_bytes]\n}",err,grib_get_error_message(err));
        return ;
    }

    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");
    }

    if(more)
    {
        for(i = 0; i < d->depth + 3 ; i++) fprintf(self->dumper.out," ");
        fprintf(self->dumper.out,"... %d more values\n",more);
    }

    for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");
    fprintf(self->dumper.out,"} # %s %s \n",a->creator->op, a->name);
    grib_context_free(d->handle->context,buf);
}
static int find(grib_nearest* nearest, grib_handle* h,
                double inlat, double inlon,unsigned long flags,
                double* outlats,double* outlons, double *values,
                double *distances,int *indexes, size_t *len) {
  grib_nearest_latlon_reduced* self = (grib_nearest_latlon_reduced*) nearest;
  int ret=0,kk=0,ii=0,jj=0;
  int j=0;
  long* pla=NULL;
  long* pl=NULL;
  size_t nvalues=0;
  grib_iterator* iter=NULL;
  double lat=0,lon=0;
  long iradius;
  double radius;
  int ilat=0,ilon=0;

  if( (ret =  grib_get_size(h,self->values_key,&nvalues))!= GRIB_SUCCESS)
     return ret;
  nearest->values_count = nvalues;

  if (grib_is_missing(h,self->radius,&ret)) {
    grib_context_log(h->context, GRIB_LOG_DEBUG,"Key '%s' is missing", self->radius);
    return ret ? ret : GRIB_GEOCALCULUS_PROBLEM;
  }

  if( (ret =  grib_get_long(h,self->radius,&iradius))!= GRIB_SUCCESS)
    return ret;
  radius=((double)iradius)/1000.0;

  if (!nearest->h || (flags & GRIB_NEAREST_SAME_GRID)==0) {
    double dummy=0;
    double olat=1.e10;
    long n=0;

	ilat=0,ilon=0;
    if (grib_is_missing(h,self->Nj,&ret)) {
      grib_context_log(h->context, GRIB_LOG_DEBUG,"Key '%s' is missing", self->Nj);
      return ret ? ret : GRIB_GEOCALCULUS_PROBLEM;
    }

    if( (ret =  grib_get_long(h,self->Nj,&n))!= GRIB_SUCCESS)
     return ret;
    self->lats_count=n;

    if (self->lats) grib_context_free(nearest->context,self->lats);
    self->lats=(double*)grib_context_malloc( nearest->context,
                               self->lats_count* sizeof(double));
    if (!self->lats) return GRIB_OUT_OF_MEMORY;

    if (self->lons) grib_context_free(nearest->context,self->lons);
    self->lons=(double*)grib_context_malloc( nearest->context,
                               nearest->values_count*sizeof(double));
    if (!self->lons) return GRIB_OUT_OF_MEMORY;

    iter=grib_iterator_new(h,0,&ret);
    if (ret) {
        grib_context_log(h->context,GRIB_LOG_ERROR,"unable to create iterator");
        return ret;
    }
    while(grib_iterator_next(iter,&lat,&lon,&dummy)) {
		if (olat!=lat) {
			self->lats[ilat++]=lat;
			olat=lat;
		}
      self->lons[ilon++]=lon;
    }
	self->lats_count=ilat;
	grib_iterator_delete(iter);
	
  }
  nearest->h=h;

  if (!self->distances || (flags & GRIB_NEAREST_SAME_POINT)==0
                       || (flags & GRIB_NEAREST_SAME_GRID)==0) {
    double* lons=NULL;
    int nlon=0;
    size_t plsize=0;
    long nplm1=0;
    int nearest_lons_found=0;
	double lon_first,lon_last;
	int islocal=0;
	long plmax;
	double dimin;

	if ((ret=grib_get_double(h,self->lonFirst,&lon_first))!=GRIB_SUCCESS) {
			grib_context_log(h->context,GRIB_LOG_ERROR,
				"grib_nearest_latlon_reduced.find(): unable to get %s %s\n",self->lonFirst,
				grib_get_error_message(ret));
			return ret;
	}
	if ((ret=grib_get_double(h,self->lonLast,&lon_last))!=GRIB_SUCCESS) {
			grib_context_log(h->context,GRIB_LOG_ERROR,
				"grib_nearest_latlon_reduced.find(): unable to get %s %s\n",self->lonLast,
				grib_get_error_message(ret));
			return ret;
	}

    plsize=self->lats_count;
	if( (ret=grib_get_size(h,self->pl,&plsize))!= GRIB_SUCCESS)
	       return ret;
    pla=(long*)grib_context_malloc(h->context,plsize*sizeof(long));
    if (!pla) return GRIB_OUT_OF_MEMORY;
    if( (ret=grib_get_long_array(h,self->pl,pla,&plsize))!= GRIB_SUCCESS)
       return ret;

	pl=pla;
	while ((*pl)==0) {pl++;}

	plmax=pla[0];
	for (j=0;j<plsize;j++) if (plmax<pla[j]) plmax=pla[j];
	dimin=360.0/plmax;

	if ( 360-fabs(lon_last-lon_first) < 2 * dimin ) {islocal=0;}
	else {islocal=1;}

	if (islocal) 
		for (j=0;j<plsize;j++) pla[j]--;

	/* printf("XXXX islocal=%d\n",islocal); */
    while (inlon<0) inlon+=360;
	while (inlon>360) inlon-=360;

	ilat=self->lats_count;
    if (self->lats[ilat-1] > self->lats[0]) {
       if (inlat < self->lats[0] || inlat > self->lats[ilat-1])
		   return GRIB_OUT_OF_AREA;
    } else {
      if (inlat > self->lats[0] || inlat < self->lats[ilat-1])
         return GRIB_OUT_OF_AREA;
    }

    if (!self->distances)
      self->distances=(double*)grib_context_malloc( nearest->context,4*sizeof(double));
    if (!self->distances) return GRIB_OUT_OF_MEMORY;

    grib_binary_search(self->lats,ilat-1,inlat,
                                &(self->j[0]),&(self->j[1]));

	nlon=0;
	for (jj=0;jj<self->j[0];jj++) nlon+=pl[jj];
	nplm1=pl[self->j[0]]-1;

    lons=self->lons+nlon;

    nearest_lons_found=0;
    if (lons[nplm1]>lons[0]) {
      if (inlon< lons[0] || inlon > lons[nplm1]) {
        if (lons[nplm1]-lons[0]-360 <=
             lons[nplm1]-lons[nplm1-1]) {
           self->k[0]=0;
           self->k[1]=nplm1;
           nearest_lons_found=1;
         } else return GRIB_OUT_OF_AREA;
       }
    } else {
      if (inlon >lons[0] || inlon< lons[nplm1]) {
         if (lons[0]-lons[nplm1]-360 <=
             lons[0]-lons[1]) {
           self->k[0]=0;
           self->k[1]=nplm1;
           nearest_lons_found=1;
         } else return GRIB_OUT_OF_AREA;
       }
    }

    if (!nearest_lons_found) {
      grib_binary_search(lons,pl[self->j[0]]-1,inlon,
                                &(self->k[0]),&(self->k[1]));
	}
    self->k[0]+=nlon;
    self->k[1]+=nlon;

	nlon=0;
	for (jj=0;jj<self->j[1];jj++) nlon+=pl[jj];
	nplm1=pl[self->j[1]]-1;

    lons=self->lons+nlon;

    nearest_lons_found=0;
    if (lons[nplm1]>lons[0]) {
      if (inlon<lons[0] || inlon>lons[nplm1]) {
        if (lons[nplm1]-lons[0]-360 <=
             lons[nplm1]-lons[nplm1-1]) {
           self->k[2]=0;
           self->k[3]=nplm1;
           nearest_lons_found=1;
         } else return GRIB_OUT_OF_AREA;
       }
    } else {
      if (inlon>lons[0] || inlon<lons[nplm1]) {
         if (lons[0]-lons[nplm1]-360 <=
             lons[0]-lons[1]) {
           self->k[2]=0;
           self->k[3]=nplm1;
           nearest_lons_found=1;
         } else return GRIB_OUT_OF_AREA;
       }
    }

    if (!nearest_lons_found) {
      grib_binary_search(lons,pl[self->j[1]]-1,inlon,
                                &(self->k[2]),&(self->k[3]));
	}

    self->k[2]+=nlon;
    self->k[3]+=nlon;

    kk=0;
    for (jj=0;jj<2;jj++) {
      for (ii=0;ii<2;ii++) {
        self->distances[kk]=grib_nearest_distance(radius,inlon,inlat,
                     self->lons[self->k[kk]],self->lats[self->j[jj]]);
        kk++;
      }
    }

    grib_context_free(h->context,pla);
  }

  kk=0;
  for (jj=0;jj<2;jj++) {
    for (ii=0;ii<2;ii++) {
      distances[kk]=self->distances[kk];
      outlats[kk]=self->lats[self->j[jj]];
      outlons[kk]=self->lons[self->k[kk]];
      grib_get_double_element_internal(h,self->values_key,self->k[kk],&(values[kk]));
      indexes[kk]=self->k[kk];
      kk++;
    }
  }

  return GRIB_SUCCESS;
}
Example #28
0
char* grib_context_strdup(const grib_context* c,const char* s){
	char *dup = (char*)grib_context_malloc(c,(strlen(s)*sizeof(char))+1);
	if(dup) strcpy(dup,s);
	return dup;
}
Example #29
0
void* grib_context_malloc_clear(const grib_context* c, size_t size){
	void *p = grib_context_malloc(c,size);
	if(p) memset(p,0,size);
	return p;
}
static int  unpack_double(grib_accessor* a, double* val, size_t *len)
{
  grib_accessor_data_sh_unpacked* self =  (grib_accessor_data_sh_unpacked*)a;

  size_t i = 0;
  int ret = GRIB_SUCCESS;
  long   hcount = 0;
  long   lcount = 0;
  long   hpos = 0;
  long   lup = 0;
  long   mmax = 0;
  long   n_vals = 0;
  double *scals  = NULL;
  /* double *pscals=NULL; */
  double dummy=0;

  double s = 0;
  double d = 0;
  double laplacianOperator = 0;
  unsigned char* buf = NULL;
  unsigned char* hres = NULL;
  unsigned char* lres = NULL;
  unsigned long packed_offset;
  long   lpos = 0;

  long   maxv = 0;
  long   GRIBEX_sh_bug_present =0;
  long ieee_floats  = 0;

  long   offsetdata           = 0;
  long   bits_per_value          = 0;
  double reference_value      = 0;
  long   binary_scale_factor         = 0;
  long   decimal_scale_factor = 0;


  long   sub_j= 0;
  long   sub_k= 0;
  long   sub_m= 0;
  long   pen_j= 0;
  long   pen_k= 0;
  long   pen_m= 0;

  double operat= 0;
  int err=0;

  decode_float_proc decode_float = NULL;

  n_vals = 0;
  err=grib_value_count(a,&n_vals);
  if (err) return err;

  if(*len < n_vals){
    *len = n_vals;
    return GRIB_ARRAY_TOO_SMALL;
  }

  if((ret = grib_get_long_internal(grib_handle_of_accessor(a),self->offsetdata,&offsetdata))
      != GRIB_SUCCESS)   return ret;

  if((ret = grib_get_long_internal(grib_handle_of_accessor(a),self->GRIBEX_sh_bug_present,&GRIBEX_sh_bug_present))
      != GRIB_SUCCESS)
    return ret;

  if((ret = grib_get_long_internal(grib_handle_of_accessor(a),self->ieee_floats,&ieee_floats)) != GRIB_SUCCESS)
    return ret;

  if((ret = grib_get_long_internal(grib_handle_of_accessor(a),self->sub_j,&sub_j)) != GRIB_SUCCESS)
    return ret;
  if((ret = grib_get_long_internal(grib_handle_of_accessor(a),self->sub_k,&sub_k)) != GRIB_SUCCESS)
    return ret;
  if((ret = grib_get_long_internal(grib_handle_of_accessor(a),self->sub_m,&sub_m)) != GRIB_SUCCESS)
    return ret;
  if((ret = grib_get_long_internal(grib_handle_of_accessor(a),self->pen_j,&pen_j)) != GRIB_SUCCESS)
    return ret;
  if((ret = grib_get_long_internal(grib_handle_of_accessor(a),self->pen_k,&pen_k)) != GRIB_SUCCESS)
    return ret;
  if((ret = grib_get_long_internal(grib_handle_of_accessor(a),self->pen_m,&pen_m)) != GRIB_SUCCESS)
    return ret;

  self->dirty=0;

  switch (ieee_floats) {
    case 0:
      decode_float=grib_long_to_ibm;
      break;
    case 1:
      decode_float=grib_long_to_ieee;
      break;
    case 2:
      decode_float=grib_long_to_ieee64;
      break;
    default:
      return GRIB_NOT_IMPLEMENTED;
  }
  
  Assert (sub_j == sub_k);
  Assert (sub_j == sub_m);
  Assert (pen_j == pen_k);
  Assert (pen_j == pen_m);

  buf = (unsigned char*)grib_handle_of_accessor(a)->buffer->data;

  maxv = pen_j+1;

  buf  += offsetdata;
  hres = buf;
  lres = buf;

  packed_offset = offsetdata +  4*(sub_k+1)*(sub_k+2);

  lpos = 8*(packed_offset-offsetdata);

  s = grib_power(binary_scale_factor,2);
  d = grib_power(-decimal_scale_factor,10) ;

  scals   = (double*)grib_context_malloc(a->context,maxv*sizeof(double));
  Assert(scals);
  if((ret = grib_get_double_internal(grib_handle_of_accessor(a),self->laplacianOperator,&laplacianOperator))
      != GRIB_SUCCESS)
    return ret;

  scals[0] = 0;
  for(i=1;i<maxv;i++){
    operat = pow(i*(i+1),laplacianOperator);
    if(operat !=  0)
      scals[i] = (1.0/operat);
    else{
      scals[i] = 0;
    }
  }


  i=0;

  while(maxv>0)
  {
    lup=mmax;
    if(sub_k>=0)
    {
      for(hcount=0;hcount<sub_k+1;hcount++)
      {
        val[i++] =  decode_float(grib_decode_unsigned_long(hres,&hpos,32))*d;
        val[i++] =  decode_float(grib_decode_unsigned_long(hres,&hpos,32))*d;

        if (GRIBEX_sh_bug_present && hcount==sub_k){
          /*  bug in ecmwf data, last row (K+1)is scaled but should not */
          val[i-2] *= scals[lup];
          val[i-1] *= scals[lup];
        }
        lup++;
      }
      sub_k--;
    }

    /* pscals=scals+lup; */
    for(lcount=hcount; lcount < maxv ; lcount++)
    {
      dummy =  (double) ((grib_decode_unsigned_long(lres, &lpos,
                   bits_per_value)*s)+reference_value);
      dummy =  (double) ((grib_decode_unsigned_long(lres, &lpos,
                   bits_per_value)*s)+reference_value);
      lup++;
    }

    maxv--;
    hcount=0;
    mmax++;
  }

  Assert(*len >= i);
  *len = n_vals;

  if(d != 1) {
    for(i=0;i<*len;i++)
      val[i++] *= d;
  }

  (void)dummy; /* suppress gcc warning */
  grib_context_free(a->context,scals);

  return ret;

}