Ejemplo n.º 1
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;
}
Ejemplo n.º 2
0
int get_box(grib_handle *h,double *east,double *north,double *west,double *south) {
	int ret=0;
	long iScansNegatively,jScansPositively;

	ret=grib_get_long(h,"iScansNegatively",&iScansNegatively);
	if (ret) return ret;
	ret=grib_get_long(h,"jScansPositively",&jScansPositively);
	if (ret) return ret;
	if (iScansNegatively) {
		grib_get_double(h,"longitudeOfFirstGridPointInDegrees",west);
		grib_get_double(h,"longitudeOfLastGridPointInDegrees",east);
	} else {
		grib_get_double(h,"longitudeOfFirstGridPointInDegrees",east);
		grib_get_double(h,"longitudeOfLastGridPointInDegrees",west);
	}
	if (jScansPositively) {
		grib_get_double(h,"latitudeOfFirstGridPointInDegrees",south);
		grib_get_double(h,"latitudeOfLastGridPointInDegrees",north);
	} else {
		grib_get_double(h,"latitudeOfFirstGridPointInDegrees",north);
		grib_get_double(h,"latitudeOfLastGridPointInDegrees",south);
	}
	if (*east>*west) *east-=360;
	return ret;
}
Ejemplo n.º 3
0
int grib_fieldset_column_copy_from_handle(grib_handle* h,grib_fieldset* set,int i) {
  int err=0;
  long lval=0;
  double dval=0;
  char sval[1024];
  size_t slen=1024;
  if (!set || !h || set->columns[i].type == 0)
    return GRIB_INVALID_ARGUMENT;

  if (set->columns[i].size >= set->columns[i].values_array_size)
    grib_fieldset_columns_resize(set,set->columns[i].values_array_size+GRIB_ARRAY_INCREMENT);

  switch (set->columns[i].type) {
    case GRIB_TYPE_LONG:
      err=grib_get_long(h,set->columns[i].name,&lval);
      set->columns[i].long_values[set->columns[i].size]=lval;
      break;
    case GRIB_TYPE_DOUBLE:
      err=grib_get_double(h,set->columns[i].name,&dval);
      set->columns[i].double_values[set->columns[i].size]=dval;
      break;
    case GRIB_TYPE_STRING:
      err=grib_get_string(h,set->columns[i].name,sval,&slen);
      set->columns[i].string_values[set->columns[i].size]=grib_context_strdup(h->context,sval);
      break;
  }

  set->columns[i].errors[set->columns[i].size]=err;
  set->columns[i].size++;

  return err;
}
Ejemplo n.º 4
0
double dget(grib_handle *h,const char* what)
{
	int e; double val;
	if((e = grib_get_double(h,what,&val)) != GRIB_SUCCESS)
	{
		fprintf(stderr,"%s, field %d [%s]: cannot get %s: %s\n",file,field,param,what,grib_get_error_message(e));
		exit(1);
		val = -1;
	}
	return val;
}
Ejemplo n.º 5
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;

}
Ejemplo n.º 6
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("}");
}
Ejemplo n.º 7
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;
}
Ejemplo n.º 8
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;
}
static int pack_double(grib_accessor* a, const double* cval, size_t *len)
{
    grib_accessor_data_g1simple_packing* self =  (grib_accessor_data_g1simple_packing*)a;
    grib_accessor_class* super = *(a->cclass->super);

    size_t n_vals = *len;
    long half_byte = 0;
    int ret = 0;
    long offsetdata = 0;
    long offsetsection = 0;
    double reference_value = 0;
    long   binary_scale_factor = 0;
    long   bits_per_value = 0;
    long   decimal_scale_factor = 0;
    double decimal = 1;
    size_t buflen = 0;
    unsigned char*  buf = NULL;
    unsigned char*  encoded = NULL;
    double divisor = 1;
    int i;
    long off = 0;
    grib_context* c=a->parent->h->context;
    grib_handle* h=a->parent->h;
    char* ieee_packing_s=NULL;
    char* packingType_s=NULL;
    char* precision_s=NULL;
    double units_factor=1.0;
    double units_bias=0.0;
    double* val=(double*)cval;
    double missingValue=9999.0;
    long constantFieldHalfByte=0;
    int err=0;

    if(*len != 0) {
        if(self->units_factor &&
                (grib_get_double_internal(a->parent->h,self->units_factor,&units_factor)== GRIB_SUCCESS)) {
            grib_set_double_internal(a->parent->h,self->units_factor,1.0);
        }

        if(self->units_bias &&
                (grib_get_double_internal(a->parent->h,self->units_bias,&units_bias)== GRIB_SUCCESS)) {
            grib_set_double_internal(a->parent->h,self->units_bias,0.0);
        }

        if (units_factor != 1.0) {
            if (units_bias != 0.0)
                for (i=0;i<n_vals;i++) val[i]=val[i]*units_factor+units_bias;
            else
                for (i=0;i<n_vals;i++) val[i]*=units_factor;
        } else if (units_bias != 0.0)
            for (i=0;i<n_vals;i++) val[i]+=units_bias;

        if (c->ieee_packing && self->ieee_packing) {
            long precision=c->ieee_packing==32 ? 1 : 2;
            size_t lenstr=strlen(self->ieee_packing);

            packingType_s=grib_context_strdup(c,self->packingType);
            ieee_packing_s=grib_context_strdup(c,self->ieee_packing);
            precision_s=grib_context_strdup(c,self->precision);

            grib_set_string(h,packingType_s,ieee_packing_s,&lenstr);
            grib_set_long(h,precision_s,precision);

            grib_context_free(c,packingType_s);
            grib_context_free(c,ieee_packing_s);
            grib_context_free(c,precision_s);
            return grib_set_double_array(h,"values",val,*len);
        }
    }

    ret = super->pack_double(a,val,len);
    switch (ret) {
    case GRIB_CONSTANT_FIELD:
        ret=grib_get_long(a->parent->h,"constantFieldHalfByte",&constantFieldHalfByte);
        if (ret) constantFieldHalfByte=0;
        if((ret = grib_set_long_internal(a->parent->h,self->half_byte, constantFieldHalfByte))
                != GRIB_SUCCESS)
            return ret;
        grib_buffer_replace(a, NULL, 0,1,1);
        return GRIB_SUCCESS;
        break;
    case GRIB_NO_VALUES:
        ret=grib_get_long(a->parent->h,"constantFieldHalfByte",&constantFieldHalfByte);
        if (ret) constantFieldHalfByte=0;
        /* TODO move to def file */
        grib_get_double(a->parent->h,"missingValue", &missingValue);
        if((err = grib_set_double_internal(a->parent->h,self->reference_value, missingValue)) !=
                GRIB_SUCCESS)
            return err;
        if((ret = grib_set_long_internal(a->parent->h,self->binary_scale_factor, binary_scale_factor))
                != GRIB_SUCCESS)
            return ret;
        if((ret = grib_set_long_internal(a->parent->h,self->half_byte, constantFieldHalfByte))
                != GRIB_SUCCESS)
            return ret;
        grib_buffer_replace(a, NULL, 0,1,1);
        return GRIB_SUCCESS;
        break;
    case GRIB_INVALID_BPV:
        grib_context_log(a->parent->h->context,GRIB_LOG_ERROR,"unable to compute packing parameters\n");
        return ret;
    case GRIB_SUCCESS:
        break;
    default:
        grib_context_log(a->parent->h->context,GRIB_LOG_FATAL,"unable to compute packing parameters\n");
        return ret;
    }

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

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

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

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

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

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

    decimal = grib_power(decimal_scale_factor,10) ;
    divisor = grib_power(-binary_scale_factor,2);

    buflen = (((bits_per_value*n_vals)+7)/8)*sizeof(unsigned char);
    if((buflen + (offsetdata-offsetsection)) %2) {
        buflen++;
        /*
    a->length++;
    a->parent->h->buffer->ulength++;
         */
    }
    half_byte = (buflen*8)-((*len)*bits_per_value);
    grib_context_log(a->parent->h->context,GRIB_LOG_DEBUG,
            "HALF byte: buflen=%d bits_per_value=%ld len=%d half_byte=%ld\n",
            buflen,bits_per_value,*len,half_byte);

    Assert(half_byte <= 0x0f);

    if((ret = grib_set_long_internal(a->parent->h,self->half_byte, half_byte))
            != GRIB_SUCCESS)
        return ret;

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

    grib_encode_double_array(n_vals,val,bits_per_value,reference_value,decimal,divisor,encoded,&off);

    grib_context_log(a->parent->h->context, GRIB_LOG_DEBUG,
            "grib_accessor_data_g1simple_packing : pack_double : packing %s, %d values", a->name, n_vals);

    grib_buffer_replace(a, buf, buflen,1,1);

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

    return GRIB_SUCCESS;
}
Ejemplo n.º 10
0
int codes_get_double(grib_handle* h, const char* key, double* value)
{
    return grib_get_double(h,key,value);
}
static int find(grib_nearest* nearest, grib_handle* h,
                double inlat, double inlon,unsigned long flags,
                double* outlats,double* outlons, double *values,
                double *distances,int *indexes, size_t *len) {
  grib_nearest_latlon_reduced* self = (grib_nearest_latlon_reduced*) nearest;
  int ret=0,kk=0,ii=0,jj=0;
  int j=0;
  long* pla=NULL;
  long* pl=NULL;
  size_t nvalues=0;
  grib_iterator* iter=NULL;
  double lat=0,lon=0;
  long iradius;
  double radius;
  int ilat=0,ilon=0;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    lons=self->lons+nlon;

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

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

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

    lons=self->lons+nlon;

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

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

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

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

    grib_context_free(h->context,pla);
  }

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

  return GRIB_SUCCESS;
}
static int find(grib_nearest* nearest, grib_handle* h,
                double inlat, double inlon,unsigned long flags,
                double* outlats,double* outlons, double *values,
                double *distances,int *indexes, size_t *len) {
  grib_nearest_reduced* self = (grib_nearest_reduced*) nearest;
  int ret=0,kk=0,ii=0,jj=0;
  long* pla=NULL;
  long* pl=NULL;
  size_t nvalues=0;
  grib_iterator* iter=NULL;
  double lat=0,lon=0;
  long iradius;
  double radius;
  int ilat=0,ilon=0;


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

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

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

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

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

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

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

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

    iter=grib_iterator_new(h,0,&ret);
    while(grib_iterator_next(iter,&lat,&lon,&dummy)) {
		if (olat!=lat) {
			self->lats[ilat++]=lat;
			olat=lat;
		}
      self->lons[ilon++]=lon;
    }
	self->lats_count=ilat;
	grib_iterator_delete(iter);
	
  }
  nearest->h=h;

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

	/*TODO global from the def file*/
	global=1;
	grib_get_long(h,"global",&global);

	if (!global) {
		/*TODO longitudeOfFirstGridPointInDegrees from the def file*/
		if ((ret=grib_get_double(h,"longitudeOfFirstGridPointInDegrees",&lon_first))!=GRIB_SUCCESS) {
			grib_context_log(h->context,GRIB_LOG_ERROR,
				"grib_nearest_reduced.find(): unable to get longitudeOfFirstGridPointInDegrees %s\n",
				grib_get_error_message(ret));
			return ret;
		}
		/*TODO longitudeOfLastGridPointInDegrees from the def file*/
		if ((ret=grib_get_double(h,"longitudeOfLastGridPointInDegrees",&lon_last))!=GRIB_SUCCESS) {
			grib_context_log(h->context,GRIB_LOG_ERROR,
				"grib_nearest_reduced.find(): unable to get longitudeOfLastGridPointInDegrees %s\n",
				grib_get_error_message(ret));
			return ret;
		}
		/* if (lon_last<0) lon_last+=360; */
		/* if (lon_first<0) lon_first+=360; */
	} else {
	  while (inlon<0) inlon+=360;
	  while (inlon>360) inlon-=360;
	}

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

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

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

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

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

	nlon=0;
	if (global) {
		for (jj=0;jj<self->j[0];jj++) nlon+=pl[jj];
		nplm1=pl[self->j[0]]-1;
	} else {
		nlon=0;
		for (jj=0;jj<self->j[0];jj++) {
	    	row_count=0;ilon_first=0;ilon_last=0;
	       	grib_get_reduced_row(pl[jj],lon_first,lon_last,&row_count,&ilon_first,&ilon_last);
		   	nlon+=row_count;
		}
	    row_count=0;ilon_first=0;ilon_last=0;
	    grib_get_reduced_row(pl[self->j[0]],lon_first,lon_last,&row_count,&ilon_first,&ilon_last);
		nplm1=row_count-1;
	}
    lons=self->lons+nlon;

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

    if (!nearest_lons_found) {
	  if (!global) {
		  row_count=0;ilon_first=0;ilon_last=0;
		  grib_get_reduced_row(pl[self->j[0]],lon_first,lon_last,&row_count,&ilon_first,&ilon_last);
	  } else {
		  row_count=pl[self->j[0]];
	  }

      grib_binary_search(lons,row_count-1,inlon,
                                &(self->k[0]),&(self->k[1]));
	}
    self->k[0]+=nlon;
    self->k[1]+=nlon;

	nlon=0;
	if (global) {
		for (jj=0;jj<self->j[1];jj++) nlon+=pl[jj];
		nplm1=pl[self->j[1]]-1;
	} else {
	    long row_count,ilon_first,ilon_last;
		for (jj=0;jj<self->j[1];jj++) {
	    	row_count=0;ilon_first=0;ilon_last=0;
	       	grib_get_reduced_row(pl[jj],lon_first,lon_last,&row_count,&ilon_first,&ilon_last);
		   	nlon+=row_count;
		}
	    row_count=0;ilon_first=0;ilon_last=0;
	    grib_get_reduced_row(pl[self->j[1]],lon_first,lon_last,&nplm1,&ilon_first,&ilon_last);
		nplm1--;
	}
    lons=self->lons+nlon;

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

    if (!nearest_lons_found) {
	  if (!global) {
		  row_count=0;ilon_first=0;ilon_last=0;
		  grib_get_reduced_row(pl[self->j[1]],lon_first,lon_last,&row_count,&ilon_first,&ilon_last);
	  } else {
		  row_count=pl[self->j[1]];
	  }

      grib_binary_search(lons,row_count-1,inlon,
                                &(self->k[2]),&(self->k[3]));
	}

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

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

    grib_context_free(h->context,pla);
  }

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

  return GRIB_SUCCESS;
}
Ejemplo n.º 13
0
int main(int argc, char* argv[]) {
	grib_handle *h1,*h2;
	int ret=0;
	FILE *f1,*f2;
	char* infile1;
	char* infile2;
	double *v1,*v2,*v,*gv;
	double *lon1,*lon2,*lon,*glon;
	double *lat1,*lat2,*lat,*glat;
	size_t size1,size2,size,gsize;
	double err1,err2,err;
	int i,j;
	grib_context* c;
	grib_iterator *iter1,*iter2;

	c=grib_context_get_default();

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

	infile1=argv[1];
	infile2=argv[2];

	f1=fopen(infile1,"r");
	if (!f1) {
		perror(infile1);
		exit(1);
	}

	f2=fopen(infile2,"r");
	if (!f2) {
		perror(infile2);
		exit(1);
	}

	while ((h1=grib_handle_new_from_file(0,f1,&ret))!=NULL) {
		if ((h2=grib_handle_new_from_file(c,f2,&ret))==NULL) {
			printf("unable to create handle from file %s\n",infile2);
			GRIB_CHECK(ret,0);
			exit(1);
		}
		GRIB_CHECK(grib_get_size(h1,"values",&size1),0);
		v1=malloc(size1*sizeof(double));
		if (!v1) {printf("unable to allocate v1\n");exit(1);}
		lat1=malloc(size1*sizeof(double));
		if (!lat1) {printf("unable to allocate lat1\n");exit(1);}
		lon1=malloc(size1*sizeof(double));
		if (!lon1) {printf("unable to allocate lon1\n");exit(1);}
		GRIB_CHECK(grib_get_double(h1,"packingError",&err1),0);

		iter1=grib_iterator_new(h1,0,&ret);
		GRIB_CHECK(ret,0);

		GRIB_CHECK(grib_get_size(h2,"values",&size2),0);
		v2=malloc(size2*sizeof(double));
		if (!v2) {printf("unable to allocate v2\n");exit(1);}
		lat2=malloc(size2*sizeof(double));
		if (!lat2) {printf("unable to allocate lat2\n");exit(1);}
		lon2=malloc(size2*sizeof(double));
		if (!lon2) {printf("unable to allocate lon2\n");exit(1);}
		GRIB_CHECK(grib_get_double(h2,"packingError",&err2),0);

		iter2=grib_iterator_new(h2,0,&ret);
		GRIB_CHECK(ret,0);

		lat=lat1; lon=lon1; v=v1;
		while(grib_iterator_next(iter1,lat,lon,v)) {
				lat++;
				if (*lon < 0 ) *lon+=360;
				lon++;
				v++;
			}
		lat=lat2; lon=lon2; v=v2;
		while(grib_iterator_next(iter2,lat,lon,v)) {
				lat++;
				if (*lon < 0 ) *lon+=360;
				lon++;
				v++;
			}

		if (size1 > size2) {
			lat=lat2;lon=lon2;v=v2;
			size=size2;
			glat=lat1;glon=lon1;gv=v1;
			gsize=size1;
		} else {
			lat=lat1;lon=lon1;v=v1;
			size=size1;
			glat=lat2;glon=lon2;gv=v2;
			gsize=size2;
		}
		if (err1>err2) err=err1;
		else err=err2;

		j=0;
		for (i=0;i<size;i++) {
			while (j < gsize && ( lat[i]!=glat[j] || lon[i]!=glon[j] ) ) j++;
			if (j == gsize) {
				j=0;
				while (j < gsize && ( lat[i]!=glat[j] || lon[i]!=glon[j] ) ) j++;
			}
			if (j==gsize) {
				printf("lat=%g lon=%g not found in global\n",lat[i],lon[i]);
				exit(1);
			}
			if (fabs(v[i]-gv[j])>err) {
				ret=1;
				printf("lat=%g lon=%g  sub area value=%g global value=%g\n",
					lat[i],lon[i],v[i],gv[j]);
			}

		}
		free(v);free(gv);free(lat);free(glat);free(lon);free(glon);

	}

	fclose(f1);
	fclose(f2);

	return ret;
}
Ejemplo n.º 14
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=0;
  int i=0;

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

  out = fopen(outfile,"w");
  if(!in) {
    printf("ERROR: unable to open file %s\n",outfile);
    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_get_double(h,"missingValue",&missing),0);

  /* 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);

  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;
}
Ejemplo n.º 15
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;
}
Ejemplo n.º 16
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;
}
static int    unpack_double   (grib_accessor* a, double* val, size_t *len)
{
  grib_accessor_statistics* self = (grib_accessor_statistics*)a;
  int ret = 0,i=0;
  double* values;
  size_t size=0;
  double max,min,avg,sd,value,skew,kurt,x;
  double missing=0;
  long number_of_missing=0;
  grib_context* c=a->parent->h->context;
  grib_handle* h=a->parent->h;

  if (!a->dirty) return GRIB_SUCCESS;

  if ( (ret=grib_get_size(h,self->values,&size)) != GRIB_SUCCESS) return ret;

  grib_context_log(a->parent->h->context,GRIB_LOG_DEBUG,
    "grib_accessor_statistics: computing statistics for %d values",size);

  if((ret=grib_get_double(a->parent->h,self->missing_value,&missing))
       != GRIB_SUCCESS) return ret;

  values=(double*)grib_context_malloc_clear(c,size*sizeof(double));
  if (!values) return GRIB_OUT_OF_MEMORY;

  if((ret = grib_get_double_array_internal(h,self->values,values,&size))
       != GRIB_SUCCESS) {
        grib_context_free(c,values);
        return ret;
  }

  number_of_missing=0;
  i=0;
  while (i<size && values[i] == missing ) {i++;number_of_missing++;}
  max=values[i];
  min=values[i];
  avg=values[i];
  for (i=number_of_missing+1;i<size;i++) {
    value=values[i];
    if (value > max && value != missing) max=value; 
    if (value < min && value != missing) min=value; 
    if (value != missing) avg+=value;                      
    else number_of_missing++;
  }

  avg/=(size-number_of_missing);

  sd=0; skew=0; kurt=0;
  for (i=0;i<size;i++) {
    if (values[i] != missing) {
      x=(avg-values[i])*(avg-values[i]);
      sd+=x;
      x*=(avg-values[i]);
      skew+=x;
      kurt+=x*(avg-values[i]);
    }
  }

  kurt=kurt;
  if (size-number_of_missing!=0)
	  sd=sqrt(sd/(size-number_of_missing));
  skew=skew;
  
  a->dirty=0;

  grib_context_free(c,values);

  self->v[0]=max;
  self->v[1]=min;
  self->v[2]=avg;
  self->v[3]=number_of_missing;
  self->v[4]=sd;
  self->v[5]=skew;
  self->v[6]=kurt;
  self->v[7]= sd == 0 ? 1 : 0;

  for (i=0;i<self->number_of_elements;i++)
    val[i]=self->v[i];
  
  return ret;
}
Ejemplo n.º 18
0
Archivo: Grib.cpp Proyecto: WFRT/Comps
float InputGrib::getOffset(grib_handle* iH) {
   double offset;
   int err = grib_get_double(iH, "stepRange", &offset); 
   assert(err == 0);
   return (float) offset;
}