diff --git a/src/grib_handle.c b/src/grib_handle.c index 20ef22072..097071f09 100644 --- a/src/grib_handle.c +++ b/src/grib_handle.c @@ -782,7 +782,9 @@ static grib_handle* grib_handle_new_from_file_multi ( grib_context* c, FILE* f,i gl->gts_header_len=gtslen; grib_context_free ( c,save_gts_header ); gtslen=0; - } else gl->gts_header=NULL; + } else { + gl->gts_header=NULL; + } return gl; } @@ -934,11 +936,16 @@ grib_handle* bufr_new_from_file( grib_context* c, FILE* f,int *error ) void *data = NULL; size_t olen = 0; grib_handle *gl = NULL; - off_t offset=0; + off_t gts_header_offset=0; + off_t offset=0, end_msg_offset=0; + char *gts_header=0,*save_gts_header=0; + int gtslen=0; if ( c == NULL ) c = grib_context_get_default(); + gts_header_offset=grib_context_tell( c,f); data = wmo_read_bufr_from_file_malloc ( f, 0,&olen,&offset,error ); + end_msg_offset=grib_context_tell ( c,f ); if ( *error != GRIB_SUCCESS ) { @@ -948,6 +955,26 @@ grib_handle* bufr_new_from_file( grib_context* c, FILE* f,int *error ) return NULL; } + if ( c->gts_header_on ) + { + int g=0; + grib_context_seek ( c,gts_header_offset,SEEK_SET,f ); + gtslen=offset-gts_header_offset; + gts_header=(char*)grib_context_malloc ( c,sizeof ( unsigned char ) *gtslen ); + save_gts_header=gts_header; + grib_context_read ( c,gts_header,gtslen,f ); + g=gtslen; + while ( gts_header!=NULL && g != 0 && *gts_header != '\03' ) + { + /*printf("--------%d %X \n",gtslen,*gts_header);*/ + gts_header++; + g--; + } + if ( g>8 ) {gts_header++;gtslen=g-1;} + else gts_header=save_gts_header; + grib_context_seek ( c,end_msg_offset,SEEK_SET,f ); + } + gl = grib_handle_new_from_message ( c, data, olen ); if ( !gl ) @@ -965,6 +992,17 @@ grib_handle* bufr_new_from_file( grib_context* c, FILE* f,int *error ) grib_context_increment_handle_total_count(c); if (gl->offset == 0) grib_context_set_handle_file_count(c,1); + if ( c->gts_header_on && gtslen >=8 ) + { + gl->gts_header=(char*)grib_context_malloc ( c,sizeof ( unsigned char ) *gtslen ); + memcpy ( gl->gts_header,gts_header,gtslen ); + gl->gts_header_len=gtslen; + grib_context_free ( c,save_gts_header ); + gtslen=0; + } else { + gl->gts_header=NULL; + } + return gl; } @@ -1018,8 +1056,10 @@ static grib_handle* grib_handle_new_from_file_no_multi ( grib_context* c,FILE* f int gtslen=0; if ( c == NULL ) c = grib_context_get_default(); + + gts_header_offset=grib_context_tell( c,f); data = wmo_read_grib_from_file_malloc ( f, headers_only,&olen,&offset,error ); - end_msg_offset=offset+olen; + end_msg_offset=grib_context_tell ( c,f ); if ( *error != GRIB_SUCCESS ) { @@ -1076,6 +1116,8 @@ static grib_handle* grib_handle_new_from_file_no_multi ( grib_context* c,FILE* f gl->gts_header_len=gtslen; grib_context_free ( c,save_gts_header ); gtslen=0; + } else { + gl->gts_header=NULL; } return gl; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 51078d24f..47004d201 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -93,6 +93,7 @@ list( APPEND tests_data_reqd bufr_ecc-393 bufr_ecc-433 bufr_ecc-750 + bufr_ecc-875 grib_ecc-490 grib_ecc-756 grib_ecc-873 diff --git a/tests/bufr_ecc-875.sh b/tests/bufr_ecc-875.sh new file mode 100755 index 000000000..38d16f6eb --- /dev/null +++ b/tests/bufr_ecc-875.sh @@ -0,0 +1,42 @@ +#!/bin/sh +# Copyright 2005-2018 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 + +# --------------------------------------------------------- +# This is the test for the JIRA issue ECC-875 +# bufr_copy with -g option to copy the GTS header +# --------------------------------------------------------- +label="bufr_ecc-875-test" +tempOut=temp.${label}.out +tempRef=temp.${label}.ref +tempBufr=temp.${label}.bufr + +# Case 1: +# -------------------------------- +input=${data_dir}/gts/EGRR20150317121020_00493212.DAT +${tools_dir}/bufr_copy -g -w numberOfSubsets=9 $input $tempBufr +${tools_dir}/gts_ls $tempBufr | sed 1d | head -n -3 > $tempOut + +cat > $tempRef <