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 native_type(grib_expression* g,grib_handle *h)
{
  grib_expression_accessor* e = (grib_expression_accessor*)g;
  int type = 0;
  int err;
  if((err=grib_get_native_type(h,e->name,&type)) != GRIB_SUCCESS)
    grib_context_log(h->context, GRIB_LOG_ERROR,
    "Error in native_type %s : %s", e->name,grib_get_error_message(err));
  return type;
}
Example #3
0
static grib_values* get_key_values(grib_runtime_options* options,grib_handle* h) {
  int i=0;
  int ret=0;
  char value[MAX_STRING_LEN]={0,};
  char* notfound="not found";

  for (i=0;i<options->print_keys_count;i++) {
    size_t len=MAX_STRING_LEN;
    ret=GRIB_SUCCESS;

    if (grib_is_missing(h,options->print_keys[i].name,&ret) && ret==GRIB_SUCCESS) {
      options->print_keys[i].type=GRIB_TYPE_MISSING;
      sprintf(value,"MISSING");

    } else if ( ret != GRIB_NOT_FOUND ){
      if (options->print_keys[i].type == GRIB_TYPE_UNDEFINED) {
        grib_get_native_type(h,options->print_keys[i].name,&(options->print_keys[i].type));
      }

      switch (options->print_keys[i].type) {
        case GRIB_TYPE_STRING:
          ret=grib_get_string( h,options->print_keys[i].name,value,&len);
          break;
        case GRIB_TYPE_DOUBLE:
          ret=grib_get_double( h,options->print_keys[i].name,
                  &(options->print_keys[i].double_value));
          sprintf(value,"%g",options->print_keys[i].double_value);
          break;
        case GRIB_TYPE_LONG:
          ret=grib_get_long( h,options->print_keys[i].name,
                  &(options->print_keys[i].long_value));
          sprintf(value,"%ld",(long)options->print_keys[i].long_value);
          break;
		default:
		  fprintf(dump_file,"invalid type for %s\n",options->print_keys[i].name);
		  exit(1);

      }
    }

    if (ret != GRIB_SUCCESS) {
      if (options->fail) GRIB_CHECK_NOLINE(ret,options->print_keys[i].name);
        if (ret == GRIB_NOT_FOUND) strcpy(value,notfound);
        else {
          fprintf(dump_file,"%s %s\n",grib_get_error_message(ret),options->print_keys[i].name);
          exit(ret);
        }
    }
    options->print_keys[i].string_value=strdup(value);
  }
  return options->print_keys;

}
Example #4
0
static void print_key_values(grib_runtime_options* options,grib_handle* h)
{
    int i;
    int ret=0;
    char* s="\"keys\" : {";
    double dvalue=0;
    long lvalue=0;
    char value[MAX_STRING_LEN];
    size_t len=MAX_STRING_LEN;
    for (i=0;i<options->print_keys_count;i++) {
        ret=GRIB_SUCCESS;
        printf("%s",s);
        len=MAX_STRING_LEN;
        printf("\"%s\" : ",options->print_keys[i].name);
        if (grib_is_missing(h,options->print_keys[i].name,&ret) && ret==GRIB_SUCCESS)
            printf("\"missing\"");
        else if ( ret == GRIB_SUCCESS ) {
            if (options->print_keys[i].type == GRIB_TYPE_UNDEFINED)
                grib_get_native_type(h,options->print_keys[i].name,&(options->print_keys[i].type));
            switch (options->print_keys[i].type) {
            case GRIB_TYPE_STRING:
                ret=grib_get_string( h,options->print_keys[i].name,value,&len);
                printf("\"%s\"",value);
                break;
            case GRIB_TYPE_DOUBLE:
                ret=grib_get_double( h,options->print_keys[i].name,&dvalue);
                printf("%g",dvalue);
                break;
            case GRIB_TYPE_LONG:
                ret=grib_get_long( h,options->print_keys[i].name,&lvalue);
                printf("%ld",lvalue);
                break;
            default:
                printf("invalid_type");
                break;
            }
        }
        if (ret == GRIB_NOT_FOUND) printf("null");
        s=", ";
    }
    printf("}");
}
Example #5
0
int codes_get_native_type(grib_handle* h, const char* name,int* type)
{
    return grib_get_native_type(h,name,type);
}
Example #6
0
static int compare_values(grib_handle* h1,grib_handle *h2,const char *name)
{
  size_t len1 = 0;
  size_t len2 = 0;
  int err;
  int err1;
  int err2;
  int type1,type2;

  char *sval1 = NULL,*sval2 = NULL;
  unsigned char *uval1 = NULL,*uval2 = NULL;
  double *dval1 = NULL, *dval2 = NULL;
  long *lval1 = NULL, *lval2 = NULL;

  if((err = grib_get_native_type(h1,name,&type1)) != GRIB_SUCCESS)
  {
    printf("Oops... cannot get type of [%s] in 1st field: %s\n",name,grib_get_error_message(err));
    return err;
  }

  if((err = grib_get_native_type(h2,name,&type2)) != GRIB_SUCCESS)
  {
    if(err == GRIB_NOT_FOUND)
    {
      printf("[%s] not found in 2nd field\n",name);
      return err;
    }

    printf("Oops... cannot get type of [%s] in 2nd field: %s\n",name,grib_get_error_message(err));
    return err;
  }

  if(type1 != type2)
  {
    printf("Warning, [%s] has different types: 1st field: [%s], 2nd field: [%s]\n",
        name,grib_get_type_name(type1),grib_get_type_name(type2));
    /* return GRIB_TYPE_MISMATCH; */
  }

  if(type1 == GRIB_TYPE_LABEL)
    return err;

  if(type1 == GRIB_TYPE_SECTION)
    return err;


  if((err = grib_get_size(h1,name,&len1)) != GRIB_SUCCESS)
  {
    printf("Oops... cannot get size of [%s] in 1st field: %s\n",name,grib_get_error_message(err));
    return err;
  }

  if((err = grib_get_size(h2,name,&len2)) != GRIB_SUCCESS)
  {
    if(err == GRIB_NOT_FOUND)
    {
      printf("[%s] not found in 2nd field\n",name);
      return err;
    }

    printf("Oops... cannot get size of [%s] in 2nd field: %s\n",name,grib_get_error_message(err));
    return err;
  }

  if(len1 != len2)
  {
    printf("[%s] has different size: 1st field: %ld, 2nd field: %ld\n",name,(long)len1,(long)len2);
    return GRIB_COUNT_MISMATCH;
  }

  switch(type1)
  {
    case GRIB_TYPE_STRING:

      sval1 = (char*)grib_context_malloc(h1->context,len1*sizeof(char));
      sval2 = (char*)grib_context_malloc(h2->context,len2*sizeof(char));

      if((err1 = grib_get_string(h1,name,sval1,&len1)) != GRIB_SUCCESS)
      {
        printf("Oops... cannot get string value of [%s] in 1st field: %s\n",
          name,grib_get_error_message(err1));
      }

      if((err2 = grib_get_string(h2,name,sval2,&len2)) != GRIB_SUCCESS)
      {
        printf("Oops... cannot get string value of [%s] in 2nd field: %s\n",
          name,grib_get_error_message(err2));
      }

      if(err1 == GRIB_SUCCESS && err2 == GRIB_SUCCESS)
      {
        if(strcmp(sval1,sval2) != 0)
        {
          printf("[%s] string values are different: [%s] and [%s]\n",
            name,sval1,sval2);
          err1 = GRIB_VALUE_MISMATCH;
        }
      }

      grib_context_free(h1->context,sval1);
      grib_context_free(h2->context,sval2);

      if(err1) return err1;
      if(err2) return err2;

      break;

    case GRIB_TYPE_LONG:

      lval1 = (long*)grib_context_malloc(h1->context,len1*sizeof(long));
      lval2 = (long*)grib_context_malloc(h2->context,len2*sizeof(long));

      if((err1 = grib_get_long_array(h1,name,lval1,&len1)) != GRIB_SUCCESS)
      {
        printf("Oops... cannot get long value of [%s] in 1st field: %s\n",
          name,grib_get_error_message(err1));
      }

      if((err2 = grib_get_long_array(h2,name,lval2,&len2)) != GRIB_SUCCESS)
      {
        printf("Oops... cannot get long value of [%s] in 2nd field: %s\n",
          name,grib_get_error_message(err2));
      }

      if(err1 == GRIB_SUCCESS && err2 == GRIB_SUCCESS)
      {
        int i;
        for(i = 0; i < len1; i++)
          if(lval1[i] != lval2[i])
          {
              if(len1 == 1)
                printf("[%s] long  values are different: [%ld] and [%ld]\n",
                  name,lval1[i],lval2[i]);
              else
                printf("[%s] long value %d of %ld are different: [%ld] and [%ld]\n",
                  name,i,(long)len1,lval1[i],lval2[i]);

            err1 = GRIB_VALUE_MISMATCH;
            break;
          }
      }

      grib_context_free(h1->context,lval1);
      grib_context_free(h2->context,lval2);

      if(err1) return err1;
      if(err2) return err2;
      break;

    case GRIB_TYPE_DOUBLE:
      dval1 = (double*)grib_context_malloc(h1->context,len1*sizeof(double));
      dval2 = (double*)grib_context_malloc(h2->context,len2*sizeof(double));

      if((err1 = grib_get_double_array(h1,name,dval1,&len1)) != GRIB_SUCCESS)
      {
        printf("Oops... cannot get double value of [%s] in 1st field: %s\n",
          name,grib_get_error_message(err1));
      }

      if((err2 = grib_get_double_array(h2,name,dval2,&len2)) != GRIB_SUCCESS)
      {
        printf("Oops... cannot get double value of [%s] in 2nd field: %s\n",
          name,grib_get_error_message(err2));
      }

      if(err1 == GRIB_SUCCESS && err2 == GRIB_SUCCESS)
      {
        int i;
        for(i = 0; i < len1; i++)
          if(!same(dval1[i],dval2[i]))
          {
              if(len1 == 1)
                printf("[%s] double values are different: [%g] and [%g], diff: %g\n",
                  name,dval1[i],dval2[i],dval1[i] - dval2[i]);
              else
                printf("[%s] double value %d of %ld are different: [%g] and [%g], diff: %g\n",
                  name,i,(long)len1,dval1[i],dval2[i],dval1[i] - dval2[i]);

            err1 = GRIB_VALUE_MISMATCH;
            break;
          }
      }

      grib_context_free(h1->context,dval1);
      grib_context_free(h2->context,dval2);

      if(err1) return err1;
      if(err2) return err2;
      break;

    case GRIB_TYPE_BYTES:

      uval1 = (unsigned char*)grib_context_malloc(h1->context,len1*sizeof(unsigned char));
      uval2 = (unsigned char*)grib_context_malloc(h2->context,len2*sizeof(unsigned char));

      if((err1 = grib_get_bytes(h1,name,uval1,&len1)) != GRIB_SUCCESS)
      {
        printf("Oops... cannot get bytes value of [%s] in 1st field: %s\n",
          name,grib_get_error_message(err1));
      }

      if((err2 = grib_get_bytes(h2,name,uval2,&len2)) != GRIB_SUCCESS)
      {
        printf("Oops... cannot get bytes value of [%s] in 2nd field: %s\n",
          name,grib_get_error_message(err2));
      }

      if(err1 == GRIB_SUCCESS && err2 == GRIB_SUCCESS)
      {
        if(memcmp(uval1,uval2,len1) != 0)
        {
        int i;
        for(i = 0; i < len1; i++)
          if(uval1[i] != uval2[i])
          {
              if(len1 == 1)
                printf("[%s] byte values are different: [%02x] and [%02x]\n",
                  name,uval1[i],uval2[i]);
              else
                printf("[%s] byte value %d of %ld are different: [%02x] and [%02x]\n",
                  name,i,(long)len1,uval1[i],uval2[i]);

            err1 = GRIB_VALUE_MISMATCH;
            break;
          }
          err1 = GRIB_VALUE_MISMATCH;
        }
      }

      grib_context_free(h1->context,uval1);
      grib_context_free(h2->context,uval2);

      if(err1) return err1;
      if(err2) return err2;
      break;

    case GRIB_TYPE_LABEL:
      break;

    default:
      printf("Cannot compare [%s], unsupported type %d\n",name,type1);
      return GRIB_UNABLE_TO_COMPARE_ACCESSORS;
      break;
  }

  return GRIB_SUCCESS;

}