diff --git a/tests/bufr_get.sh b/tests/bufr_get.sh index 0449cfc92..6be077eae 100755 --- a/tests/bufr_get.sh +++ b/tests/bufr_get.sh @@ -119,6 +119,25 @@ result=`${tools_dir}/bufr_get -p unpack:d,heightOfStation aaen_55.bufr` result=`${tools_dir}/bufr_get -p unpack:s,heightOfStation aaen_55.bufr` [ "$result" = "0 858000" ] +# ---------------------------------------- +# Wrong message type +# ---------------------------------------- +f=$data_dir/sample.grib2 +set +e +${tools_dir}/bufr_get -p edition $f > $fLog 2>&1 +status=$? +set -e +[ $status -ne 0 ] +grep -q "Input file seems to be GRIB" $fLog + +f=$ECCODES_SAMPLES_PATH/budg.tmpl +set +e +${tools_dir}/bufr_get -p edition $f > $fLog 2>&1 +status=$? +set -e +[ $status -ne 0 ] +grep -q "Input file seems to be GRIB" $fLog + # ---------------------------------------- # Unreadable message diff --git a/tests/grib_get_fail.sh b/tests/grib_get_fail.sh index 1dd028fcb..7f1c967e4 100755 --- a/tests/grib_get_fail.sh +++ b/tests/grib_get_fail.sh @@ -88,5 +88,16 @@ grep -q "unreadable message" $tempText rm -f $outfile +# ---------------------- +# Wrong message type +# ---------------------- +set +e +${tools_dir}/grib_get -p edition $ECCODES_SAMPLES_PATH/BUFR3.tmpl > $tempText 2>&1 +status=$? +set -e +[ $status -ne 0 ] +grep -q "Input file seems to be BUFR" $tempText + + # Clean up rm -f $tempText diff --git a/tools/grib_tools.cc b/tools/grib_tools.cc index 2124bc0ab..13ce07eaa 100644 --- a/tools/grib_tools.cc +++ b/tools/grib_tools.cc @@ -10,6 +10,7 @@ #include "grib_tools.h" #include +#include #if HAVE_LIBJASPER /* Remove compiler warnings re macros being redefined */ @@ -308,6 +309,29 @@ static int grib_tool_with_orderby(grib_runtime_options* options) static char iobuf[1024 * 1024]; +// Read the first few bytes of the file to guess what kind of product +// it could be. Returns an empty string if it fails +static std::string guess_file_product(const std::string& filename) +{ + std::string result; + char buffer[5] = {0,}; + FILE* fin = fopen(filename.c_str(), "rb"); + if (fin) { + size_t bytes = fread(buffer, 1, sizeof(buffer), fin); + if (bytes == sizeof(buffer)) { + if (strncmp(buffer, "GRIB", 4)==0) { + result = "GRIB"; + } else if (strncmp(buffer, "BUDG", 4)==0) { + result = "GRIB"; + } else if (strncmp(buffer, "BUFR", 4)==0) { + result = "BUFR"; + } + } + fclose(fin); + } + return result; +} + static int grib_tool_without_orderby(grib_runtime_options* options) { int err = 0; @@ -419,6 +443,10 @@ static int grib_tool_without_orderby(grib_runtime_options* options) if (infile->handle_count == 0) { fprintf(stderr, "%s: No messages found in %s\n", tool_name, infile->name); + std::string product = guess_file_product(infile->name); + if (!product.empty()) { + fprintf(stderr, "%s: Input file seems to be %s\n", tool_name, product.c_str()); + } if (options->fail) exit(1); }