2020-07-25 20:16:23 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2005- ECMWF.
|
|
|
|
*
|
|
|
|
* 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_api_internal.h"
|
|
|
|
|
2021-10-06 14:43:09 +00:00
|
|
|
static const size_t MIN_NUM_COLUMNS = 8;
|
2021-10-10 17:22:35 +00:00
|
|
|
static const size_t NUM_DESCRIPTOR_DIGITS = 6; /* FXY */
|
2021-10-06 14:43:09 +00:00
|
|
|
|
|
|
|
#define NUMBER(a) (sizeof(a) / sizeof(a[0]))
|
2021-10-06 14:44:45 +00:00
|
|
|
static const char* allowed_types[] = { "long", "double", "table", "flag", "string" };
|
2021-10-06 14:43:09 +00:00
|
|
|
|
|
|
|
static int check_descriptor_type(const char* atype)
|
|
|
|
{
|
|
|
|
size_t i = 0, numTypes = NUMBER(allowed_types);
|
2021-10-06 14:44:45 +00:00
|
|
|
for (i = 0; i < numTypes; ++i) {
|
|
|
|
if (strcmp(atype, allowed_types[i]) == 0) {
|
2021-10-06 14:43:09 +00:00
|
|
|
return GRIB_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return GRIB_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2020-07-25 20:16:23 +00:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
char* filename = NULL;
|
|
|
|
FILE* fp = NULL;
|
|
|
|
char line[1024] = {0,};
|
|
|
|
char** list = NULL;
|
2021-10-06 14:44:45 +00:00
|
|
|
size_t i = 0, line_number = 0;
|
|
|
|
long lValue = 0;
|
|
|
|
char* str_code = NULL; /* descriptor */
|
|
|
|
char* str_key = NULL;
|
|
|
|
char* str_type = NULL;
|
2021-10-06 14:43:09 +00:00
|
|
|
char* str_scale = NULL;
|
2021-10-06 14:44:45 +00:00
|
|
|
char* str_ref = NULL;
|
2021-10-06 14:43:09 +00:00
|
|
|
char* str_width = NULL;
|
2020-07-25 20:16:23 +00:00
|
|
|
char* str_units = NULL;
|
2020-07-29 09:50:15 +00:00
|
|
|
bufr_descriptor v;
|
2020-07-25 20:22:44 +00:00
|
|
|
const size_t maxlen_keyName = sizeof(v.shortName);
|
2021-10-06 14:44:45 +00:00
|
|
|
const size_t maxlen_units = sizeof(v.units);
|
2020-07-25 20:16:23 +00:00
|
|
|
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(argc == 2);
|
2020-07-25 20:16:23 +00:00
|
|
|
|
|
|
|
filename = argv[1];
|
|
|
|
fp = fopen(filename, "r");
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(fp);
|
2020-07-25 20:16:23 +00:00
|
|
|
|
|
|
|
while (fgets(line, sizeof(line) - 1, fp)) {
|
|
|
|
++line_number;
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(strlen(line) > 0);
|
2020-07-25 20:16:23 +00:00
|
|
|
if (line[0] == '#') continue; /* Ignore first line with column titles */
|
|
|
|
list = string_split(line, "|");
|
2021-10-06 14:43:09 +00:00
|
|
|
if (!list) {
|
2022-11-12 13:34:09 +00:00
|
|
|
fprintf(stderr, "Error on line %zu: string_split failed!\n", line_number);
|
2021-10-06 14:43:09 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
for (i = 0; list[i] != NULL; ++i) {} /* count how many tokens */
|
|
|
|
if (i < MIN_NUM_COLUMNS) {
|
2022-11-12 13:34:09 +00:00
|
|
|
fprintf(stderr, "Error on line %zu: Number of columns (=%zu) < required miniumum (=%zu)!\n",
|
2021-10-06 14:43:09 +00:00
|
|
|
line_number, i, MIN_NUM_COLUMNS);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
str_code = list[0];
|
2023-08-04 18:58:14 +00:00
|
|
|
if (string_to_long(str_code, &lValue, 1) != GRIB_SUCCESS) {
|
2022-11-12 13:34:09 +00:00
|
|
|
fprintf(stderr, "Error on line %zu: descriptor code '%s' (column 1) is not numeric.\n",
|
2021-10-06 14:43:09 +00:00
|
|
|
line_number, str_code);
|
|
|
|
return 1;
|
2021-10-10 17:22:35 +00:00
|
|
|
}
|
|
|
|
if (strlen(str_code) != NUM_DESCRIPTOR_DIGITS) {
|
2022-11-12 13:51:52 +00:00
|
|
|
fprintf(stderr, "Error on line %zu: descriptor code '%s' (column 1) is not %zu digits.\n",
|
2021-10-10 17:22:35 +00:00
|
|
|
line_number, str_code, NUM_DESCRIPTOR_DIGITS);
|
|
|
|
return 1;
|
2021-10-06 14:43:09 +00:00
|
|
|
}
|
2021-10-06 14:44:45 +00:00
|
|
|
str_key = list[1];
|
2021-10-06 14:43:09 +00:00
|
|
|
str_type = list[2];
|
|
|
|
if (check_descriptor_type(str_type) != GRIB_SUCCESS) {
|
2022-11-12 13:34:09 +00:00
|
|
|
fprintf(stderr, "Error on line %zu: descriptor key type '%s' (column 3) is not valid.\n",
|
2021-10-06 14:43:09 +00:00
|
|
|
line_number, str_type);
|
|
|
|
fprintf(stderr, "Please choose one of:\n");
|
2021-10-06 14:44:45 +00:00
|
|
|
for (i = 0; i < NUMBER(allowed_types); ++i) {
|
2021-10-06 14:43:09 +00:00
|
|
|
fprintf(stderr, "\t%s\n", allowed_types[i]);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2020-07-25 20:22:44 +00:00
|
|
|
if (strlen(str_key) >= maxlen_keyName) {
|
2022-11-12 13:51:52 +00:00
|
|
|
fprintf(stderr, "Error on line %zu: descriptor key name '%s' (column 2) exceeds %zu characters.\n",
|
2020-07-25 20:22:44 +00:00
|
|
|
line_number, str_key, maxlen_keyName);
|
2020-07-25 20:16:23 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2021-10-06 14:43:09 +00:00
|
|
|
str_units = list[4];
|
2020-07-25 20:22:44 +00:00
|
|
|
if (strlen(str_units) >= maxlen_units) {
|
2022-11-12 13:51:52 +00:00
|
|
|
fprintf(stderr, "Error on line %zu: descriptor units '%s' (column 5) exceeds %zu characters.\n",
|
2020-07-25 20:22:44 +00:00
|
|
|
line_number, str_units, maxlen_units);
|
2020-07-25 20:16:23 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2021-10-06 14:43:09 +00:00
|
|
|
str_scale = list[5];
|
2021-10-06 14:44:45 +00:00
|
|
|
str_ref = list[6];
|
2021-10-06 14:43:09 +00:00
|
|
|
str_width = list[7];
|
2023-08-04 18:58:14 +00:00
|
|
|
if (string_to_long(str_scale, &lValue, 1) != GRIB_SUCCESS) {
|
2022-11-12 13:34:09 +00:00
|
|
|
fprintf(stderr, "Error on line %zu: descriptor scale '%s' (column 6) is not numeric.\n",
|
2021-10-06 14:43:09 +00:00
|
|
|
line_number, str_scale);
|
|
|
|
return 1;
|
|
|
|
}
|
2023-08-04 18:58:14 +00:00
|
|
|
if (string_to_long(str_ref, &lValue, 1) != GRIB_SUCCESS) {
|
2022-11-12 13:34:09 +00:00
|
|
|
fprintf(stderr, "Error on line %zu: descriptor reference '%s' (column 7) is not numeric.\n",
|
2021-10-06 14:43:09 +00:00
|
|
|
line_number, str_ref);
|
|
|
|
return 1;
|
|
|
|
}
|
2023-08-04 18:58:14 +00:00
|
|
|
// The final width column can have spaces etc at the end. So turn off strict mode
|
|
|
|
if (string_to_long(str_width, &lValue, /*strict=*/0) != GRIB_SUCCESS) {
|
2022-11-12 13:34:09 +00:00
|
|
|
fprintf(stderr, "Error on line %zu: descriptor width '%s' (column 8) is not numeric.\n",
|
2021-10-06 14:43:09 +00:00
|
|
|
line_number, str_width);
|
|
|
|
return 1;
|
|
|
|
}
|
2020-07-25 20:16:23 +00:00
|
|
|
|
2021-10-06 14:44:45 +00:00
|
|
|
for (i = 0; list[i] != NULL; ++i)
|
|
|
|
free(list[i]);
|
2020-07-25 20:16:23 +00:00
|
|
|
free(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
2020-12-22 16:47:23 +00:00
|
|
|
grib_context_delete(grib_context_get_default());
|
2020-07-25 20:16:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|