Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
int main(int argc, char** argv)
{
    int err = 0,i;
    double *values = NULL;
    double max,min,average;
    size_t values_len= 0;

    FILE* in = NULL;
    char* filename ;
    grib_handle *h = NULL;

    if (argc<2) usage(argv[0]);
    filename=argv[1];

    in = fopen(filename,"r");
    if(!in) {
        printf("ERROR: unable to open file %s\n",filename);
        return 1;
    }

    /* create new handle from a message in a file*/
    h = grib_handle_new_from_file(0,in,&err);
    if (h == NULL) {
        printf("Error: unable to create handle from file %s\n",filename);
    }


    /* get the size of the values array*/
    GRIB_CHECK(grib_get_size(h,"values",&values_len),0);

    values = (double*)malloc(values_len*sizeof(double));

    /* get data values*/
    GRIB_CHECK(grib_get_double_array(h,"values",values,&values_len),0);

    for(i = 0; i < values_len; i++)
        printf("%d  %.10e\n",i+1,values[i]);

    free(values);


    GRIB_CHECK(grib_get_double(h,"max",&max),0);
    GRIB_CHECK(grib_get_double(h,"min",&min),0);
    GRIB_CHECK(grib_get_double(h,"average",&average),0);

    printf("%d values found in %s\n",(int)values_len,filename);
    printf("max=%.10e min=%.10e average=%.10e\n",max,min,average);

    grib_handle_delete(h);

    fclose(in);
    return 0;
}
Exemplo n.º 3
0
int main(int argc, char** argv) {
  int err = 0,i;
  double *values = NULL;
  size_t values_len= 0;
  double element_value=0;

  FILE* in = NULL;
  char* filename ;
  grib_handle *h = NULL;

  if (argc<2) usage(argv[0]);
  filename=argv[1];

  in = fopen(filename,"r");
  if(!in) {
    printf("ERROR: unable to open file %s\n",filename);
    return 1;
  }

  /* create new handle from a message in a file*/
  h = grib_handle_new_from_file(0,in,&err);
  if (h == NULL) {
    printf("Error: unable to create handle from file %s\n",filename);
  }


  /* get the size of the values array*/
  GRIB_CHECK(grib_get_size(h,"values",&values_len),0);

  values = (double*)malloc(values_len*sizeof(double));

  /* get data values*/
  GRIB_CHECK(grib_get_double_array(h,"values",values,&values_len),0);

  if (values_len < 100) exit(1);

  for (i = 0; i < 100; i++) {
	  /* get double element */
     GRIB_CHECK(grib_get_double_element(h,"values",i,&element_value),0);

     if (element_value != values[i]) {
	    exit(1);
	 }
  }

  free(values);

  grib_handle_delete(h);

  fclose(in);
  return 0;
}
Exemplo n.º 4
0
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
{
    size_t i=0;
    int err=0;

    if (!options->skip) {
        double* v=NULL;
        size_t size=0;
        if ( options->repack ) {
            GRIB_CHECK_NOLINE(grib_get_size(h,"values",&size),0);

            v = (double*)calloc(size,sizeof(double));
            if(!v) {
                fprintf(stderr,"failed to allocate %d bytes\n",(int)(size*sizeof(double)));
                exit(1);
            }

            GRIB_CHECK_NOLINE(grib_get_double_array(h,"values",v,&size),0);
        }

        if (options->set_values_count != 0) {
            err=grib_set_values(h,options->set_values,options->set_values_count);
            if( err != GRIB_SUCCESS && options->fail) exit(err);
        }

        if ( options->repack ) {

            if (grib_options_on("d:")) {
                for(i = 0; i< size; i++)
                    v[i] =  options->constant;
            }
#if 0
            if (grib_options_on("n:")) {
                for(i = 0; i< size; i++)
                    v[i] =  options->constant;
            }
#endif

            if (err == GRIB_SUCCESS) {
                GRIB_CHECK_NOLINE(grib_set_double_array(h,"values",v,size),0);
            }
            free(v);
        }

        if( err != GRIB_SUCCESS && options->fail) exit(err);
    }

    if (!options->skip || !options->strict) grib_tools_write_message(options,h);

    return 0;
}
Exemplo n.º 5
0
static int find(grib_nearest* nearest, grib_handle* h,
                double inlat, double inlon,unsigned long flags,
                double* outlats,double* outlons,
                double *outvalues,double *distances,int* indexes, size_t *len) {
  grib_nearest_sh* self = (grib_nearest_sh*) nearest;
  long J,K,M;
  double *values;
  int size,i,ret;
  size_t vsize=0;
  double val;

  if( (ret =  grib_get_long(h,self->J,&J))!= GRIB_SUCCESS)
    return ret;
  if( (ret =  grib_get_long(h,self->K,&K))!= GRIB_SUCCESS)
    return ret;
  if( (ret =  grib_get_long(h,self->M,&M))!= GRIB_SUCCESS)
    return ret;

  size=2*LEGENDRE_SIZE(J);
  vsize=size;
  values=(double*)grib_context_malloc_clear(h->context,sizeof(double)*size);
  if (!values) {
    grib_context_log(h->context,GRIB_LOG_ERROR,
                     "nearest_sh: unable to allocate %d bytes",
                     sizeof(double)*size);
    return GRIB_OUT_OF_MEMORY;
  }
  
  if( (ret =  grib_get_double_array(h,self->values_key,values,&vsize))
       != GRIB_SUCCESS)
    return ret;
  
  Assert(vsize==size);

  val=grib_invtrans(h->context,J,inlat,inlon,values);

  grib_context_free(h->context,values);

  for (i=0;i<4;i++) {
    outlats[i]=inlat;
    outlons[i]=inlon;
    outvalues[i]=val;
    indexes[i]=-1;
  }
  
  return GRIB_SUCCESS;
}
static int unpack_double_element(grib_accessor* a, size_t idx,double* val)
{
    size_t size;
    double* values;
    int err = 0;
    
    /* GRIB-564: The index idx relates to codedValues NOT values! */

    err=grib_get_size(a->parent->h,"codedValues",&size);
    if (err) return err;
    if (idx > size) return GRIB_INVALID_NEAREST;

    values=(double*)grib_context_malloc_clear(a->parent->h->context,size*sizeof(double));
    err=grib_get_double_array(a->parent->h,"codedValues",values,&size);
    if (err) return err;
    *val=values[idx];
    grib_context_free(a->parent->h->context,values);
    return err;
}
Exemplo n.º 7
0
int main(int argc, char* argv[]) {
  flong ksec0[ISECTION_0];
  flong ksec1[ISECTION_1];
  flong ksec2[ISECTION_2];
  flong ksec3[ISECTION_3];
  flong ksec4[ISECTION_4];

  double rsec2[RSECTION_2];
  double rsec3[RSECTION_3];
  flong sec4len;
  flong miss=0;
  const void *msg;
  flong gribex_msg_len=0;
  grib_handle *h=NULL;
  grib_context* c=NULL;
  FILE* fin=NULL;
  /*char finname[]="/marsdev/data/p4/grib_api/main/data/exp/performance/sh.grib";*/
  char* finname;
  double *values=NULL;
  size_t nvalues=0;
  int count,e=0;
  int maxnvalues;
  unsigned char buffer[50000000];
  size_t length = sizeof(buffer);
  int repeat=300;
  grib_timer *taen,*tade,*tgen,*tgde;

  taen=grib_get_timer(0,"grib_api encoding", 0, 0);
  tgen=grib_get_timer(0,"gribex   encoding", 0, 0);
  tade=grib_get_timer(0,"grib_api decoding", 0, 0);
  tgde=grib_get_timer(0,"gribex   decoding", 0, 0);

  if (argc != 2) usage(argv[0]);
  finname=argv[1];

  fin = fopen(finname,"r");
  if(!fin) {perror(finname);exit(1);}

  c=grib_context_get_default();
  GRIB_CHECK(grib_read_any_from_file(c,fin,buffer,&length),0);
  fclose(fin);

  /* unpack all data with grib_api */
  maxnvalues=0;
  h=grib_handle_new_from_message_copy(c,buffer,length);
  GRIB_CHECK(e,0);

  /* decode values with grib_api*/
  GRIB_CHECK(grib_get_size(h,"values",&nvalues),0);
  values=(double*)grib_context_malloc(c,sizeof(double)*nvalues);
  if (!values) { printf("%s: memory allocation error\n",argv[0]); exit(1); }
  if (maxnvalues<nvalues) maxnvalues=nvalues;

  printf("decoding with grib_api\n");
  grib_timer_start(tade);
  for (count=0;count<repeat;count++) {
    GRIB_CHECK(grib_get_double_array(h,"values",values,&nvalues),0);
  }
  grib_timer_stop(tade,0);
  printf("%d messages decoded\n",count);

  /* encode values with grib_api*/

  printf("encoding with grib_api\n");
  grib_timer_start(taen);
  for (count=0;count<repeat;count++) {
    GRIB_CHECK(grib_set_double_array(h,"values",values,nvalues),0);
  }
  grib_timer_stop(taen,0);
  printf("%d messages encoded\n",count);
  grib_handle_delete(h);
  grib_context_free(c,values);

  /* decode with gribex */
  msg=(char*)buffer;

  sec4len=maxnvalues+100000;
  values  = (double*)grib_context_malloc(c,sizeof(double)*(sec4len));

  printf("decoding with gribex\n");
  grib_timer_start(tgde);
  for (count=0;count<repeat;count++) {
    gribex_msg_len=length;
    gribex_check(cgribex(  miss, ksec0,ksec1,ksec2,rsec2,ksec3,rsec3,
                      ksec4,values,sec4len, (char*)msg,&gribex_msg_len,"D"));
  }
  grib_timer_stop(tgde,0);

  printf("%d messages decoded\n",count);

#if 0
  printf("encoding with gribex\n");
  buflen=nvalues+100000;
  buf=(char*)grib_context_malloc(c,buflen*sizeof(char));
  grib_timer_start(tgen);
  for (count=0;count<repeat;count++) {
    gribex_msg_len=buflen;
    gribex_check(cgribex(  miss, ksec0,ksec1,ksec2,rsec2,ksec3,rsec3,
                      ksec4,values,nvalues, buf,&gribex_msg_len,"C"));
  }
  grib_timer_stop(tgen,0);

  printf("%d messages encoded\n",count);
#endif

  grib_print_all_timers();

  grib_context_free(c,values);
  return 0;
}
Exemplo n.º 8
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;

}
Exemplo n.º 9
0
int main(int argc, char** argv)
{
    int err = 0;
    size_t size=0;

    FILE* in = NULL;
    char* infile = "../../data/regular_latlon_surface.grib1";
    FILE* out = NULL;
    char* outfile = "out.precision.grib1";
    grib_handle *h = NULL;
    const void* buffer = NULL;
    double* values1=NULL;
    double* values2=NULL;
    double maxa=0;
    double maxv=0,minv=0;
    double maxr=0,r=0;
    long decimalPrecision;
    long bitsPerValue1=0, bitsPerValue2=0;
    int i=0;

    in = fopen(infile,"r");
    if(!in) {
        printf("ERROR: unable to open input file %s\n",infile);
        return 1;
    }

    out = fopen(outfile,"w");
    if(!out) {
        printf("ERROR: unable to open output file %s\n",outfile);
        fclose(in);
        return 1;
    }

    /* create a new handle from a message in a file */
    h = grib_handle_new_from_file(0,in,&err);
    if (h == NULL) {
        printf("Error: unable to create handle from file %s\n",infile);
    }

    /* bitsPerValue before changing the packing parameters */
    GRIB_CHECK(grib_get_long(h,"bitsPerValue",&bitsPerValue1),0);
    assert(bitsPerValue1 == 16);

    /* get the size of the values array*/
    GRIB_CHECK(grib_get_size(h,"values",&size),0);
    assert(size == 496);

    values1 = (double*)malloc(size*sizeof(double));
    /* get data values before changing the packing parameters*/
    GRIB_CHECK(grib_get_double_array(h,"values",values1,&size),0);

    /* changing decimal precision to 2 means that 2 decimal digits
     are preserved when packing.  */
    decimalPrecision=2;
    GRIB_CHECK(grib_set_long(h,"changeDecimalPrecision",decimalPrecision),0);

    /* bitsPerValue after changing the packing parameters */
    GRIB_CHECK(grib_get_long(h,"bitsPerValue",&bitsPerValue2),0);
    assert(bitsPerValue2 == 12);

    values2 = (double*)malloc(size*sizeof(double));
    /* get data values after changing the packing parameters*/
    GRIB_CHECK(grib_get_double_array(h,"values",values2,&size),0);

    /* computing error */
    maxa=0;
    maxr=0;
    maxv=values2[0];
    minv=maxv;
    for (i=0;i<size;i++) {
        double a=fabs(values2[i]-values1[i]);
        if ( values2[i] > maxv ) maxv=values2[i];
        if ( values2[i] < minv ) minv=values2[i];
        if ( values2[i] !=0 ) r=fabs((values2[i]-values1[i])/values2[i]);
        if ( a > maxa ) maxa=a;
        if ( r > maxr ) maxr=r;
    }
    printf("max absolute error = %g\n",maxa);
    printf("max relative error = %g\n",maxr);
    printf("min value = %g\n",minv);
    printf("max value = %g\n",maxv);
    assert(fabs(minv - EXPECTED_MIN) < EPSILON);
    assert(fabs(maxv - EXPECTED_MAX) < EPSILON);

    /* get the coded message in a buffer */
    GRIB_CHECK(grib_get_message(h,&buffer,&size),0);

    /* write the buffer in a file*/
    if(fwrite(buffer,1,size,out) != size)
    {
        perror(argv[1]);
        exit(1);
    }

    grib_handle_delete(h);

    fclose(in);
    fclose(out);

    return 0;
}
Exemplo n.º 10
0
int main(int argc, char* argv[]) {
  int i;
  grib_handle *h=NULL;
  grib_handle *hso=NULL;
  grib_context* c=NULL;
  FILE* fin=NULL;
  FILE* fout=NULL;
  char* finname;
  char* ofilename;
  char defofilename[]="so_perf.out";
  double *values=NULL;
  double gvalues[1000000]={0,};
  double sovalues[1000000]={0,};
  int append=0;
  size_t nvalues=0;
  int count,e=0;
  int repeatso=1;
  int repeatsimple=1;
  grib_timer *tes,*tds,*teso,*tdso;
  char grid_simple[]="grid_simple";
  size_t grid_simple_l=strlen(grid_simple);
  char packingType[50]={0,};
  size_t len=50;
  char param[50]={0,};
  char gridType[50]={0,};
  char outfilename[255]={0,};
  size_t filesize_so=0;
  size_t filesize_simple=0;
  double perc=0;
  long bitsPerValue=0;
  int iarg=1;
  char grid[20]={0,};
  char shortName[20]={0,};
  long level;
  char levelType[20]={0,};
  char buffer[BUFF_SIZE]={0,};
  char buf[BUFF_SIZE]={0,};
  size_t length;
  int sec4len;
  flong ksec0[ISECTION_0];
  flong ksec1[ISECTION_1];
  flong ksec2[ISECTION_2];
  flong ksec3[ISECTION_3];
  flong ksec4[ISECTION_4];
  flong miss=0;
  const void *msg;
  flong gribex_msg_len=0;
  double packingError=0;


  double rsec2[RSECTION_2];
  double rsec3[RSECTION_3];

  tes=grib_get_timer(0,"encoding simple", 0, 0);
  tds=grib_get_timer(0,"decoding simple", 0, 0);
  teso=grib_get_timer(0,"encoding so", 0, 0);
  tdso=grib_get_timer(0,"decoding so", 0, 0);

  if (argc != 4 && argc != 6 ) usage(argv[0]);
  if (!strcmp(argv[iarg],"-w")) {
	append=0;
	iarg++;
	ofilename=argv[iarg];
	iarg++;
  } else if (!strcmp(argv[iarg],"-a")) {
	append=1;
	iarg++;
	ofilename=argv[iarg];
	iarg++;
  } else {
	append=0;
  	ofilename=defofilename;
  }
  finname=argv[iarg++];
  repeatsimple=atoi(argv[iarg++]);
  bitsPerValue=atoi(argv[iarg++]);

  fin = fopen(finname,"r");
  if(!fin) {perror(finname);exit(1);}

  c=grib_context_get_default();
  length=BUFF_SIZE;
  GRIB_CHECK(grib_read_any_from_file(c,fin,buffer,&length),0);
  fclose(fin);


  if (append) 
	  fout = fopen(ofilename,"a");
  else
	  fout = fopen(ofilename,"w");

  if(!fout) {perror(ofilename);exit(1);}

  c=grib_context_get_default();
  e=0;
  h=grib_handle_new_from_message_copy(c,buffer,length);

  GRIB_CHECK(e,0);

  len=50;
  grib_get_string(h,"shortName",param,&len);

  len=20;
  grib_get_string(h,"levelType",levelType,&len);

  if (!strcmp(levelType,"pl")) {
  	GRIB_CHECK(grib_get_long(h,"level",&level),0);
	sprintf(shortName,"%s%ld",param,level);
  } else {
	sprintf(shortName,"%s",param);
  }

  /* grib_set_long(h,"editionNumber",2); */
  GRIB_CHECK(grib_get_size(h,"values",&nvalues),0);
  values=(double*)grib_context_malloc_clear(c,sizeof(double)*nvalues);
  if (!values) { printf("%s: memory allocation error\n",argv[0]); exit(1); }

  len=50;
  grib_get_string(h,"gridType",gridType,&len);

  len=50;
  grib_get_string(h,"packingType",packingType,&len);

  GRIB_CHECK(grib_get_double_array(h,"values",values,&nvalues),0);
  grib_set_long(h,"bitsPerValue",bitsPerValue);
  GRIB_CHECK(grib_set_double_array(h,"values",values,nvalues),0);
  GRIB_CHECK(grib_get_double(h,"packingError",&packingError),0);

  printf("--------------------------------\n");
  printf("- %s - gridType=%s packingType=%s numberOfValues=%ld bitsPerValue=%ld\n",
	param,gridType,packingType,(long)nvalues,bitsPerValue);

  if (!strcmp(packingType,"spectral_complex") || !strcmp(packingType,"spectral_simple")) {
	 printf("unable to process spectral data\n");
	 exit(1);
  }

  if (!strcmp(gridType,"reduced_gg") || !strcmp(gridType,"regular_gg")) {
	 long N;
	 grib_get_long(h,"N",&N);
	 printf("    N=%ld\n",N);
	 sprintf(grid,"%ld",N);
  }
  if (!strcmp(gridType,"regular_ll")) {
	 double Di,Dj;
	 grib_get_double(h,"DiInDegrees",&Di);
	 grib_get_double(h,"DjInDegrees",&Dj);
	 printf("    Di=%g Dj=%g\n",Di,Dj);
	 sprintf(grid,"%g/%g",Di,Dj);
  }


  if (!append) 
    fprintf(fout,
	"shortName gridType numberOfValues bitsPerValue grid sizeSimple sizeso encodeso encodeSimple decodeso decodeSimple\n");

  sec4len=nvalues+100000;

  /* decode values grid_simple */
  if (strcmp(packingType,grid_simple))
    grib_set_string(h,"packingType",grid_simple,&grid_simple_l);
  grib_timer_start(tds);
  for (count=0;count<repeatsimple;count++) 
    GRIB_CHECK(grib_get_double_array(h,"values",values,&nvalues),0);
  grib_timer_stop(tds,0);

  *outfilename='\0';
  sprintf(outfilename,"%s_%s_%ld_%ld_simple.grib1",param,gridType,bitsPerValue,(long)nvalues);
  filesize_simple=grib_handle_write(h,outfilename);
  printf("file size simple = %ld\n",(long)filesize_simple);

  /* encode values grid_simple*/
  grib_timer_start(tes);
  for (count=0;count<repeatsimple;count++) 
    GRIB_CHECK(grib_set_double_array(h,"values",values,nvalues),0);
  grib_timer_stop(tes,0);

  /* decode with gribex*/
  msg=(char*)buffer;
  gribex_msg_len=BUFF_SIZE;
  sec4len=nvalues+100000;
  gribex_check(cgribex(  miss, ksec0,ksec1,ksec2,rsec2,ksec3,rsec3,
                      ksec4,gvalues,sec4len, (char*)msg,&gribex_msg_len,"D")); 

  /* encode values second order with gribex*/
  ksec4[1] = bitsPerValue;
  /* to indicate complex packing. */
  ksec4[3] = 64;

  /* to indicate extended flags are present. */
  ksec4[5] = 16;

  ksec4[9] = 16; 
  ksec4[11] = 8; 
  ksec4[12] = 4; 
  ksec4[13] = 0; 
  ksec4[14] = -1; 

  gribex_msg_len=BUFF_SIZE;
  grib_timer_start(teso);
  gribex_check(cgribex(  miss, ksec0,ksec1,ksec2,rsec2,ksec3,rsec3,
                      ksec4,gvalues,nvalues, buf,&gribex_msg_len,"K"));
  grib_timer_stop(teso,0);

  hso=grib_handle_new_from_message_copy(c,buf,gribex_msg_len);

  GRIB_CHECK(grib_get_double_array(h,"values",sovalues,&nvalues),0);

  *outfilename='\0';
  sprintf(outfilename,"%s_%s_%ld_%ld_so.grib1",param,gridType,bitsPerValue,(long)nvalues);
  filesize_so=grib_handle_write(hso,outfilename);
  printf("file size so   = %ld\n",(long)filesize_so);

  perc=(double)filesize_simple/(double)filesize_so; 

  printf("compression ratio = %g \n",perc);
  printf("space savings = %g \n",(1.0-1.0/perc)*100);

  grib_handle_delete(h);
  /* decode values second order */
  /* decode with gribex*/
  msg=(char*)buf;
  gribex_msg_len=BUFF_SIZE;
  sec4len=nvalues+100000;
  grib_timer_start(tdso);
  gribex_check(cgribex(  miss, ksec0,ksec1,ksec2,rsec2,ksec3,rsec3,
                      ksec4,gvalues,sec4len, (char*)msg,&gribex_msg_len,"D")); 
  grib_timer_stop(tdso,0);

  for (i=0;i<nvalues;i++) {
	if (fabs(gvalues[i]-values[i])>packingError) {
	  printf("++++++++ Wrong coding\n");
	  printf("packingError=%g gvalues[%d]=%.20e values[%d]=%.20e abs. err=%g \n",packingError,i,
	  gvalues[i],i,values[i],gvalues[i]-values[i]);
	}
  }

  for (i=0;i<nvalues;i++) {
	if (fabs(gvalues[i]-sovalues[i])>packingError) {
	  printf("++++++++ Wrong coding\n");
	  printf("packingError=%g gvalues[%d]=%.20e sovalues[%d]=%.20e abs. err=%g \n",packingError,i,
	  gvalues[i],i,sovalues[i],gvalues[i]-sovalues[i]);
	}
  }

  grib_handle_delete(hso);
  grib_context_free(c,values);

  print_timer(teso,repeatso);
  print_timer(tdso,repeatso);
  print_timer(tes,repeatsimple);
  print_timer(tds,repeatsimple);

  fprintf(fout,"%s %s %ld %ld %s %ld %ld %g %g %g %g\n",
	shortName,gridType,(long)nvalues,bitsPerValue,
  	grid,(long)filesize_simple,(long)filesize_so,teso->timer_/repeatso,tes->timer_/repeatsimple,tdso->timer_/repeatso,tds->timer_/repeatsimple);

  fclose(fout);
  return 0;
}
Exemplo n.º 11
0
int main(int argc, char** argv) {
  int err = 0;
  double *values = NULL;
  size_t values_len= 0;

  size_t i = 0;

  double latitudeOfFirstGridPointInDegrees;
  double longitudeOfFirstGridPointInDegrees;
  double latitudeOfLastGridPointInDegrees;
  double longitudeOfLastGridPointInDegrees;

  double jDirectionIncrementInDegrees;
  double iDirectionIncrementInDegrees;

  long numberOfPointsAlongAParallel;
  long numberOfPointsAlongAMeridian;

  double average = 0;

  FILE* in = NULL;
  char* filename = "../data/regular_latlon_surface.grib1";
  grib_handle *h = NULL;

  in = fopen(filename,"r");
  if(!in) {
    printf("ERROR: unable to open file %s\n",filename);
    return 1;
  }

  /* create new handle from a message in a file*/
  h = grib_handle_new_from_file(0,in,&err);
  if (h == NULL) {
    printf("Error: unable to create handle from file %s\n",filename);
  }

  /* get as a long*/
  GRIB_CHECK(grib_get_long(h,"numberOfPointsAlongAParallel",&numberOfPointsAlongAParallel),0);
  printf("numberOfPointsAlongAParallel=%ld\n",numberOfPointsAlongAParallel);

  /* get as a long*/
  GRIB_CHECK(grib_get_long(h,"numberOfPointsAlongAMeridian",&numberOfPointsAlongAMeridian),0);
  printf("numberOfPointsAlongAMeridian=%ld\n",numberOfPointsAlongAMeridian);

  /* get as a double*/
  GRIB_CHECK(grib_get_double(h,"latitudeOfFirstGridPointInDegrees",&latitudeOfFirstGridPointInDegrees),0);
  printf("latitudeOfFirstGridPointInDegrees=%g\n",latitudeOfFirstGridPointInDegrees);

  /* get as a double*/
  GRIB_CHECK(grib_get_double(h,"longitudeOfFirstGridPointInDegrees",&longitudeOfFirstGridPointInDegrees),0);
  printf("longitudeOfFirstGridPointInDegrees=%g\n",longitudeOfFirstGridPointInDegrees);

  /* get as a double*/
  GRIB_CHECK(grib_get_double(h,"latitudeOfLastGridPointInDegrees",&latitudeOfLastGridPointInDegrees),0);
  printf("latitudeOfLastGridPointInDegrees=%g\n",latitudeOfLastGridPointInDegrees);

  /* get as a double*/
  GRIB_CHECK(grib_get_double(h,"longitudeOfLastGridPointInDegrees",&longitudeOfLastGridPointInDegrees),0);
  printf("longitudeOfLastGridPointInDegrees=%g\n",longitudeOfLastGridPointInDegrees);

  /* get as a double*/
  GRIB_CHECK(grib_get_double(h,"jDirectionIncrementInDegrees",&jDirectionIncrementInDegrees),0);
  printf("jDirectionIncrementInDegrees=%g\n",jDirectionIncrementInDegrees);

  /* get as a double*/
  GRIB_CHECK(grib_get_double(h,"iDirectionIncrementInDegrees",&iDirectionIncrementInDegrees),0);
  printf("iDirectionIncrementInDegrees=%g\n",iDirectionIncrementInDegrees);

  /* get the size of the values array*/
  GRIB_CHECK(grib_get_size(h,"values",&values_len),0);

  values = malloc(values_len*sizeof(double));

  /* get data values*/
  GRIB_CHECK(grib_get_double_array(h,"values",values,&values_len),0);

  average = 0;
  for(i = 0; i < values_len; i++)
    average += values[i];

  average /=(double)values_len;

  free(values);

  printf("There are %d values, average is %g\n",(int)values_len,average);

  grib_handle_delete(h);

  fclose(in);
  return 0;
}
Exemplo n.º 12
0
int main(int argc, char* argv[]) {
  grib_handle *h=NULL;
  grib_context* c=NULL;
  FILE* fin=NULL;
  FILE* fout=NULL;
  char* finname;
  char* ofilename;
  char defofilename[]="ccsds_perf.out";
  double *values=NULL;
  int append=0;
  size_t nvalues=0;
  int count,e=0;
  int repeatccsds=1;
  int repeatsimple=1;
  grib_timer *tes,*tds,*tej,*tdj;
  char grid_ccsds[]="grid_ccsds";
  size_t grid_ccsds_l=strlen(grid_ccsds);
  char grid_simple[]="grid_simple";
  size_t grid_simple_l=strlen(grid_simple);
  char packingType[50]={0,};
  size_t len=50;
  char param[50]={0,};
  char gridType[50]={0,};
  char outfilename[255]={0,};
  size_t filesize_ccsds=0;
  size_t filesize_simple=0;
  double perc=0;
  long bitsPerValue=0;
  int iarg=1;
  char grid[20]={0,};
  char shortName[20]={0,};
  long level;
  char levelType[20]={0,};

  tes=grib_get_timer(0,"encoding simple", 0, 0);
  tds=grib_get_timer(0,"decoding simple", 0, 0);
  tej=grib_get_timer(0,"encoding ccsds", 0, 0);
  tdj=grib_get_timer(0,"decoding ccsds", 0, 0);

  if (argc != 4 && argc != 6 ) usage(argv[0]);
  if (!strcmp(argv[iarg],"-w")) {
    append=0;
    iarg++;
    ofilename=argv[iarg];
    iarg++;
  } else if (!strcmp(argv[iarg],"-a")) {
    append=1;
    iarg++;
    ofilename=argv[iarg];
    iarg++;
  } else {
    append=0;
    ofilename=defofilename;
  }
  finname=argv[iarg++];
  repeatsimple=atoi(argv[iarg++]);
  bitsPerValue=atoi(argv[iarg++]);

  fin = fopen(finname,"r");
  if(!fin) {perror(finname);exit(1);}

  if (append)
      fout = fopen(ofilename,"a");
  else
      fout = fopen(ofilename,"w");

  if(!fout) {perror(ofilename);exit(1);}

  c=grib_context_get_default();
  e=0;
  h=grib_handle_new_from_file(c,fin,&e);
  fclose(fin);

  GRIB_CHECK(e,0);

  len=50;
  grib_get_string(h,"shortName",param,&len);

  len=20;
  grib_get_string(h,"levelType",levelType,&len);

  if (!strcmp(levelType,"pl")) {
    GRIB_CHECK(grib_get_long(h,"level",&level),0);
    sprintf(shortName,"%s%ld",param,level);
  } else {
    sprintf(shortName,"%s",param);
  }


  grib_set_long(h,"editionNumber",2);
  GRIB_CHECK(grib_get_size(h,"values",&nvalues),0);
  values=(double*)grib_context_malloc(c,sizeof(double)*nvalues);
  if (!values) { printf("%s: memory allocation error\n",argv[0]); exit(1); }

  len=50;
  grib_get_string(h,"gridType",gridType,&len);

  len=50;
  grib_get_string(h,"packingType",packingType,&len);

  GRIB_CHECK(grib_get_double_array(h,"values",values,&nvalues),0);
  grib_set_long(h,"bitsPerValue",bitsPerValue);
  GRIB_CHECK(grib_set_double_array(h,"values",values,nvalues),0);

  printf("--------------------------------\n");
  printf("- %s - gridType=%s packingType=%s numberOfValues=%ld bitsPerValue=%ld\n",
    param,gridType,packingType,(long)nvalues,bitsPerValue);

  if (!strcmp(packingType,"spectral_complex") || !strcmp(packingType,"spectral_simple")) {
     printf("unable to process spectral data\n");
     exit(1);
  }

  if (!strcmp(gridType,"reduced_gg") || !strcmp(gridType,"regular_gg")) {
     long N;
     grib_get_long(h,"N",&N);
     printf("    N=%ld\n",N);
     sprintf(grid,"%ld",N);
  }
  if (!strcmp(gridType,"regular_ll")) {
     double Di,Dj;
     grib_get_double(h,"DiInDegrees",&Di);
     grib_get_double(h,"DjInDegrees",&Dj);
     printf("    Di=%g Dj=%g\n",Di,Dj);
     sprintf(grid,"%g/%g",Di,Dj);
  }


  if (!append)
    fprintf(fout,
    "shortName gridType numberOfValues bitsPerValue grid sizeSimple sizeccsds encodeccsds encodeSimple decodeccsds decodeSimple\n");

  /* decode values grid_simple */
  if (strcmp(packingType,grid_simple))
    grib_set_string(h,"packingType",grid_simple,&grid_simple_l);
  /* printf("decoding simple\n"); */
  grib_timer_start(tds);
  for (count=0;count<repeatsimple;count++)
    GRIB_CHECK(grib_get_double_array(h,"values",values,&nvalues),0);
  grib_timer_stop(tds,0);
  /* printf("%d messages decoded\n\n",count); */

  *outfilename='\0';
  sprintf(outfilename,"%s_%s_%ld_simple.grib2",param,gridType,bitsPerValue);
  filesize_simple=grib_handle_write(h,outfilename);
  printf("file size simple = %ld\n",(long)filesize_simple);

  /* encode values grid_simple*/
  /* printf("encoding simple\n"); */
  grib_timer_start(tes);
  for (count=0;count<repeatsimple;count++)
    GRIB_CHECK(grib_set_double_array(h,"values",values,nvalues),0);
  grib_timer_stop(tes,0);
  /* printf("%d messages encoded \n\n",count); */

  /* decode values grid_ccsds */
  grib_set_string(h,"packingType",grid_ccsds,&grid_ccsds_l);
  /* printf("decoding ccsds\n"); */
  grib_timer_start(tdj);
  for (count=0;count<repeatccsds;count++)
    GRIB_CHECK(grib_get_double_array(h,"values",values,&nvalues),0);
  grib_timer_stop(tdj,0);
  /* printf("%d messages decoded\n\n",count); */

  *outfilename='\0';
  sprintf(outfilename,"%s_%s_%ld_ccsds.grib2",param,gridType,bitsPerValue);
  filesize_ccsds=grib_handle_write(h,outfilename);
  printf("file size ccsds   = %ld\n",(long)filesize_ccsds);

  perc=(double)filesize_simple/(double)filesize_ccsds;

  printf("compression ratio = %g \n",perc);
  printf("space savings = %g \n",(1.0-1.0/perc)*100);

  /* encode values grid_ccsds*/
  /* printf("encoding ccsds\n"); */
  grib_timer_start(tej);
  for (count=0;count<repeatccsds;count++)
    GRIB_CHECK(grib_set_double_array(h,"values",values,nvalues),0);
  grib_timer_stop(tej,0);
  /* printf("%d messages encoded \n\n",count); */

  grib_handle_delete(h);
  grib_context_free(c,values);

  print_timer(tej,repeatccsds);
  print_timer(tdj,repeatccsds);
  print_timer(tes,repeatsimple);
  print_timer(tds,repeatsimple);
  printf("--------------------------------\n\n");
  fprintf(fout,"%s %s %ld %ld %s %ld %ld %g %g %g %g\n",
    shortName,gridType,(long)nvalues,bitsPerValue,
    grid,(long)filesize_simple,(long)filesize_ccsds,tej->timer_/repeatccsds,tes->timer_/repeatsimple,tdj->timer_/repeatccsds,tds->timer_/repeatsimple);

  return 0;
}
Exemplo n.º 13
0
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h) {
  grib_rule* r = options->rules;
  int i;
  int err=0;
  double scale = 1;
  double offset = 0;

  int set = options->set_values_count;

  while(r) {
    if(r->condition)  {
      long ok = 0;
      GRIB_CHECK_NOLINE(grib_expression_evaluate_long(h,r->condition,&ok),NULL);
      if(ok) {
        grib_rule_entry* e = r->entries;
        while(e) {
          if (!strcmp(e->name,"skip")) return 0;
          Assert(set < (sizeof(options->set_values)/sizeof(options->set_values[0])));
          options->set_values[set].name = e->name;
          options->set_values[set].error=grib_expression_set_value(h,
                                              e->value,&(options->set_values[set]));
          set++;
          e = e->next;
        }

      }
    }
    r = r->next;
  }

  if(options->verbose)
  {
    printf("Applying %d:\n",options->infile->handle_count);
    grib_print_values(options->set_values, set);
    printf("\n");
  }

  err=grib_set_values(h,options->set_values, set);

  if( err != GRIB_SUCCESS) {
      for(i = 0; i < options->set_values_count ; i++) {
        if(options->set_values[i].error != GRIB_SUCCESS &&
           options->set_values[i].error != GRIB_CONCEPT_NO_MATCH && options->fail)
           grib_context_log(h->context,GRIB_LOG_ERROR,"unable to set key \"%s\" (%s)",
              options->set_values[i].name,grib_get_error_message( options->set_values[i].error));
      }
  }

  grib_get_double(h,"scaleValuesBy",&scale);
  grib_get_double(h,"offsetValuesBy",&offset);

  if(scale != 1 || offset != 0)
  {
    double *v;
    size_t size;
    int i;

    GRIB_CHECK_NOLINE(grib_get_size(h,"values",&size),0);

    v    = (double*)calloc(size,sizeof(double));
    if(!v) {
      fprintf(stderr,"failed to allocate %ld bytes\n",(long)(size*sizeof(double)));
      exit(1);
    }

    GRIB_CHECK_NOLINE(grib_get_double_array(h,"values",v,&size),0);

    if(options->verbose)
    {
      printf("Applying scale=%g/offset=%g\n",scale,offset);
    }

    for(i = 0; i< size; i++)
      v[i] =  v[i] * scale + offset;

    GRIB_CHECK_NOLINE(grib_set_double_array(h,"values",v,size),0);
    free(v);
  }

  grib_tools_write_message(options,h);

  return 0;
}
Exemplo n.º 14
0
int main(int argc, const char *argv[])
{
	int i,j;
	FILE *in,*out;
	int e;
	grib_handle *result = NULL,*h;
	double* values = NULL;
	double *tmp = NULL;
	size_t size,count;

    fprintf(stderr, "\nWARNING: The tool %s is deprecated.\n\n", argv[0]);

	if(argc < 3) usage(argv[0]);

	out = fopen(argv[argc-1],"w");
	if(!out) {
		perror(argv[argc-1]);
		exit(1);
	}

	for(i = 1; i < argc-1; i++)
	{
		in = fopen(argv[i],"r");
		if(!in) {
			perror(argv[i]);
			exit(1);
		}

		while( (h = grib_handle_new_from_file(NULL,in,&e)) != NULL )
		{
			if(result == NULL)
			{
				GRIB_CHECK(grib_get_size(h,"values",&size),argv[i]);

				values = (double*)calloc(size,sizeof(double));
				assert(values);

				tmp    = (double*)calloc(size,sizeof(double));
				assert(tmp);

				result = h;
			}

			GRIB_CHECK(grib_get_size(h,"values",&count),argv[i]);
			assert(count == size);

			GRIB_CHECK(grib_get_double_array(h,"values",tmp,&count),argv[i]);
			assert(count == size);

			for(j = 0; j < count; j++)
				values[j] += tmp[j];

			if(h != result)
				grib_handle_delete(h);
		}

		GRIB_CHECK(e,argv[argc-2]);
	}

	if(result)
	{
		const void *buffer; 
		GRIB_CHECK(grib_set_double_array(result,"values",values,size),argv[i]);
		GRIB_CHECK(grib_get_message(result,&buffer,&size),argv[0]);

		if(fwrite(buffer,1,size,out) != size)
		{
			perror(argv[argc-1]);
			exit(1);
		}
		grib_handle_delete(result);
	}

	if(fclose(out))
	{
		perror(argv[argc-1]);
		exit(1);
	}

	return 0;
}
Exemplo n.º 15
0
int codes_get_double_array(grib_handle* h, const char* key, double* vals, size_t *length)
{
    return grib_get_double_array(h,key,vals,length);
}
Exemplo n.º 16
0
int main(int argc, char *argv[])
{
  grib_handle* h = NULL;
  FILE* f = NULL;
  int i = 0;
  int err = 0;
  char *mode = "file";

  for(i = 1; i < argc; i++)
  {

    if(argv[i][0] == '-')
    {
      mode = argv[i]+1;
      continue;
    }

    f = fopen(argv[i],"r");
    if(!f) {
      perror(argv[i]);
      exit(1);
    }

    while((h = grib_handle_new_from_file(0,f,&err)) != NULL)
    {
      long width;
      long height;
      double max, min;
      double *values = NULL;
      size_t count;
      int i;

      GRIB_CHECK(grib_get_size(h,"values",&count),0);
      values = (double *)malloc(sizeof(double)*count);

      GRIB_CHECK(grib_get_long(h,"Ni",&width),0);
      GRIB_CHECK(grib_get_long(h,"Nj",&height),0);
      GRIB_CHECK(grib_get_double_array(h,"values",values,&count),0);

      max=values[0];
      min=values[0];
      for (i=1; i<count;++i)
      {
        if(values[i]>max)
          max = values[i];
        if(values[i]<min)
          min = values[i];
      }

      /* PPM header */
      printf("P5\n%ld %ld\n255\n",width,height);
      for (i=0; i<count;++i)
      {
        char c = (values[i] - min)*255 / (max - min);
        printf("%c",c);
      }

      grib_handle_delete(h);
    }
    fclose(f);
    if(err)
    {
      fprintf(stderr,"%s\n",grib_get_error_message(err));
      exit(1);
    }
  }
  return 0;
}
Exemplo n.º 17
0
Arquivo: Grib.cpp Projeto: WFRT/Comps
float InputGrib::getValueCore(const Key::Input& iKey) const {
#ifdef WITH_GRIB
   // Check that date hasn't been checked before
   std::map<int,std::map<float, bool> >::const_iterator it0 = mMissingFiles.find(iKey.date);
   if(it0 != mMissingFiles.end()) {
      // Date has missing files
      std::map<float, bool>::const_iterator it1 = it0->second.find(iKey.offset);
      if(it1 != it0->second.end()) {
         writeMissingToCache(iKey);
         return Global::MV;
      }
   }

   std::string localVariable;
   bool found = getLocalVariableName(iKey.variable, localVariable);
   assert(found);

   int numLocations = Input::getNumLocations();

   std::string filename = getFilename(iKey);
   std::stringstream ss;
   ss << "InputGrib: Loading " << filename << " " << iKey.date << " " << iKey.offset << " " << iKey.location << " " << localVariable;
   Global::logger->write(ss.str(), Logger::message);
   bool foundVariable = false;
   FILE* fid = fopen(filename.c_str(),"r");
   float value = Global::MV;
   if(fid) {
      // GRIB File found
      int err = 0;
      grib_handle* h = NULL;

      // double s = Global::clock();

      // Try to use an index to read file as this is much faster. Fall back on just reading the 
      // GRIB file.
      grib_index* gribIndex = getIndex(iKey, localVariable);
      bool validIndex = (gribIndex != NULL);
      if(!validIndex) {
         std::stringstream ss;
         ss << "InputGrib: No index file available for " << filename;
         Global::logger->write(ss.str(), Logger::message);
      }

      int counter = 1;
      // Loop over available variables (in index or in file)
      while(1) {
         // Read message from file or index
         if(!validIndex) {
            h = grib_handle_new_from_file(0,fid,&err);
         }
         else {
            h = grib_handle_new_from_index(gribIndex,&err);
         }
         if(h == NULL)
            break; // No more messages to process

         std::string currVariable = getVariableName(h);
         std::stringstream ss;
         ss << "InputGrib: Reading message #" << counter << ": " << currVariable;
         Global::logger->write(ss.str(), Logger::message);

         // Check if the current variable is defined in the variable list
         int variableId;
         found = getVariableIdFromLocalVariable(currVariable, variableId);
         if(!found) {
            std::stringstream ss;
            ss << "InputGrib: Found variable " << currVariable << " in " << filename << " but this is not mapped to any variable in namelist" << std::endl;
            Global::logger->write(ss.str(), Logger::message);
         }
         else {
            // Only read the current variable if necessary
            if(mCacheOtherVariables || currVariable == localVariable) {
               std::vector<float> currValues;
               currValues.resize(numLocations, Global::MV);
               int numValid = 0;

               // Check that the message has the right number of locations
               size_t N;
               GRIB_CHECK(grib_get_size(h,"values",&N),0);
               if(N == numLocations) {
                  foundVariable = foundVariable || (currVariable == localVariable);
                  double* arr = new double[N];

                  GRIB_CHECK(grib_get_double_array(h,"values",arr,&N),0);
                  currValues.assign(arr, arr + numLocations);
                  for(int i = 0; i < (int) currValues.size(); i++) {
                     if(currValues[i] == mMV)
                        currValues[i] = Global::MV;
                     else
                        numValid++;
                  }
                  std::stringstream ss;
                  ss << "InputGrib: Number of valid values: " << numValid;
                  Global::logger->write(ss.str(), Logger::message);
                  delete arr;
               }
               else {
                  std::stringstream ss;
                  ss << "GribInput: Discarding variable " << currVariable << " in " << filename
                     << " because it has incorrect number of locations";
                  Global::logger->write(ss.str(), Logger::debug);
               }

               Key::Input key = iKey;
               key.offset = getOffset(h);
               // Cache values
               for(int i = 0; i < numLocations; i++) {
                  key.location = i;
                  key.variable = variableId;
                  if(key.location == iKey.location && currVariable == localVariable) {
                     // Found the value
                     value = currValues[i];
                  }
                  if(mCacheOtherLocations || key.location == iKey.location) {
                     //if(currVariable == localVariable)
                     //   std::cout << currValues[i] << std::endl;
                     Input::addToCache(key, currValues[i]);
                  }
               }
            }
         }
         if(h) {
            grib_handle_delete(h);
         }

         // Quit reading file if we have found the variable we need
         if(!mCacheOtherVariables && (currVariable == localVariable)) {
            break;
         }
         counter++;
      }
      if(!foundVariable) {
         // File was there, but couldn't find variable
         std::stringstream ss;
         ss << "InputGrib: Could not find variable " << localVariable << " in " << filename;
         Global::logger->write(ss.str(), Logger::warning);
         writeMissingToCache(iKey);
      }
      if(validIndex) {
         grib_index_delete(gribIndex);
      }

      // double e = Global::clock();
      //std::cout << "Grib read time: " << e - s << " seconds" << std::endl;

      fclose(fid);
      return value;
   }
   else {
      // GRIB file not found
      std::stringstream ss;
      ss << "GribInput: File not found: " << filename;
      Global::logger->write(ss.str(), Logger::message);

      std::vector<float> currValues;
      currValues.resize(numLocations, Global::MV);
      for(int i = 0; i < numLocations; i++) {
         Key::Input key = iKey;
         key.location = i;
         if(mCacheOtherLocations || key.location == iKey.location)
            Input::addToCache(key, currValues[i]);
      }
      return Global::MV;
   }
#else
   return Global::MV;
#endif
}
void QNCLContourLine::plot(QGRIB2Message *grb2Msg, QString outputPath, QString outputFileName)
{
    /*float xbvalues1[7] = {0.,60.,120.,180.,240.,300.,360.};
    char *xblabels1[7] = {"0","60E","120E","180","120W","60W","0"};
    float xbvalues2[11] = {0.,3.,6.,9.,12.,15.,18.,21.,24.,27.,30.};
    float ylvalues[7] = {-90., -60.,-30.,0.,30.,60.,90.};
    char *xblabels2[11] = {"0","3","6","9","12","15","18","21","24","27","30"};
    char *yllabels[7] = {"90S","60S","30S","EQ","30N","60N","90N"};*/
    /*rules setting*/
    float       xbValues[7] = {0};
    char        *xbLabels[7];
    float       ybValues[7] = {0};
    char        *ybLabels[7];
    int         xLabelCount = 0, yLabelCount = 0;
    double *    dData;
    float  *    fData;
    //float       cmap[255][3] = {0};
    int         ni, nj;
    int         mi, mj;
    int         xStart, xEnd;
    int         yStart, yEnd;
    int         appid, jan, cn, mp, tx, srlist;
    ng_size_t   length[2];
    float       cmap[256][3];
    //float  *    cmap;
    size_t      nSize;
    ng_size_t   icount[2];
    int         k = 0;
    int         ps2;//1, ps2, png1;
    char        setting[256]= {0};

    /*x label setting*/
    memset(xbLabels, 0, sizeof(xbLabels));
    memset(ybLabels, 0, sizeof(ybLabels));
    xLabelCount = width/100 + 1;
    xLabelCount = (xLabelCount < 2)?2:((xLabelCount>7)?7:xLabelCount);
    for (int i = 0; i < xLabelCount; i ++) {
        xbValues[i] = leftLongitude + (rightLongitude - leftLongitude) * i / (xLabelCount - 1);
        xbLabels[i] = (char *)calloc(10, 1);
        if ((xbValues[i] == 0) || (xbValues[i] == 180) || (xbValues[i]) == 360)
            sprintf(xbLabels[i], "%d", ((int)xbValues[i])%360);
        else if ((xbValues[i] > 0) && (xbValues[i] < 180)) {
            if ((xbValues[i]-(int)xbValues[i]) == 0)
                sprintf(xbLabels[i], "%dE", ((int)xbValues[i])%360);
            else
                sprintf(xbLabels[i], "%3.4fE", xbValues[i]);
        }
        else if ((xbValues[i] > 180) && (xbValues[i] < 360)) {
            if ((xbValues[i]-(int)xbValues[i]) == 0)
                sprintf(xbLabels[i], "%dW", (360 - (int)xbValues[i]));
            else
                sprintf(xbLabels[i], "%3.4fW", 360. - xbValues[i]);
        }
    }
    yLabelCount = height/100+1;
    yLabelCount = (yLabelCount < 2)?2:((yLabelCount>7)?7:yLabelCount);
    for (int i = 0; i < yLabelCount; i++) {
        ybValues[i] = topLatitude - (topLatitude - bottomLatitude) * i / (yLabelCount - 1);
        ybLabels[i] = (char *)calloc(10, 1);
        if (ybValues[i] == 0)
            sprintf(ybLabels[i], "%d", ((int)ybValues[i])%360);
        else if ((ybValues[i] > 0) && (ybValues[i] <= 90)) {
            if ((ybValues[i]-(int)ybValues[i]) == 0)
                sprintf(ybLabels[i], "%dN", ((int)ybValues[i])%360);
            else
                sprintf(ybLabels[i], "%3.4fN", ybValues[i]);
        }
        else if ((ybValues[i] < 0) && (ybValues[i] >= -90)) {
            if ((ybValues[i]-(int)ybValues[i]) == 0)
                sprintf(ybLabels[i], "%dW", ((int)ybValues[i])%360);
            else
                sprintf(ybLabels[i], "%3.4fW", ybValues[i]);
        }
    }

    /*get data from grib2 message between in the contour setting*/
    grib_handle * hgrib = grb2Msg->getGRIBHandle();
    QString     workName;
    workName = outputFileName;
    workName = workName.left(workName.indexOf('.'));
    ni = grb2Msg->getGrid()->xp();
    nj = grb2Msg->getGrid()->yp();
    nSize = ni * nj;
    dData = (double *)calloc(ni*nj, sizeof(double));



    grib_get_double_array(hgrib, "values", dData, &nSize);
    mi = ((rightLongitude - leftLongitude) * 1000000 / grb2Msg->getGrid()->dx()) + 1;
    mj = ((topLatitude - bottomLatitude) * 1000000 / grb2Msg->getGrid()->dy()) + 1;
    if (mi > ni)
        mi = ni;
    if (mj > nj)
        mj = nj;
    fData = (float *)calloc(mi * mj, sizeof(float));
    xStart = (int)((leftLongitude * 1000000 - grb2Msg->getGrid()->lon1())/grb2Msg->getGrid()->dx());
    if ((xStart * grb2Msg->getGrid()->dx()) > (leftLongitude * 1000000))
        xStart --;
    if (xStart < 0)
        xStart = 0;
    xEnd = (int)((rightLongitude * 1000000 - grb2Msg->getGrid()->lon1())/grb2Msg->getGrid()->dx()) + 1;
    if ((xEnd * grb2Msg->getGrid()->dx()) < (rightLongitude * 1000000))
        xEnd++;
    if (xEnd > ni)
        xEnd = ni;
    yStart = (int)((grb2Msg->getGrid()->lat1() - topLatitude * 1000000)/grb2Msg->getGrid()->dy());
    if ((grb2Msg->getGrid()->lat1() - yStart * grb2Msg->getGrid()->dy()) < (topLatitude * 1000000))
        yStart--;
    if (yStart < 0)
        yStart = 0;
    yEnd = (int)((grb2Msg->getGrid()->lat1() - bottomLatitude * 1000000)/grb2Msg->getGrid()->dy()) + 1;
    if ((grb2Msg->getGrid()->lat1() - yEnd * grb2Msg->getGrid()->dy()) > (bottomLatitude * 1000000))
        yEnd++;
    if (yEnd > nj)
        yEnd = nj;
    for (int jj = yStart; jj < yEnd; jj++) {
        for (int ii = xStart; ii < xEnd; ii ++) {
            fData[k++] = dData[jj * ni + ii];
        }
    }

    NhlInitialize();
    srlist = NhlRLCreate(NhlSETRL);
    NhlRLClear(srlist);
    NhlRLSetString(srlist, "appDefaultParent", "True");
    memset(setting, 0, sizeof(setting));
    strncpy(setting, outputPath.toStdString().c_str(), sizeof(setting)-1);
    NhlRLSetString(srlist, "appUsrDir", setting);
    NhlCreate(&appid, grb2Msg->getName().toStdString().c_str(), NhlappClass, 0, srlist);

    NhlRLClear(srlist);
    QString outputpsFileName = outputPath + "/" + outputFileName;
    memset(setting, 0, sizeof(setting));
    strncpy(setting, outputpsFileName.toStdString().c_str(), sizeof(setting)-1);
    NhlRLSetString(srlist, NhlNwkFileName, setting);
    /*NhlRLSetString(srlist, NhlNwkFormat, "ps");*/
    NhlRLSetString(srlist, NhlNwkFormat, "png");

    /*NhlRLSetString(srlist, NhlNwkOrientation, "landscape");
    NhlRLSetString(srlist, NhlNwkOrientation, "portrait");
    NhlRLSetInteger(srlist, NhlNwkDeviceLowerX, 0);
    NhlRLSetInteger(srlist, NhlNwkDeviceLowerY, 60);
    NhlRLSetInteger(srlist, NhlNwkDeviceUpperX, width);
    NhlRLSetInteger(srlist, NhlNwkDeviceUpperY, height); */
    float   fcolor[3] = {0};
    float   bcolor[3] = {0};
    int r, g, b;
    foregroundColor.getRgb(&r, &g, &b);
    fcolor[0] = ((float)r)/255.;
    fcolor[1] = ((float)g)/255.;
    fcolor[2] = ((float)b)/255.;
    length[0] = 3;
    NhlRLSetFloatArray(srlist, NhlNwkForegroundColor, fcolor, length[0]);
    backgroundColor.getRgb(&r, &g, &b);
    bcolor[0] = ((float)r)/255.;
    bcolor[1] = ((float)g)/255.;
    bcolor[2] = ((float)b)/255.;
    length[0] = 3;
    NhlRLSetFloatArray(srlist, NhlNwkBackgroundColor, bcolor, length[0]);
    length[0] = loadColormap(colormap, &cmap[0][0], 256);
    length[1] = 3;
    if (length[0] > 0) {
        NhlRLSetMDFloatArray(srlist,NhlNwkColorMap,&cmap[0][0], 2, length);
    } else {
        memset(setting, 0, sizeof(setting));
        strncpy(setting, colormap.toStdString().c_str(), sizeof(setting)-1);
        NhlRLSetString(srlist, NhlNwkColorMap, setting);
    }
    /*NhlCreate(&ps2, workName.toStdString().c_str(), NhlpsWorkstationClass, 0, srlist);*/
    NhlCreate(&ps2, workName.toStdString().c_str(), NhlcairoImageWorkstationClass, 0, srlist);


    icount[1] = xEnd - xStart;
    icount[0] = yEnd - yStart; /**/
    NhlRLClear(srlist);
    NhlRLSetMDFloatArray(srlist, NhlNsfDataArray, fData, 2, icount);
    NhlRLSetFloat(srlist, NhlNsfXCStartV, leftLongitude);
    NhlRLSetFloat(srlist, NhlNsfXCEndV, rightLongitude);
    NhlRLSetFloat(srlist, NhlNsfYCStartV, topLatitude);
    NhlRLSetFloat(srlist, NhlNsfYCEndV, bottomLatitude);
    NhlCreate(&jan, "sf", NhlscalarFieldClass, appid, srlist);
    //NhlRLSetString(srlist, NhlNwkOrientation, "landscape");
    /*NhlRLSetString(srlist, NhlNwkOrientation, "portrait");*/
    NhlRLSetString(srlist, NhlNcnLineDrawOrder, "predraw");

    /** Create a ContourPlot object.*/
    NhlRLClear(srlist);
    NhlRLSetInteger(srlist, NhlNcnScalarFieldData, jan);
    NhlRLSetFloat(srlist, NhlNvpXF, .1);
    NhlRLSetFloat(srlist, NhlNvpYF, .75);
    NhlRLSetFloat(srlist, NhlNvpWidthF, 0.8);
    NhlRLSetFloat(srlist, NhlNvpHeightF, 0.4);
    NhlRLSetString(srlist, NhlNcnInfoLabelOn, "False");
    NhlRLSetString(srlist, NhlNcnHighLabelsOn, "False");
    NhlRLSetString(srlist, NhlNcnLowLabelsOn, "False");
    //NhlRLSetIntegerArray(srlist, NhlNcnLineColors, linecolors, 15);

    NhlRLSetString(srlist, NhlNcnLineDrawOrder, "predraw");
    NhlRLSetString(srlist, NhlNcnFillDrawOrder, "predraw");
    NhlRLSetString(srlist, NhlNcnLabelDrawOrder, "predraw");
    memset(setting, 0, sizeof(setting));
    strncpy(setting, levelSelectionMode.toStdString().c_str(), sizeof(setting)-1);
    NhlRLSetString(srlist, NhlNcnLevelSelectionMode, setting);
    NhlRLSetInteger(srlist, NhlNcnMaxLevelCount, maxLevelCount);
    if (levelSelectionMode == "ManualLevels") {
        NhlRLSetFloat(srlist, NhlNcnMinLevelValF, minLevel);
        NhlRLSetFloat(srlist, NhlNcnMaxLevelValF, maxLevel);
        NhlRLSetFloat(srlist, NhlNcnLevelSpacingF, levelSpacing);
    } else if (levelSelectionMode == "ExplicitLevels") {
        length[0] = maxLevelCount;
        NhlRLSetFloatArray(srlist, NhlNcnLevelSpacingF, lineLevels, length[0]);
    } else if (levelSelectionMode == "EqualSpacedLevels") {
        NhlRLSetFloat(srlist, NhlNcnLevelSpacingF, levelSpacing);
    }

    if (plotMode == 0) {
        NhlRLSetString(srlist, NhlNcnFillOn, "False");
        NhlRLSetString(srlist, NhlNcnLinesOn, "True");
        NhlRLSetString(srlist, NhlNcnLineLabelsOn, "True");
        NhlRLSetInteger(srlist, NhlNcnLineLabelInterval, 2);
        NhlRLSetString(srlist, NhlNcnLineLabelPlacementMode, "computed");
        if (monoColor == false) {
            NhlRLSetString(srlist, NhlNcnMonoLineColor, "False");
            NhlRLSetIntegerArray(srlist, NhlNcnLineColors, lineColors, maxLevelCount);
        } else {
            NhlRLSetString(srlist, NhlNcnMonoLineColor, "True");
            /*NhlRLSetString(srlist, NhlNcnLineColor, "Black");*/
            NhlRLSetInteger(srlist, NhlNcnLineColor, lineColor);
        }
    } else {
        NhlRLSetString(srlist, NhlNcnFillOn, "True");
        NhlRLSetString(srlist, NhlNcnLinesOn, "False");
        NhlRLSetString(srlist, NhlNcnFillMode, "AreaFill");
        NhlRLSetString(srlist, NhlNcnLineLabelsOn, "False");
        length[0] = maxLevelCount;
        NhlRLSetIntegerArray(srlist, NhlNcnFillColors, lineColors, length[0]);
    }
    NhlRLSetString(srlist, NhlNtmXBMode, "EXPLICIT");
    NhlRLSetFloatArray(srlist, NhlNtmXBValues, xbValues, xLabelCount);
    NhlRLSetStringArray(srlist, NhlNtmXBLabels, &xbLabels[0], xLabelCount);
    NhlRLSetString(srlist, NhlNtmYLMode, "EXPLICIT");
    NhlRLSetFloatArray(srlist, NhlNtmYLValues, ybValues, yLabelCount);
    NhlRLSetStringArray(srlist, NhlNtmYLLabels, &ybLabels[0], yLabelCount);
    NhlRLSetString(srlist, NhlNtmXTLabelsOn, "False");
    NhlRLSetString(srlist, NhlNtmYRLabelsOn, "False");
    NhlRLSetFloat(srlist, NhlNtmXBLabelFontHeightF, .010);
    NhlRLSetFloat(srlist, NhlNtmYLLabelFontHeightF, .010);
    NhlRLSetFloat(srlist, NhlNtmXBMajorOutwardLengthF, .006);
    NhlRLSetFloat(srlist, NhlNtmXBMajorLengthF, .006);
    NhlRLSetFloat(srlist, NhlNtmXTMajorLengthF, 0.);
    NhlRLSetFloat(srlist, NhlNtmXTMajorOutwardLengthF, 0.);
    NhlRLSetFloat(srlist, NhlNtmYLMajorOutwardLengthF, .006);
    NhlRLSetFloat(srlist, NhlNtmYLMajorLengthF, .006);
    NhlRLSetString(srlist, NhlNtmXBMinorOn, "False");
    NhlRLSetString(srlist, NhlNtmXTMinorOn, "False");
    NhlRLSetString(srlist, NhlNtmYLMinorOn, "False");
    NhlRLSetString(srlist, NhlNtmYRMinorOn, "False");
    if (monoThickness == true) {
        NhlRLSetString(srlist, NhlNcnMonoLineThickness, "True");
        NhlRLSetFloat(srlist, NhlNcnLineThicknessF, thickness);
    } else {
        NhlRLSetString(srlist, NhlNcnMonoLineThickness, "False");
        NhlRLSetFloatArray(srlist, NhlNcnLineThicknesses, lineThicknesses, maxLevelCount);
    }
    NhlCreate(&cn, "cn", NhlcontourPlotClass, ps2, srlist);

    /* Create a MapPlot object. */
    NhlRLClear(srlist);
    NhlRLSetFloat(srlist, NhlNvpXF, .1);
    NhlRLSetFloat(srlist, NhlNvpYF, .75);
    NhlRLSetFloat(srlist, NhlNvpWidthF, 0.8);
    NhlRLSetFloat(srlist, NhlNvpHeightF, 0.4);
    /*NhlRLSetString(srlist, NhlNmpFillOn, "True");*/
    NhlRLSetString(srlist, NhlNmpFillOn, "False");
    NhlRLSetString(srlist, NhlNmpLabelsOn, "False");
    /*NhlRLSetString(srlist, NhlNmpGeophysicalLineColor, "Black");
    NhlRLSetString(srlist, NhlNmpDefaultFillColor, "DarkSalmon");
    NhlRLSetString(srlist, NhlNmpLandFillColor, "DarkSalmon");
    NhlRLSetString(srlist, NhlNmpOceanFillColor, "Blue");
    NhlRLSetString(srlist, NhlNmpInlandWaterFillColor, "Blue");*/
    NhlRLSetString(srlist, NhlNmpOutlineOn, "True");
    NhlRLSetString(srlist, NhlNgsLineColor, "Black");
    /*NhlRLSetString(srlist, NhlNmpOutlineOn, "True");
    NhlRLSetString(srlist, NhlNmpOutlineBoundarySets, "Geophysical");*/
    /*NhlRLSetString(srlist, NhlNmpOutlineDrawOrder, "predraw");*/
    NhlRLSetString(srlist, NhlNmpOutlineBoundarySets, "Geophysical");
    NhlRLSetFloat(srlist, NhlNmpGeophysicalLineThicknessF, 1);
    NhlRLSetString(srlist, NhlNmpGeophysicalLineColor, "Black");
    /*NhlRLSetString(srlist, NhlNmpPerimDrawOrder, "predraw");*/
    memset(setting, 0, sizeof(setting));
    strncpy(setting, mapProject.toStdString().c_str(), sizeof(setting)-1);
    if (mapProject == "Polar Stereographic") {
        NhlRLSetString(srlist, NhlNmpProjection, "Stereographic");
        if (polarPosition == "NH") {
            NhlRLSetFloat(srlist, NhlNmpMinLatF, 0);
            NhlRLSetFloat(srlist, NhlNmpMaxLatF, 90);
            NhlRLSetFloat(srlist, NhlNmpMinLonF, leftLongitude);
            NhlRLSetFloat(srlist, NhlNmpMaxLonF, rightLongitude);
            NhlRLSetFloat(srlist, NhlNmpCenterLonF, (leftLongitude+rightLongitude)/2);
            NhlRLSetFloat(srlist, NhlNmpCenterLatF, 90);
        } else {
            NhlRLSetFloat(srlist, NhlNmpMinLatF, -90);
            NhlRLSetFloat(srlist, NhlNmpMaxLatF, 0);
            NhlRLSetFloat(srlist, NhlNmpMinLonF, leftLongitude);
            NhlRLSetFloat(srlist, NhlNmpMaxLonF, rightLongitude);
            NhlRLSetFloat(srlist, NhlNmpCenterLonF, (leftLongitude+rightLongitude)/2);
            NhlRLSetFloat(srlist, NhlNmpCenterLatF, -90);
        }
    } else {
        NhlRLSetString(srlist, NhlNmpProjection, setting);
        NhlRLSetFloat(srlist, NhlNmpMinLatF, bottomLatitude);
        NhlRLSetFloat(srlist, NhlNmpMaxLatF, topLatitude);
        NhlRLSetFloat(srlist, NhlNmpMinLonF, leftLongitude);
        NhlRLSetFloat(srlist, NhlNmpMaxLonF, rightLongitude);
        NhlRLSetFloat(srlist, NhlNmpCenterLonF, (leftLongitude+rightLongitude)/2);
        NhlRLSetFloat(srlist, NhlNmpCenterLatF, (topLatitude + bottomLatitude)/2);
    }

    NhlRLSetString(srlist, NhlNmpGridAndLimbOn, "False");
    NhlRLSetString(srlist, NhlNmpLimitMode, "latlon");
    NhlCreate(&mp, "mp", NhlmapPlotClass, ps2, srlist);

    /* Create a TextItem object. */
    QString title = grb2Msg->getParameter()->nclName();
    QString level;
    level.setNum(grb2Msg->getLevel()->firstLevel());
    switch (grb2Msg->getLevel()->firstLevelType()) {
    case 100:
    case 101:
        level += "Pa";
        break;
    case 0:
    case 106:
        level += "m";
        break;
    }
    title += " " + level + " " + grb2Msg->getTimeLevel()->getName() ;

    NhlRLClear(srlist);
    NhlRLSetFloat(srlist, NhlNtxPosXF, 0.5);
    NhlRLSetFloat(srlist, NhlNtxPosYF, 0.8);
    NhlRLSetString(srlist, NhlNtxJust, "CENTERCENTER");
    memset(setting, 0, sizeof(setting));
    strncpy(setting, title.toStdString().c_str(), sizeof(setting)-1);
    NhlRLSetString(srlist, NhlNtxString, setting);
    NhlRLSetFloat(srlist, NhlNtxFontHeightF, .030);
    NhlRLSetInteger(srlist, NhlNtxFont, 25);
    NhlCreate(&tx, "tx", NhltextItemClass, ps2, srlist);

    NhlDraw(mp);
    NhlDraw(cn);
    NhlDraw(tx);
    NhlFrame(ps2);

    NhlDestroy(ps2);
    NhlDestroy(appid);
    for (int idx = 0; idx < xLabelCount; idx ++) {
        free(xbLabels[idx]);
    }
    for (int idx = 0; idx < yLabelCount; idx ++) {
        free(ybLabels[idx]);
    }
    free(dData);
    free(fData);
}
static int find(grib_nearest* nearest, grib_handle* h,
                double inlat, double inlon,unsigned long flags,
                double* outlats,double* outlons, double *values,
                double *distances,double *distances,int *indexes,size_t *len) {
  grib_nearest_reduced* self = (grib_nearest_reduced*) nearest;
  int ret=0,kk=0,ii=0,jj=0;
  double* pl=NULL;
  int ilat;
  size_t nvalues=0;
  if (!nearest->h || (flags & GRIB_NEAREST_SAME_DATA)==0 || nearest->h!=h) {
    grib_iterator* iter=NULL;
    double lat=0,lon=0;

    if( (ret =  grib_get_size(h,self->values_key,&nvalues))!= GRIB_SUCCESS)
       return ret;
    nearest->values_count = nvalues;
    if (nearest->values) grib_context_free(nearest->context,nearest->values);
    nearest->values = grib_context_malloc(h->context,nvalues*sizeof(double));
    if (!nearest->values) return GRIB_OUT_OF_MEMORY;

    ret=grib_get_double_array_internal( h,self->values_key,
                                   nearest->values,&(nearest->values_count));
    if (ret!=GRIB_SUCCESS) grib_context_log(nearest->context,GRIB_LOG_ERROR,
       "nearest: unable to get values array");

    if (!nearest->h || (flags & GRIB_NEAREST_SAME_GRID)==0) {
      double dummy=0;
      double olat=1.e10;
      ilat=0,ilon=0;
      long n=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=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=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);
      while(grib_iterator_next(iter,&lat,&lon,&dummy)) {
        if (olat!=lat) {self->lats[ilat++]=lat;olat=lat;}
        self->lons[ilon++]=lon;
      }
      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;
    int plsize=0;

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

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

    plsize=self->lats_count;
    pl=(double*)grib_context_malloc(h->context,plsize*sizeof(double));
    if (!pl) return GRIB_OUT_OF_MEMORY;
    if( (ret=grib_get_double_array(h,"pl",pl,&plsize))!= GRIB_SUCCESS)
       return ret;

    nlon=0;
    for (jj=0;jj<self->j[0];jj++) {
      nlon+=pl[jj];
    }
    lons=self->lons+nlon;
    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];
    }
    lons=self->lons+nlon;
    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 (ii=0;ii<2;ii++) {
      for (jj=0;jj<2;jj++) {
        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,pl);
  }

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

  return GRIB_SUCCESS;
}
Exemplo n.º 20
0
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h) {
  int err=0;
  double *lat=0,*lon=0,*val=0;
  double missing_value=9999.;
  int skip_missing=1;
  char *kmiss=NULL, *p=NULL;
  char *missing_string=NULL;
  int i=0;
  grib_values* values=NULL;
  grib_iterator* iter = NULL;
  char* format=NULL;
  char* default_format="%.10e";
  int print_keys=grib_options_on("p:");
  long numberOfPoints=0;
  double *data_values=0,*lats=0,*lons=0;
  int n = 0;
  size_t size=0;

  if (grib_options_on("F:"))
    format=grib_options_get_option("F:");
  else
    format=default_format;

  if ((err=grib_get_long(h,"numberOfPoints",&numberOfPoints)) !=GRIB_SUCCESS) {
    fprintf(dump_file,"ERROR: unable to get number of points\n");
    return err;
  }

  iter=grib_iterator_new(h,0,&err);

  data_values=(double*)calloc(numberOfPoints+1,sizeof(double));

  if (iter) {
    lats=(double*)calloc(numberOfPoints+1,sizeof(double));
    lons=(double*)calloc(numberOfPoints+1,sizeof(double));
    lat=lats; lon=lons; val=data_values;
    while(grib_iterator_next(iter,lat++,lon++,val++)) {}
  } else if (err==GRIB_NOT_IMPLEMENTED || err==GRIB_SUCCESS){
    size=numberOfPoints;
    grib_get_double_array(h,"values",data_values,&size);
    if (size!=numberOfPoints) {
      if (!grib_options_on("q"))
       fprintf(dump_file,"ERROR: wrong number of points %d\n",(int)numberOfPoints);
      if (grib_options_on("f")) exit(1);
    }
  } else {
    grib_context_log(h->context,GRIB_LOG_ERROR,
                           "%s",grib_get_error_message(err));
    exit(err);
  }

  skip_missing=1;
  if (grib_options_on("m:")) {
    char* end=0;
    double mval=0;
    skip_missing=0;
    kmiss=grib_options_get_option("m:");
    p=kmiss;
    while (*p != ':' && *p != '\0') p++;
    if (*p == ':' && *(p+1) != '\0') {
      *p='\0';
      missing_string=strdup(p+1);
    } else {
      missing_string=strdup(kmiss);
    }
    mval=strtod(kmiss,&end);
    if (end==NULL) missing_value=mval;
    grib_set_double(h,"missingValue",missing_value);
  }

  if (iter)
    fprintf(dump_file,"Latitude, Longitude, ");

  fprintf(dump_file,"Value");

  if (print_keys)
    for (i=0;i<options->print_keys_count; i++)
      fprintf(dump_file,", %s",options->print_keys[i].name);

  fprintf(dump_file,"\n");

  if (print_keys)
    values=get_key_values(options,h);

  if (skip_missing==0){
    for (i=0;i<numberOfPoints;i++) {
      if (iter) fprintf(dump_file,"%9.3f%9.3f ",lats[i],lons[i]);

      if (data_values[i] == missing_value)
        fprintf(dump_file,"%s",missing_string);
      else
        fprintf(dump_file,format,data_values[i]);

      if (print_keys)
        print_key_values(values,options->print_keys_count);
      fprintf(dump_file,"\n");
      n++;
    }

  } else if ( skip_missing==1 ){
    for (i=0;i<numberOfPoints;i++) {
      if (data_values[i] != missing_value){
        if (iter) fprintf(dump_file,"%9.3f%9.3f ",lats[i],lons[i]);
        fprintf(dump_file,format,data_values[i]);
        if (print_keys)
          print_key_values(values,options->print_keys_count);
        fprintf(dump_file,"\n");
        n++;
      }
    }
  }

  if (iter) grib_iterator_delete(iter);

  free(data_values);
  if (iter) {
    free(lats);
    free(lons);
  }

  return 0;
}
Exemplo n.º 21
0
int main(int argc, char** argv)
{
  int err = 0;
  size_t size=0;

  FILE* in = NULL;
  char* infile = "../../data/regular_latlon_surface.grib1";
  FILE* out = NULL;
  char* outfile = "out.grib1";
  grib_handle *h = NULL;
  const void* buffer = NULL;
  size_t values_len;
  double* values;
  double missing=9999;
  int i=0;

  in = fopen(infile,"r");
  if(!in) {
    printf("ERROR: unable to open input file %s\n",infile);
    return 1;
  }

  out = fopen(outfile,"w");
  if(!out) {
    printf("ERROR: unable to open output file %s\n",outfile);
        fclose(in);
    return 1;
  }

  h = grib_handle_new_from_file(0,in,&err);
  if (h == NULL) {
    printf("Error: unable to create handle from file %s\n",infile);
  }

  GRIB_CHECK(grib_set_double(h,"missingValue",missing),0);

  /* get the size of the values array*/
  GRIB_CHECK(grib_get_size(h,"values",&values_len),0);

  values = (double*)malloc(values_len*sizeof(double));

  /* get data values*/
  GRIB_CHECK(grib_get_double_array(h,"values",values,&values_len),0);

  GRIB_CHECK(grib_set_long(h,"bitmapPresent",1),0);

  for(i = 0; i < 10; i++)
    values[i]=missing;

  GRIB_CHECK(grib_set_double_array(h,"values",values,values_len),0);

  /* get the coded message in a buffer */
  GRIB_CHECK(grib_get_message(h,&buffer,&size),0);

  /* write the buffer in a file*/
  if(fwrite(buffer,1,size,out) != size)
  {
     perror(outfile);
     exit(1);
  }

  /* delete handle */
  grib_handle_delete(h);

  fclose(in);
  fclose(out);

  return 0;
}
Exemplo n.º 22
0
int main(int argc, char** argv) {
  int err = 0;
  size_t values_len=0;
  double *values = NULL;
  char error_msg[100] ;
 
  size_t slong = sizeof(long) * 8 ;

  int i;

  FILE* in = NULL;
  char* filename ;

  grib_handle *h = NULL;

  if (argc<2) usage(argv[0]);
  filename=argv[1];

  in = fopen(filename,"r");
  if(!in) {
    printf("ERROR: unable to open file %s\n",filename);
    return 1;
  }

  /* create new handle from a message in a file*/
  h = grib_handle_new_from_file(0,in,&err);
  if (h == NULL) {
    printf("Error: unable to create handle from file %s\n",filename);
  }

  for (i=0;i<255;i++) {

    /* get the size of the values array*/
    GRIB_CHECK(grib_get_size(h,"values",&values_len),0);

    values = malloc(values_len*sizeof(double));

    err = grib_get_double_array(h,"values",values,&values_len);

    GRIB_CHECK(grib_set_long(h,"bitsPerValue",i),0);

    err = grib_set_double_array(h,"values",values,values_len);

    free(values);
    values = 0;

    /*
     * check if encoding of the data fails when the number of bpv
     * is not supported; bpv allowed on decoding is bpv < size of long
     */

    if (i < slong && err == 0) {
      /* do nothing */
    } else if (i >= slong  && err == GRIB_INVALID_BPV) {
      /* do nothing  */
    } else {
      sprintf(error_msg,"Error decoding when bpv=%d. Error message:%s",i,grib_get_error_message(err));
      perror(error_msg);
      exit(1);
    }

    values = malloc(values_len*sizeof(double));
    err = grib_get_double_array(h,"values",values,&values_len);

    /*
     * check if decoding of the data fails when the number of bpv
     * is not supported; bpv allowed on decoding is bpv <= size of long
     */

    if (i <= slong && err == 0) {
      /* do nothing */
    } else if (i > slong  && err == GRIB_INVALID_BPV) {
      /* do nothing  */
    } else {
      sprintf(error_msg,"Error decoding when bpv=%d. Error message:%s",i,grib_get_error_message(err));
      perror(error_msg);
      exit(1);
    }

    free(values);
    values = 0;
  }

  grib_handle_delete(h);
  h=0;

  fclose(in);

  return 0;
}
Exemplo n.º 23
0
static void compare_handles(int n,grib_handle* h1,grib_handle* h2,FILE* out)
{
    size_t len1 = 0;
    size_t len2 = 0;

    double *dval1 = NULL, *dval2 = NULL, *dval3 = NULL;
    double maxe = 0,mine = 0;
    double maxa = 0,mina = 0;
    int i,maxi = 0,maxai = 0;
    double lat,lon;

    GRIB_CHECK(grib_get_size(h1,"values",&len1),NULL);
    GRIB_CHECK(grib_get_size(h2,"values",&len2),NULL);

    if(len1 != len2)
    {
        printf("Field size mismatch %ld != %ld\n",(long)len1,(long)len2);
        exit(1);
    }

    dval1 = (double*)grib_context_malloc(h1->context,len1*sizeof(double));
    Assert(dval1);
    dval2 = (double*)grib_context_malloc(h2->context,len2*sizeof(double));
    Assert(dval2);
    if(out)
        dval3 = (double*)grib_context_malloc(h2->context,len2*sizeof(double));
    Assert(dval2);

    GRIB_CHECK(grib_get_double_array(h1,"values",dval1,&len1),NULL);
    GRIB_CHECK(grib_get_double_array(h2,"values",dval2,&len2),NULL);


    for(i = 0; i < len1; i++)
    {
        double e = err(dval1[i],dval2[i]);
        double a = fabs(dval1[i]-dval2[i]);
        if(i == 0)  maxe = mine = e;
        if(i == 0)  maxa = mina = a;
        if(out) dval3[i] = absolute ? a : e;
        if(e < mine) mine = e;
        if(e > maxe) {
            maxe = e;
            maxi = i;
        }
        if(a < mina) mina = a;
        if(a > maxa) {
            maxa = a;
            maxai = i;
        }
    }

    if(out)
    {
        const void *buffer;
        size_t size;
        GRIB_CHECK(grib_set_long(h1,"generatingProcessIdentifier",255),NULL); /* To prevent Magics to scale the field */
        GRIB_CHECK(grib_set_long(h1,"numberOfBitsContainingEachPackedValue",24),NULL);
        GRIB_CHECK(grib_set_double_array(h1,"values",dval3,len1),NULL);
        GRIB_CHECK(grib_get_message(h1,&buffer,&size),NULL);

        if(fwrite(buffer,1,size,out) != size)
        {
            perror(outname);
            exit(1);
        }

    }

    printf("Field %ld: min relative error: %g%%, max relative error: %g%% [%g,%g,%g]\n",(long)n,mine*100,maxe*100,
           dval1[maxi],dval2[maxi],fabs(dval1[maxi]-dval2[maxi]));

    if(location(h1,maxi,&lat,&lon))
        printf("   latitude = %g, longitude = %g\n",lat,lon);

    printf("Field %ld: min absolute error: %g, max absolute error: %g [%g,%g]\n",(long)n,mina,maxa,
           dval1[maxai],dval2[maxai]);
    if(location(h1,maxai,&lat,&lon))
        printf("   latitude = %g, longitude = %g\n",lat,lon);


    grib_context_free(h1->context,dval1);
    grib_context_free(h2->context,dval2);
    grib_context_free(h2->context,dval3);
}
Exemplo n.º 24
0
int main(int argc, char* argv[]) {
  grib_handle *h=NULL;
  grib_context* c=NULL;
  FILE* fin=NULL;
  double *values=NULL;
  char buffer[BUFF_SIZE]={0,};
  char buf[BUFF_SIZE]={0,};
  size_t length;
  int sec4len;
  flong ksec0[ISECTION_0];
  flong ksec1[ISECTION_1];
  flong ksec2[ISECTION_2];
  flong ksec3[ISECTION_3];
  flong ksec4[ISECTION_4];
  flong miss=0;
  const void *msg;
  flong gribex_msg_len=0;
  double rsec2[RSECTION_2];
  double rsec3[RSECTION_3];
  size_t nvalues=0;
  double* gvalues;
  FILE* fout;
  int one=1;

  char finname[]="sh.grib";

  fin = fopen(finname,"r");
  if(!fin) {perror(finname);exit(1);}

  c=grib_context_get_default();

  length=BUFF_SIZE;
  GRIB_CHECK(grib_read_any_from_file(c,fin,buffer,&length),0);
  fclose(fin);

  h=grib_handle_new_from_message_copy(c,buffer,length);

  GRIB_CHECK(grib_get_size(h,"values",&nvalues),0);
  values=(double*)grib_context_malloc_clear(c,sizeof(double)*nvalues);
  gvalues=(double*)grib_context_malloc_clear(c,sizeof(double)*nvalues);
  if (!values) { printf("%s: memory allocation error\n",argv[0]); exit(1); }
  if (!gvalues) { printf("%s: memory allocation error\n",argv[0]); exit(1); }

  GRIB_CHECK(grib_get_double_array(h,"values",values,&nvalues),0);
  GRIB_CHECK(grib_set_long(h,"computeLaplacianOperator",1),0);
  GRIB_CHECK(grib_set_double_array(h,"values",values,nvalues),0);
  grib_write_message(h,"out_grib_api.grib","w");

  sec4len=nvalues+100000;

  /* decode with gribex*/
  msg=(char*)buffer;
  gribex_msg_len=length;
  sec4len=nvalues+100000;
  gribex_check(cgribex(  miss, ksec0,ksec1,ksec2,rsec2,ksec3,rsec3,
                      ksec4,gvalues,sec4len, (char*)msg,&gribex_msg_len,"D")); 

  /* encode values with gribex*/
  gribex_msg_len=BUFF_SIZE;
  grsmkp_(&one);
  gribex_check(cgribex(  miss, ksec0,ksec1,ksec2,rsec2,ksec3,rsec3,
                      ksec4,gvalues,nvalues, buf,&gribex_msg_len,"C"));


  fout=fopen("out_gribex.grib","w");
	if (!fout) {perror("out_gribex.grib");exit(1);}
	if (fwrite(buf,1,gribex_msg_len,fout)!=gribex_msg_len) {
		perror("out_gribex.grib"); exit(1);
	}
	fclose(fout);

  return 0;
}
Exemplo n.º 25
0
int dump_values(FILE* out,grib_handle* h,const char *name,int missingOK)
{
	size_t len = 0;
	int err;
	int type;

	char *sval = NULL;
	unsigned char *uval = NULL; 
	double *dval = NULL;
	long *lval = NULL;


	int i;

	if((err = grib_get_type(h,name,&type)) != GRIB_SUCCESS)
	{
		printf("# Oops... cannot get type of [%s]: %s\n",name,
				grib_get_error_message(err));
		return err;
	}

	if((err = grib_get_size(h,name,&len)) != GRIB_SUCCESS)
	{
		printf("# Oops... cannot get size of [%s]: %s\n",name,
				grib_get_error_message(err));
		return err;
	}

	switch(type)
	{
		case GRIB_TYPE_STRING:

			sval = grib_context_malloc(h->context,len*sizeof(char));

			if((err = grib_get_string(h,name,sval,&len)) != GRIB_SUCCESS)
			{
				printf("# Oops... cannot get string value of [%s]: %s\n",
						name,grib_get_error_message(err));
			}
			else
			{
				fprintf(out,"%s='%s';\n",name,sval);
			}

			grib_context_free(h->context,sval);

			if(err) return err;

			break;

		case GRIB_TYPE_LONG:
			lval = grib_context_malloc(h->context,len*sizeof(long));

			if((err = grib_get_long_array(h,name,lval,&len)) != GRIB_SUCCESS)
			{
				printf("# Oops... cannot get long value of [%s]: %s\n",
						name,grib_get_error_message(err));
			}
			else
			{
				if(len == 1)
				{
					if(missingOK && lval[0] == GRIB_MISSING_LONG)
						fprintf(out,"%s = missing;\n",name);
					else
						fprintf(out,"%s = %ld;\n",name,lval[0]);
				}
				else {
					fprintf(out,"%s = {", name);
					if(len >= 10) fprintf(out," # %ld\n",(long)len);
					for(i = 0; i < len; i++){
						fprintf(out,"%ld, ",lval[i]);
						if((i+1)%10 == 0)
							fprintf(out,"\n");
					}
					fprintf(out,"};\n");
				}
			}

			grib_context_free(h->context,lval);

			if(err) return err;
			break;

		case GRIB_TYPE_DOUBLE:
			dval = grib_context_malloc(h->context,len*sizeof(double));

			if((err = grib_get_double_array(h,name,dval,&len)) != GRIB_SUCCESS)
			{
				printf("# Oops... cannot get double value of [%s]: %s\n",
						name,grib_get_error_message(err));
			}
			else
			{
				if(len == 1)
				{
					if(missingOK && dval[0] == GRIB_MISSING_DOUBLE)
						fprintf(out,"%s = missing;\n",name);
					else {
						fprintf(out,"%s = %g;\n",name,dval[0]);
					}
				}
				else {
					fprintf(out,"%s = { \n",name);
					if(len >= 10) fprintf(out," # %ld\n",(long)len);
					for(i = 0; i < len; i++)
					{
						fprintf(out,"%f, ",dval[i]);
						if((i+1)%10 == 0)
							fprintf(out,"\n");
					}
					fprintf(out,"};\n");
				}
			}

			grib_context_free(h->context,dval);

			if(err) return err;
			break;

		case GRIB_TYPE_BYTES:
			uval = grib_context_malloc(h->context,len*sizeof(unsigned char));

			if((err = grib_get_bytes(h,name,uval,&len)) != GRIB_SUCCESS)
			{
				printf("# Oops... cannot get bytes value of [%s]: %s\n",
						name,grib_get_error_message(err));
			}
			else
			{
				fprintf(out,"%s = { \n",name);
					if(len >= 10) fprintf(out," # %ld\n",(long)len);
					for(i = 0; i < len; i++)
					{
						fprintf(out,"%02x, ",uval[i]);
						if((i+1)%10 == 0)
							fprintf(out,"\n");
					}
					fprintf(out,"};\n");

			}

			grib_context_free(h->context,uval);

			if(err) return err;

			break;

		case GRIB_TYPE_LABEL:
			break;

		case GRIB_TYPE_SECTION:
			break;

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

	return GRIB_SUCCESS;

}