File: grib_copy_keys.c

package info (click to toggle)
eccodes 2.45.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 154,456 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: 278; xml: 183; awk: 66
file content (69 lines) | stat: -rw-r--r-- 1,836 bytes parent folder | download | duplicates (3)
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
/*
 * (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 "eccodes.h"
#include <ctype.h>

static void usage(const char* prog)
{
    printf("usage: %s in1.grib in2.grib\n", prog);
    exit(1);
}

int main(int argc, char* argv[])
{
    codes_handle *hfrom, *hto;
    FILE* in;
    char *in_name1, *in_name2;
    int i = 0, err = 0;
    const char* keys[]   = { "gridType", "pl", "values" };
    const int keys_count = sizeof(keys) / sizeof(keys[0]);

    if (argc != 3) usage(argv[0]);

    in_name1 = argv[1];
    in_name2 = argv[2];

    in = fopen(in_name1, "rb");
    if (!in) {
        perror(in_name1);
        exit(1);
    }

    hfrom = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
    CODES_CHECK(err, 0);
    fclose(in);

    in = fopen(in_name2, "rb");
    if (!in) {
        perror(in_name2);
        exit(1);
    }

    hto = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
    CODES_CHECK(err, 0);
    fclose(in);

    for (i = 0; i < keys_count; i++) {
        printf("Copying key: %s\n", keys[i]);
        err = codes_copy_key(hfrom, hto, keys[i], GRIB_TYPE_UNDEFINED);
        CODES_CHECK(err, 0);
    }

    /* codes_write_message(hto,  "temp.out", "w"); CODES_CHECK(err, 0); */
    {
        int dump_flags = GRIB_DUMP_FLAG_CODED | GRIB_DUMP_FLAG_OCTET | GRIB_DUMP_FLAG_VALUES | GRIB_DUMP_FLAG_READ_ONLY;
        codes_dump_content(hto, stdout, "wmo", dump_flags, NULL);
    }

    codes_handle_delete(hfrom);
    codes_handle_delete(hto);

    return err;
}