eccodes/tools/grib_get_data.cc

358 lines
12 KiB
C++
Raw Normal View History

2013-03-25 12:04:10 +00:00
/*
2020-01-28 14:32:34 +00:00
* (C) Copyright 2005- ECMWF.
2013-03-25 12:04:10 +00:00
*
* 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 "grib_tools.h"
2020-01-22 13:10:59 +00:00
static void print_key_values(grib_values* values, int values_count);
static grib_values* get_key_values(grib_runtime_options* options, grib_handle* h);
grib_option grib_options[] = {
/* {id, args, help}, on, command_line, value */
{ "S", 0, 0, 1, 0, 0 },
{ "M", 0, 0, 0, 1, 0 },
{ "m:", "missingValue",
"\n\t\tThe missing value is given through this option."
"\n\t\tAny string is allowed and it is printed in place of the missing"
"\n\t\tvalues. Default is to skip the missing values.\n",
0, 1, 0 },
{ "p:", 0, 0, 0, 1, 0 },
{ "F:", "format", "\n\t\tC style format for data values. Default is \"%.10e\"\n", 0, 1, 0 },
{ "L:", "format", "\n\t\tC style format for latitudes/longitudes. Default is \"%9.3f%9.3f\"\n", 0, 1, 0 },
2020-01-22 13:10:59 +00:00
{ "w:", 0, 0, 0, 1, 0 },
{ "s:", 0, 0, 0, 1, 0 },
{ "f", 0, 0, 0, 1, 0 },
{ "G", 0, 0, 0, 1, 0 },
{ "7", 0, 0, 0, 1, 0 },
{ "X:", 0, 0, 0, 1, 0 },
2023-11-02 19:52:42 +00:00
{ "V", 0, 0, 0, 1, 0 },
{ "h", 0, 0, 0, 1, 0 },
2013-03-25 12:04:10 +00:00
};
2020-07-17 14:37:57 +00:00
const char* tool_description =
2020-01-22 13:10:59 +00:00
"Print a latitude, longitude, data values list.\n"
"\tNote: Rotated grids are first unrotated";
2020-07-17 14:37:57 +00:00
const char* tool_name = "grib_get_data";
const char* tool_online_doc = "https://confluence.ecmwf.int/display/ECC/grib_get_data";
2020-07-17 14:37:57 +00:00
const char* tool_usage = "[options] grib_file grib_file ...";
2013-03-25 12:04:10 +00:00
extern FILE* dump_file;
2020-01-22 13:10:59 +00:00
int grib_options_count = sizeof(grib_options) / sizeof(grib_option);
2013-03-25 12:04:10 +00:00
2020-01-22 13:10:59 +00:00
int main(int argc, char* argv[])
{
2020-01-22 13:10:59 +00:00
return grib_tool(argc, argv);
}
2013-03-25 12:04:10 +00:00
int grib_tool_before_getopt(grib_runtime_options* options)
{
2014-01-10 14:07:02 +00:00
return 0;
2013-03-25 12:04:10 +00:00
}
int grib_tool_init(grib_runtime_options* options)
{
2014-01-10 14:07:02 +00:00
return 0;
2013-03-25 12:04:10 +00:00
}
2020-01-22 13:10:59 +00:00
int grib_tool_new_filename_action(grib_runtime_options* options, const char* file)
{
2014-01-10 14:07:02 +00:00
return 0;
2013-03-25 12:04:10 +00:00
}
2020-01-22 13:10:59 +00:00
int grib_tool_new_file_action(grib_runtime_options* options, grib_tools_file* file)
{
2020-07-17 14:37:57 +00:00
exit_if_input_is_directory(tool_name, file->name);
2014-01-10 14:07:02 +00:00
return 0;
2013-03-25 12:04:10 +00:00
}
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
{
2020-01-22 13:10:59 +00:00
int err = 0;
2021-02-07 22:06:42 +00:00
double missingValue = 9999;
int skip_missing = 1;
char* missing_string = NULL;
int i = 0;
grib_values* values = NULL;
grib_iterator* iter = NULL;
char format_values[32] = {0,};
2021-02-04 11:53:15 +00:00
char format_latlons[32] = {0,};
const char* default_format_values = "%.10e";
const char* default_format_latlons = "%9.3f%9.3f";
2021-02-07 22:06:42 +00:00
int print_keys = grib_options_on("p:");
long numberOfPoints = 0;
long bitmapPresent = 0;
long* bitmap = NULL; /* bitmap array */
size_t bmp_len = 0;
2020-01-22 13:10:59 +00:00
double *data_values = 0, *lats = 0, *lons = 0;
size_t size = 0, num_bytes = 0;
2017-08-08 15:57:41 +00:00
long hasMissingValues = 0;
if (!options->skip) {
if (options->set_values_count != 0)
2020-01-22 13:10:59 +00:00
err = grib_set_values(h, options->set_values, options->set_values_count);
if (err != GRIB_SUCCESS && options->fail)
exit(err);
}
if (grib_options_on("m:")) {
/* User wants to see missing values */
char* theEnd = NULL;
2020-01-22 13:10:59 +00:00
double mval = 0;
char* kmiss = grib_options_get_option("m:");
char* p = kmiss;
skip_missing = 0;
while (*p != ':' && *p != '\0')
p++;
if (*p == ':' && *(p + 1) != '\0') {
*p = '\0';
missing_string = strdup(p + 1);
}
2020-01-22 13:10:59 +00:00
else {
missing_string = strdup(kmiss);
}
mval = strtod(kmiss, &theEnd);
if (kmiss != theEnd && *theEnd == '\0')
missingValue = mval;
2020-01-22 13:10:59 +00:00
grib_set_double(h, "missingValue", missingValue);
/*missing_string=grib_options_get_option("m:");*/
}
2014-01-10 14:07:02 +00:00
if (grib_options_on("F:")) {
const char* str = grib_options_get_option("F:");
snprintf(format_values, sizeof(format_values), "%s", str);
2021-02-07 22:06:42 +00:00
}
else {
snprintf(format_values, sizeof(format_values), "%s", default_format_values);
}
if (grib_options_on("L:")) {
/* Do a very basic sanity check */
const char* str = grib_options_get_option("L:");
2022-06-10 19:51:39 +00:00
if (string_count_char(str, '%') != 2) {
2022-06-15 11:04:07 +00:00
fprintf(stderr, "ERROR: Invalid lats/lons format option \"%s\".\n"
" The default is: \"%s\"."
" For higher precision, try: \"%%12.6f%%12.6f\"\n",
2021-02-04 11:53:15 +00:00
str, default_format_latlons);
exit(1);
}
snprintf(format_latlons, sizeof(format_latlons), "%s ", str); /* Add a final space to separate from data values */
2021-02-07 22:06:42 +00:00
}
else {
snprintf(format_latlons, sizeof(format_latlons), "%s ", default_format_latlons);
}
2014-01-10 14:07:02 +00:00
2020-01-22 13:10:59 +00:00
if ((err = grib_get_long(h, "numberOfPoints", &numberOfPoints)) != GRIB_SUCCESS) {
2021-02-04 11:53:15 +00:00
fprintf(stderr, "ERROR: Unable to get number of points\n");
exit(err);
2014-01-10 14:07:02 +00:00
}
2020-01-22 13:10:59 +00:00
iter = grib_iterator_new(h, 0, &err);
2014-01-10 14:07:02 +00:00
2020-01-22 13:10:59 +00:00
num_bytes = (numberOfPoints + 1) * sizeof(double);
data_values = (double*)calloc(numberOfPoints + 1, sizeof(double));
2014-01-10 14:07:02 +00:00
if (!data_values) {
2023-01-06 20:33:42 +00:00
fprintf(stderr, "ERROR: Failed to allocate %zu bytes for data values (number of points=%ld)\n",
num_bytes, numberOfPoints);
2014-01-10 14:07:02 +00:00
exit(GRIB_OUT_OF_MEMORY);
}
if (iter) {
2020-01-22 13:10:59 +00:00
double *lat = 0, *lon = 0, *val = 0;
lats = (double*)calloc(numberOfPoints + 1, sizeof(double));
lons = (double*)calloc(numberOfPoints + 1, sizeof(double));
lat = lats;
lon = lons;
val = data_values;
while (grib_iterator_next(iter, lat++, lon++, val++)) {}
}
else if (err == GRIB_NOT_IMPLEMENTED || err == GRIB_SUCCESS) {
size = numberOfPoints;
err = grib_get_double_array(h, "values", data_values, &size);
if (err) {
2020-01-22 13:10:59 +00:00
grib_context_log(h->context, GRIB_LOG_ERROR, "Cannot decode values: %s",
grib_get_error_message(err));
exit(1);
}
if (size != (size_t)numberOfPoints) {
2023-08-26 19:22:51 +00:00
fprintf(stderr, "ERROR: Wrong number of points %ld\n", numberOfPoints);
2020-01-22 13:10:59 +00:00
if (grib_options_on("f"))
exit(1);
2014-01-10 14:07:02 +00:00
}
2020-01-22 13:10:59 +00:00
}
else {
grib_context_log(h->context, GRIB_LOG_ERROR,
"%s", grib_get_error_message(err));
2014-01-10 14:07:02 +00:00
exit(err);
}
2017-08-08 15:57:41 +00:00
/* Cater for GRIBs which have missing values but no bitmap */
/* See ECC-511 */
2020-01-22 13:10:59 +00:00
GRIB_CHECK(grib_get_long(h, "missingValuesPresent", &hasMissingValues), 0);
GRIB_CHECK(grib_get_long(h, "bitmapPresent", &bitmapPresent), 0);
if (bitmapPresent) {
GRIB_CHECK(grib_get_size(h, "bitmap", &bmp_len), 0);
bitmap = (long*)malloc(bmp_len * sizeof(long));
GRIB_CHECK(grib_get_long_array(h, "bitmap", bitmap, &bmp_len), 0);
}
2014-01-10 14:07:02 +00:00
if (iter)
fprintf(dump_file, "Latitude Longitude ");
2014-01-10 14:07:02 +00:00
2020-01-22 13:10:59 +00:00
fprintf(dump_file, "Value");
2014-01-10 14:07:02 +00:00
if (print_keys)
2020-01-22 13:10:59 +00:00
for (i = 0; i < options->print_keys_count; i++)
fprintf(dump_file, " %s", options->print_keys[i].name);
2014-01-10 14:07:02 +00:00
2020-01-22 13:10:59 +00:00
fprintf(dump_file, "\n");
2014-01-10 14:07:02 +00:00
if (print_keys)
2020-01-22 13:10:59 +00:00
values = get_key_values(options, h);
2014-01-10 14:07:02 +00:00
2020-01-22 13:10:59 +00:00
if (skip_missing == 0) {
/* Show missing values in data */
2020-01-22 13:10:59 +00:00
for (i = 0; i < numberOfPoints; i++) {
2017-08-08 15:57:41 +00:00
int is_missing_val = 0;
if (hasMissingValues) {
2020-01-22 13:10:59 +00:00
if (bitmapPresent)
is_missing_val = (bitmap[i] == 0);
else
is_missing_val = (data_values[i] == missingValue);
}
2020-01-22 13:10:59 +00:00
if (iter)
fprintf(dump_file, format_latlons, lats[i], lons[i]);
2014-01-10 14:07:02 +00:00
if (is_missing_val)
2020-01-22 13:10:59 +00:00
fprintf(dump_file, "%s", missing_string);
2014-01-10 14:07:02 +00:00
else
fprintf(dump_file, format_values, data_values[i]);
2014-01-10 14:07:02 +00:00
if (print_keys)
2020-01-22 13:10:59 +00:00
print_key_values(values, options->print_keys_count);
fprintf(dump_file, "\n");
2014-01-10 14:07:02 +00:00
}
2020-01-22 13:10:59 +00:00
}
else if (skip_missing == 1) {
/* Skip the missing values in data */
2020-01-22 13:10:59 +00:00
for (i = 0; i < numberOfPoints; i++) {
2017-08-08 15:57:41 +00:00
int is_missing_val = 0;
if (hasMissingValues) {
2020-01-22 13:10:59 +00:00
if (bitmapPresent)
is_missing_val = (bitmap[i] == 0);
else
is_missing_val = (data_values[i] == missingValue);
}
2020-01-22 13:10:59 +00:00
if (!is_missing_val) {
if (iter)
fprintf(dump_file, format_latlons, lats[i], lons[i]);
fprintf(dump_file, format_values, data_values[i]);
2014-01-10 14:07:02 +00:00
if (print_keys)
2020-01-22 13:10:59 +00:00
print_key_values(values, options->print_keys_count);
fprintf(dump_file, "\n");
2014-01-10 14:07:02 +00:00
}
}
}
2020-01-22 13:10:59 +00:00
if (iter)
grib_iterator_delete(iter);
if (bitmap)
free(bitmap);
2014-01-10 14:07:02 +00:00
free(data_values);
2018-10-25 15:50:35 +00:00
free(missing_string);
2014-01-10 14:07:02 +00:00
if (iter) {
free(lats);
free(lons);
}
return 0;
2013-03-25 12:04:10 +00:00
}
int grib_tool_skip_handle(grib_runtime_options* options, grib_handle* h)
{
2014-01-10 14:07:02 +00:00
grib_handle_delete(h);
return 0;
2013-03-25 12:04:10 +00:00
}
int grib_tool_finalise_action(grib_runtime_options* options)
{
2014-01-10 14:07:02 +00:00
return 0;
2013-03-25 12:04:10 +00:00
}
2020-01-22 13:10:59 +00:00
static void print_key_values(grib_values* values, int values_count)
{
2020-01-22 13:10:59 +00:00
int i = 0;
for (i = 0; i < values_count; i++) {
fprintf(dump_file, " %s", values[i].string_value);
2014-01-10 14:07:02 +00:00
}
2013-03-25 12:04:10 +00:00
}
2020-01-22 13:10:59 +00:00
static grib_values* get_key_values(grib_runtime_options* options, grib_handle* h)
{
2020-01-22 13:10:59 +00:00
int i = 0;
int ret = 0;
char value[MAX_STRING_LEN] = {0,};
const char* notfound = "not found";
2020-01-22 13:10:59 +00:00
for (i = 0; i < options->print_keys_count; i++) {
size_t len = MAX_STRING_LEN;
ret = GRIB_SUCCESS;
if (grib_is_missing(h, options->print_keys[i].name, &ret) && ret == GRIB_SUCCESS) {
options->print_keys[i].type = GRIB_TYPE_MISSING;
snprintf(value, sizeof(value), "MISSING");
2020-01-22 13:10:59 +00:00
}
else if (ret != GRIB_NOT_FOUND) {
2014-01-10 14:07:02 +00:00
if (options->print_keys[i].type == GRIB_TYPE_UNDEFINED) {
2020-01-22 13:10:59 +00:00
grib_get_native_type(h, options->print_keys[i].name, &(options->print_keys[i].type));
2014-01-10 14:07:02 +00:00
}
switch (options->print_keys[i].type) {
2020-01-22 13:10:59 +00:00
case GRIB_TYPE_STRING:
ret = grib_get_string(h, options->print_keys[i].name, value, &len);
break;
case GRIB_TYPE_DOUBLE:
ret = grib_get_double(h, options->print_keys[i].name,
&(options->print_keys[i].double_value));
snprintf(value, sizeof(value), "%g", options->print_keys[i].double_value);
2020-01-22 13:10:59 +00:00
break;
case GRIB_TYPE_LONG:
ret = grib_get_long(h, options->print_keys[i].name,
&(options->print_keys[i].long_value));
snprintf(value, sizeof(value), "%ld", (long)options->print_keys[i].long_value);
2020-01-22 13:10:59 +00:00
break;
default:
fprintf(dump_file, "invalid type for %s\n", options->print_keys[i].name);
exit(1);
2014-01-10 14:07:02 +00:00
}
}
if (ret != GRIB_SUCCESS) {
2020-01-22 13:10:59 +00:00
if (options->fail)
GRIB_CHECK_NOLINE(ret, options->print_keys[i].name);
if (ret == GRIB_NOT_FOUND)
strcpy(value, notfound);
2014-01-10 14:07:02 +00:00
else {
2020-01-22 13:10:59 +00:00
fprintf(dump_file, "%s %s\n", grib_get_error_message(ret), options->print_keys[i].name);
2014-01-10 14:07:02 +00:00
exit(ret);
}
}
2020-01-22 13:10:59 +00:00
options->print_keys[i].string_value = strdup(value);
2014-01-10 14:07:02 +00:00
}
return options->print_keys;
2013-03-25 12:04:10 +00:00
}
int grib_no_handle_action(grib_runtime_options* options, int err)
{
2020-01-22 13:10:59 +00:00
fprintf(dump_file, "\t\t\"ERROR: unreadable message\"\n");
return 0;
}