static int  get_native_type(grib_accessor* a)
{
    grib_accessor_g2_mars_labeling* self = (grib_accessor_g2_mars_labeling*)a;
    char* key=NULL;
    int ret=0;
    int type=0;

    switch (self->index) {
    case 0:
        key=(char*)self->the_class;
        break;
    case 1:
        key=(char*)self->type;
        break;
    case 2:
        key=(char*)self->stream;
        break;
    default :
        grib_context_log(a->parent->h->context,GRIB_LOG_ERROR,
                "invalid first argument of g2_mars_labeling in %s",a->name);
        return GRIB_INTERNAL_ERROR;
        break;
    }

    ret=grib_get_native_type(a->parent->h,key,&type);
    if (ret) grib_context_log(a->parent->h->context,GRIB_LOG_ERROR,
            "unable to get native type for %s",key);
    return type;
}
static int unpack_string(grib_accessor*a , char*  v, size_t *len){

  double val = 0;
  size_t l = 1;
  char repres[1024];

  grib_unpack_double (a , &val, &l);

  sprintf(repres,"%.0f", val);

  l = strlen(repres)+1;

  if(l >*len ){
    grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, "grib_accessor_long : unpack_string : Buffer too small for %s ", a->name );

    *len = l;
    return GRIB_BUFFER_TOO_SMALL;
  }
  grib_context_log(a->parent->h->context,GRIB_LOG_DEBUG, "grib_accessor_long: Casting double %s to string  ", a->name);

  *len = l;

  strcpy(v,repres);
  return GRIB_SUCCESS;


}
Exemplo n.º 3
0
/* --------------- 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_double(grib_accessor* a, const double* val, size_t *len)
{
  int ret=GRIB_SUCCESS;
  long bitmap_present=0;

  grib_accessor_data_apply_gdsnotpresent* self =  (grib_accessor_data_apply_gdsnotpresent*)a;

  if (*len ==0) return GRIB_NO_VALUES;

  ret=grib_set_long(a->parent->h,self->bitmap_present,bitmap_present);
  if(ret) {
    grib_context_log(a->parent->h->context, GRIB_LOG_ERROR,
       "Accessor %s cannont pack value for %s error %d \n", a->name, self->bitmap_present, ret);
    return ret;
  }

#if 0
  if(!grib_find_accessor(a->parent->h,self->bitmap)){
    grib_context_log(a->parent->h->context, GRIB_LOG_ERROR,
       "Accessor %s cannont access bitmap \n", a->name, self->bitmap_present, ret);
    return ret;
  }
#endif


  ret = grib_set_double_array_internal(a->parent->h,self->coded_values,val,*len);
  if(ret) {
    grib_context_log(a->parent->h->context, GRIB_LOG_ERROR,
       "Accessor %s cannont pack value for %s error %d \n", a->name, self->coded_values, ret);
    return ret;
  }

  return ret;
}
Exemplo n.º 5
0
static int    pack_long   (grib_accessor* a, const long *val, size_t *len)
{
	grib_accessor_bit *ac = (grib_accessor_bit*) a;
	grib_accessor* owner = NULL;
	unsigned char *mdata = 0;
	if(*len < 1)
	{
		grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, "grib_accessor_bit : pack_long : At least one value to pack for %s", a->name );
		*len = 0;
		return GRIB_ARRAY_TOO_SMALL;
	}

	owner = grib_find_accessor(a->parent->h,ac->owner);

	if(!owner){
		grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, "grib_accessor_bit : Cannot get the owner %s for computing the bit value of %s ",ac->owner, a->name);
		*len = 0;
		return GRIB_NOT_FOUND;
	}

	mdata = a->parent->h->buffer->data;
	mdata += grib_byte_offset(owner);

	grib_set_bit( mdata,7-ac->bit_index , *val>0);

	*len = 1;
	return GRIB_SUCCESS;
}
Exemplo n.º 6
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;
}
Exemplo n.º 7
0
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;

}
Exemplo n.º 8
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;
}
Exemplo n.º 9
0
static grib_handle* try_template(grib_context* c,const char* dir,const char* name)
{
  char path[1024];
  grib_handle *g = NULL;
  int err = 0;

  sprintf(path,"%s/%s.tmpl",dir,name);

  if (c->debug==-1) {
    printf("GRIB_API DEBUG: try_template path='%s'\n", path);
  }

  if(access(path,F_OK) == 0)
  {
    FILE* f = fopen(path,"r");
    if(!f)
    {
      grib_context_log(c,GRIB_LOG_PERROR,"cannot open %s",path);
      return NULL;
    }
    g = grib_handle_new_from_file(c,f,&err);
    if (!g) {
       grib_context_log(c,GRIB_LOG_ERROR,"cannot create GRIB handle from %s",path);
    }
    fclose(f);
  }

  return g;
}
Exemplo n.º 10
0
static int unpack_string(grib_accessor*a , char*  v, size_t *len){

  long val = 0;
  size_t l = 1;
  char repres[1024];

  grib_unpack_long (a , &val, &l);

  if ((val == GRIB_MISSING_LONG) && ((a->flags & GRIB_ACCESSOR_FLAG_CAN_BE_MISSING) != 0) )
    sprintf(repres,"MISSING");
  else
    sprintf(repres,"%ld", val);

  l = strlen(repres)+1;

  if(l >*len ){
    grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, "grib_accessor_long : unpack_string : Buffer too small for %s ", a->name );

    *len = l;
    return GRIB_BUFFER_TOO_SMALL;
  }
  grib_context_log(a->parent->h->context,GRIB_LOG_DEBUG, "grib_accessor_long: Casting long %s to string ", a->name);

  *len = l;

  strcpy(v,repres);
  return GRIB_SUCCESS;

}
Exemplo n.º 11
0
static int unpack_bytes(grib_accessor* a, unsigned char* val, size_t *len)
{
  unsigned char* buf = a->parent->h->buffer->data;
  grib_accessor_g1bitmap* self = (grib_accessor_g1bitmap*)a;
  long tlen;
  int err;
  long length = grib_byte_count(a);
  long offset = grib_byte_offset(a);

  if(*len < length )
  {
    grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, "Wrong size for %s it is %d bytes long\n", a->name ,length );
    *len = length;
    return GRIB_ARRAY_TOO_SMALL;
  }

  if ((err=grib_get_long_internal(a->parent->h, self->unusedBits, &tlen)) != GRIB_SUCCESS)
    grib_context_log(a->parent->h->context, GRIB_LOG_ERROR,
                     "grib_accessor_class_bitmap.unpack_bytes : cannot get %s err=%d",self->unusedBits,err);
  
  length-= tlen/8;
  memcpy(val,buf + offset,length );
  *len = length;

  return GRIB_SUCCESS;
}
Exemplo n.º 12
0
static int grib_fieldset_columns_resize(grib_fieldset* set,size_t newsize) {
  double* newdoubles;
  long* newlongs;
  char** newstrings;
  int* newerrors;
  int i=0;
  grib_context* c;

  if (!set || !set->columns) return GRIB_INVALID_ARGUMENT;

  c=set->context;

  if (newsize <= set->columns[0].values_array_size) return 0;

  for (i=0;i<set->columns_size;i++) {

    switch (set->columns[i].type) {
      case GRIB_TYPE_LONG:
        newlongs=(long*)grib_context_realloc(c,set->columns[i].long_values,
              newsize*sizeof(long));
        if (!newlongs ) {
          grib_context_log(c, GRIB_LOG_ERROR,
              "grib_fieldset_columns_resize : Cannot malloc %d bytes",newsize-set->columns[i].values_array_size);
          return GRIB_OUT_OF_MEMORY;
        } else set->columns[i].long_values=newlongs;
        break;
      case GRIB_TYPE_DOUBLE:
        newdoubles=(double*)grib_context_realloc(c,set->columns[i].double_values,
             newsize*sizeof(double));
        if (!newdoubles) {
          grib_context_log(c, GRIB_LOG_ERROR,
              "grib_fieldset_columns_resize : Cannot malloc %d bytes",newsize-set->columns[i].values_array_size);
          return GRIB_OUT_OF_MEMORY;
        } else set->columns[i].double_values=newdoubles;
        break;
      case GRIB_TYPE_STRING:
        newstrings=(char**)grib_context_realloc(c,set->columns[i].string_values,
              newsize*sizeof(char*));
        if (!newstrings) {
          grib_context_log(c, GRIB_LOG_ERROR,
              "grib_fieldset_columns_resize : Cannot malloc %d bytes",newsize-set->columns[i].values_array_size);
          return GRIB_OUT_OF_MEMORY;
        } else set->columns[i].string_values=newstrings;
        break;
    }
    newerrors=(int*)grib_context_realloc(c,set->columns[i].errors,newsize*sizeof(int));
    if (!newerrors) {
      grib_context_log(c, GRIB_LOG_ERROR,
          "grib_fieldset_columns_resize : Cannot malloc %d bytes",
          set->columns[i].errors,newsize*sizeof(int));
      return GRIB_OUT_OF_MEMORY;
    } else set->columns[i].errors=newerrors;

    set->columns[i].values_array_size=newsize;

  }

  return GRIB_SUCCESS;
}
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;
}
Exemplo n.º 14
0
char *grib_context_full_path(grib_context* c,const char* basename)
{
	int err=0;
	char full[1024]={0,};
	grib_string_list* dir=NULL;
	grib_string_list* fullpath=0;
	if (!c) c=grib_context_get_default();

	GRIB_PTHREAD_ONCE(&once,&init);
	GRIB_MUTEX_LOCK(&mutex_c);

	if(*basename == '/' || *basename ==  '.') {
	    GRIB_MUTEX_UNLOCK(&mutex_c);
		return (char*)basename;
	} else {
		if ((fullpath=grib_trie_get(c->def_files,basename))!=NULL) {
			GRIB_MUTEX_UNLOCK(&mutex_c);
			return fullpath->value;
		}
		if (!c->grib_definition_files_dir)
			err=init_definition_files_dir(c);

		if (err != GRIB_SUCCESS) {
			grib_context_log(c,GRIB_LOG_ERROR,
					"Unable to find definition files directory");
			GRIB_MUTEX_UNLOCK(&mutex_c);
			return NULL;
		}

		dir=c->grib_definition_files_dir;

		while (dir) {
			sprintf(full,"%s/%s",dir->value,basename);
			if (!access(full,F_OK)) {
				fullpath=grib_context_malloc_clear_persistent(c,sizeof(grib_string_list));
				Assert(fullpath);
				fullpath->value=grib_context_strdup(c,full);
				grib_trie_insert(c->def_files,basename,fullpath);
				grib_context_log(c,GRIB_LOG_DEBUG,"Found def file %s",full);
				GRIB_MUTEX_UNLOCK(&mutex_c);
				return fullpath->value;
			}
			dir=dir->next;
		}

	}

	/* Store missing files so we don't check for them again and again */
	grib_trie_insert(c->def_files,basename,&grib_file_not_found);
	/*grib_context_log(c,GRIB_LOG_ERROR,"Def file \"%s\" not found",basename);*/
	GRIB_MUTEX_UNLOCK(&mutex_c);
	full[0]=0;
	return NULL;
}
Exemplo n.º 15
0
static int  create_accessor(grib_section* p, grib_action* act, grib_loader *h ){
  int ret = GRIB_SUCCESS;
  grib_action_template* a = ( grib_action_template*)act;
  grib_action* la = NULL;
  grib_action* next = NULL;
  grib_accessor* as = NULL;
  grib_section*         gs = NULL;

  char fname[1024]={0,};
  char *fpath=0;

  as = grib_accessor_factory(p, act,0,NULL);

  if(!as) return GRIB_INTERNAL_ERROR;
  if(a->arg){
    ret = grib_recompose_name(p->h,as,a->arg,fname,1);

	if ((fpath=grib_context_full_defs_path(p->h->context,fname))==NULL) {
      if (!a->nofail) {
        grib_context_log(p->h->context,GRIB_LOG_ERROR,
                         "Unable to find template %s from %s ",act->name,fname);
        return GRIB_FILE_NOT_FOUND;
      }
	  la = get_empty_template(p->h->context,&ret);
	  if (ret) return ret;
    } else 
      la = grib_parse_file(p->h->context, fpath);
  }
  as->flags |= GRIB_ACCESSOR_FLAG_HIDDEN;
  gs = as->sub_section;
  gs->branch = la; /* Will be used to prevent unecessary reparse */

  grib_push_accessor(as,p->block);

  if(la){
    next = la;

    while(next){
      ret = grib_create_accessor(gs, next,h);
      if(ret != GRIB_SUCCESS) {
      if(p->h->context->debug)
    {
      grib_context_log(p->h->context,GRIB_LOG_ERROR,
      "Error processing template %s: %s [%s] %04lx",
      fname,grib_get_error_message(ret),next->name,next->flags);
    }
      return ret;
    }
      next= next->next;
    }
  }
  return GRIB_SUCCESS;
}
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;
}
Exemplo n.º 17
0
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;
}
Exemplo n.º 18
0
static int pack_double(grib_accessor* a, const double* val, size_t *len)
{
  grib_accessor_scale* self = (grib_accessor_scale*)a;
  int ret = 0;

  long value = 0;
  long divisor = 0;
  long multiplier = 0;
  long truncating=0;
  double x;

  ret = grib_get_long_internal(a->parent->h, self->divisor,&divisor);
  if(ret != GRIB_SUCCESS) {
    grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, "Accessor %s cannont gather value for %s error %d \n", a->name, self->divisor, ret);
    return ret;
  }
  ret = grib_get_long_internal(a->parent->h, self->multiplier,&multiplier);
  if(ret != GRIB_SUCCESS){
    grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, "Accessor %s cannont gather value for %s error %d \n", a->name, self->divisor, ret);
    return ret;
  }
  if (self->truncating) {
    ret = grib_get_long_internal(a->parent->h, self->truncating,&truncating);
    if(ret != GRIB_SUCCESS){
      grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, "Accessor %s cannont gather value for %s error %d \n", a->name, self->truncating, ret);
      return ret;
    }
  }

  if (multiplier == 0) {
    grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, "Accessor %s cannont divide by a zero multiplier %s error %d  \n", a->name, self->multiplier, ret);
    return GRIB_ENCODING_ERROR;
  }

  x=*val * (double)divisor / (double)multiplier;
  if (*val == GRIB_MISSING_DOUBLE) value = GRIB_MISSING_LONG;
  else if (truncating) {
  	value = (long)x;
  } else {
	value = x > 0 ? (long)(x+0.5) : (long)(x-0.5) ;
  }

  ret = grib_set_long_internal(a->parent->h, self->value,value);
  if(ret )
    grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, "Accessor %s cannont pack value for %s error %d \n", a->name, self->value, ret);

  if (ret == GRIB_SUCCESS) *len = 1;

  return ret;
}
static grib_trie* load_list(grib_context* c,grib_expression* e, int* err) {

  grib_expression_is_in_list* self = (grib_expression_is_in_list*)e;

  char* filename=NULL;
  char line[1024]={0,};
  grib_trie* list=NULL;
  FILE* f=NULL;

  *err=GRIB_SUCCESS;

  filename=grib_context_full_defs_path(c,self->list);
  if (!filename) {
    grib_context_log(c,GRIB_LOG_ERROR,"unable to find def file %s",self->list);
    *err=GRIB_FILE_NOT_FOUND;
    return NULL;
  } else {
    grib_context_log(c,GRIB_LOG_DEBUG,"found def file %s",filename);
  }
  list=(grib_trie*)grib_trie_get(c->lists,filename);
  if (list) {
	grib_context_log(c,GRIB_LOG_DEBUG,"using list %s from cache",self->list);
  	return list;
  } else {
	grib_context_log(c,GRIB_LOG_DEBUG,"using list %s from file %s",self->list,filename);
  }

  f=codes_fopen(filename,"r");
  if (!f) {*err=GRIB_IO_PROBLEM; return NULL;}

  list=grib_trie_new(c);

  while(fgets(line,sizeof(line)-1,f)) {
	unsigned char* p=(unsigned char*)line;
	while (*p!=0) {
		if (*p<33) {*p=0; break;}
		p++;
	}
  	grib_trie_insert(list,line,line);
  }

  grib_trie_insert(c->lists,filename,list);

  fclose(f);

  return list;

}
static int unpack_string(grib_accessor* a, char* val, size_t *len)
{
    grib_accessor_g2_mars_labeling* self = (grib_accessor_g2_mars_labeling*)a;
    char* key=NULL;

    switch (self->index) {
    case 0:
        key=(char*)self->the_class;
        break;
    case 1:
        key=(char*)self->type;
        break;
    case 2:
        key=(char*)self->stream;
        break;
    default :
        grib_context_log(a->parent->h->context,GRIB_LOG_ERROR,
                "invalid first argument of g2_mars_labeling in %s",a->name);
        return GRIB_INTERNAL_ERROR;
        break;
    }

    return grib_get_string(a->parent->h, key,val,len);

}
static int  unpack_double(grib_accessor* a, double* val, size_t *len)
{
    grib_accessor_data_g1shsimple_packing* self =  (grib_accessor_data_g1shsimple_packing*)a;
    int err =  GRIB_SUCCESS;

    size_t coded_n_vals = 0;
    size_t n_vals = 0;

    if((err = grib_get_size(a->parent->h,self->coded_values,&coded_n_vals)) != GRIB_SUCCESS)
        return err;

    n_vals = coded_n_vals + 1;

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

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

    val++;

    if((err = grib_get_double_array_internal(a->parent->h,self->coded_values,val,&coded_n_vals)) != GRIB_SUCCESS)
        return err;

    grib_context_log(a->parent->h->context, GRIB_LOG_DEBUG,
            "grib_accessor_data_g1shsimple_packing_bitmap : unpack_double : creating %s, %d values",
            a->name, n_vals);

    *len =  n_vals;

    return err;
}
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;
}
Exemplo n.º 23
0
static int    unpack_long   (grib_accessor* a, long* val, size_t *len)
{
	grib_accessor_bit *ac = (grib_accessor_bit*) a;
	int ret = 0;

	long data = 0;

	if(*len < 1)
	{
		grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, "grib_accessor_bit : unpack_long : Wrong size for %s it contains %d values ", a->name , 1 );
		*len = 0;
		return GRIB_ARRAY_TOO_SMALL;
	}
	
	if((ret = grib_get_long_internal(a->parent->h,ac->owner,&data)) != GRIB_SUCCESS){
		*len = 0;
		return ret;
	}

	if(data & (1<<ac->bit_index))      
		*val = 1;
	else
		*val = 0;

	*len = 1;
	return GRIB_SUCCESS;
}
static int convert_time_range(
        grib_handle* h,
        long stepUnits,                      /* unit */
        long indicatorOfUnitForTimeRange,    /* coded_unit */
        long* lengthOfTimeRange              /* coded_time_range */
        )
{
    Assert(lengthOfTimeRange != NULL);

    if (indicatorOfUnitForTimeRange != stepUnits) {
        long u2sf_step_unit;
        long coded_time_range_sec = (*lengthOfTimeRange)*u2s2[indicatorOfUnitForTimeRange];
        if (coded_time_range_sec < 0) {
            long u2sf;
            int factor = 60;
            if (u2s2[indicatorOfUnitForTimeRange] % factor)
                return GRIB_DECODING_ERROR;
            if (u2s[stepUnits] % factor)
                return GRIB_DECODING_ERROR;
            u2sf = u2s2[indicatorOfUnitForTimeRange]/factor;
            coded_time_range_sec = (*lengthOfTimeRange)*u2sf;
            u2sf_step_unit = u2s[stepUnits]/factor;
        } else {
            u2sf_step_unit = u2s[stepUnits];
        }
        if (coded_time_range_sec % u2sf_step_unit != 0) {
            grib_context_log(h->context,GRIB_LOG_ERROR,"unable to convert endStep in stepUnits");
            return GRIB_WRONG_STEP_UNIT;
        }
        *lengthOfTimeRange = coded_time_range_sec / u2sf_step_unit;
    }

    return GRIB_SUCCESS;
}
Exemplo n.º 25
0
static int unpack_long(grib_accessor* a, long* val, size_t *len)
{
  int ret=0;
  grib_accessor_time* self = (grib_accessor_time*)a;

  long hour = 0;
  long minute = 0;
  long second = 0;

  if ((ret=grib_get_long_internal(a->parent->h, self->hour,&hour))!=GRIB_SUCCESS)
    return ret;
  if ((ret=grib_get_long_internal(a->parent->h, self->minute,&minute))!=GRIB_SUCCESS)
    return ret;
  if ((ret=grib_get_long_internal(a->parent->h, self->second,&second))!=GRIB_SUCCESS)
    return ret;

  /* We ignore the 'seconds' in our time calculation! */
  if (second != 0) {
     grib_context_log(a->parent->h->context, GRIB_LOG_ERROR,
                  "Truncating time: non-zero seconds(%d) ignored", second);
  }

  if(*len < 1)
    return GRIB_WRONG_ARRAY_SIZE;

  *val = hour*100 + minute;

  if(hour == 255)
    *val = 12*100;

  if(hour != 255 && minute == 255)
    *val = hour*100;

  return GRIB_SUCCESS;
}
Exemplo n.º 26
0
static int    unpack_double   (grib_accessor* a, double* val, size_t *len)
{
  grib_accessor_scale* self = (grib_accessor_scale*)a;
  int ret = 0;
  long value = 0;
  long multiplier = 0;
  long divisor = 0;

  if(*len < 1){
    ret = GRIB_ARRAY_TOO_SMALL;
    grib_context_log(a->parent->h->context, GRIB_LOG_ERROR,
      "Accessor %s cannot gather value for %s and/or %s error %d",
      a->name,self->multiplier, self->divisor, ret);
    return ret;
  }

  if((ret = grib_get_long_internal(a->parent->h, self->divisor,&divisor)) != GRIB_SUCCESS)
    return ret;

  if((ret = grib_get_long_internal(a->parent->h, self->multiplier,&multiplier)) != GRIB_SUCCESS)
    return ret;

  if((ret = grib_get_long_internal(a->parent->h, self->value,&value)) != GRIB_SUCCESS)
    return ret;

  if (value == GRIB_MISSING_LONG) *val=GRIB_MISSING_DOUBLE;
  else *val = ((double)(value*multiplier))/divisor;
  /*printf("unpack_double: divisor=%ld multiplier=%ld long_value=%ld scaled_value=%.30f\n",(double)divisor,(double)multiplier,value,*val);*/

  if (ret == GRIB_SUCCESS) *len = 1;

  return ret;
}
static int pack_long(grib_accessor* a, const long* val, size_t *len)
{
    grib_accessor_g2_mars_labeling* self = (grib_accessor_g2_mars_labeling*)a;
    char* key=NULL;
    int ret=0;

    switch (self->index) {
    case 0:
        key=(char*)self->the_class;
        break;
    case 1:
        key=(char*)self->type;
        break;
    case 2:
        key=(char*)self->stream;
        break;
    default :
        grib_context_log(a->parent->h->context,GRIB_LOG_ERROR,
                "invalid first argument of g2_mars_labeling in %s",a->name);
        return GRIB_INTERNAL_ERROR;
        break;
    }

    ret=grib_set_long(a->parent->h, key,*val);
    if (ret) return ret; /* failed */

    return extra_set(a,*val);
}
Exemplo n.º 28
0
static void grib_fieldset_delete_columns(grib_fieldset* set) {
  int i=0;
  grib_context* c;

  if (!set) return;
  c=set->context;

  for (i=0;i<set->columns_size;i++) {
    int j=0;
    switch (set->columns[i].type) {
      case GRIB_TYPE_LONG:
        grib_context_free(c,set->columns[i].long_values);
        break;
      case GRIB_TYPE_DOUBLE:
        grib_context_free(c,set->columns[i].double_values);
        break;
      case GRIB_TYPE_STRING:
        for (j=0;j<set->columns[i].size;j++)
          grib_context_free(c,set->columns[i].string_values[j]);
        break;
      default:
        grib_context_log(c, GRIB_LOG_ERROR,
          "grib_fieldset_new_column : unknown column type %d",set->columns[i].type);
    }
    grib_context_free(c,set->columns[i].errors);
    grib_context_free(c,set->columns[i].name);
  }
  grib_context_free(c,set->columns);
}
Exemplo n.º 29
0
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;
}
Exemplo n.º 30
0
static int create_accessor(grib_section* p, grib_action* act,grib_loader *h)
{
	grib_action_put* a = ( grib_action_put*)act;

	grib_section* ts = NULL;

	grib_accessor* ga = NULL;

	ga = grib_find_accessor(p->h, grib_arguments_get_name(p->h,a->args,1));
	if(ga)
		ts = ga->sub_section;
		/* ts = grib_get_sub_section(ga); */
	else  return GRIB_BUFFER_TOO_SMALL;

	if(ts){
		ga = grib_accessor_factory( ts, act,0,a->args);
		if(ga)grib_push_accessor(ga,ts->block);
		else  return GRIB_BUFFER_TOO_SMALL;

	}
	else{
		grib_context_log(act->context, GRIB_LOG_ERROR, "Action_class_put  : create_accessor_buffer : No Section named %s to export %s ", grib_arguments_get_name(p->h,a->args,1), grib_arguments_get_name(p->h,a->args,0));
	}
	return GRIB_SUCCESS; 
}