Example #1
0
File: Grib.cpp Project: WFRT/Comps
std::string InputGrib::getVariableName(grib_handle* iH) {
   std::stringstream ss;
   char name1[512];
   char name2[512];
   char name3[512];
   char name4[512];
   size_t len1 = 512;
   size_t len2 = 512;
   size_t len3 = 512;
   size_t len4 = 512;

   int err = grib_get_string(iH, "shortName", name3, &len3); 
   assert(err == 0);
   std::string shortName = std::string(name3);

   err = grib_get_string(iH, "typeOfLevel", name1, &len1); 
   assert(err == 0);
   std::string typeOfLevel = std::string(name1);

   err = grib_get_string(iH, "levtype", name2, &len2); 
   assert(err == 0);
   std::string levtype = std::string(name2);
   if(typeOfLevel == "nominalTop") {
      levtype = "ntop";
   }

   err = grib_get_string(iH, "levelist", name4, &len4); 
   std::string level = "undef";
   if(err != GRIB_NOT_FOUND) {
      level = std::string(name4);
   }

   ss << shortName << "_" << levtype << "_" << level;
   return ss.str();
}
static int unpack_string(grib_accessor* a, char* val, size_t *len)
{
    grib_accessor_g2_mars_labeling* self = (grib_accessor_g2_mars_labeling*)a;
    char* key=NULL;

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

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

}
Example #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;
}
Example #4
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;
}
Example #5
0
char* sget(grib_handle *h,const char* what,char* val,size_t size)
{
	int e; 
	if((e = grib_get_string(h,what,val,&size)) != GRIB_SUCCESS)
	{
		fprintf(stderr,"%s, field %d [%s]: cannot get %s: %s\n",file,field,param,what,grib_get_error_message(e));
		exit(1);
	}
	return val;
}
/* See GRIB-488 */
static int is_special_expver(grib_handle* h)
{
    int ret = 0;
    char strExpVer[50]={0,};
    size_t slen=50;
    ret = grib_get_string(h, "experimentVersionNumber", strExpVer, &slen);
    if (ret == GRIB_SUCCESS && !strcmp(strExpVer, "1605")) {
        return 1; /* Special case of expVer 1605! */
    }

    return 0;
}
Example #7
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;

}
static int pack_long(grib_accessor* a, const long* val, size_t *len)
{
    grib_accessor_g2_chemical* self = (grib_accessor_g2_chemical*)a;
    long productDefinitionTemplateNumber=-1;
    long productDefinitionTemplateNumberNew=-1;
    /*long type=-1;
    long stream=-1;*/
    long eps=-1;
    char stepType[15]={0,};
    size_t slen=15;
    /*int chemical = *val;*/
    int isInstant=0;
    /*long derivedForecast=-1;*/
    int ret = 0;

    if (grib_get_long(a->parent->h, self->productDefinitionTemplateNumber,&productDefinitionTemplateNumber)!=GRIB_SUCCESS)
        return GRIB_SUCCESS;

    /*
     grib_get_long(a->parent->h, self->type,&type);
     grib_get_long(a->parent->h, self->stream,&stream);
     */
    ret = grib_get_string(a->parent->h, self->stepType, stepType, &slen);
    Assert(ret == GRIB_SUCCESS);

    eps = is_productDefinitionTemplateNumber_EPS(productDefinitionTemplateNumber);

    if (!strcmp(stepType,"instant")) isInstant=1;

    if ( eps == 1 ) {
        if (isInstant) {
            productDefinitionTemplateNumberNew=41;
        } else {
            productDefinitionTemplateNumberNew=43;
        }
    } else {
        if (isInstant) {
            productDefinitionTemplateNumberNew=40;
        } else {
            productDefinitionTemplateNumberNew=42;
        }
    }

    if (productDefinitionTemplateNumber != productDefinitionTemplateNumberNew) {
        grib_set_long(a->parent->h, self->productDefinitionTemplateNumber,productDefinitionTemplateNumberNew);
        /*if (derivedForecast>=0) grib_set_long(a->parent->h, self->derivedForecast,derivedForecast);*/
    }

    return 0;
}
Example #9
0
int main(int argc,char* argv[]) {
  
  grib_index* index=NULL;
  grib_handle* h=NULL;
  char* infile=NULL;
  int i,j,k,l;
  long ostep,olevel,onumber;
  char oshortName[200];
  size_t lenshortName=200;
  int ret=0,count=0;

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

  printf("indexing...\n");
  /* Create an index from a grib file with a given set of keys*/
  index=grib_index_new_from_file(0,infile,"shortName,level,number,step",&ret);
  if (ret) {printf("error: %s\n",grib_get_error_message(ret)); exit(ret);}
  printf("end indexing...\n");

  /* ask the index to order the fields thorough some keys*/
  ret=grib_index_orderby(index,"level,shortName,number:desc,step:asc");
  if (ret!=GRIB_SUCCESS) exit(1);

  count=0;
  /* loop through all the fields in the asked order*/
  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;
}
Example #10
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;

}
Example #11
0
static void print_key_values(grib_runtime_options* options,grib_handle* h)
{
    int i;
    int ret=0;
    char* s="\"keys\" : {";
    double dvalue=0;
    long lvalue=0;
    char value[MAX_STRING_LEN];
    size_t len=MAX_STRING_LEN;
    for (i=0;i<options->print_keys_count;i++) {
        ret=GRIB_SUCCESS;
        printf("%s",s);
        len=MAX_STRING_LEN;
        printf("\"%s\" : ",options->print_keys[i].name);
        if (grib_is_missing(h,options->print_keys[i].name,&ret) && ret==GRIB_SUCCESS)
            printf("\"missing\"");
        else if ( ret == GRIB_SUCCESS ) {
            if (options->print_keys[i].type == GRIB_TYPE_UNDEFINED)
                grib_get_native_type(h,options->print_keys[i].name,&(options->print_keys[i].type));
            switch (options->print_keys[i].type) {
            case GRIB_TYPE_STRING:
                ret=grib_get_string( h,options->print_keys[i].name,value,&len);
                printf("\"%s\"",value);
                break;
            case GRIB_TYPE_DOUBLE:
                ret=grib_get_double( h,options->print_keys[i].name,&dvalue);
                printf("%g",dvalue);
                break;
            case GRIB_TYPE_LONG:
                ret=grib_get_long( h,options->print_keys[i].name,&lvalue);
                printf("%ld",lvalue);
                break;
            default:
                printf("invalid_type");
                break;
            }
        }
        if (ret == GRIB_NOT_FOUND) printf("null");
        s=", ";
    }
    printf("}");
}
Example #12
0
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;


}
Example #13
0
int codes_get_string(grib_handle* h, const char* key, char* mesg, size_t *length)
{
    return grib_get_string(h,key,mesg,length);
}
Example #14
0
static int compare_values(grib_handle* h1,grib_handle *h2,const char *name)
{
  size_t len1 = 0;
  size_t len2 = 0;
  int err;
  int err1;
  int err2;
  int type1,type2;

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

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

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

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

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

  if(type1 == GRIB_TYPE_LABEL)
    return err;

  if(type1 == GRIB_TYPE_SECTION)
    return err;


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

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

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

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

  switch(type1)
  {
    case GRIB_TYPE_STRING:

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

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

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

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

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

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

      break;

    case GRIB_TYPE_LONG:

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

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

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

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

            err1 = GRIB_VALUE_MISMATCH;
            break;
          }
      }

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

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

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

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

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

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

            err1 = GRIB_VALUE_MISMATCH;
            break;
          }
      }

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

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

    case GRIB_TYPE_BYTES:

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

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

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

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

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

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

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

    case GRIB_TYPE_LABEL:
      break;

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

  return GRIB_SUCCESS;

}
Example #15
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;
}
static grib_codetable* load_table(grib_accessor_codetable* self)
{
  size_t size = 0;
  grib_handle*    h = ((grib_accessor*)self)->parent->h;
  grib_context*   c = h->context;
  grib_codetable* t = NULL;
  grib_codetable* next=NULL ;
  grib_accessor* a=(grib_accessor*)self;
  char *filename=0;
  char name[1024]={0,};
  char recomposed[1024]={0,};
  char localRecomposed[1024]={0,};
  char *localFilename=0;
  char localName[1024]={0,};
  char masterDir[1024]={0,};
  char localDir[1024]={0,};
  size_t len=1024;

  if (self->masterDir != NULL)
    grib_get_string(h,self->masterDir,masterDir,&len);

  len=1024;
  if (self->localDir != NULL)
    grib_get_string(h,self->localDir,localDir,&len);

  if (*masterDir!=0) {
    sprintf(name,"%s/%s",masterDir,self->tablename);
    grib_recompose_name(h, NULL,name, recomposed,0);
    filename=grib_context_full_path(c,recomposed);
  } else {
    grib_recompose_name(h, NULL,self->tablename, recomposed,0);
    filename=grib_context_full_path(c,recomposed);
  }

  if (*localDir!=0) {
    sprintf(localName,"%s/%s",localDir,self->tablename);
    grib_recompose_name(h, NULL,localName, localRecomposed,0);
    localFilename=grib_context_full_path(c,localRecomposed);
  }
  
  next=c->codetable;
  while(next) {
    if((filename && next->filename[0] && strcmp(filename,next->filename[0]) == 0) &&
       ((localFilename==0 && next->filename[1]==NULL) ||
           ((localFilename!=0 && next->filename[1]!=NULL)
           && strcmp(localFilename,next->filename[1]) ==0)) )
      return next;
    next = next->next;
  }

  if (a->flags & GRIB_ACCESSOR_FLAG_TRANSIENT) {
	  Assert(a->vvalue!=NULL);
    size=a->vvalue->length*8;
  } else {
    size = grib_byte_count((grib_accessor*)self) * 8;
  }
  size = grib_power(size,2);

  t = (grib_codetable*)grib_context_malloc_clear_persistent(c,sizeof(grib_codetable) +
      (size-1)*sizeof(code_table_entry));

  if (filename!=0) grib_load_codetable(c,filename,recomposed,size,t);

  if (localFilename!=0) grib_load_codetable(c,localFilename,localRecomposed,size,t);

  if (t->filename[0]==NULL && t->filename[1]==NULL) {
    grib_context_free_persistent(c,t);
    return NULL;
  }
  
  return t;

}
Example #17
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;
}
Example #18
0
/*
 A new handle is available from the current input file and can be processed here.
 The handle available in this function is in the set of messages satisfying the constraint
 of the -w option. They are not to be skipped.
*/
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
{
    size_t size=4;
    double v=0;
    int err=0;
    int i;

    if (!options->skip) {

        if (options->set_values_count != 0)
            err=grib_set_values(h,options->set_values,options->set_values_count);

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

    if (options->latlon) {
        int err=0;
        double min;
        if (!n) n=grib_nearest_new(h,&err);
        if (err == GRIB_NOT_IMPLEMENTED) {
            char grid_type[100];
            size_t grid_type_len=100;
            int err1=grib_get_string(h, "gridType", grid_type, &grid_type_len);
            if (err1 == GRIB_SUCCESS) {
                fprintf(stderr,"Nearest neighbour functionality is not supported for grid type: %s\n", grid_type);
            }
        }
        GRIB_CHECK_NOLINE(err,0);
        GRIB_CHECK_NOLINE(grib_nearest_find(n,h,lat,lon,0,
                options->lats,options->lons,options->values,
                options->distances,options->indexes,&size),0);

        if (!options->latlon_mask) {
            min=options->distances[0];
            options->latlon_idx=0;
            i=0;
            for (i=1;i<4;i++) {
                if (min>options->distances[i]) {
                    min=options->distances[i];
                    options->latlon_idx=i;
                }
            }
        }

        if (json_latlon) {
            char* s="\n[\n";
            double missingValue=9999;
            char value[MAX_STRING_LEN];
            size_t len=MAX_STRING_LEN;
            printf("%s",new_handle);
            printf("{\n");
            print_key_values(options,h);
            printf("\n, \"selected\" : %d",options->latlon_idx);
            printf(", \"method\" : ");
            if (options->latlon_mask) printf("\"nearest_land\"");
            else printf("\"nearest\"");
            printf("\n, \"neighbours\" : ");
            for (i=0;i<4;i++) {
                printf("%s",s);
                len=MAX_STRING_LEN;
                printf("{\"index\" : %d, \"latitude\" : %g, \"longitude\" : %g, \"distance\" : %g, "
                        "\"distance_unit\" : \"km\", ",
                        (int)options->indexes[i],options->lats[i],options->lons[i],
                        options->distances[i]);
                if (grib_get_double_element(h,"values",options->indexes[i],&v) == GRIB_SUCCESS) {
                    if (v==missingValue) printf("\"value\" : null ");
                    else printf("\"value\" : %g ",v);
                }

                if (grib_get_string( h,"units",value,&len)==GRIB_SUCCESS)
                    printf(", \"unit\" : \"%s\"",value);

                if (options->latlon_mask)
                    printf(", \"mask_value\" : %.2f",options->mask_values[i]);
                printf("}");
                s="\n,";
            }

            printf("\n]");
            printf("\n}");
        }
    }
    new_handle="\n,";
    return 0;
}
static grib_trie* load_dictionary(grib_context* c,grib_accessor* a, int* err)
{
    grib_accessor_dictionary* self = (grib_accessor_dictionary*)a;

    char* filename=NULL;
    char line[1024]={0,};
    char key[1024]={0,};
    char masterDir[1024]={0,};
    char localDir[1024]={0,};
    char dictName[1024]={0,};
    char *localFilename=0;
    char* list=0;
    size_t len=1024;
    grib_trie* dictionary=NULL;
    FILE* f=NULL;
    int i=0;
    grib_handle* h=grib_handle_of_accessor(a);

    *err=GRIB_SUCCESS;

    len=1024;
    if (self->masterDir != NULL) grib_get_string(h,self->masterDir,masterDir,&len);
    len=1024;
    if (self->localDir != NULL) grib_get_string(h,self->localDir,localDir,&len);

    if (*masterDir!=0) {
        char name[1024]={0,};
        char recomposed[1024]={0,};
        sprintf(name,"%s/%s",masterDir,self->dictionary);
        grib_recompose_name(h, NULL,name, recomposed,0);
        filename=grib_context_full_defs_path(c,recomposed);
    } else {
        filename=grib_context_full_defs_path(c,self->dictionary);
    }

    if (*localDir!=0) {
        char localName[1024]={0,};
        char localRecomposed[1024]={0,};
        sprintf(localName,"%s/%s",localDir,self->dictionary);
        grib_recompose_name(h, NULL,localName, localRecomposed,0);
        localFilename=grib_context_full_defs_path(c,localRecomposed);
        sprintf(dictName,"%s:%s",localFilename,filename);
    } else {
        sprintf(dictName,"%s",filename);
    }

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

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

    dictionary=grib_trie_new(c);

    while(fgets(line,sizeof(line)-1,f)) {
        i=0;
        while (line[i] != '|' && line[i] != 0)  {
            key[i]=line[i];
            i++;
        }
        key[i]=0;
        list=(char*)grib_context_malloc_clear(c,strlen(line)+1);
        memcpy(list,line,strlen(line));
        grib_trie_insert(dictionary,key,list);
    }

    fclose(f);

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

        while(fgets(line,sizeof(line)-1,f)) {
            i=0;
            while (line[i] != '|' && line[i] != 0)  {
                key[i]=line[i];
                i++;
            }
            key[i]=0;
            list=(char*)grib_context_malloc_clear(c,strlen(line)+1);
            memcpy(list,line,strlen(line));
            grib_trie_insert(dictionary,key,list);
        }


        fclose(f);
    }
    grib_trie_insert(c->lists,filename,dictionary);
    return dictionary;
}
Example #20
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;
}
Example #21
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;
}
static int extra_set(grib_accessor* a,long val)
{
/*TODO chemicals*/
    int ret=0;
    grib_accessor_g2_mars_labeling* self = (grib_accessor_g2_mars_labeling*)a;
    char stepType[30]={0,};
    size_t stepTypelen=30;
    long derivedForecast=-1;
    long productDefinitionTemplateNumberNew=-1;
    long productDefinitionTemplateNumber;
    long typeOfProcessedData=-1;
    long typeOfGeneratingProcess=-1;

    switch (self->index) {
    case 0:
        /* class */
        return ret;
        break;
    case 1:
        /* type */
        switch (val) {
        case 0:		/* Unknown       (0) */
            typeOfProcessedData=255;
            typeOfGeneratingProcess=255;
            break;
        case 1:		/* First guess          (fg) */
        case 3:		/* Initialised analysis (ia) */
            typeOfProcessedData=0;
            typeOfGeneratingProcess=1;
            break;
        case 2:		/* Analysis                    (an) */
        case 4:		/* Oi analysis                 (oi) */
        case 5:		/* 3d variational analysis     (3v) */
        case 6:		/* 4d variational analysis     (4v) */
        case 7:		/* 3d variational gradients    (3g) */
        case 8:		/* 4d variational gradients    (4g) */
            typeOfProcessedData=0;
            typeOfGeneratingProcess=0;
            break;
        case 9:		/* Forecast  (fc) */
            typeOfProcessedData=1;
            typeOfGeneratingProcess=2;
            break;
        case 10:	/* Control forecast  (cf) */
            typeOfProcessedData=3;
            typeOfGeneratingProcess=4;
            break;
        case 11:	/* Perturbed forecast    (pf) */
            typeOfProcessedData=4;
            typeOfGeneratingProcess=4;
            break;
        case 12:	/* Errors in first guess  (ef) */
        case 13:	/* Errors in analysis     (ea) */
            typeOfProcessedData=255;
            typeOfGeneratingProcess=7;
            break;
        case 14:	/* Cluster means              (cm) */
        case 15:	/* Cluster std deviations     (cs) */
            typeOfProcessedData=255;
            typeOfGeneratingProcess=4;
            break;
        case 16:	/* Forecast probability  (fp) */
            typeOfProcessedData=8;
            typeOfGeneratingProcess=5;
            break;
        case 17:	/* Ensemble mean  (em) */
            derivedForecast=0;
            grib_get_string(a->parent->h,self->stepType,stepType,&stepTypelen);
            if (!strcmp(stepType,"instant")) {
                productDefinitionTemplateNumberNew=2;
            } else {
                productDefinitionTemplateNumberNew=12;
            }
            typeOfProcessedData=255;
            typeOfGeneratingProcess=4;
            break;
        case 18:	/* Ensemble standard deviation     (es) */
            derivedForecast=4;
            grib_get_string(a->parent->h,self->stepType,stepType,&stepTypelen);
            if (!strcmp(stepType,"instant")) {
                productDefinitionTemplateNumberNew=2;
            } else {
                productDefinitionTemplateNumberNew=12;
            }
            typeOfProcessedData=255;
            typeOfGeneratingProcess=4;
            break;
        case 19:	/* Forecast accumulation           (fa)  */
        case 20:	/* Climatology                     (cl)  */
        case 21:	/* Climate simulation              (si)  */
        case 22:	/* Climate 30 days simulation      (s3)  */
        case 23:	/* Empirical distribution          (ed)  */
        case 24:	/* Tubes                           (tu)  */
        case 25:	/* Flux forcing realtime           (ff)  */
        case 26:	/* Ocean forward                   (of)  */
        case 27:	/* Extreme forecast index          (efi) */
        case 28:	/* Extreme forecast index control  (efic)*/
        case 29:	/* Probability boundaries          (pb)  */
            typeOfProcessedData=255;
            typeOfGeneratingProcess=255;
            break;
        case 30:	/* Event probability      (ep) */
            typeOfProcessedData=8;
            typeOfGeneratingProcess=5;
            break;
        case 31:	/* Bias-corrected Forecast      (bf) */
            typeOfProcessedData=1;
            typeOfGeneratingProcess=3;
            break;
        case 32:	/* Climate distribution      (cd)  */
        case 33:	/* 4D analysis increments    (4i)  */
        case 34:	/* Gridded observations      (go)  */
        case 35:	/* Model errors              (me)  */
        case 36:	/* Probability distribution  (pd)  */
        case 37:	/* Cluster information       (ci)  */
        case 38:	/* Shift of Tail             (sot) */
        case 40:	/* Images                    (im)  */
        case 42:	/* Simulated images          (sim) */
            typeOfProcessedData=255;
            typeOfGeneratingProcess=255;
            break;
        case 43:	/* Weighted ensemble mean                   (wem)  */
        case 44:	/* Weighted ensemble standard deviation     (wes)  */
        case 45:	/* Cluster representative                   (cr)   */
        case 46:	/* Scaled ensemble standard deviation       (ses)  */
        case 47:	/* Time average ensemble mean               (taem) */
        case 48:	/* Time average ensemble standard deviation (taes) */
            typeOfProcessedData=255;
            typeOfGeneratingProcess=4;
            break;
        case 50:	/* Sensitivity gradient            (sg)   */
        case 52:	/* Sensitivity forecast            (sf)   */
        case 60:	/* Perturbed analysis              (pa)   */
        case 61:	/* Initial condition perturbation  (icp)  */
        case 62:	/* Singular vector                 (sv)   */
        case 63:	/* Adjoint singular vector         (as)   */
        case 64:	/* Signal variance                 (svar) */
            typeOfProcessedData=255;
            typeOfGeneratingProcess=255;
            break;
        case 65:	/* Calibration/Validation forecast  (cv) */
            typeOfProcessedData=5;
            typeOfGeneratingProcess=4;
            break;
        case 70:	/* Ocean reanalysis     (or) */
        case 71:	/* Flux forcing         (fx) */
        case 80:	/* Forecast mean        (fcmean) */
        case 81:	/* Forecast maximum     (fcmax) */
        case 82:	/* Forecast minimum     (fcmin) */
        case 83:	/* Forecast standard deviation  (fcstdev) */
        case 88:	/* Gridded satellite data */
        case 89:	/* GFAS analysis */
            typeOfProcessedData=255;
            typeOfGeneratingProcess=255;
            break;
        case 84:	/* Ensemble mean of temporal mean   (emtm) */
        case 85:	/* Ensemble standard deviation of temporal mean  (estdtm) */
            typeOfProcessedData=255;
            typeOfGeneratingProcess=4;
            break;
        default :
            grib_context_log(a->parent->h->context,GRIB_LOG_ERROR,"unknown mars.type %d",(int)val);
            return GRIB_ENCODING_ERROR;
        }
        case 2:
            /* stream */
            switch (val) {
            case 1030:  /* enda */
            case 1249:  /* elda */
            case 1250:  /* ewla */
                grib_get_string(a->parent->h,self->stepType,stepType,&stepTypelen);
                if (!strcmp(stepType,"instant")) {
                    productDefinitionTemplateNumberNew=1;
                } else {
                    productDefinitionTemplateNumberNew=11;
                }
                break;
            }
            break;
        default :
            grib_context_log(a->parent->h->context,GRIB_LOG_ERROR,
                        "invalid first argument of g2_mars_labeling in %s",a->name);
            return GRIB_INTERNAL_ERROR;
            break;
    }

    if (productDefinitionTemplateNumberNew>=0) {
        grib_get_long(a->parent->h,self->productDefinitionTemplateNumber,&productDefinitionTemplateNumber);
        if (productDefinitionTemplateNumber!=productDefinitionTemplateNumberNew)
            grib_set_long(a->parent->h,self->productDefinitionTemplateNumber,productDefinitionTemplateNumberNew);
    }

    if (derivedForecast>=0) {
        grib_set_long(a->parent->h,self->derivedForecast,derivedForecast);
    }

    if (typeOfProcessedData>0)
        grib_set_long(a->parent->h,self->typeOfProcessedData,typeOfProcessedData);
    if (typeOfGeneratingProcess>0)
        grib_set_long(a->parent->h,self->typeOfGeneratingProcess,typeOfGeneratingProcess);

    return ret;
}
Example #23
0
int dump_values(FILE* out,grib_handle* h,const char *name,int missingOK)
{
	size_t len = 0;
	int err;
	int type;

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


	int i;

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

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

	switch(type)
	{
		case GRIB_TYPE_STRING:

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

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

			grib_context_free(h->context,sval);

			if(err) return err;

			break;

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

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

			grib_context_free(h->context,lval);

			if(err) return err;
			break;

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

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

			grib_context_free(h->context,dval);

			if(err) return err;
			break;

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

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

			}

			grib_context_free(h->context,uval);

			if(err) return err;

			break;

		case GRIB_TYPE_LABEL:
			break;

		case GRIB_TYPE_SECTION:
			break;

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

	return GRIB_SUCCESS;

}