ECC-327: bufr_dump -EC

This commit is contained in:
Shahram Najm 2016-09-01 18:52:33 +01:00
parent 1f3a80b58c
commit bd68931b64
8 changed files with 947 additions and 0 deletions

View File

@ -274,6 +274,7 @@ list( APPEND grib_api_srcs
grib_dumper_class_debug.c
grib_dumper_class_default.c
grib_dumper_class_keys.c
grib_dumper_class_C.c
grib_dumper_class_filter.c
grib_dumper_class_fortran.c
grib_dumper_class_python.c

View File

@ -289,6 +289,7 @@ libeccodes_la_prototypes= \
grib_dumper_class_debug.c \
grib_dumper_class_default.c \
grib_dumper_class_keys.c \
grib_dumper_class_C.c \
grib_dumper_class_filter.c \
grib_dumper_class_fortran.c \
grib_dumper_class_python.c \

View File

@ -1,4 +1,5 @@
/* This file is automatically generated by ./make_class.pl, do not edit */
extern grib_dumper_class* grib_dumper_class_C;
extern grib_dumper_class* grib_dumper_class_c_code;
extern grib_dumper_class* grib_dumper_class_compare;
extern grib_dumper_class* grib_dumper_class_debug;

827
src/grib_dumper_class_C.c Normal file
View File

@ -0,0 +1,827 @@
/*
* Copyright 2005-2016 ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
*/
#include "grib_api_internal.h"
#include <ctype.h>
/*
This is used by make_class.pl
START_CLASS_DEF
CLASS = dumper
IMPLEMENTS = dump_long;dump_bits
IMPLEMENTS = dump_double;dump_string;dump_string_array
IMPLEMENTS = dump_bytes;dump_values
IMPLEMENTS = dump_label;dump_section
IMPLEMENTS = init;destroy
IMPLEMENTS = header;footer
MEMBERS = long section_offset
MEMBERS = long empty
MEMBERS = long end
MEMBERS = long isLeaf
MEMBERS = long isAttribute
MEMBERS = grib_string_list* keys
END_CLASS_DEF
*/
/* START_CLASS_IMP */
/*
Don't edit anything between START_CLASS_IMP and END_CLASS_IMP
Instead edit values between START_CLASS_DEF and END_CLASS_DEF
or edit "dumper.class" and rerun ./make_class.pl
*/
static void init_class (grib_dumper_class*);
static int init (grib_dumper* d);
static int destroy (grib_dumper*);
static void dump_long (grib_dumper* d, grib_accessor* a,const char* comment);
static void dump_bits (grib_dumper* d, grib_accessor* a,const char* comment);
static void dump_double (grib_dumper* d, grib_accessor* a,const char* comment);
static void dump_string (grib_dumper* d, grib_accessor* a,const char* comment);
static void dump_string_array (grib_dumper* d, grib_accessor* a,const char* comment);
static void dump_bytes (grib_dumper* d, grib_accessor* a,const char* comment);
static void dump_values (grib_dumper* d, grib_accessor* a);
static void dump_label (grib_dumper* d, grib_accessor* a,const char* comment);
static void dump_section (grib_dumper* d, grib_accessor* a,grib_block_of_accessors* block);
static void header (grib_dumper*,grib_handle*);
static void footer (grib_dumper*,grib_handle*);
typedef struct grib_dumper_C {
grib_dumper dumper;
/* Members defined in C */
long section_offset;
long empty;
long end;
long isLeaf;
long isAttribute;
grib_string_list* keys;
} grib_dumper_C;
static grib_dumper_class _grib_dumper_class_C = {
0, /* super */
"C", /* name */
sizeof(grib_dumper_C), /* size */
0, /* inited */
&init_class, /* init_class */
&init, /* init */
&destroy, /* free mem */
&dump_long, /* dump long */
&dump_double, /* dump double */
&dump_string, /* dump string */
&dump_string_array, /* dump string array */
&dump_label, /* dump labels */
&dump_bytes, /* dump bytes */
&dump_bits, /* dump bits */
&dump_section, /* dump section */
&dump_values, /* dump values */
&header, /* header */
&footer, /* footer */
};
grib_dumper_class* grib_dumper_class_C = &_grib_dumper_class_C;
/* END_CLASS_IMP */
static void dump_attributes(grib_dumper* d, grib_accessor* a, const char* prefix);
GRIB_INLINE static int grib_inline_strcmp(const char* a, const char* b)
{
if (*a != *b) return 1;
while((*a!=0 && *b!=0) && *(a) == *(b) ) {a++;b++;}
return (*a==0 && *b==0) ? 0 : 1;
}
typedef struct string_count string_count;
struct string_count {
char* value;
int count;
string_count* next;
};
static int depth=0;
static void init_class (grib_dumper_class* c){}
static int init(grib_dumper* d)
{
grib_dumper_C *self = (grib_dumper_C*)d;
grib_context* c=d->handle->context;
self->section_offset=0;
self->empty=1;
d->count=1;
self->isLeaf=0;
self->isAttribute=0;
self->keys=grib_context_malloc_clear(c,sizeof(grib_string_list));
return GRIB_SUCCESS;
}
static int destroy(grib_dumper* d)
{
grib_dumper_C *self = (grib_dumper_C*)d;
grib_string_list* next=self->keys;
grib_string_list* cur=self->keys;
grib_context* c=d->handle->context;
while(next) {
cur=next;
next=next->next;
grib_context_free(c,cur->value);
grib_context_free(c,cur);
}
return GRIB_SUCCESS;
}
static char* dval_to_string(grib_context* c, double v)
{
char* sval=grib_context_malloc_clear(c,sizeof(char)*40);
sprintf(sval,"%.18e",v);
return sval;
}
static void dump_values(grib_dumper* d, grib_accessor* a)
{
grib_dumper_C *self = (grib_dumper_C*)d;
double value; size_t size = 0;
double *values=NULL;
int err = 0;
int i,r,icount;
int cols=2;
long count=0;
char* sval;
grib_context* c=a->context;
grib_handle* h=grib_handle_of_accessor(a);
grib_value_count(a,&count);
size=count;
if ( (a->flags & GRIB_ACCESSOR_FLAG_DUMP) == 0 || (a->flags & GRIB_ACCESSOR_FLAG_READ_ONLY) !=0)
return;
if (size>1) {
values=(double*)grib_context_malloc_clear(c,sizeof(double)*size);
err=grib_unpack_double(a,values,&size);
} else {
err=grib_unpack_double(a,&value,&size);
}
self->empty=0;
if (size>1) {
fprintf(self->dumper.out," free(rvalues); rvalues = NULL;\n\n");
fprintf(self->dumper.out," rvalues = (double*)malloc(%lu*sizeof(double));\n", (unsigned long)size);
fprintf(self->dumper.out," size = %lu;", size);
icount=0;
for (i=0; i<size-1; ++i) {
if (icount>cols || i==0) {fprintf(self->dumper.out,"\n ");icount=0;}
sval=dval_to_string(c,values[i]);
fprintf(self->dumper.out,"rvalues[%d]=%s; ", i, sval);
grib_context_free(c,sval);
icount++;
}
if (icount>cols || i==0) {fprintf(self->dumper.out,"\n ");icount=0;}
sval=dval_to_string(c,values[i]);
fprintf(self->dumper.out,"rvalues[%d]=%s;", i,sval);
grib_context_free(c,sval);
depth-=2;
fprintf(self->dumper.out,"\n");
grib_context_free(c,values);
if ((r=compute_key_rank(h,self->keys,a->name))!=0)
fprintf(self->dumper.out," CODES_CHECK(codes_set_double_array(h, \"#%d#%s\",rvalues, size), 0);\n", r, a->name);
else
fprintf(self->dumper.out," CODES_CHECK(codes_set_double_array(h, \"%s\", rvalues, size), 0);\n",a->name);
} else {
r=compute_key_rank(h,self->keys,a->name);
if( !grib_is_missing_double(a,value) ) {
sval=dval_to_string(c,value);
if (r!=0)
fprintf(self->dumper.out," CODES_CHECK(codes_set_double(h, \"#%d#%s\", %s), 0);\n", r, a->name, sval);
else
fprintf(self->dumper.out," CODES_CHECK(codes_set_double(h, \"%s\", %s), 0);\n", a->name, sval);
grib_context_free(c,sval);
}
}
if (self->isLeaf==0) {
char* prefix;
int dofree=0;
if (r!=0) {
prefix=grib_context_malloc_clear(c,sizeof(char)*(strlen(a->name)+10));
dofree=1;
sprintf(prefix,"#%d#%s",r,a->name);
} else prefix=(char*)a->name;
dump_attributes(d,a,prefix);
if (dofree) grib_context_free(c,prefix);
depth-=2;
}
(void)err; /* TODO */
}
static void dump_values_attribute(grib_dumper* d, grib_accessor* a, const char* prefix)
{
grib_dumper_C *self = (grib_dumper_C*)d;
double value; size_t size = 0;
double *values=NULL;
int err = 0;
int i,icount;
int cols=2;
long count=0;
char* sval;
grib_context* c=a->context;
grib_value_count(a,&count);
size=count;
if ( (a->flags & GRIB_ACCESSOR_FLAG_DUMP) == 0 || (a->flags & GRIB_ACCESSOR_FLAG_READ_ONLY) !=0)
return;
if (size>1) {
values=(double*)grib_context_malloc_clear(c,sizeof(double)*size);
err=grib_unpack_double(a,values,&size);
} else {
err=grib_unpack_double(a,&value,&size);
}
self->empty=0;
if (size>1) {
fprintf(self->dumper.out," free(rvalues); rvalues = NULL;\n");
fprintf(self->dumper.out," rvalues = (double*)malloc(%lu*sizeof(double));\n", (unsigned long)size);
fprintf(self->dumper.out," size = %lu;", size);
icount=0;
for (i=0; i<size-1; ++i) {
if (icount>cols || i==0) {fprintf(self->dumper.out,"\n ");icount=0;}
sval=dval_to_string(c,values[i]);
fprintf(self->dumper.out,"rvalues[%d]=%s; ", i, sval);
grib_context_free(c,sval);
icount++;
}
if (icount>cols || i==0) {fprintf(self->dumper.out,"\n ");icount=0;}
sval=dval_to_string(c,values[i]);
fprintf(self->dumper.out,"rvalues[%d]=%s;", i,sval);
grib_context_free(c,sval);
depth-=2;
fprintf(self->dumper.out,"\n");
grib_context_free(c,values);
fprintf(self->dumper.out," CODES_CHECK(codes_set_double_array(h, \"%s->%s\", rvalues, size), 0);\n", prefix,a->name);
} else {
/* int r=compute_key_rank(h,self->keys,a->name); */
if( !grib_is_missing_double(a,value) ) {
sval=dval_to_string(c,value);
fprintf(self->dumper.out," CODES_CHECK(codes_set_double(h, \"%s->%s\", %s), 0);\n", prefix,a->name, sval);
grib_context_free(c,sval);
}
}
if (self->isLeaf==0) {
char* prefix1;
prefix1=grib_context_malloc_clear(c,sizeof(char)*(strlen(a->name)+strlen(prefix)+5));
sprintf(prefix1,"%s->%s",prefix,a->name);
dump_attributes(d,a,prefix1);
grib_context_free(c,prefix1);
depth-=2;
}
(void)err; /* TODO */
}
static void dump_long(grib_dumper* d,grib_accessor* a, const char* comment)
{
grib_dumper_C *self = (grib_dumper_C*)d;
long value; size_t size = 0;
long *values=NULL;
int err = 0;
int i,r,icount;
int cols=4;
long count=0;
grib_context* c=a->context;
grib_handle* h=grib_handle_of_accessor(a);
grib_value_count(a,&count);
size=count;
if ( (a->flags & GRIB_ACCESSOR_FLAG_DUMP) == 0 ) return;
if ( (a->flags & GRIB_ACCESSOR_FLAG_READ_ONLY) != 0) {
if (self->isLeaf==0) {
char* prefix;
int dofree=0;
r=compute_key_rank(h,self->keys,a->name);
if (r!=0) {
prefix=grib_context_malloc_clear(c,sizeof(char)*(strlen(a->name)+10));
dofree=1;
sprintf(prefix,"#%d#%s",r,a->name);
} else prefix=(char*)a->name;
dump_attributes(d,a,prefix);
if (dofree) grib_context_free(c,prefix);
depth-=2;
}
return;
}
if (size>1) {
values=(long*)grib_context_malloc_clear(a->context,sizeof(long)*size);
err=grib_unpack_long(a,values,&size);
} else {
err=grib_unpack_long(a,&value,&size);
}
self->empty=0;
if (size>1) {
fprintf(self->dumper.out," free(ivalues); ivalues = NULL;\n\n");
fprintf(self->dumper.out," ivalues = (long*)malloc(%lu*sizeof(long));\n", (unsigned long)size);
fprintf(self->dumper.out," size = %lu;", size);
icount=0;
for (i=0;i<size-1;i++) {
if (icount>cols || i==0) {fprintf(self->dumper.out,"\n ");icount=0;}
fprintf(self->dumper.out,"ivalues[%d]=%ld; ", i, values[i]);
icount++;
}
if (icount>cols || i==0) {fprintf(self->dumper.out,"\n ");icount=0;}
fprintf(self->dumper.out,"ivalues[%d]=%ld;", i, values[i]);
depth-=2;
fprintf(self->dumper.out,"\n");
grib_context_free(a->context,values);
if ((r=compute_key_rank(h,self->keys,a->name))!=0)
fprintf(self->dumper.out," CODES_CHECK(codes_set_long_array(h, \"#%d#%s\", ivalues, size), 0);\n",r,a->name);
else
fprintf(self->dumper.out," CODES_CHECK(codes_set_long_array(h, \"%s\", ivalues, size), 0);\n",a->name);
} else {
r=compute_key_rank(h,self->keys,a->name);
if( !grib_is_missing_long(a,value) ) {
if (r!=0)
fprintf(self->dumper.out," CODES_CHECK(codes_set_long(h, \"#%d#%s\", ", r,a->name);
else
fprintf(self->dumper.out," CODES_CHECK(codes_set_long(h, \"%s\", ", a->name);
fprintf(self->dumper.out,"%ld), 0);\n",value);
}
}
if (self->isLeaf==0) {
char* prefix;
int dofree=0;
if (r!=0) {
prefix=grib_context_malloc_clear(c,sizeof(char)*(strlen(a->name)+10));
dofree=1;
sprintf(prefix,"#%d#%s",r,a->name);
} else prefix=(char*)a->name;
dump_attributes(d,a,prefix);
if (dofree) grib_context_free(c,prefix);
depth-=2;
}
(void)err; /* TODO */
}
static void dump_long_attribute(grib_dumper* d, grib_accessor* a, const char* prefix)
{
grib_dumper_C *self = (grib_dumper_C*)d;
long value; size_t size = 0;
long *values=NULL;
int err = 0;
int i,icount;
int cols=4;
long count=0;
grib_context* c=a->context;
grib_value_count(a,&count);
size=count;
if ( (a->flags & GRIB_ACCESSOR_FLAG_DUMP) == 0 || (a->flags & GRIB_ACCESSOR_FLAG_READ_ONLY) != 0)
return;
if (size>1) {
values=(long*)grib_context_malloc_clear(a->context,sizeof(long)*size);
err=grib_unpack_long(a,values,&size);
} else {
err=grib_unpack_long(a,&value,&size);
}
self->empty=0;
if (size>1) {
fprintf(self->dumper.out," free(ivalues); ivalues = NULL;\n");
fprintf(self->dumper.out," ivalues = (long*)malloc(%lu*sizeof(long));\n", (unsigned long)size);
fprintf(self->dumper.out," size = %lu;", size);
icount=0;
for (i=0;i<size-1;i++) {
if (icount>cols || i==0) {fprintf(self->dumper.out,"\n ");icount=0;}
fprintf(self->dumper.out,"ivalues[%d]=%ld; ", i, values[i]);
icount++;
}
if (icount>cols || i==0) {fprintf(self->dumper.out,"\n ");icount=0;}
fprintf(self->dumper.out,"ivalues[%d]=%ld;", i, values[i]);
depth-=2;
fprintf(self->dumper.out,"\n");
grib_context_free(a->context,values);
fprintf(self->dumper.out," CODES_CHECK(codes_set_long_array(h, \"%s->%s\", ivalues, size), 0);\n", prefix,a->name);
} else {
/* int r=compute_key_rank(h,self->keys,a->name); */
if( !grib_is_missing_long(a,value) ) {
fprintf(self->dumper.out," CODES_CHECK(codes_set_long(h, \"%s->%s\", ", prefix,a->name);
fprintf(self->dumper.out,"%ld), 0);\n",value);
}
}
if (self->isLeaf==0) {
char* prefix1;
prefix1=grib_context_malloc_clear(c,sizeof(char)*(strlen(a->name)+strlen(prefix)+5));
sprintf(prefix1,"%s->%s",prefix,a->name);
dump_attributes(d,a,prefix1);
grib_context_free(c,prefix1);
depth-=2;
}
(void)err; /* TODO */
}
static void dump_bits(grib_dumper* d, grib_accessor* a, const char* comment)
{
}
static void dump_double(grib_dumper* d, grib_accessor* a, const char* comment)
{
grib_dumper_C *self = (grib_dumper_C*)d;
double value; size_t size = 1;
int r;
char* sval;
grib_handle* h=grib_handle_of_accessor(a);
grib_context* c=h->context;
grib_unpack_double(a,&value,&size);
if ( (a->flags & GRIB_ACCESSOR_FLAG_DUMP) == 0 || (a->flags & GRIB_ACCESSOR_FLAG_READ_ONLY) != 0)
return;
self->empty=0;
r=compute_key_rank(h,self->keys,a->name);
if( !grib_is_missing_double(a,value) ) {
sval=dval_to_string(c,value);
if (r!=0)
fprintf(self->dumper.out," codes_set_double(h, \"#%d#%s\", %s);\n", r,a->name, sval);
else
fprintf(self->dumper.out," codes_set_double(h, \"%s\", %s);\n", a->name, sval);
grib_context_free(c,sval);
}
if (self->isLeaf==0) {
char* prefix;
int dofree=0;
if (r!=0) {
prefix=grib_context_malloc_clear(c,sizeof(char)*(strlen(a->name)+10));
dofree=1;
sprintf(prefix,"#%d#%s",r,a->name);
} else prefix=(char*)a->name;
dump_attributes(d,a,prefix);
if (dofree) grib_context_free(c,prefix);
depth-=2;
}
}
static void dump_string_array(grib_dumper* d, grib_accessor* a, const char* comment)
{
grib_dumper_C *self = (grib_dumper_C*)d;
char **values;
size_t size = 0,i=0;
grib_context* c=NULL;
int err = 0;
long count=0;
int r;
grib_handle* h=grib_handle_of_accessor(a);
c=a->context;
if ( (a->flags & GRIB_ACCESSOR_FLAG_DUMP) == 0 || (a->flags & GRIB_ACCESSOR_FLAG_READ_ONLY) != 0)
return;
grib_value_count(a,&count);
size=count;
if (size==1) {
dump_string(d,a,comment);
return;
}
fprintf(self->dumper.out," if(allocated(svalues)) deallocate(svalues)\n");
fprintf(self->dumper.out," allocate(svalues(%lu))\n", (unsigned long)size);
fprintf(self->dumper.out," svalues=(/");
self->empty=0;
values=(char**)grib_context_malloc_clear(c,size*sizeof(char*));
if (!values) {
grib_context_log(c,GRIB_LOG_FATAL,"unable to allocate %d bytes",(int)size);
return;
}
err = grib_unpack_string_array(a,values,&size);
for (i=0;i<size-1;i++) {
fprintf(self->dumper.out," \"%s\", &\n",values[i]);
}
fprintf(self->dumper.out," \"%s\" /)\n",values[i]);
if (self->isLeaf==0) {
if ((r=compute_key_rank(h,self->keys,a->name))!=0)
fprintf(self->dumper.out," call codes_set_string_array(ibufr,'#%d#%s',svalues)\n",r,a->name);
else
fprintf(self->dumper.out," call codes_set_string_array(ibufr,'%s',svalues)\n",a->name);
}
if (self->isLeaf==0) {
char* prefix;
int dofree=0;
if (r!=0) {
prefix=grib_context_malloc_clear(c,sizeof(char)*(strlen(a->name)+10));
dofree=1;
sprintf(prefix,"#%d#%s",r,a->name);
} else prefix=(char*)a->name;
dump_attributes(d,a,prefix);
if (dofree) grib_context_free(c,prefix);
depth-=2;
}
grib_context_free(c,values);
(void)err; /* TODO */
}
static void dump_string(grib_dumper* d, grib_accessor* a, const char* comment)
{
grib_dumper_C *self = (grib_dumper_C*)d;
char *value=NULL;
char *p = NULL;
size_t size = 0;
grib_context* c=NULL;
int r;
int err = _grib_get_string_length(a,&size);
grib_handle* h=grib_handle_of_accessor(a);
c=a->context;
if (size==0) return;
if ( (a->flags & GRIB_ACCESSOR_FLAG_DUMP) == 0 || (a->flags & GRIB_ACCESSOR_FLAG_READ_ONLY) != 0)
return;
value=(char*)grib_context_malloc_clear(c,size);
if (!value) {
grib_context_log(c,GRIB_LOG_FATAL,"unable to allocate %d bytes",(int)size);
return;
}
self->empty=0;
err = grib_unpack_string(a,value,&size);
p=value;
r=compute_key_rank(h,self->keys,a->name);
if (grib_is_missing_string(a,(unsigned char *)value,size))
return;
while(*p) { if(!isprint(*p)) *p = '.'; p++; }
if (self->isLeaf==0) {
depth+=2;
if (r!=0)
fprintf(self->dumper.out," call codes_set(ibufr,'#%d#%s',",r,a->name);
else
fprintf(self->dumper.out," call codes_set(ibufr,'%s',",a->name);
}
fprintf(self->dumper.out,"\'%s\')\n",value);
if (self->isLeaf==0) {
char* prefix;
int dofree=0;
if (r!=0) {
prefix=grib_context_malloc_clear(c,sizeof(char)*(strlen(a->name)+10));
dofree=1;
sprintf(prefix,"#%d#%s",r,a->name);
} else prefix=(char*)a->name;
dump_attributes(d,a,prefix);
if (dofree) grib_context_free(c,prefix);
depth-=2;
}
grib_context_free(c,value);
(void)err; /* TODO */
}
static void dump_bytes(grib_dumper* d, grib_accessor* a, const char* comment)
{
}
static void dump_label(grib_dumper* d, grib_accessor* a, const char* comment)
{
}
static void _dump_long_array(grib_handle* h, FILE* f, const char* key, const char* print_key)
{
long* val;
size_t size=0,i;
int cols=9,icount=0;
if (grib_get_size(h,key,&size)==GRIB_NOT_FOUND) return;
fprintf(f," free(ivalues); ivalues = NULL;\n");
fprintf(f," ivalues = (long*)malloc(%lu*sizeof(long));\n", (unsigned long)size);
fprintf(f," size = %lu;", size);
val=grib_context_malloc_clear(h->context,sizeof(long)*size);
grib_get_long_array(h,key,val,&size);
for (i=0;i<size-1;i++) {
if (icount>cols || i==0) {fprintf(f,"\n ");icount=0;}
fprintf(f,"ivalues[%lu]=%ld; ", i, val[i]);
icount++;
}
if (icount>cols) {fprintf(f,"\n ");}
fprintf(f,"ivalues[%lu]=%ld;\n", size-1, val[size-1]);
grib_context_free(h->context,val);
fprintf(f," CODES_CHECK(codes_set_long_array(h, \"%s\", ivalues, size), 0);\n",print_key);
}
static void dump_section(grib_dumper* d, grib_accessor* a, grib_block_of_accessors* block)
{
grib_dumper_C *self = (grib_dumper_C*)d;
if (!grib_inline_strcmp(a->name,"BUFR") ||
!grib_inline_strcmp(a->name,"GRIB") ||
!grib_inline_strcmp(a->name,"META")
) {
grib_handle* h=grib_handle_of_accessor(a);
depth=2;
self->empty=1;
depth+=2;
_dump_long_array(h,self->dumper.out,"dataPresentIndicator","inputDataPresentIndicator");
_dump_long_array(h,self->dumper.out,"delayedDescriptorReplicationFactor","inputDelayedDescriptorReplicationFactor");
_dump_long_array(h,self->dumper.out,"shortDelayedDescriptorReplicationFactor","inputShortDelayedDescriptorReplicationFactor");
_dump_long_array(h,self->dumper.out,"extendedDelayedDescriptorReplicationFactor","inputExtendedDelayedDescriptorReplicationFactor");
grib_dump_accessors_block(d,block);
depth-=2;
} else if (!grib_inline_strcmp(a->name,"groupNumber")) {
if ( (a->flags & GRIB_ACCESSOR_FLAG_DUMP) == 0)
return;
self->empty=1;
depth+=2;
grib_dump_accessors_block(d,block);
depth-=2;
} else {
grib_dump_accessors_block(d,block);
}
}
static void dump_attributes(grib_dumper* d,grib_accessor* a, const char* prefix)
{
int i=0;
grib_dumper_C *self = (grib_dumper_C*)d;
unsigned long flags;
while (i < MAX_ACCESSOR_ATTRIBUTES && a->attributes[i]) {
self->isAttribute=1;
if ( (d->option_flags & GRIB_DUMP_FLAG_ALL_ATTRIBUTES ) == 0
&& (a->attributes[i]->flags & GRIB_ACCESSOR_FLAG_DUMP)== 0 )
{
i++;
continue;
}
self->isLeaf=a->attributes[i]->attributes[0]==NULL ? 1 : 0;
flags=a->attributes[i]->flags;
a->attributes[i]->flags |= GRIB_ACCESSOR_FLAG_DUMP;
switch (grib_accessor_get_native_type(a->attributes[i])) {
case GRIB_TYPE_LONG:
dump_long_attribute(d,a->attributes[i],prefix);
break;
case GRIB_TYPE_DOUBLE:
dump_values_attribute(d,a->attributes[i],prefix);
break;
case GRIB_TYPE_STRING:
break;
}
a->attributes[i]->flags=flags;
i++;
}
self->isLeaf=0;
self->isAttribute=0;
}
static void header(grib_dumper* d, grib_handle* h)
{
grib_dumper_C *self = (grib_dumper_C*)d;
char sampleName[200]={0};
long localSectionPresent,edition,bufrHeaderCentre,isSatellite;
grib_get_long(h,"localSectionPresent",&localSectionPresent);
grib_get_long(h,"bufrHeaderCentre",&bufrHeaderCentre);
grib_get_long(h,"edition",&edition);
if (localSectionPresent && bufrHeaderCentre==98 ) {
grib_get_long(h,"isSatellite",&isSatellite);
if (isSatellite)
sprintf(sampleName,"BUFR%ld_local_satellite",edition);
else
sprintf(sampleName,"BUFR%ld_local",edition);
} else {
sprintf(sampleName,"BUFR%ld",edition);
}
if (d->count<2) {
fprintf(self->dumper.out,"/* This program has been automatically generated with bufr_dump -EC */\n");
fprintf(self->dumper.out,"/* Using ecCodes version: ");
grib_print_api_version(self->dumper.out);
fprintf(self->dumper.out, " */\n\n");
fprintf(self->dumper.out,"#include \"eccodes.h\"\n");
fprintf(self->dumper.out,"int main()\n");
fprintf(self->dumper.out,"{\n");
fprintf(self->dumper.out," size_t size=0;\n");
fprintf(self->dumper.out," const void* buffer = NULL;\n");
fprintf(self->dumper.out," FILE * fout = NULL;\n");
fprintf(self->dumper.out," codes_handle* h = NULL;\n");
fprintf(self->dumper.out," long* ivalues = NULL;\n");
fprintf(self->dumper.out," char** svalues = NULL;\n");
fprintf(self->dumper.out," double* rvalues = NULL;\n\n");
fprintf(self->dumper.out," const char* sampleName = \"%s\";\n\n", sampleName);
}
fprintf(self->dumper.out," h = codes_bufr_handle_new_from_samples(NULL, sampleName);\n");
fprintf(self->dumper.out," if (h == NULL) {\n");
fprintf(self->dumper.out," fprintf(stderr, \"ERROR creating BUFR from %%s\\n\", sampleName);\n");
fprintf(self->dumper.out," return 1;\n");
fprintf(self->dumper.out," }\n");
}
static void footer(grib_dumper* d, grib_handle* h)
{
grib_dumper_C *self = (grib_dumper_C*)d;
fprintf(self->dumper.out,"\n codes_set_long(h, \"pack\", 1);\n");
if (d->count==1)
fprintf(self->dumper.out," fout = fopen(\"outfile.bufr\", \"w\");\n");
else
fprintf(self->dumper.out," fout = fopen(\"outfile.bufr\", \"a\");\n");
/*fprintf(self->dumper.out," fout = fopen(\"outfile.bufr\", \"w\");");*/
fprintf(self->dumper.out," if (!fout) {\n");
fprintf(self->dumper.out," fprintf(stderr, \"Failed to open output file.\\n\");\n");
fprintf(self->dumper.out," return 1;\n");
fprintf(self->dumper.out," }\n");
fprintf(self->dumper.out," CODES_CHECK(codes_get_message(h,&buffer,&size),0);\n");
fprintf(self->dumper.out," if (fwrite(buffer,1,size,fout) != size) {\n");
fprintf(self->dumper.out," fprintf(stderr, \"Failed to write data.\\n\");\n");
fprintf(self->dumper.out," return 1;\n");
fprintf(self->dumper.out," }\n");
fprintf(self->dumper.out," if (fclose(fout)) {\n");
fprintf(self->dumper.out," fprintf(stderr, \"Failed to close file handle.\\n\");\n");
fprintf(self->dumper.out," return 1;\n");
fprintf(self->dumper.out," }\n");
fprintf(self->dumper.out," \n");
fprintf(self->dumper.out," codes_handle_delete(h);\n");
fprintf(self->dumper.out," free(ivalues); ivalues = NULL;\n");
fprintf(self->dumper.out," free(rvalues); rvalues = NULL;\n");
fprintf(self->dumper.out," free(svalues); svalues = NULL;\n\n");
}

View File

@ -1,4 +1,5 @@
/* This file is automatically generated by ./make_class.pl, do not edit */
{ "C", &grib_dumper_class_C, },
{ "c_code", &grib_dumper_class_c_code, },
{ "debug", &grib_dumper_class_debug, },
{ "default", &grib_dumper_class_default, },

View File

@ -112,6 +112,9 @@ list( APPEND tests2
if( HAVE_FORTRAN AND ENABLE_EXTRA_TESTS )
list(APPEND tests2 bufr_dump_fortran)
endif()
if( ENABLE_EXTRA_TESTS )
list(APPEND tests2 bufr_dump_C)
endif()
# These tests do not require any data downloads
foreach( test ${tests1} )
@ -143,6 +146,9 @@ endforeach()
if( HAVE_FORTRAN AND ENABLE_EXTRA_TESTS )
set_tests_properties ( eccodes_t_bufr_dump_fortran PROPERTIES TIMEOUT 2500)
endif()
if( ENABLE_EXTRA_TESTS )
set_tests_properties ( eccodes_t_bufr_dump_C PROPERTIES TIMEOUT 2500)
endif()
if( HAVE_PYTHON AND ENABLE_EXTRA_TESTS )

104
tests/bufr_dump_C.sh Executable file
View File

@ -0,0 +1,104 @@
#!/bin/sh
# Copyright 2005-2016 ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
#
# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
#
. ./include.sh
#Define a common label for all the tmp files
label="bufr_dump_C_test"
#Create log file
fLog=${label}".log"
rm -f $fLog
tempBufr=outfile.bufr
tempDir=${label}.dir
mkdir -p $tempDir
#bufr_files=`cat ${data_dir}/bufr/bufr_data_files.txt`
bufr_files="
207003.bufr ascs_139.bufr b006_96.bufr goga_89.bufr new.bufr soil_7.bufr
aaen_55.bufr aseh_139.bufr btem_109.bufr hirb_55.bufr nomi_206.bufr ssbt_127.bufr
aben_55.bufr asel_139.bufr cmwi_87.bufr hirs_55.bufr ocea_131.bufr stuk_7.bufr
ahws_139.bufr ashs_139.bufr cmwn_87.bufr ias1_240.bufr ocea_21.bufr syno_1.bufr
airs_57.bufr atap_55.bufr cori_156.bufr iasi_241.bufr pilo_91.bufr syno_2.bufr
alws_139.bufr ateu_155.bufr crit_202.bufr itwt_233.bufr profiler_european.bufr syno_3.bufr
amsa_55.bufr atms_201.bufr csrh_189.bufr jaso_214.bufr rada_250.bufr syno_4.bufr
amsb_55.bufr atov_55.bufr emsg_189.bufr maer_207.bufr rado_250.bufr syno_multi.bufr
amse_55.bufr avhm_87.bufr emsg_87.bufr mhen_55.bufr s4kn_165.bufr temp-land-with-substituted-values.bufr
amsu_55.bufr avhn_87.bufr euwv_87.bufr mhsa_55.bufr sb19_206.bufr temp_101.bufr
amv2_87.bufr avhr_58.bufr fy3a_154.bufr mhsb_55.bufr sbu8_206.bufr tmr7_129.bufr
amv3_87.bufr b002_95.bufr fy3b_154.bufr mhse_55.bufr smin_49.bufr uegabe.bufr
asbh_139.bufr b002_96.bufr g2to_206.bufr mloz_206.bufr smis_49.bufr
asbl_139.bufr b003_56.bufr go15_87.bufr modi_87.bufr smiu_49.bufr
asca_139.bufr b005_87.bufr goee_87.bufr modw_87.bufr smos_203.bufr
asch_139.bufr b005_89.bufr goes_87.bufr monw_87.bufr sn4k_165.bufr
"
# pkgconfig should be one level above the test dir
PKGCONFIG_FILE=../eccodes.pc
CACHE_FILE=../CMakeCache.txt
COMPILE_AND_RUN=0
if command -v pkg-config >/dev/null 2>&1; then
if [ -f "$PKGCONFIG_FILE" ]; then
# Work out the C compiler and flags from pkgconfig
COMPILER=`pkg-config --variable=CC $PKGCONFIG_FILE`
FLAGS_COMPILER=`pkg-config --cflags $PKGCONFIG_FILE`
FLAGS_LINKER=`pkg-config --libs $PKGCONFIG_FILE`
# The pkgconfig variables refer to the install directory. Change to build dir
BUILD_DIR=`grep -w eccodes_BINARY_DIR $CACHE_FILE | cut -d'=' -f2`
INSTALL_DIR=`grep -w CMAKE_INSTALL_PREFIX $CACHE_FILE | cut -d'=' -f2`
FLAGS_LINKER=`echo $FLAGS_LINKER | sed -e "s:$INSTALL_DIR:$BUILD_DIR:g"`
FLAGS_COMPILER=`echo $FLAGS_COMPILER | sed -e "s:$INSTALL_DIR:$BUILD_DIR:g"`
# TODO: For now only support when shared libs enabled
SHARED_LIBS=`grep -w BUILD_SHARED_LIBS $CACHE_FILE | cut -d'=' -f2`
if [ "$SHARED_LIBS" = "ON" ]; then
COMPILE_AND_RUN=1
fi
fi
fi
cd $tempDir
for file in ${bufr_files}
do
tempSrc=$label.$file.c
tempExe=$label.$file.exe
# Generate C code from BUFR file
${tools_dir}bufr_dump -EC ${data_dir}/bufr/$file > $tempSrc
# Compile
if [ $COMPILE_AND_RUN -eq 1 ]; then
# TODO: eccodes.h and the generated eccodes_version.h need to be pointed to
# Should be copied over to the build/include dir
INCL_DIR1=${proj_dir}/src
INCL_DIR2=${data_dir}/../src
$COMPILER -o $tempExe $tempSrc -I${INCL_DIR1} -I${INCL_DIR2} $FLAGS_COMPILER $FLAGS_LINKER
# The executable always creates a file called outfile.bufr
# valgrind --error-exitcode=1 ./$tempExe
./$tempExe
${tools_dir}bufr_compare ${data_dir}/bufr/$file $tempBufr
TEMP_JSON1=${label}.$file.json
TEMP_JSON2=${label}.$tempBufr.json
${tools_dir}bufr_dump ${data_dir}/bufr/$file > $TEMP_JSON1
${tools_dir}bufr_dump $tempBufr > $TEMP_JSON2
diff $TEMP_JSON1 $TEMP_JSON2
rm -f $TEMP_JSON1 $TEMP_JSON2
fi
rm -f $tempExe $tempSrc $tempBufr
done

View File

@ -25,6 +25,7 @@ grib_option grib_options[]={
"\n\t\tOptions: filter -> filter instructions file to encode input BUFR"
"\n\t\t fortran -> fortran program to encode the input BUFR"
"\n\t\t python -> python script to encode the input BUFR"
"\n\t\t C -> C program to encode the input BUFR"
"\n\t\tDefault mode is filter.\n",
0,1,"filter"},
{"S",0,0,1,0,0},
@ -306,6 +307,11 @@ int grib_tool_finalise_action(grib_runtime_options* options)
if (!strcmp(options->dump_mode,"fortran")) {
fprintf(stdout,"end program bufr_create_message\n");
}
if (!strcmp(options->dump_mode,"C")) {
fprintf(stdout," return 0;\n");
fprintf(stdout,"}\n");
}
if (!strcmp(options->dump_mode,"python")) {
fprintf(stdout,"\n\n");
fprintf(stdout,"def main():\n");