eccodes/tools/grib_repair.cc

131 lines
3.4 KiB
C++
Raw Normal View History

2013-03-25 14:23:07 +00:00
/*
2020-01-28 14:32:34 +00:00
* (C) Copyright 2005- ECMWF.
2013-03-25 14:23:07 +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.
*/
2013-03-25 12:04:10 +00:00
2013-03-25 14:23:07 +00:00
#include "grib_api_internal.h"
2013-03-25 12:04:10 +00:00
2021-07-26 16:15:47 +00:00
static const char* ENV_VAR = "ECCODES_GRIB_REPAIR_MAX_NUM_MESSAGES";
2020-01-22 13:10:59 +00:00
static void usage(const char* name)
{
fprintf(stderr, "Usage: %s in out [bad]\n", name);
2016-01-11 11:21:37 +00:00
exit(1);
2013-03-25 12:04:10 +00:00
}
2020-01-22 13:10:59 +00:00
#define SIZE 50 * 1024 * 1024
2014-07-29 15:45:39 +00:00
char buffer[SIZE];
2013-03-25 12:04:10 +00:00
2020-01-22 13:10:59 +00:00
int main(int argc, char** argv)
2016-01-11 11:21:37 +00:00
{
2020-01-22 13:10:59 +00:00
FILE *in, *out, *bad;
2016-01-19 14:46:17 +00:00
char *cout, *cbad;
2016-01-11 11:21:37 +00:00
size_t data_len = SIZE;
2021-07-26 16:15:47 +00:00
unsigned long count = 0;
unsigned long MAX_NUM_MESSAGES = 100*1000;
2020-01-22 13:10:59 +00:00
unsigned char* data;
2021-07-26 16:15:47 +00:00
char* sMaxNumMessages = NULL;
2016-01-11 11:21:37 +00:00
2020-01-22 13:10:59 +00:00
if (argc != 3 && argc != 4)
usage(argv[0]);
2016-01-11 11:21:37 +00:00
2020-01-22 13:10:59 +00:00
in = fopen(argv[1], "r");
if (!in) {
2016-01-11 11:21:37 +00:00
perror(argv[1]);
exit(1);
}
cout = argv[2];
2020-01-22 13:10:59 +00:00
out = fopen(argv[2], "w");
if (!out) {
2016-01-11 11:21:37 +00:00
perror(argv[2]);
exit(1);
}
2020-01-22 13:10:59 +00:00
if (argc == 4) {
2016-01-11 11:21:37 +00:00
cbad = argv[3];
2020-01-22 13:10:59 +00:00
bad = fopen(argv[3], "w");
if (!bad) {
2016-01-11 11:21:37 +00:00
perror(argv[3]);
exit(1);
}
}
else {
2020-01-22 13:10:59 +00:00
bad = out;
2016-01-11 11:21:37 +00:00
cbad = cout;
}
2022-03-18 13:56:21 +00:00
2021-07-26 16:15:47 +00:00
sMaxNumMessages = getenv(ENV_VAR);
if (sMaxNumMessages) {
long lmax = 0;
if (string_to_long(sMaxNumMessages, &lmax, 1) == GRIB_SUCCESS) {
2021-07-26 16:15:47 +00:00
MAX_NUM_MESSAGES = lmax;
}
}
2016-01-11 11:21:37 +00:00
2020-01-22 13:10:59 +00:00
for (;;) {
2016-01-11 11:21:37 +00:00
size_t len = SIZE;
2020-01-22 13:10:59 +00:00
long ret = wmo_read_grib_from_file(in, buffer, &len);
if (ret == GRIB_END_OF_FILE && len == 0)
2016-01-11 11:21:37 +00:00
break;
2021-07-26 16:15:47 +00:00
if (count > MAX_NUM_MESSAGES) {
/* ECC-1265 */
printf("\nExceeded the maximum number of messages. Limit = %lu.\n", MAX_NUM_MESSAGES);
printf("This limit can be overridden by setting the environment variable %s.\n", ENV_VAR);
break;
}
2016-01-11 11:21:37 +00:00
2021-07-26 16:15:47 +00:00
printf("GRIB %lu: size: %ld code: %ld (%s)\n", ++count, (long)len, ret, grib_get_error_message(ret));
2016-01-11 11:21:37 +00:00
2020-01-22 13:10:59 +00:00
switch (ret) {
case 0:
if (fwrite(buffer, 1, len, out) != len) {
perror(cout);
2016-01-11 11:21:37 +00:00
exit(1);
}
2020-01-22 13:10:59 +00:00
break;
case GRIB_WRONG_LENGTH:
case GRIB_PREMATURE_END_OF_FILE:
fseek(in, -4, SEEK_CUR);
memset(buffer + len - 4, '7', 4); /* add in 7777 at end */
len = data_len = SIZE;
data = (unsigned char*)&buffer[0];
ret = grib_read_any_from_memory(NULL, &data, &data_len, buffer, &len);
2021-07-26 16:15:47 +00:00
printf(" -> GRIB %lu: size: %ld code: %ld (%s)\n", count, (long)len, ret, grib_get_error_message(ret));
2020-01-22 13:10:59 +00:00
if (ret == 0) {
if (fwrite(buffer, 1, len, bad) != len) {
perror(cbad);
exit(1);
}
}
break;
2016-01-11 11:21:37 +00:00
}
}
2020-01-22 13:10:59 +00:00
if (fclose(in)) {
2016-01-11 11:21:37 +00:00
perror(argv[1]);
exit(1);
}
2020-01-22 13:10:59 +00:00
if (fclose(out)) {
2016-01-11 11:21:37 +00:00
perror(argv[2]);
exit(1);
}
2020-01-22 13:10:59 +00:00
if (argc == 4) {
if (fclose(bad)) {
2016-01-11 11:21:37 +00:00
perror(argv[3]);
exit(1);
}
}
return 0;
2013-03-25 12:04:10 +00:00
}