diff --git a/src/action_class_section.c b/src/action_class_section.c index 2364f1e20..dabed334e 100644 --- a/src/action_class_section.c +++ b/src/action_class_section.c @@ -158,6 +158,9 @@ static int notify_change(grib_action* act, grib_accessor * notified, loader.lookup_long = grib_lookup_long_from_handle; loader.init_accessor = grib_init_accessor_from_handle; + if (h->kid != NULL) { + return GRIB_INTERNAL_ERROR; + } Assert(h->kid == NULL); tmp_handle->loader = &loader; diff --git a/src/grib_handle.c b/src/grib_handle.c index fea3f15b4..610c1ce00 100644 --- a/src/grib_handle.c +++ b/src/grib_handle.c @@ -145,7 +145,8 @@ int grib_handle_delete ( grib_handle* h ) grib_dependency *d = h->dependencies; grib_dependency *n; - Assert ( h->kid == NULL ); + if ( h->kid != NULL ) + return GRIB_INTERNAL_ERROR; while ( d ) { diff --git a/src/grib_util.c b/src/grib_util.c index c70905c6b..593fa7751 100644 --- a/src/grib_util.c +++ b/src/grib_util.c @@ -686,6 +686,14 @@ grib_handle* grib_util_set_spec2(grib_handle* h, return NULL; } + if (packing_spec->deleteLocalDefinition) { + /* TODO: We need two calls because of grib1/grib2 issues re removing local defs! */ + if (editionNumber==1){ + SET_LONG_VALUE("deleteLocalDefinition",1); + } + SET_LONG_VALUE("setLocalDefinition", 0); + } + len=100; grib_get_string(h,"packingType",input_packing_type,&len); grib_get_long(h,"bitsPerValue",&input_bits_per_value); @@ -1278,10 +1286,16 @@ grib_handle* grib_util_set_spec2(grib_handle* h, } if (packing_spec->editionNumber && packing_spec->editionNumber!=editionNumber) { - grib_set_long(outh,"edition", packing_spec->editionNumber); + *err = grib_set_long(outh,"edition", packing_spec->editionNumber); + if (*err != GRIB_SUCCESS) { + fprintf(stderr,"GRIB_UTIL_SET_SPEC: Failed to change edition to %ld: %s\n", + packing_spec->editionNumber, grib_get_error_message(*err)); + goto cleanup; + } } if (packing_spec->deleteLocalDefinition) { + /* TODO: We need two calls because of grib1/grib2 issues re removing local defs! */ grib_set_long(outh,"setLocalDefinition", 0); grib_set_long(outh,"deleteLocalDefinition", 1); } diff --git a/tests/grib_util_set_spec.c b/tests/grib_util_set_spec.c index df7d2c880..906419100 100644 --- a/tests/grib_util_set_spec.c +++ b/tests/grib_util_set_spec.c @@ -167,6 +167,6 @@ int main(int argc, char *argv[]) test_regular_ll(infile, outfile); test_reduced_gg(infile, outfile); - + printf("ALL OK\n"); return 0; } diff --git a/tests/grib_util_set_spec.sh b/tests/grib_util_set_spec.sh index 8a53124b9..a50545c31 100755 --- a/tests/grib_util_set_spec.sh +++ b/tests/grib_util_set_spec.sh @@ -9,13 +9,17 @@ # . ./include.sh +#test_dir="valgrind --error-exitcode=1 "$test_dir -### Regular Lat/Lon Grid -########################################### +# -------------------------------------------------- +# Regular Lat/Lon Grid +# -------------------------------------------------- infile=../data/latlon.grib outfile=out.grib_util_set_spec.grib +tempOut=temp.grib_util_set_spec.grib rm -f $outfile +# GRIB1 with local definition for MARS ${test_dir}grib_util_set_spec $infile $outfile > /dev/null res=`${tools_dir}grib_get -p edition,section2Used,Ni,Nj,numberOfValues,bitsPerValue $outfile` @@ -24,8 +28,29 @@ res=`${tools_dir}grib_get -p edition,section2Used,Ni,Nj,numberOfValues,bitsPerVa # Check output file geometry ${tools_dir}grib_get_data $outfile > /dev/null -### Reduced Gaussian Grid N=32 second order packing -########################################### +# Remove the local definition from input +${tools_dir}grib_set -s deleteLocalDefinition=1 $infile $tempOut +${test_dir}grib_util_set_spec $tempOut $outfile > /dev/null + +# Add another grib1 local definition (which is not in grib2) +${tools_dir}grib_set -s setLocalDefinition=1,localDefinitionNumber=5 $infile $tempOut +infile=$tempOut +${test_dir}grib_util_set_spec $tempOut $outfile > /dev/null +res=`${tools_dir}grib_get -p edition,section2Used $outfile` +[ "$res" = "2 0" ] + +# GRIB2 input with local definition +infile=../data/regular_latlon_surface.grib2 +${test_dir}grib_util_set_spec $infile $outfile > /dev/null +grib_check_key_equals $outfile section2Used 0 +# GRIB2 input without local definition +infile=$ECCODES_SAMPLES_PATH/GRIB2.tmpl +${test_dir}grib_util_set_spec $infile $outfile > /dev/null +grib_check_key_equals $outfile section2Used 0 + +# -------------------------------------------------- +# Reduced Gaussian Grid N=32 second order packing +# -------------------------------------------------- infile=../data/reduced_gaussian_model_level.grib2 outfile=out.grib_util_set_spec.grib rm -f $outfile @@ -57,4 +82,4 @@ ${tools_dir}grib_get_data $outfile > /dev/null ### Clean up -rm -f $outfile +rm -f $outfile $tempOut