Examples: write errors to stderr

This commit is contained in:
Shahram Najm 2020-05-14 20:21:31 +01:00
parent fb36d2661b
commit 1f99dc511d
29 changed files with 73 additions and 75 deletions

View File

@ -36,14 +36,14 @@ int main(int argc, char* argv[])
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
fprintf(stderr, "Error: unable to open file %s\n", infile);
return 1;
}
/* loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n", cnt);
fprintf(stderr, "Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}

View File

@ -53,7 +53,7 @@ int main(int argc, char* argv[])
}
ho = codes_bufr_handle_new_from_samples(NULL, sampleName);
if (ho == NULL) {
fprintf(stderr, "ERROR creating BUFR from %s\n", sampleName);
fprintf(stderr, "Error: creating BUFR from %s\n", sampleName);
fclose(f);
return 1;
}
@ -80,14 +80,14 @@ int main(int argc, char* argv[])
while ((h = codes_handle_new_from_file(0, f, PRODUCT_BUFR, &err)) != NULL) {
if (!h) {
printf("ERROR: Unable to create BUFR handle\n");
fprintf(stderr, "Error: Unable to create BUFR handle\n");
return 1;
}
/* codes_copy_key(h,ho,"unexpandedDescriptors",0); */
err = codes_set_long(h, "unpack", 1);
if (err) {
printf("ERROR: Unable to unpack BUFR message. Quitting\n");
printf(" %s\n", codes_get_error_message(err));
fprintf(stderr, "Error: Unable to unpack BUFR message. Quitting\n");
fprintf(stderr, " %s\n", codes_get_error_message(err));
return 1;
}

View File

@ -43,14 +43,14 @@ int main(int argc, char* argv[])
/* open bufr file */
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
fprintf(stderr, "Error: unable to open file %s\n", filename);
return 1;
}
/* loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n", cnt);
fprintf(stderr, "Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}

View File

@ -36,14 +36,14 @@ int main(int argc, char* argv[])
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
fprintf(stderr, "Error: unable to open file %s\n", infile);
return 1;
}
/* loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n", cnt);
fprintf(stderr, "Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}

View File

@ -29,14 +29,14 @@ int main(int argc, char* argv[])
FILE* in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
fprintf(stderr, "Error: unable to open file %s\n", infile);
return 1;
}
/* loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n", cnt);
fprintf(stderr, "Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}
@ -49,7 +49,7 @@ int main(int argc, char* argv[])
CODES_CHECK(codes_get_size(h, "stationOrSiteName", &len), 0);
strArray = (char**)malloc(len * sizeof(char*));
if (!strArray) {
printf("Error: Could not allocate memory\n");
fprintf(stderr, "Error: Could not allocate memory\n");
return 1;
}
CODES_CHECK(codes_get_string_array(h, "stationOrSiteName", strArray, &size), 0);

View File

@ -47,7 +47,7 @@ int main(int argc, char* argv[])
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
fprintf(stderr, "Error: unable to open file %s\n", filename);
return 1;
}
@ -55,7 +55,7 @@ int main(int argc, char* argv[])
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
codes_bufr_keys_iterator* kiter = NULL;
if (h == NULL) {
printf("Error: unable to create handle for message %d\n", cnt);
fprintf(stderr, "Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}
@ -68,7 +68,7 @@ int main(int argc, char* argv[])
/* get BUFR key iterator */
kiter = codes_bufr_keys_iterator_new(h, 0);
if (!kiter) {
printf("ERROR: Unable to create BUFR keys iterator\n");
fprintf(stderr, "Error: Unable to create BUFR keys iterator\n");
exit(1);
}

View File

@ -31,14 +31,14 @@ int main(int argc, char* argv[])
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
fprintf(stderr, "Error: unable to open file %s\n", infile);
return 1;
}
/* loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n", cnt);
fprintf(stderr, "Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}

View File

@ -39,14 +39,14 @@ int main(int argc, char* argv[])
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
fprintf(stderr, "Error: unable to open file %s\n", filename);
return 1;
}
/* loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n", cnt);
fprintf(stderr, "Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}

View File

@ -40,14 +40,14 @@ int main(int argc, char* argv[])
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
fprintf(stderr, "Error: unable to open file %s\n", infile);
return 1;
}
/* Loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n", cnt);
fprintf(stderr, "Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}
@ -76,7 +76,7 @@ int main(int argc, char* argv[])
/* Check the size (including all the subsets) */
CODES_CHECK(codes_get_size(h, key_name, &len), 0);
if (len != numObs) {
printf("inconsistent number of %s values found!\n", key_name);
fprintf(stderr, "Error: inconsistent number of %s values found!\n", key_name);
return 1;
}
@ -93,7 +93,7 @@ int main(int argc, char* argv[])
/* Check the size (including all the subsets) */
CODES_CHECK(codes_get_size(h, key_name, &len), 0);
if (len != numObs) {
printf("inconsistent number of %s values found!\n", key_name);
fprintf(stderr, "Error: inconsistent number of %s values found!\n", key_name);
return 1;
}
@ -107,7 +107,7 @@ int main(int argc, char* argv[])
/* Check the size (including all the subsets) */
CODES_CHECK(codes_get_size(h, key_name, &len), 0);
if (len != numObs) {
printf("inconsistent number of %s values found!\n", key_name);
fprintf(stderr, "Error: inconsistent number of %s values found!\n", key_name);
return 1;
}

View File

@ -21,7 +21,6 @@
* example. It is advised to use bufr_dump to understand the structure of the messages.
*/
#include "eccodes.h"
int main(int argc, char* argv[])
@ -39,14 +38,14 @@ int main(int argc, char* argv[])
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
fprintf(stderr, "Error: unable to open file %s\n", infile);
return 1;
}
/* loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n", cnt);
fprintf(stderr, "Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}

View File

@ -47,14 +47,14 @@ int main(int argc, char* argv[])
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
fprintf(stderr, "Error: unable to open file %s\n", infile);
return 1;
}
/* Loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n", cnt);
fprintf(stderr, "Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}
@ -104,7 +104,7 @@ int main(int argc, char* argv[])
/* Check the size */
CODES_CHECK(codes_get_size(h, key_name, &len), 0);
if (len != sigt_len) {
printf("inconsistent number of geopotential values found!\n");
fprintf(stderr, "Error: inconsistent number of geopotential values found!\n");
free_memory(sigt_pres, sigt_geo, sigt_t, sigt_td);
return 1;
}
@ -114,7 +114,7 @@ int main(int argc, char* argv[])
/* Get temperature */
if (len != sigt_len) { /* Check the size */
printf("inconsistent number of temperature values found!\n");
fprintf(stderr, "Error: inconsistent number of temperature values found!\n");
free_memory(sigt_pres, sigt_geo, sigt_t, sigt_td);
return 1;
}
@ -125,7 +125,7 @@ int main(int argc, char* argv[])
/* Get dew point */
if (len != sigt_len) { /* Check the size */
printf("inconsistent number of dewpoint temperature values found!\n");
fprintf(stderr, "Error: inconsistent number of dewpoint temperature values found!\n");
free_memory(sigt_pres, sigt_geo, sigt_t, sigt_td);
return 1;
}

View File

@ -48,7 +48,7 @@ int main(int argc, char* argv[])
out = fopen(argv[1], "wb");
if (!in || !out) {
perror("ERROR: unable to open files");
perror("Error: unable to open files");
if (out) fclose(out);
if (in) fclose(in);
return 1;
@ -57,7 +57,7 @@ int main(int argc, char* argv[])
/* loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n", cnt);
fprintf(stderr, "Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}

View File

@ -36,13 +36,13 @@ int main(int argc, char* argv[])
infile = argv[1];
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
fprintf(stderr, "Error: unable to open file %s\n", infile);
return 1;
}
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_ANY, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n", cnt);
fprintf(stderr, "Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}

View File

@ -65,7 +65,7 @@ int main(int argc, char* argv[])
/* create handle for next GRIB message */
h = codes_handle_new_from_index(index, &ret);
if (ret) {
printf("Error: %s\n", codes_get_error_message(ret));
fprintf(stderr, "Error: %s\n", codes_get_error_message(ret));
exit(ret);
}

View File

@ -32,14 +32,14 @@ int main(int argc, char** argv)
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open input file %s\n", filename);
fprintf(stderr, "Error: unable to open input file %s\n", filename);
return 1;
}
/* create new handle from a message in a file */
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
if (h == NULL) {
printf("Error: unable to create handle from file %s\n", filename);
fprintf(stderr, "Error: unable to create handle from file %s\n", filename);
return 1;
}
@ -48,18 +48,18 @@ int main(int argc, char** argv)
lats = (double*)malloc(numberOfPoints * sizeof(double));
if (!lats) {
printf("unable to allocate %ld bytes\n", (long)(numberOfPoints * sizeof(double)));
fprintf(stderr, "Error: unable to allocate %ld bytes\n", (long)(numberOfPoints * sizeof(double)));
return 1;
}
lons = (double*)malloc(numberOfPoints * sizeof(double));
if (!lons) {
printf("unable to allocate %ld bytes\n", (long)(numberOfPoints * sizeof(double)));
fprintf(stderr, "Error: unable to allocate %ld bytes\n", (long)(numberOfPoints * sizeof(double)));
free(lats);
return 1;
}
values = (double*)malloc(numberOfPoints * sizeof(double));
if (!values) {
printf("unable to allocate %ld bytes\n", (long)(numberOfPoints * sizeof(double)));
fprintf(stderr, "Error: unable to allocate %ld bytes\n", (long)(numberOfPoints * sizeof(double)));
free(lats);
free(lons);
return 1;

View File

@ -46,14 +46,14 @@ int main(int argc, char** argv)
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
fprintf(stderr, "Error: unable to open file %s\n", filename);
return 1;
}
/* create new handle from the first message in the file*/
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
if (h == NULL) {
printf("Error: unable to create handle from file %s\n", filename);
fprintf(stderr, "Error: unable to create handle from file %s\n", filename);
return 1;
}
fclose(in);

View File

@ -45,14 +45,14 @@ int main(int argc, char* argv[])
/* Create an index given set of keys*/
index = codes_index_new(0, "shortName,level,number,step", &ret);
if (ret) {
printf("error: %s\n", codes_get_error_message(ret));
fprintf(stderr, "Error: %s\n", codes_get_error_message(ret));
exit(ret);
}
/* Indexes a file */
ret = codes_index_add_file(index, infile);
if (ret) {
printf("error: %s\n", codes_get_error_message(ret));
fprintf(stderr, "Error: %s\n", codes_get_error_message(ret));
exit(ret);
}
printf("end indexing...\n");
@ -128,7 +128,7 @@ int main(int argc, char* argv[])
while ((h = codes_handle_new_from_index(index, &ret)) != NULL) {
count++;
if (ret) {
printf("error: %d\n", ret);
fprintf(stderr, "Error: %d\n", ret);
exit(ret);
}
lenshortName = 200;
@ -143,7 +143,7 @@ int main(int argc, char* argv[])
codes_handle_delete(h);
}
if (ret && ret != GRIB_END_OF_INDEX) {
printf("error: %s\n", codes_get_error_message(ret));
fprintf(stderr, "Error: %s\n", codes_get_error_message(ret));
exit(ret);
}
}

View File

@ -46,7 +46,7 @@ int main(int argc, char** argv)
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
fprintf(stderr, "Error: unable to open file %s\n", filename);
return 1;
}

View File

@ -69,13 +69,13 @@ int main(int argc, char* argv[])
msg_count++;
printf("-- GRIB N. %d --\n", msg_count);
if (!h) {
printf("ERROR: Unable to create grib handle\n");
fprintf(stderr, "Error: Unable to create grib handle\n");
exit(1);
}
kiter = codes_keys_iterator_new(h, key_iterator_filter_flags, name_space);
if (!kiter) {
printf("ERROR: Unable to create keys iterator\n");
fprintf(stderr, "Error: Unable to create keys iterator\n");
exit(1);
}

View File

@ -37,14 +37,14 @@ int main(int argc, char** argv)
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open input file %s\n", filename);
fprintf(stderr, "Error: unable to open input file %s\n", filename);
return 1;
}
/* create new handle from a message in a file*/
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
if (h == NULL) {
printf("Error: unable to create handle from file %s\n", filename);
fprintf(stderr, "Error: unable to create handle from file %s\n", filename);
}
CODES_CHECK(codes_get_long(h, "numberOfContributingSpectralBands", &numberOfContributingSpectralBands), 0);

View File

@ -37,7 +37,7 @@ int main(int argc, char** argv)
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
fprintf(stderr, "Error: unable to open file %s\n", filename);
return 1;
}

View File

@ -47,7 +47,7 @@ int main(int argc, char** argv)
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
fprintf(stderr, "ERROR: unable to open file %s\n", filename);
return 1;
}

View File

@ -47,13 +47,13 @@ int main(int argc, char** argv)
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open input file %s\n", infile);
fprintf(stderr, "Error: unable to open input file %s\n", infile);
return 1;
}
out = fopen(outfile, "wb");
if (!out) {
printf("ERROR: unable to open output file %s\n", outfile);
fprintf(stderr, "Error: unable to open output file %s\n", outfile);
fclose(in);
return 1;
}
@ -61,7 +61,7 @@ int main(int argc, char** argv)
/* create a new handle from a message in a file */
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
if (h == NULL) {
printf("Error: unable to create handle from file %s\n", infile);
fprintf(stderr, "Error: unable to create handle from file %s\n", infile);
}
/* bitsPerValue before changing the packing parameters */

View File

@ -41,14 +41,14 @@ int main(int argc, char** argv)
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
fprintf(stderr, "Error: unable to open file %s\n", filename);
return 1;
}
/* create new handle from a message in a file*/
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
if (h == NULL) {
printf("Error: unable to create handle from file %s\n", filename);
fprintf(stderr, "Error: unable to create handle from file %s\n", filename);
}
/* get the size of the values array*/

View File

@ -13,7 +13,6 @@
*
* Description: how to set a bitmap in a GRIB message
*
*
*/
#include <stdio.h>
@ -39,20 +38,20 @@ int main(int argc, char** argv)
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open input file %s\n", infile);
fprintf(stderr, "Error: unable to open input file %s\n", infile);
return 1;
}
out = fopen(outfile, "wb");
if (!out) {
printf("ERROR: unable to open output file %s\n", outfile);
fprintf(stderr, "Error: unable to open output file %s\n", outfile);
fclose(in);
return 1;
}
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
if (h == NULL) {
printf("Error: unable to create handle from file %s\n", infile);
fprintf(stderr, "Error: unable to create handle from file %s\n", infile);
}
CODES_CHECK(codes_set_double(h, "missingValue", missing), 0);

View File

@ -49,7 +49,7 @@ int main(int argc, char** argv)
printf("Using samples path: %s\n", samples_path);
h = codes_grib_handle_new_from_samples(0, sample_filename);
if (h == NULL) {
printf("Error: unable to create handle from sample file %s\n", sample_filename);
fprintf(stderr, "Error: unable to create handle from sample file %s\n", sample_filename);
exit(1);
}

View File

@ -38,13 +38,13 @@ int main(int argc, char** argv)
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
fprintf(stderr, "Error: unable to open file %s\n", infile);
return 1;
}
out = fopen(outfile, "wb");
if (!out) {
printf("ERROR: unable to open file %s\n", outfile);
fprintf(stderr, "Error: unable to open file %s\n", outfile);
fclose(in);
return 1;
}
@ -52,7 +52,7 @@ int main(int argc, char** argv)
/* create a new handle from a message in a file */
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
if (h == NULL) {
printf("Error: unable to create handle from file %s\n", infile);
fprintf(stderr, "Error: unable to create handle from file %s\n", infile);
}
/* set centre as a long */

View File

@ -36,13 +36,13 @@ int main(int argc, char** argv)
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open input file %s\n", infile);
fprintf(stderr, "Error: unable to open input file %s\n", infile);
return 1;
}
out = fopen(outfile, "wb");
if (!out) {
printf("ERROR: unable to open output file %s\n", outfile);
fprintf(stderr, "Error: unable to open output file %s\n", outfile);
fclose(in);
return 1;
}
@ -50,7 +50,7 @@ int main(int argc, char** argv)
/* create a new handle from a message in a file */
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
if (h == NULL) {
printf("Error: unable to create handle from file %s\n", infile);
fprintf(stderr, "Error: unable to create handle from file %s\n", infile);
}
CODES_CHECK(codes_set_string(h, "typeOfFirstFixedSurface", str, &str_len), 0);

View File

@ -29,7 +29,7 @@ int main(int argc, char* argv[])
h = codes_handle_new_from_file(c, f, PRODUCT_GRIB, &err);
if (!h) {
printf("unable to create handle from file %s\n", infile);
fprintf(stderr, "unable to create handle from file %s\n", infile);
exit(err);
}
fclose(f);
@ -48,7 +48,7 @@ int main(int argc, char* argv[])
for (i = 0; i < nvalues; i++) {
if (values[i].error == err) name = (char*)values[i].name;
}
printf("ERROR: \"%s\" %s\n", name, codes_get_error_message(err));
fprintf(stderr, "Error: \"%s\" %s\n", name, codes_get_error_message(err));
}
values[1].name = "levelll";
@ -57,7 +57,7 @@ int main(int argc, char* argv[])
for (i = 0; i < nvalues; i++) {
if (values[i].error == err) name = (char*)values[i].name;
}
printf("ERROR: \"%s\" %s\n", name, codes_get_error_message(err));
fprintf(stderr, "Error: \"%s\" %s\n", name, codes_get_error_message(err));
}
return 0;