示例#1
0
static void compare_files(const char* file1,const char* file2, blacklist* bl)
{
  FILE *f1 = fopen(file1,"r");
  FILE *f2 = fopen(file2,"r");
  int e1 = 0;
  int e2 = 0;
  grib_handle *h1;
  grib_handle *h2;
  int count = 0;
  int err = 0;
  grib_context* c=grib_context_get_default( );

  grib_multi_support_off(c);

  if(!f1) {
    perror(file1);
    exit(1);
  }

  if(!f2) {
    perror(file2);
    exit(1);
  }

  h1 = grib_handle_new_from_file(c,f1,&e1);
  h2 = grib_handle_new_from_file(c,f2,&e2);

  while(h1 && h2)
  {
    ++count;

    printf("..............................\n");
    if(compare_handles(h1,h2,bl)) err++;
    printf("..............................\n");
    if(compare_handles(h2,h1,bl)) err++;

    grib_handle_delete(h1);
    grib_handle_delete(h2);

    e1 = e2 = 0;

    h1 = grib_handle_new_from_file(c,f1,&e1);
    h2 = grib_handle_new_from_file(c,f2,&e2);
  }

  GRIB_CHECK(e1,file1);
  GRIB_CHECK(e2,file2);

  if(h1 != h2)
  {
    fprintf(stderr,"%s: Premature eof on %s\n",prog,h1 == 0 ? file1 : file2);
    exit(1);
  }

  if(err) exit(1);

}
示例#2
0
int main(int argc,char* argv[]) {
    struct stat finfo;
    char shortName[20]={0,};
    size_t len=20;
    grib_handle* h=NULL;
    size_t fsize;
    unsigned char* data=NULL;
    unsigned char* p=NULL;
    void* pdata=NULL;
    int error=0;
    int count=0;
    char* filename=NULL;
    FILE* f=NULL;
    long level=0;
    grib_context* c=grib_context_get_default();

    if (argc==3 && !strcmp(argv[1],"-m")) {
        grib_multi_support_on(0);
        filename=argv[2];
    }
    else if (argc==2) filename=argv[1];
    else usage(argv[0]);

    f=fopen(filename,"r");
    if (!f) {perror(filename);exit(1);}

    fstat(fileno((FILE*)f),&finfo);
    fsize=finfo.st_size;

    data=(unsigned char*)malloc(fsize);
    p=data;

    if (!data) {
        fprintf(stderr,"unable to allocate %ld bytes\n",(long)fsize);
        exit(1);
    }

    if( fread(data, 1, fsize, f)  != fsize) {
        perror(filename);
        exit(1);
    }
    fclose(f);
    pdata=&data;

    while ((h=grib_handle_new_from_multi_message(c,(void**)pdata,&fsize,&error))!=NULL) {
        count++;
        len=20;
        GRIB_CHECK(grib_get_string(h,"shortName",shortName,&len),"shortName");
        GRIB_CHECK(grib_get_long(h,"level",&level),"level");
        printf("%d %s %ld\n",count,shortName,level);
        grib_handle_delete(h);
    }

    free(p);

    return 0;
}
示例#3
0
static void compare_files(const char* file1,const char* file2,FILE* out)
{
    FILE *f1 = fopen(file1,"r");
    FILE *f2 = fopen(file2,"r");
    int e1 = 0;
    int e2 = 0;
    grib_handle *h1;
    grib_handle *h2;
    const void* b1;
    const void* b2;
    size_t s1;
    size_t s2;
    int count = 0;

    if(!f1) {
        perror(file1);
        exit(1);
    }

    if(!f2) {
        perror(file2);
        exit(1);
    }

    h1 = grib_handle_new_from_file(NULL,f1,&e1);
    h2 = grib_handle_new_from_file(NULL,f2,&e2);

    while(h1 && h2)
    {
        ++count;

        GRIB_CHECK(grib_get_message(h1,&b1,&s1),file1);
        GRIB_CHECK(grib_get_message(h2,&b2,&s2),file2);

        compare_handles(count,h1,h2,out);

        grib_handle_delete(h1);
        grib_handle_delete(h2);

        e1 = e2 = 0;

        h1 = grib_handle_new_from_file(NULL,f1,&e1);
        h2 = grib_handle_new_from_file(NULL,f2,&e2);
    }


    GRIB_CHECK(e1,file1);
    GRIB_CHECK(e2,file2);

    if(h1 != h2)
    {
        fprintf(stderr,"%s: Premature eof on %s\n",prog,h1 == 0 ? file1 : file2);
        exit(1);
    }


}
示例#4
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;
}
示例#5
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;
}
示例#6
0
int main(int argc, char *argv[])
{
  FILE *f=NULL;
  char* infname=NULL;
  int ret=0;
  grib_context* gc;
  grib_handle* h;
  FILE* of=NULL;
  const void* buffer=NULL;
  size_t size=0;
  char* ofname=NULL;

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

  infname=argv[1];
  ofname=argv[2];

  f = fopen(infname,"r");
  if(!f) {
    perror(infname);
    exit(1);
  }

  gc = grib_context_get_default();
  h=grib_handle_new(gc);
  if (!h) {
    printf("Error: unable to create handle\n");
    exit(1);
  }

  GRIB_CHECK(grib_load_from_text_file(h,f),infname);

  if(fclose(f)) {
    perror(infname);
    exit(1);
  }

  of = fopen(ofname,"w");
  if(!of) {
     perror(ofname);
     exit(1);
  }

  grib_get_message(h,&buffer,&size);

  if(fwrite(buffer,1,size,of) != size) {
    perror(ofname);
    exit(1);
  }

  if(fclose(of)) {
    perror(ofname);
    exit(1);
  }

  return ret;

}
示例#7
0
int dump_file(const char* file,const char* out)
{
	FILE *f = fopen(file,"r");
	FILE *o = fopen(out,"w");
	int e = 0;
	grib_handle *h;
	const void* b;
	size_t s;
	int count = 0;
	char identifier[10];
	size_t size = sizeof(identifier);

	if(!f) {
		perror(file);
		exit(1);
	}

	h = grib_handle_new_from_file(NULL,f,&e);

	while(h)
	{
		GRIB_CHECK(grib_get_message(h,&b,&s),file);
		GRIB_CHECK(grib_get_string(h,"identifier",identifier,&size),file);

		++count;



		fprintf(o,"%s { # %d\n",identifier,count);
		dump_handle(o,h);
		fprintf(o,"};\n");

		grib_handle_delete(h);

		e = 0;

		h = grib_handle_new_from_file(NULL,f,&e);
	}


	GRIB_CHECK(e,file);

	return 0;

}
示例#8
0
grib_handle * GribFile::getNextHandle_()
{
    int errorCode = 0;
    grib_handle * gribHandle = grib_handle_new_from_file( 0, gribFile_, & errorCode );

	GRIB_CHECK( errorCode, 0 );

	return gribHandle;
}
示例#9
0
int main(int argc, char** argv) {
  int err = 0;
  FILE* in = NULL;
  FILE* of = NULL;
  long step;
  char* filename=NULL;
  char* ofilename=NULL;
  grib_handle *h = NULL;
  grib_multi_handle *mh=NULL;

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

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

  /* new grib handle from input file */
  h = grib_handle_new_from_file(0,in,&err);  
  GRIB_CHECK(err,0);
  /* create a new empty multi field handle */
  mh=grib_multi_handle_new(0);
  if (!mh) {
    printf("unable to create multi field handle\n");
	exit(1);
  }

  for (step=12;step<=120;step+=12) {
    /* set step */
    grib_set_long(h,"step",step);
	/* append h to mh repeating the sections preceding section 4 */
	grib_multi_handle_append(h,4,mh);
  }

  /* open output file */
  of=fopen(ofilename,"w");
  if(!of) {
	printf("ERROR: unable to open file %s\n",ofilename);
    exit(1);
  }

  /* write multi fields handle to output file */
  grib_multi_handle_write(mh,of);
  fclose(of);

  /* clean memory */
  grib_handle_delete(h);
  grib_multi_handle_delete(mh);

  fclose(in);
  return 0;
}
示例#10
0
int main(int argc, char** argv)
{
	int err = 0;
	long parameterCategory=0,parameterNumber=0,discipline=0;
	FILE* in = NULL;
	char* filename = "../../data/multi.grib2";
	grib_handle *h = NULL;

	/* turn on support for multi fields messages */
	grib_multi_support_on(0);

	/* turn off support for multi fields messages */
	/* grib_multi_support_off(0); */

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

	while ((h = grib_handle_new_from_file(0,in,&err)) != NULL ) {

		GRIB_CHECK(err,0);

		GRIB_CHECK(grib_get_long(h,"discipline",&discipline),0);
		printf("discipline=%ld\n",discipline);

		GRIB_CHECK(grib_get_long(h,"parameterCategory",&parameterCategory),0);
		printf("parameterCategory=%ld\n",parameterCategory);

		GRIB_CHECK(grib_get_long(h,"parameterNumber",&parameterNumber),0);
		printf("parameterNumber=%ld\n",parameterNumber);

		if ( discipline == 0 && parameterCategory==2) {
			if (parameterNumber == 2) printf("-------- u -------\n");
			if (parameterNumber == 3) printf("-------- v -------\n");
		}
		grib_handle_delete(h);
	}

	fclose(in);
	return 0;
}
示例#11
0
int main ( int argc, char* argv[]) {
	grib_handle *hfrom,*hto,*h;
	FILE *in;
	char *in_name,*in_name1,*out_name;
	int err=0;
	int what=0;

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

	in_name=argv[1];
	in_name1=argv[2];
	out_name=argv[3];

	in=fopen(in_name,"r");
	if (!in) {
		perror(in_name);
		exit(1);
	}

	hfrom=grib_handle_new_from_file(0,in,&err);
	GRIB_CHECK(err,0);
	fclose(in);

	in=fopen(in_name1,"r");
	if (!in) {
		perror(in_name1);
		exit(1);
	}

	hto=grib_handle_new_from_file(0,in,&err);
	GRIB_CHECK(err,0);
	fclose(in);

	what=GRIB_SECTION_PRODUCT | GRIB_SECTION_LOCAL;
	h=grib_util_sections_copy(hfrom,hto,what,&err);
	GRIB_CHECK(err,0);

	err=grib_write_message(h,out_name,"w");

	return err;
}
示例#12
0
size_t grib_handle_write(grib_handle* h,char* filename) {
  FILE* of=NULL;
  const void *buffer; size_t size;

  of = fopen(filename,"w");
  if(!of) {
      perror(filename);
      exit(1);
  }
  GRIB_CHECK(grib_get_message(h,&buffer,&size),0);
  if(fwrite(buffer,1,size,of) != size) {
    perror(filename);
    exit(1);
  }
  fclose(of);

  return size;

}
示例#13
0
文件: nc.c 项目: erdc-cm/grib_api
int main(int argc,char* argv[]) {

	char* file;
	int err=0;
	grib_handle* h;
	char identifier[7]={0,};
	size_t len=7;
	grib_context* c=grib_context_get_default();
	 
	if (argc>2) usage(argv[0]);

	file=argv[1];

	h=grib_handle_new_from_nc_file(c,file,&err);
	grib_get_string(h,"identifier",identifier,&len);
	printf("%s\n",identifier);
	GRIB_CHECK(err,0);

	return err;


}
示例#14
0
文件: Grib.cpp 项目: WFRT/Comps
void InputGrib::getLocationsCore(std::vector<Location>& iLocations) const {
#ifdef WITH_GRIB
   std::string filename = getLocationFilename();
   FILE* fid = fopen(filename.c_str(),"r");
   if(!fid) {
      Global::logger->write("No sample file available for Grib", Logger::error);
   }
   else {
      int err = 0;
      grib_handle* h = grib_handle_new_from_file(0,fid,&err);
      if (h == NULL) {
         std::stringstream ss;
         ss << "Unable to create handle from file " << filename;
         Global::logger->write(ss.str(),Logger::error);
      }

      // Iterate over all locations
      grib_iterator* iter=grib_iterator_new(h,0,&err);                                     
      if (err != GRIB_SUCCESS) GRIB_CHECK(err,0);                       
      int id = 0;
      double lat, lon;
      double value;
      while(grib_iterator_next(iter,&lat,&lon,&value)) {   
         float elev = 0;//elevs[i];
         assert(lat <= 90 && lat >= -90);
         Location loc(getName(), id, lat, lon);
         loc.setElev(elev);
         iLocations.push_back(loc);
         id++;
      }
      grib_iterator_delete(iter);
      grib_handle_delete(h);
   }
   fclose(fid);
#else
    Global::logger->write("InputGrib: Program not compiled with GRIB", Logger::error);
#endif
}
示例#15
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;
}
示例#16
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;
}
示例#17
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;
}
示例#18
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;
}
示例#19
0
int main(int argc,char* argv[])
{
  grib_index* index=NULL;
  grib_handle* h=NULL;
  long *step,*level,*number;
  char** shortName=NULL;
  int i,j,k,l;
  size_t stepSize,levelSize,shortNameSize,numberSize;
  long ostep,olevel,onumber;
  char oshortName[200];
  size_t lenshortName=200;
  int ret=0,count=0;

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

  printf("indexing...\n");

  index=grib_index_read(0,"out.gribidx",&ret);
  GRIB_CHECK(ret,0);

  printf("end indexing...\n");

  /* get the number of distinct values of "step" in the index */
  GRIB_CHECK(grib_index_get_size(index,"step",&stepSize),0);
  step=(long*)malloc(sizeof(long)*stepSize);
  if (!step) exit(1);
  /* get the list of distinct steps from the index */
  /* the list is in ascending order */
  GRIB_CHECK(grib_index_get_long(index,"step",step,&stepSize),0);
  printf("stepSize=%ld\n",(long)stepSize);
  for (i=0;i<stepSize;i++) printf("%ld ",step[i]);
  printf("\n");

  /*same as for "step"*/
  GRIB_CHECK(grib_index_get_size(index,"level",&levelSize),0);
  level=(long*)malloc(sizeof(long)*levelSize);
  if (!level) exit(1);
  /*same as for "step"*/
  GRIB_CHECK(grib_index_get_long(index,"level",level,&levelSize),0);
  printf("levelSize=%ld\n",(long)levelSize);
  for (i=0;i<levelSize;i++) printf("%ld ",level[i]);
  printf("\n");

  /*same as for "step"*/
  GRIB_CHECK(grib_index_get_size(index,"number",&numberSize),0);
  number=(long*)malloc(sizeof(long)*numberSize);
  if (!number) exit(1);
  /*same as for "step"*/
  GRIB_CHECK(grib_index_get_long(index,"number",number,&numberSize),0);
  printf("numberSize=%ld\n",(long)numberSize);
  for (i=0;i<numberSize;i++) printf("%ld ",number[i]);
  printf("\n");

  /*same as for "step"*/
  GRIB_CHECK(grib_index_get_size(index,"shortName",&shortNameSize),0);
  shortName=(char**)malloc(sizeof(char*)*shortNameSize);
  if (!shortName) exit(1);
  /*same as for "step"*/
  GRIB_CHECK(grib_index_get_string(index,"shortName",shortName,&shortNameSize),0);
  printf("shortNameSize=%ld\n",(long)shortNameSize);
  for (i=0;i<shortNameSize;i++) printf("%s ",shortName[i]);
  printf("\n");

  count=0;
  /* nested loops on the keys values of the index */
  /* different order of the nested loops doesn't affect performance*/
  for (i=0;i<shortNameSize;i++) {
    /* select the grib with shortName=shortName[i] */
    grib_index_select_string(index,"shortName",shortName[i]);

    for (l=0;l<levelSize;l++) {
      /* select the grib with level=level[i] */
      grib_index_select_long(index,"level",level[l]);

      for (j=0;j<numberSize;j++) {
        /* select the grib with number=number[i] */
        grib_index_select_long(index,"number",number[j]);

        for (k=0;k<stepSize;k++) {
          /* select the grib with step=step[i] */
          grib_index_select_long(index,"step",step[k]);

		 /* create a new grib_handle from the index with the constraints 
		    imposed by the select statements. It is a loop because
			in the index there could be more than one grib with those
			constrants */
         while ((h=grib_handle_new_from_index(index,&ret))!=NULL){
		   count++;
           if (ret) {printf("error: %d\n",ret); exit(ret);}
           lenshortName=200;
		   grib_get_string(h,"shortName",oshortName,&lenshortName);
		   grib_get_long(h,"level",&olevel);
		   grib_get_long(h,"number",&onumber);
		   grib_get_long(h,"step",&ostep);
		   printf("shortName=%s ",oshortName);
		   printf("level=%ld ",olevel);
		   printf("number=%ld ",onumber);
		   printf("step=%ld \n",ostep);
		   grib_handle_delete(h);
		 }
         if (ret!=GRIB_END_OF_INDEX) {printf("error: %s\n",grib_get_error_message(ret)); exit(ret);}
	    } 
	  }
	}
  }
  printf("  %d messages selected\n",count);


  return 0;
}
示例#20
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;
}
示例#21
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;
}
示例#22
0
文件: list.c 项目: erdc-cm/grib_api
int main(int argc, char** argv) {
	int err = 0;

	size_t i = 0;
	size_t count;
	size_t size;

	long numberOfContributingSpectralBands;
	long values[1024];

	FILE* in = NULL;
	char* filename = "../../data/satellite.grib";
	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);
	}

	numberOfContributingSpectralBands = 2;
	GRIB_CHECK(grib_set_long(h,"numberOfContributingSpectralBands",numberOfContributingSpectralBands),0);

	numberOfContributingSpectralBands = 9;
	GRIB_CHECK(grib_set_long(h,"numberOfContributingSpectralBands",numberOfContributingSpectralBands),0);

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

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

	assert(count < sizeof(values)/sizeof(values[0]));

	size = count;
	GRIB_CHECK(grib_get_long_array(h,"scaledValueOfCentralWaveNumber",values,&size),0);
	assert(size == count);

	for(i=0;i<count;i++)
		printf("scaledValueOfCentralWaveNumber %lu = %ld\n",(unsigned long)i,values[i]);

	for(i=0;i<count;i++)
		values[i] = -values[i]; 

	size = count;
	/* size--; */
	GRIB_CHECK(grib_set_long_array(h,"scaledValueOfCentralWaveNumber",values,size),0);
	assert(size == count);

	grib_handle_delete(h);

	fclose(in);
	return 0;
}
示例#23
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;
}
示例#24
0
文件: Grib.cpp 项目: 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
}
示例#25
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;
}
示例#26
0
int main(int argc, char** argv)
{
    FILE* fin=0;
    int ret=0;
    char* fname=0;
    float lat,lon;
    double *vlat,*vlon;
    int npoints=0,i=0,n=0;
    grib_handle* h;
    double *outlats,*outlons,*values,*lsm_values,*distances;
    int* indexes;
    long step=0;
    char time[10]={0,};
    char date[10]={0,};
    long parameter=0;
    size_t len=0;
    long iid=0;
    long *id=NULL;

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

    fname=argv[1];
    fin=fopen(fname,"r");
    if(!fin) { perror(fname); exit(1); }
    npoints=0;
    while (fscanf(fin,"%ld %g %g",&iid,&lat,&lon) != EOF) npoints++;
    fclose(fin);

    id=(long*)malloc(npoints*sizeof(long));
    if (!id) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(long)));exit(1);}
    vlat=(double*)malloc(npoints*sizeof(double));
    if (!vlat) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
    vlon=(double*)malloc(npoints*sizeof(double));
    if (!vlon) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
    outlats=(double*)malloc(npoints*sizeof(double));
    if (!outlats) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
    outlons=(double*)malloc(npoints*sizeof(double));
    if (!outlons) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
    values=(double*)malloc(npoints*sizeof(double));
    if (!values) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
    lsm_values=(double*)malloc(npoints*sizeof(double));
    if (!lsm_values) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
    distances=(double*)malloc(npoints*sizeof(double));
    if (!distances) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
    indexes=(int*)malloc(npoints*sizeof(int));
    if (!indexes) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}

    fname=argv[1];
    fin=fopen(fname,"r");
    if(!fin) { perror(fname); exit(1); }
    i=0;
    while (fscanf(fin,"%ld %g %g",&iid,&lat,&lon) != EOF) {
        id[i]=iid;vlat[i]=lat;
        while(lon < 0) lon+=360;
        vlon[i]=lon;
        i++;
    }
    fclose(fin);

    fname=argv[2];
    fin=fopen(fname,"r");
    if(!fin) { perror(fname); exit(1); }
    h=grib_handle_new_from_file(0,fin,&ret);
    if (!h || ret!=GRIB_SUCCESS) {printf(" unable to create handle\n");exit(1);}

    grib_nearest_find_multiple(h,1,vlat,vlon,npoints,
            outlats,outlons,lsm_values,distances,indexes);

    grib_handle_delete(h);

    fclose(fin);

    for (n=3;n<=argc-1;n++) {
        fname=argv[n];
        fin=fopen(fname,"r");
        if(!fin) { perror(fname); exit(1); }
        while ((h=grib_handle_new_from_file(0,fin,&ret))!=NULL) {
            grib_get_double_elements(h,"values",indexes,npoints,values);

            GRIB_CHECK(grib_get_length(h, "date", &len),0);
            grib_get_string(h,"date",date,&len);
            GRIB_CHECK(grib_get_length(h, "time", &len),0);
            grib_get_string(h,"time",time,&len);
            grib_get_long(h,"step",&step);
            grib_get_long(h,"parameter",&parameter);

            printf("# %s %s %ld %ld\n",date,time,step,parameter);
            grib_handle_delete(h);
            for (i=0;i<npoints;i++)
                printf("%ld %g %g %g %g\n",
                        id[i],outlats[i],outlons[i],
                        lsm_values[i],values[i]);
        }

        fclose(fin);
    }

    return ret;
}
示例#27
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;
}
示例#28
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;
}
示例#29
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;
}
示例#30
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);
}