File: bufr_3to4.cc

package info (click to toggle)
eccodes 2.46.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 154,956 kB
  • sloc: cpp: 163,970; ansic: 26,310; sh: 22,006; f90: 6,854; perl: 6,361; python: 5,352; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 279; xml: 183; awk: 66
file content (104 lines) | stat: -rw-r--r-- 2,575 bytes parent folder | download | duplicates (4)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * (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_internal.h"

void usage(char* prog)
{
    printf("usage: %s infile outfile \n", prog);
    exit(1);
}


int main(int argc, char* argv[])
{
    FILE* infh;
    FILE* outfh;
    char* filename;
    char* ofilename;
    void* mesg   = NULL;
    void* mesgo  = NULL;
    size_t size  = 0;
    size_t osize = 0;
    off_t offset = 0;
    int write;
    int err         = 0;
    int error       = 0;
    int count       = 0;
    int edition     = 0;
    grib_context* c = grib_context_get_default();

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

    n        = 0;
    filename = argv[1];
    infh     = fopen(filename, "r");
    if (!infh) {
        perror(filename);
        exit(1);
    }

    ofilename = argv[1];
    outfh     = fopen(ofilename, "w");
    if (!outfh) {
        perror(ofilename);
        exit(1);
    }


    count = 0;
    while ((mesg = wmo_read_bufr_from_file_malloc(infh, 0, &size, &offset, &err)) != NULL) {
        if (err && err != GRIB_END_OF_FILE) {
            grib_context_log(c, GRIB_LOG_ERROR,
                             "unable to decode message: %s  \n", grib_get_error_message(err));
            error = err;
        }
        edition = mesg[7];
        write   = 1;
        switch (edition) {
            case 3:
                omesg = grib_context_malloc_clear(c, size + 4);
                memcpy(omesg, mesg, 7);
                omesg[7] = 4;
                osize    = 8;
                break;
            case 4:
                omesg = mesg;
                break;
            default:
                error = 1;
                write = 0;
                grib_context_log(c, GRIB_LOG_ERROR,
                                 "wrong edition %d, skipping message", edition);
        }
        if (write) {
            if (fwrite(omesg, 1, osize, outfh) != osize) {
                perror(ofilename);
                exit(1);
            }
        }
        if (mesg) {
            grib_context_free(c, mesg);
            mesg = NULL;
        }
        if (omesg) {
            grib_context_free(c, omesg);
            omesg = NULL;
        }
        count++;
    }


    fclose(infh);
    fclose(outfh);

    return error;
}