From dddd872c2e50db02192642ca7123a4fa525c17be Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Tue, 13 Mar 2018 15:48:00 +0000 Subject: [PATCH] ECC-651: grib_compare/bufr_compare: Enable a 'two-way' switch for symmetric comparison --- tests/grib_compare.sh | 54 +++++++++++++++++++ tools/bufr_compare.c | 10 +++- tools/grib_compare.c | 117 ++++++++++++++++++++++++++---------------- 3 files changed, 137 insertions(+), 44 deletions(-) diff --git a/tests/grib_compare.sh b/tests/grib_compare.sh index c4d8b66e2..ac954fd2e 100755 --- a/tests/grib_compare.sh +++ b/tests/grib_compare.sh @@ -20,6 +20,7 @@ rm -f $outfile ${tools_dir}/grib_set -s shortName=2d $infile $outfile ${tools_dir}/grib_compare -b indicatorOfParameter,paramId,shortName $infile $outfile > $REDIRECT +# ---------------------------------------- # Test the -r switch # ---------------------------------------- infile=${data_dir}/v.grib2 @@ -34,6 +35,7 @@ ${tools_dir}/grib_compare -r temp_comp.123 temp_comp.321 rm -f temp_comp.1 temp_comp.2 temp_comp.3 temp_comp.123 temp_comp.321 +# ---------------------------------------- # GRIB-797: test last argument being a directory # ---------------------------------------- temp_dir=tempdir.grib_compare @@ -42,6 +44,7 @@ cp $infile $temp_dir ${tools_dir}/grib_compare $infile $temp_dir rm -rf $temp_dir +# ---------------------------------------- # ECC-245: blacklist and 2nd order packing # ---------------------------------------- temp1=grib_compare_temp1.grib @@ -67,6 +70,7 @@ set -e [ $status -eq 1 ] +# ---------------------------------------- # ECC-355: -R with "all" option # ---------------------------------------- ${tools_dir}/grib_copy -w count=1 ${data_dir}/tigge_cf_ecmwf.grib2 $temp1 @@ -77,6 +81,56 @@ ${tools_dir}/grib_compare -b $BLACKLIST -R referenceValue=0.03,codedValues=2 $te # Now try the "all" option with the highest relative diff value ${tools_dir}/grib_compare -b $BLACKLIST -R all=2 $temp1 $temp2 +# ---------------------------------------- +# Two way (symmetric) comparison +# ---------------------------------------- +sample_g1=$ECCODES_SAMPLES_PATH/GRIB1.tmpl +temp_nold=$temp1 +${tools_dir}/grib_set -s deleteLocalDefinition=1 $sample_g1 $temp_nold +# Now the sample has a local definition but $temp_nold does not +set +e +${tools_dir}/grib_compare $temp_nold $sample_g1 > $outfile +status=$? +set -e +[ $status -eq 1 ] +reffile=grib_compare_temp1.ref +cat > $reffile < $outfile +status=$? +set -e +[ $status -eq 1 ] +cat > $reffile <context; + char* first_str = (handles_swapped==0? "1st" : "2nd"); + char* second_str = (handles_swapped==0? "2nd" : "1st"); type1=type; type2=type; @@ -573,7 +603,7 @@ static int compare_values(grib_runtime_options* options,grib_handle* h1,grib_han if( type1==GRIB_TYPE_UNDEFINED && (err = grib_get_native_type(h1,name,&type1)) != GRIB_SUCCESS) { printInfo(h1); - printf("Oops... cannot get type of [%s] in 1st field: %s\n",name,grib_get_error_message(err)); + printf("Oops... cannot get type of [%s] in %s field: %s\n",name,first_str,grib_get_error_message(err)); save_error(c,name); return err; } @@ -583,24 +613,22 @@ static int compare_values(grib_runtime_options* options,grib_handle* h1,grib_han if(err == GRIB_NOT_FOUND) { printInfo(h1); - printf("[%s] not found in 2nd field\n",name); + printf("[%s] not found in %s field\n",name,second_str); save_error(c,name); return err; } printInfo(h1); - printf("Oops... cannot get type of [%s] in 2nd field: %s\n",name,grib_get_error_message(err)); + printf("Oops... cannot get type of [%s] in %s field: %s\n",name,second_str,grib_get_error_message(err)); save_error(c,name); return err; } - /* - if(type1 != type2) - { - printInfo(h1); - printf("Warning, [%s] has different types: 1st field: [%s], 2nd field: [%s]\n", - name,grib_get_type_name(type1),grib_get_type_name(type2)); - return GRIB_TYPE_MISMATCH; - } + /* if(type1 != type2) { + printInfo(h1); + printf("Warning, [%s] has different types: 1st field: [%s], 2nd field: [%s]\n", + name,grib_get_type_name(type1),grib_get_type_name(type2)); + return GRIB_TYPE_MISMATCH; + } */ if(type1 == GRIB_TYPE_LABEL) @@ -613,7 +641,7 @@ static int compare_values(grib_runtime_options* options,grib_handle* h1,grib_han if((err = grib_get_size(h1,name,&len1)) != GRIB_SUCCESS) { printInfo(h1); - printf("Oops... cannot get size of [%s] in 1st field: %s\n",name,grib_get_error_message(err)); + printf("Oops... cannot get size of [%s] in %s field: %s\n",name,first_str,grib_get_error_message(err)); save_error(c,name); return err; } @@ -623,25 +651,28 @@ static int compare_values(grib_runtime_options* options,grib_handle* h1,grib_han if(err == GRIB_NOT_FOUND) { printInfo(h1); - printf("[%s] not found in 2nd field\n",name); + printf("[%s] not found in %s field\n",name,second_str); save_error(c,name); return err; } printInfo(h1); - printf("Oops... cannot get size of [%s] in 2nd field: %s\n",name,grib_get_error_message(err)); + printf("Oops... cannot get size of [%s] in %s field: %s\n",name,second_str,grib_get_error_message(err)); save_error(c,name); return err; } - /* - if(len1 != len2 && type1 != GRIB_TYPE_STRING) - { - printInfo(h1); - printf("[%s] has different size: 1st field: %ld, 2nd field: %ld\n",name,(long)len1,(long)len2); - save_error(c,name); - return GRIB_COUNT_MISMATCH; - } + if (handles_swapped) { + /* Comparing a second time with handles swapped. Do not compare keys common to both handles */ + return GRIB_SUCCESS; + } + + /* if(len1 != len2 && type1 != GRIB_TYPE_STRING) { + printInfo(h1); + printf("[%s] has different size: 1st field: %ld, 2nd field: %ld\n",name,(long)len1,(long)len2); + save_error(c,name); + return GRIB_COUNT_MISMATCH; + } */ isMissing1= ( (grib_is_missing(h1,name,&err1)==1) && (err1 == 0) ) ? 1 : 0; @@ -653,18 +684,18 @@ static int compare_values(grib_runtime_options* options,grib_handle* h1,grib_han } if (isMissing1==1) { - if (verbose) printf(" is set to missing in 1st field\n"); + if (verbose) printf(" is set to missing in %s field\n",first_str); printInfo(h1); - printf("%s is set to missing in 1st field is not missing in 2nd field\n",name); + printf("%s is set to missing in %s field is not missing in %s field\n",name,first_str,second_str); err1 = GRIB_VALUE_MISMATCH; save_error(c,name); return GRIB_VALUE_MISMATCH; } if (isMissing2==1) { - if (verbose) printf(" is set to missing in 1st field\n"); + if (verbose) printf(" is set to missing in %s field\n",first_str); printInfo(h1); - printf("%s is set to missing in 2nd field is not missing in 1st field\n",name); + printf("%s is set to missing in %s field is not missing in %s field\n",name,second_str,first_str); err1 = GRIB_VALUE_MISMATCH; save_error(c,name); return GRIB_VALUE_MISMATCH; @@ -682,16 +713,16 @@ static int compare_values(grib_runtime_options* options,grib_handle* h1,grib_han if((err1 = grib_get_string(h1,name,sval1,&len1)) != GRIB_SUCCESS) { printInfo(h1); - printf("Oops... cannot get string value of [%s] in 1st field: %s\n", - name,grib_get_error_message(err1)); + printf("Oops... cannot get string value of [%s] in %s field: %s\n", + name,first_str,grib_get_error_message(err1)); save_error(c,name); } if((err2 = grib_get_string(h2,name,sval2,&len2)) != GRIB_SUCCESS) { printInfo(h1); - printf("Oops... cannot get string value of [%s] in 2nd field: %s\n", - name,grib_get_error_message(err2)); + printf("Oops... cannot get string value of [%s] in %s field: %s\n", + name,second_str,grib_get_error_message(err2)); save_error(c,name); } @@ -724,16 +755,16 @@ static int compare_values(grib_runtime_options* options,grib_handle* h1,grib_han if((err1 = grib_get_long_array(h1,name,lval1,&len1)) != GRIB_SUCCESS) { printInfo(h1); - printf("Oops... cannot get long value of [%s] in 1st field: %s\n", - name,grib_get_error_message(err1)); + printf("Oops... cannot get long value of [%s] in %s field: %s\n", + name,first_str,grib_get_error_message(err1)); save_error(c,name); } if((err2 = grib_get_long_array(h2,name,lval2,&len2)) != GRIB_SUCCESS) { printInfo(h1); - printf("Oops... cannot get long value of [%s] in 2nd field: %s\n", - name,grib_get_error_message(err2)); + printf("Oops... cannot get long value of [%s] in %s field: %s\n", + name,second_str,grib_get_error_message(err2)); save_error(c,name); } @@ -837,16 +868,16 @@ static int compare_values(grib_runtime_options* options,grib_handle* h1,grib_han if((err1 = grib_get_double_array(h1,name,dval1,&len1)) != GRIB_SUCCESS) { printInfo(h1); - printf("Oops... cannot get double value of [%s] in 1st field: %s\n", - name,grib_get_error_message(err1)); + printf("Oops... cannot get double value of [%s] in %s field: %s\n", + name,first_str,grib_get_error_message(err1)); save_error(c,name); } if((err2 = grib_get_double_array(h2,name,dval2,&len2)) != GRIB_SUCCESS) { printInfo(h1); - printf("Oops... cannot get double value of [%s] in 2nd field: %s\n", - name,grib_get_error_message(err2)); + printf("Oops... cannot get double value of [%s] in %s field: %s\n", + name,second_str,grib_get_error_message(err2)); save_error(c,name); } @@ -945,16 +976,16 @@ static int compare_values(grib_runtime_options* options,grib_handle* h1,grib_han { printInfo(h1); save_error(c,name); - printf("Oops... cannot get bytes value of [%s] in 1st field: %s\n", - name,grib_get_error_message(err1)); + printf("Oops... cannot get bytes value of [%s] in %s field: %s\n", + name,first_str,grib_get_error_message(err1)); } if((err2 = grib_get_bytes(h2,name,uval2,&len2)) != GRIB_SUCCESS) { printInfo(h1); save_error(c,name); - printf("Oops... cannot get bytes value of [%s] in 2nd field: %s\n", - name,grib_get_error_message(err2)); + printf("Oops... cannot get bytes value of [%s] in %s field: %s\n", + name,second_str,grib_get_error_message(err2)); } if(err1 == GRIB_SUCCESS && err2 == GRIB_SUCCESS)