Tools: Print hint about product when wrong tool used

This commit is contained in:
shahramn 2024-03-15 12:46:10 +00:00
parent f199569e1f
commit 9c71ac509c
3 changed files with 58 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -10,6 +10,7 @@
#include "grib_tools.h"
#include <stdlib.h>
#include <string>
#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);
}