File: grib_multi_from_message.cc

package info (click to toggle)
eccodes 2.45.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 154,404 kB
  • sloc: cpp: 162,953; ansic: 26,308; sh: 21,742; f90: 6,854; perl: 6,361; python: 5,172; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 283; xml: 183; awk: 66
file content (76 lines) | stat: -rw-r--r-- 2,091 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * (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.
 */

/* Test: reading GRIB2 multi fields messages from memory */

#include "grib_api_internal.h"

int main(int argc, char* argv[])
{
    struct stat finfo;
    char shortName[20] = {0,};
    size_t len;
    grib_handle* h = NULL;
    size_t fsize;
    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();
    int multi_support   = 0;

    if (argc == 3 && !strcmp(argv[1], "-m")) {
        grib_multi_support_on(c);
        multi_support = 1;
        filename = argv[2];
    }
    else if (argc == 2)
        filename = argv[1];
    else
        return 1;

    ECCODES_ASSERT(filename);
    f = fopen(filename, "rb");
    ECCODES_ASSERT(f);

    stat(filename, &finfo);
    fsize = finfo.st_size;

    data = (unsigned char*)malloc(fsize);
    p    = data;
    ECCODES_ASSERT(data);

    if (fread(data, 1, fsize, f) != fsize) {
        perror(filename);
        exit(1);
    }
    fclose(f);
    pdata = &data;

    while ((h = grib_handle_new_from_multi_message(c, (void**)pdata, &fsize, &error)) != NULL) {
        count++;
        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);
        if (!multi_support) {
            grib_context_free(c, h->buffer->data); /* See grib_handle_delete and grib_buffer_delete */
        }
        grib_handle_delete(h);
    }

    free(p);

    return 0;
}