mirror of https://github.com/ecmwf/eccodes.git
GRIB-395: grib_count_in_file() function does not support multi-field messages
This commit is contained in:
parent
1b9ea7d288
commit
f3cb55b49e
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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" ]
|
|
@ -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;
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue