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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2019-12-16 12:47:29 +00:00
|
|
|
* test: reading GRIB2 multi fields messages from memory
|
2013-03-25 12:04:10 +00:00
|
|
|
*/
|
|
|
|
|
2020-05-10 14:13:45 +00:00
|
|
|
#include "grib_api_internal.h"
|
2019-11-05 14:23:23 +00:00
|
|
|
#include <assert.h>
|
2019-01-07 16:13:48 +00:00
|
|
|
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2019-12-16 12:47:29 +00:00
|
|
|
static void usage(const char* prog)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "usage: %s [-m] file.grib\n", prog);
|
2017-09-01 16:36:16 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
int main(int argc, char* argv[])
|
2019-12-16 12:47:29 +00:00
|
|
|
{
|
2014-10-02 16:23:43 +00:00
|
|
|
struct stat finfo;
|
2020-01-29 12:05:02 +00:00
|
|
|
char shortName[20] = {
|
|
|
|
0,
|
|
|
|
};
|
2019-12-16 12:47:29 +00:00
|
|
|
size_t len;
|
2020-01-29 12:05:02 +00:00
|
|
|
grib_handle* h = NULL;
|
2014-10-02 16:23:43 +00:00
|
|
|
size_t fsize;
|
2020-01-29 12:05:02 +00:00
|
|
|
unsigned char* data = NULL;
|
|
|
|
unsigned char* p = NULL;
|
|
|
|
void* pdata = NULL;
|
|
|
|
int error = 0;
|
|
|
|
int count = 0;
|
|
|
|
char* filename = NULL;
|
|
|
|
FILE* f = NULL;
|
|
|
|
long level = 0;
|
|
|
|
grib_context* c = grib_context_get_default();
|
2020-05-10 19:54:03 +00:00
|
|
|
int multi_support = 0;
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
if (argc == 3 && !strcmp(argv[1], "-m")) {
|
2020-05-10 19:54:03 +00:00
|
|
|
grib_multi_support_on(c);
|
|
|
|
multi_support = 1;
|
2020-01-29 12:05:02 +00:00
|
|
|
filename = argv[2];
|
2014-10-02 16:23:43 +00:00
|
|
|
}
|
2020-01-29 12:05:02 +00:00
|
|
|
else if (argc == 2)
|
|
|
|
filename = argv[1];
|
|
|
|
else
|
|
|
|
usage(argv[0]);
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2019-11-05 14:23:23 +00:00
|
|
|
assert(filename);
|
2020-01-29 12:05:02 +00:00
|
|
|
f = fopen(filename, "rb");
|
|
|
|
if (!f) {
|
|
|
|
perror(filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2019-12-16 12:47:29 +00:00
|
|
|
stat(filename, &finfo);
|
2020-01-29 12:05:02 +00:00
|
|
|
fsize = finfo.st_size;
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
data = (unsigned char*)malloc(fsize);
|
|
|
|
p = data;
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2014-10-02 16:23:43 +00:00
|
|
|
if (!data) {
|
2020-01-29 12:05:02 +00:00
|
|
|
fprintf(stderr, "unable to allocate %ld bytes\n", (long)fsize);
|
2014-10-02 16:23:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
if (fread(data, 1, fsize, f) != fsize) {
|
2014-10-02 16:23:43 +00:00
|
|
|
perror(filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fclose(f);
|
2020-01-29 12:05:02 +00:00
|
|
|
pdata = &data;
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
while ((h = grib_handle_new_from_multi_message(c, (void**)pdata, &fsize, &error)) != NULL) {
|
2014-10-02 16:23:43 +00:00
|
|
|
count++;
|
2020-01-29 12:05:02 +00:00
|
|
|
len = 20;
|
|
|
|
GRIB_CHECK(grib_get_string(h, "shortName", shortName, &len), "shortName");
|
|
|
|
GRIB_CHECK(grib_get_long(h, "level", &level), "level");
|
|
|
|
printf("%d %s %ld\n", count, shortName, level);
|
2020-05-10 19:54:03 +00:00
|
|
|
if (!multi_support) {
|
|
|
|
grib_context_free(c, h->buffer->data); /* See grib_handle_delete and grib_buffer_delete */
|
|
|
|
}
|
2014-10-02 16:23:43 +00:00
|
|
|
grib_handle_delete(h);
|
|
|
|
}
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2014-10-02 16:23:43 +00:00
|
|
|
free(p);
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2014-10-02 16:23:43 +00:00
|
|
|
return 0;
|
2013-03-25 12:04:10 +00:00
|
|
|
}
|