Merge grib api and eccodes: part 9

This commit is contained in:
Shahram Najm 2014-07-07 21:55:19 +01:00
parent 2113980a78
commit f2005d0bc6
8 changed files with 489 additions and 80 deletions

View File

@ -904,6 +904,29 @@ int grib_f_multi_support_off(){
return grib_f_multi_support_off_();
}
int grib_f_navigate_subgroups_(int* gid){
grib_handle* h=get_handle(*gid);
if (!h) return GRIB_NULL_HANDLE;
return grib_navigate_subgroups(h);
}
int grib_f_navigate_subgroups__(int* gid){
return grib_f_navigate_subgroups_(gid);
}
int grib_f_navigate_subgroups(int* gid){
return grib_f_navigate_subgroups_(gid);
}
int grib_f_not_navigate_subgroups_(int* gid){
grib_handle* h=get_handle(*gid);
if (!h) return GRIB_NULL_HANDLE;
return grib_not_navigate_subgroups(h);
}
int grib_f_not_navigate_subgroups__(int* gid){
return grib_f_not_navigate_subgroups_(gid);
}
int grib_f_not_navigate_subgroups(int* gid){
return grib_f_not_navigate_subgroups_(gid);
}
static int _grib_f_iterator_new_(int* gid,int* iterid,int* mode) {
int err=0;
@ -1294,6 +1317,34 @@ int grib_f_count_in_file__(int* fid,int* n) {
return grib_f_count_in_file(fid,n);
}
int bufr_f_new_from_file_(int* fid, int* gid){
int err = 0;
FILE* f = get_file(*fid);
grib_handle *h = NULL;
if(f){
h = bufr_new_from_file(0,f,0,&err);
if(h){
push_handle(h,gid);
return GRIB_SUCCESS;
} else {
*gid=-1;
return GRIB_END_OF_FILE;
}
}
*gid=-1;
return GRIB_INVALID_FILE;
}
int bufr_f_new_from_file__(int* fid, int* gid){
return bufr_f_new_from_file_( fid, gid);
}
int bufr_f_new_from_file(int* fid, int* gid){
return bufr_f_new_from_file_( fid, gid);
}
int grib_f_new_from_file_(int* fid, int* gid){
int err = 0;
FILE* f = get_file(*fid);
@ -1329,7 +1380,7 @@ int grib_f_headers_only_new_from_file_(int* fid, int* gid){
grib_handle *h = NULL;
if(f){
h=eccode_grib_new_from_file ( 0, f,1,&err);
h=grib_new_from_file ( 0, f,1,&err);
if(h){
push_handle(h,gid);
return GRIB_SUCCESS;

View File

@ -982,7 +982,7 @@ int grib_c_new_gts_from_file(FILE* f,int headers_only, int* gid){
int err = 0;
if(f){
h = eccode_gts_new_from_file(0,f,headers_only,&err);
h = gts_new_from_file(0,f,headers_only,&err);
if(h){
push_handle(h,gid);
@ -1002,7 +1002,7 @@ int grib_c_new_bufr_from_file(FILE* f,int headers_only,int* gid){
int err = 0;
if(f){
h = eccode_bufr_new_from_file(0,f,headers_only,&err);
h = bufr_new_from_file(0,f,headers_only,&err);
if(h){
push_handle(h,gid);
@ -1022,7 +1022,7 @@ int grib_c_new_from_file(FILE* f, int* gid, int headers_only){
int err = 0;
if(f){
h=eccode_grib_new_from_file(0,f,headers_only,&err);
h=grib_new_from_file(0,f,headers_only,&err);
if(h){
push_handle(h,gid);

View File

@ -857,11 +857,13 @@ grib_handle *grib_handle_new_from_partial_message(grib_context *c, void *data, s
grib_handle *grib_handle_new_from_message(grib_context *c, void *data, size_t buflen);
grib_handle *grib_handle_new_from_multi_message(grib_context *c, void **data, size_t *buflen, int *error);
grib_handle *grib_handle_new_from_file(grib_context *c, FILE *f, int *error);
grib_handle *grib_new_from_file(grib_context *c, FILE *f, int headers_only, int *error);
grib_handle *eccode_grib_new_from_file(grib_context *c, FILE *f, int headers_only, int *error);
grib_handle *eccode_gts_new_from_file(grib_context *c, FILE *f, int headers_only, int *error);
grib_handle *eccode_bufr_new_from_file(grib_context *c, FILE *f, int headers_only, int *error);
grib_handle *gts_new_from_file(grib_context *c, FILE *f, int headers_only, int *error);
grib_handle *bufr_new_from_file(grib_context *c, FILE *f, int headers_only, int *error);
grib_handle *taf_new_from_file(grib_context *c, FILE *f, int headers_only, int *error);
grib_handle *metar_new_from_file(grib_context *c, FILE *f, int headers_only, int *error);
grib_handle *codes_new_from_file(grib_context *c, FILE *f, int headers_only, int *error);
grib_multi_handle *grib_multi_handle_new(grib_context *c);
int grib_multi_handle_delete(grib_multi_handle *h);
int grib_multi_handle_append(grib_handle *h, int start_section, grib_multi_handle *mh);

View File

@ -393,7 +393,7 @@ grib_handle* grib_handle_new_from_multi_message ( grib_context* c,void** data,
grib_handle* grib_handle_new_from_file ( grib_context* c, FILE* f,int *error )
{
return eccode_grib_new_from_file(c,f,0,error);
return grib_new_from_file(c,f,0,error);
}
static grib_handle* grib_handle_new_multi ( grib_context* c,unsigned char** data,
@ -694,7 +694,7 @@ static grib_handle* grib_handle_new_from_file_multi ( grib_context* c, FILE* f,i
return gl;
}
grib_handle* eccode_grib_new_from_file ( grib_context* c, FILE* f,int headers_only,int *error )
grib_handle* grib_new_from_file ( grib_context* c, FILE* f,int headers_only,int *error )
{
grib_handle* h=0;
if (!f) {*error=GRIB_IO_PROBLEM; return NULL;}
@ -716,7 +716,7 @@ grib_handle* eccode_grib_new_from_file ( grib_context* c, FILE* f,int headers_on
}
grib_handle* eccode_gts_new_from_file ( grib_context* c, FILE* f,int headers_only,int *error )
grib_handle* gts_new_from_file ( grib_context* c, FILE* f,int headers_only,int *error )
{
void *data = NULL;
size_t olen = 0;
@ -757,7 +757,130 @@ grib_handle* eccode_gts_new_from_file ( grib_context* c, FILE* f,int headers_onl
return gl;
}
grib_handle* eccode_bufr_new_from_file ( grib_context* c, FILE* f,int headers_only,int *error )
grib_handle* taf_new_from_file ( grib_context* c, FILE* f,int headers_only,int *error )
{
void *data = NULL;
size_t olen = 0;
grib_handle *gl = NULL;
off_t offset=0;
if ( c == NULL ) c = grib_context_get_default();
data = wmo_read_taf_from_file_malloc ( f, headers_only,&olen,&offset,error );
if ( *error != GRIB_SUCCESS )
{
if ( data ) grib_context_free ( c,data );
if ( *error == GRIB_END_OF_FILE ) *error = GRIB_SUCCESS;
return NULL;
}
if (headers_only) {
gl = grib_handle_new_from_partial_message ( c, data, olen );
} else {
gl = grib_handle_new_from_message ( c, data, olen );
}
if ( !gl )
{
*error = GRIB_DECODING_ERROR;
grib_context_log ( gl->context, GRIB_LOG_ERROR, "grib_handle_new_from_file : cannot create handle \n" );
grib_context_free ( c,data );
return NULL;
}
gl->offset=offset;
gl->buffer->property = GRIB_MY_BUFFER;
c->handle_file_count++;
c->handle_total_count++;
return gl;
}
grib_handle* metar_new_from_file ( grib_context* c, FILE* f,int headers_only,int *error )
{
void *data = NULL;
size_t olen = 0;
grib_handle *gl = NULL;
off_t offset=0;
if ( c == NULL ) c = grib_context_get_default();
data = wmo_read_metar_from_file_malloc ( f, headers_only,&olen,&offset,error );
if ( *error != GRIB_SUCCESS )
{
if ( data ) grib_context_free ( c,data );
if ( *error == GRIB_END_OF_FILE ) *error = GRIB_SUCCESS;
return NULL;
}
if (headers_only) {
gl = grib_handle_new_from_partial_message ( c, data, olen );
} else {
gl = grib_handle_new_from_message ( c, data, olen );
}
if ( !gl )
{
*error = GRIB_DECODING_ERROR;
grib_context_log ( gl->context, GRIB_LOG_ERROR, "grib_handle_new_from_file : cannot create handle \n" );
grib_context_free ( c,data );
return NULL;
}
gl->offset=offset;
gl->buffer->property = GRIB_MY_BUFFER;
c->handle_file_count++;
c->handle_total_count++;
return gl;
}
grib_handle* codes_new_from_file ( grib_context* c, FILE* f,int headers_only,int *error )
{
void *data = NULL;
size_t olen = 0;
grib_handle *gl = NULL;
off_t offset=0;
if ( c == NULL ) c = grib_context_get_default();
data = wmo_read_any_from_file_malloc ( f, headers_only,&olen,&offset,error );
if ( *error != GRIB_SUCCESS )
{
if ( *error == GRIB_END_OF_FILE ) *error = GRIB_SUCCESS;
return NULL;
}
if (headers_only) {
gl = grib_handle_new_from_partial_message ( c, data, olen );
} else {
gl = grib_handle_new_from_message ( c, data, olen );
}
if ( !gl )
{
*error = GRIB_DECODING_ERROR;
grib_context_log ( gl->context, GRIB_LOG_ERROR, "grib_handle_new_from_file : cannot create handle \n" );
grib_context_free ( c,data );
return NULL;
}
gl->offset=offset;
gl->buffer->property = GRIB_MY_BUFFER;
c->handle_file_count++;
c->handle_total_count++;
return gl;
}
grib_handle* bufr_new_from_file ( grib_context* c, FILE* f,int headers_only,int *error )
{
void *data = NULL;
size_t olen = 0;

View File

@ -897,6 +897,177 @@ int grib_index_search_same(grib_index* index, grib_handle* h)
return 0;
}
int grib_index_add_file(grib_index* index,const char* filename) {
return codes_index_add_file(index,filename,CODES_GRIB);
}
int codes_index_add_file(grib_index* index,const char* filename,int message_type)
{
double dval;
size_t svallen;
long length,lval;
char buf[1024]={0,};
int err=0;
grib_file* indfile;
grib_file* newfile;
grib_index_key* index_key=NULL;
grib_handle* h=NULL;
grib_field* field;
grib_field_tree* field_tree;
grib_file* file=NULL;
grib_context* c;
typedef grib_handle* (*message_new_proc) ( grib_context*,FILE*,int,int* );
message_new_proc message_new=NULL;
switch (message_type) {
case CODES_GRIB:
message_new=grib_new_from_file;
break;
case CODES_BUFR:
message_new=bufr_new_from_file;
break;
default:
Assert(0);
}
if (!index) return GRIB_NULL_INDEX;
c=index->context;
file=grib_file_open(filename,"r",&err);
if (!file || !file->handle) return err;
if (!index->files) {
grib_filesid++;
newfile=grib_context_malloc_clear(c,sizeof(grib_file));
newfile->id=grib_filesid;
newfile->name=strdup(file->name);
index->files=newfile;
} else {
indfile=index->files;
while(indfile) {
if (!strcmp(indfile->name,file->name)) return 0;
indfile=indfile->next;
}
indfile=index->files;
while(indfile->next) indfile=indfile->next;
grib_filesid++;
newfile=grib_context_malloc_clear(c,sizeof(grib_file));
newfile->id=grib_filesid;
newfile->name=strdup(file->name);
indfile->next=newfile;
}
fseeko(file->handle,0,SEEK_SET);
while ((h=message_new(c,file->handle,0,&err))!=NULL) {
grib_string_list* v=0;
index_key=index->keys;
field_tree=index->fields;
index_key->value[0]=0;
while (index_key) {
if (index_key->type==GRIB_TYPE_UNDEFINED) {
err=grib_get_native_type(h,index_key->name,&(index_key->type));
if (err) index_key->type=GRIB_TYPE_STRING;
}
svallen=1024;
switch (index_key->type) {
case GRIB_TYPE_STRING:
err=grib_get_string(h,index_key->name,buf,&svallen);
if (err==GRIB_NOT_FOUND) sprintf(buf,GRIB_KEY_UNDEF);
break;
case GRIB_TYPE_LONG:
err=grib_get_long(h,index_key->name,&lval);
if (err==GRIB_NOT_FOUND) sprintf(buf,GRIB_KEY_UNDEF);
else sprintf(buf,"%ld",lval);
break;
case GRIB_TYPE_DOUBLE:
err=grib_get_double(h,index_key->name,&dval);
if (err==GRIB_NOT_FOUND) sprintf(buf,GRIB_KEY_UNDEF);
else sprintf(buf,"%g",dval);
break;
default :
err=GRIB_WRONG_TYPE;
return err;
}
if (err && err != GRIB_NOT_FOUND) {
grib_context_log(c,GRIB_LOG_ERROR,"unable to create index. \"%s\": %s",index_key->name,grib_get_error_message(err));
return err;
}
if (!index_key->values->value) {
index_key->values->value=grib_context_strdup(c,buf);
index_key->values_count++;
} else {
v=index_key->values;
while (v->next && strcmp(v->value,buf)) v=v->next;
if (strcmp(v->value,buf)) {
index_key->values_count++;
if (v->next) v=v->next;
v->next=grib_context_malloc_clear(c,sizeof(grib_string_list));
v->next->value=grib_context_strdup(c,buf);
}
}
if (!field_tree->value) {
field_tree->value=grib_context_strdup(c,buf);
} else {
while (field_tree->next &&
(field_tree->value==NULL ||
strcmp(field_tree->value,buf)))
field_tree=field_tree->next;
if (!field_tree->value || strcmp(field_tree->value,buf)){
field_tree->next=
(grib_field_tree*)grib_context_malloc_clear(c,
sizeof(grib_field_tree));
field_tree=field_tree->next;
field_tree->value=grib_context_strdup(c,buf);
}
}
if (index_key->next) {
if (!field_tree->next_level) {
field_tree->next_level=
grib_context_malloc_clear(c,sizeof(grib_field_tree));
}
field_tree=field_tree->next_level;
}
index_key=index_key->next;
}
field=grib_context_malloc_clear(c,sizeof(grib_field));
field->file=file;
index->count++;
field->offset=h->offset;;
err=grib_get_long(h,"totalLength",&length);
if (err) return err;
field->length=length;
if (field_tree->field) {
grib_field* pfield=field_tree->field;
while (pfield->next) pfield=pfield->next;
pfield->next=field;
} else
field_tree->field=field;
if (h) grib_handle_delete(h);
}
grib_file_close(file->name,&err);
if (err) return err;
index->rewind=1;
return GRIB_SUCCESS;
}
#if 0
int grib_index_add_file(grib_index* index, const char* filename)
{
double dval;
@ -1056,6 +1227,7 @@ int grib_index_add_file(grib_index* index, const char* filename)
index->rewind=1;
return GRIB_SUCCESS;
}
#endif
grib_index* grib_index_new_from_file(grib_context* c, char* filename, const char* keys, int *err)
{
@ -1253,21 +1425,54 @@ int grib_index_select_string(grib_index* index,const char* skey,char* value)
return 0;
}
grib_handle* grib_index_get_handle(grib_field* field,int *err)
{
grib_handle* h=NULL;
grib_file_open(field->file->name,"r",err);
if (*err!=GRIB_SUCCESS) return NULL;
fseeko(field->file->handle,field->offset,SEEK_SET);
h=grib_handle_new_from_file(0,field->file->handle,err);
if (*err!=GRIB_SUCCESS) return NULL;
grib_file_close(field->file->name,err);
return h;
grib_handle* grib_index_get_handle(grib_field* field,int *err) {
return codes_index_get_handle(field,CODES_GRIB,err);
}
grib_handle* codes_index_get_handle(grib_field* field,int message_type,int *err)
{
grib_handle* h=NULL;
typedef grib_handle* (*message_new_proc) ( grib_context*,FILE*,int,int* );
message_new_proc message_new=NULL;
grib_file_open(field->file->name,"r",err);
if (*err!=GRIB_SUCCESS) return NULL;
switch (message_type) {
case CODES_GRIB:
message_new=grib_new_from_file;
break;
case CODES_BUFR:
message_new=bufr_new_from_file;
break;
default :
Assert(0);
}
fseeko(field->file->handle,field->offset,SEEK_SET);
h=message_new(0,field->file->handle,0,err);
if (*err!=GRIB_SUCCESS) return NULL;
grib_file_close(field->file->name,err);
return h;
}
/*
* grib_handle* grib_index_get_handle(grib_field* field,int *err)
* {
* grib_handle* h=NULL;
* grib_file_open(field->file->name,"r",err);
*
* if (*err!=GRIB_SUCCESS) return NULL;
*
* fseeko(field->file->handle,field->offset,SEEK_SET);
* h=grib_handle_new_from_file(0,field->file->handle,err);
* if (*err!=GRIB_SUCCESS) return NULL;
*
* grib_file_close(field->file->name,err);
* return h;
* }
*/
static int grib_index_execute(grib_index* index)
{
grib_index_key* keys = NULL;
@ -1322,65 +1527,69 @@ char* grib_get_field_file(grib_index* index,off_t *offset)
return file;
}
grib_handle* grib_handle_new_from_index(grib_index* index,int *err)
{
grib_index_key* keys;
grib_field_list *fieldset,*next;
grib_handle* h=NULL;
grib_context* c=NULL;
grib_handle* grib_handle_new_from_index(grib_index* index,int *err) {
return codes_new_from_index(index,CODES_GRIB,err);
}
if (!index) return NULL;
c=index->context;
if (!index->rewind) {
if (!index->current) {
*err=GRIB_END_OF_INDEX;
return NULL;
}
grib_handle* codes_new_from_index(grib_index* index,int message_type,int *err) {
grib_index_key* keys;
grib_field_list *fieldset,*next;
grib_handle* h=NULL;
grib_context* c=NULL;
if (index->current->field->next)
index->current->field=index->current->field->next;
else if(index->current->next)
index->current=index->current->next;
else {*err=GRIB_END_OF_INDEX;return NULL;}
if (!index) return NULL;
c=index->context;
if (!index->rewind) {
if (!index->current) {
*err=GRIB_END_OF_INDEX;
return NULL;
}
h=grib_index_get_handle(index->current->field,err);
return h;
}
if (index->current->field->next)
index->current->field=index->current->field->next;
else if(index->current->next)
index->current=index->current->next;
else {*err=GRIB_END_OF_INDEX;return NULL;}
if (!index->fieldset) {
index->fieldset=grib_context_malloc_clear(index->context,
sizeof(grib_field_list));
if (!index->fieldset) {
grib_context_log(index->context,GRIB_LOG_ERROR,
"unable to allocat %d bytes",
sizeof(grib_field_list));
return NULL;
}
index->current=index->fieldset;
} else {
fieldset=index->fieldset;
while(fieldset->next) {
next=fieldset->next;
grib_context_free(c,fieldset);
fieldset=next;
}
fieldset->field=NULL;
fieldset->next=NULL;
index->fieldset=fieldset;
index->current=fieldset;
}
h=codes_index_get_handle(index->current->field,message_type,err);
return h;
}
*err=GRIB_END_OF_INDEX;
h=NULL;
keys=index->keys;
if (!index->fieldset) {
index->fieldset=grib_context_malloc_clear(index->context,
sizeof(grib_field_list));
if (!index->fieldset) {
grib_context_log(index->context,GRIB_LOG_ERROR,
"unable to allocat %d bytes",
sizeof(grib_field_list));
return NULL;
}
index->current=index->fieldset;
} else {
fieldset=index->fieldset;
while(fieldset->next) {
next=fieldset->next;
grib_context_free(c,fieldset);
fieldset=next;
}
fieldset->field=NULL;
fieldset->next=NULL;
index->fieldset=fieldset;
index->current=fieldset;
}
if ((*err=grib_index_execute(index))==GRIB_SUCCESS) {
*err=GRIB_END_OF_INDEX;
h=NULL;
keys=index->keys;
if ((*err=grib_index_execute(index))==GRIB_SUCCESS) {
if (!index->fieldset) {*err=GRIB_END_OF_INDEX;return NULL;}
index->current=index->fieldset;
h=codes_index_get_handle(index->current->field,message_type,err);
}
return h;
if (!index->fieldset) {*err=GRIB_END_OF_INDEX;return NULL;}
index->current=index->fieldset;
h=grib_index_get_handle(index->current->field,err);
}
return h;
}
void grib_index_rewind(grib_index* index)

View File

@ -345,7 +345,7 @@ static void print_index_key_values(grib_index* index,int counter,const char* err
static grib_handle* grib_handle_new_from_file_x(grib_context* c,FILE* f,int
mode,int headers_only,int *err) {
return eccode_bufr_new_from_file(c,f,headers_only,err);
return bufr_new_from_file(c,f,headers_only,err);
}
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h) {

View File

@ -126,12 +126,21 @@ static int is_index_file(const char* filename)
static grib_handle* grib_handle_new_from_file_x(grib_context* c,FILE* f,int mode,int headers_only,int *err)
{
if (mode==MODE_GTS)
return eccode_gts_new_from_file(c,f,headers_only,err);
return gts_new_from_file(c,f,headers_only,err);
if (mode==MODE_BUFR)
return eccode_bufr_new_from_file(c,f,headers_only,err);
return bufr_new_from_file(c,f,headers_only,err);
return eccode_grib_new_from_file(c,f,headers_only,err);
if (mode==MODE_METAR)
return metar_new_from_file(c,f,headers_only,err);
if (mode==MODE_TAF)
return taf_new_from_file(c,f,headers_only,err);
if (mode==MODE_GRIB)
return grib_new_from_file(c,f,headers_only,err);
return codes_new_from_file(c,f,headers_only,err);
}
int grib_tool(int argc, char **argv)
@ -374,10 +383,23 @@ static int grib_tool_without_orderby(grib_runtime_options* options)
static int navigate(grib_field_tree* fields,grib_runtime_options* options)
{
int err=0;
int message_type=0;
if (!fields || options->stop) return 0;
switch (options->mode) {
case MODE_GRIB:
message_type=CODES_GRIB;
break;
case MODE_BUFR:
message_type=CODES_BUFR;
break;
default :
Assert(0);
}
if (fields->field) {
grib_handle* h=grib_index_get_handle(fields->field,&err);
grib_handle* h=codes_index_get_handle(fields->field,message_type,&err);
if (!options->index2->current)
options->index2->current=grib_context_malloc_clear(options->context,sizeof(grib_field_list));
options->index2->current->field=fields->field;

View File

@ -38,6 +38,8 @@
#define MODE_GRIB 0
#define MODE_GTS 1
#define MODE_BUFR 2
#define MODE_METAR 3
#define MODE_TAF 5
typedef union grib_typed_value {
long* long_value;