GRIB-395: grib_count_in_file() function does not support multi-field messages

This commit is contained in:
Shahram Najm 2016-04-27 16:50:17 +01:00
parent 1b9ea7d288
commit f3cb55b49e
5 changed files with 342 additions and 275 deletions

View File

@ -27,6 +27,7 @@ list( APPEND tests
grib_set_pv
samples
count_messages
grib_count_messages_multi
read_message
read_from_file
get_set_uuid

View File

@ -0,0 +1,34 @@
! 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.
!
! Description: count messages in a file with GRIB2 multi-field messages
!
!
program grib_count_messages_multi
use eccodes
implicit none
integer :: ifile
character(len=100) :: grib_file
integer :: n,stat
character(len=1) :: multi_flag
call getarg(1,multi_flag)
call getarg(2,grib_file)
if (multi_flag/="0") call codes_grib_multi_support_on()
call codes_open_file(ifile,grib_file,'r')
! count the messages in the file
call codes_count_in_file(ifile,n,stat)
print *,n
call codes_close_file(ifile)
end program grib_count_messages_multi

View File

@ -0,0 +1,20 @@
#!/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
INPUT="../../data/multi.grib2"
# Without multi-field support
c=`${examples_dir}eccodes_f_grib_count_messages_multi 0 $INPUT | tr -d ' '`
[ "$c" = "30" ]
# With multi-field support (more messages should be counted)
c=`${examples_dir}eccodes_f_grib_count_messages_multi 1 $INPUT | tr -d ' '`
[ "$c" = "56" ]

View File

@ -724,7 +724,7 @@ static int read_BUFR(reader *r)
return err;
}
static int read_any(reader *r,int grib_ok,int bufr_ok, int hdf5_ok, int wrap_ok)
static int read_any(reader *r, int grib_ok, int bufr_ok, int hdf5_ok, int wrap_ok)
{
unsigned char c;
int err = 0;
@ -1037,7 +1037,7 @@ int _wmo_read_any_from_file(FILE* f,void* buffer,size_t* len,int grib_ok,int buf
int wmo_read_any_from_file(FILE* f,void* buffer,size_t* len)
{
return _wmo_read_any_from_file(f, buffer, len, 1, 1, 1 ,1);
return _wmo_read_any_from_file(f, buffer, len, 1, 1, 1, 1);
}
int wmo_read_grib_from_file(FILE* f,void* buffer,size_t* len)
@ -1111,7 +1111,7 @@ int wmo_read_metar_from_file(FILE* f,void* buffer,size_t* len)
r.read_data = f;
r.read = &stdio_read;
r.seek = &stdio_seek;
r.seek_from_start= &stdio_seek_from_start;
r.seek_from_start = &stdio_seek_from_start;
r.tell = &stdio_tell;
r.alloc_data = &u;
r.alloc = &user_provider_buffer;
@ -1177,7 +1177,7 @@ int wmo_read_any_from_stream(void* stream_data,long (*stream_proc)(void*,void* b
r.read_data = &s;
r.read = &stream_read;
r.seek = &stream_seek;
r.seek_from_start= &stream_seek;
r.seek_from_start = &stream_seek;
r.tell = &stream_tell;
r.alloc_data = &u;
r.alloc = &user_provider_buffer;
@ -1213,7 +1213,7 @@ void *wmo_read_gts_from_file_malloc(FILE* f,int headers_only,size_t *size,off_t
r.read_data = f;
r.read = &stdio_read;
r.seek = &stdio_seek;
r.seek_from_start= &stdio_seek_from_start;
r.seek_from_start = &stdio_seek_from_start;
r.tell = &stdio_tell;
r.alloc_data = &u;
r.alloc = &allocate_buffer;
@ -1492,25 +1492,37 @@ int grib_read_any_from_memory(grib_context* ctx,unsigned char** data,size_t* dat
*data_length = m.data_len;
*data = m.data;
return err;
}
int grib_count_in_file(grib_context* c, FILE* f,int* n)
{
int err=0;
void* mesg=NULL;
size_t size=0;
off_t offset=0;
*n=0;
if (!c) c=grib_context_get_default();
if ( c->multi_support_on )
{
/* GRIB-395 */
grib_handle* h=NULL;
while ((h=grib_handle_new_from_file(c, f , &err))!=NULL) {
grib_handle_delete(h);
(*n)++;
}
}
else
{
void* mesg=NULL;
size_t size=0;
off_t offset=0;
while ( (mesg=wmo_read_any_from_file_malloc ( f,0, &size,&offset,&err))!=NULL && err==GRIB_SUCCESS) {
grib_context_free(c,mesg);
(*n)++;
}
}
rewind(f);
return err==GRIB_END_OF_FILE ? 0 : err;
}