File: bufr_split_by_rdbSubtype.cc

package info (click to toggle)
eccodes 2.44.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 150,248 kB
  • sloc: cpp: 163,056; ansic: 26,308; sh: 21,602; f90: 6,854; perl: 6,363; python: 5,087; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 285; xml: 183; awk: 66
file content (205 lines) | stat: -rw-r--r-- 6,962 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * (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.
 */

/*
 * Description:
 * Split an input BUFR file into output files according to 'rdbSubtype'.
 * The output files are named split_rdbSubtype.<rdbSubtype>.bufr.
 * This is much faster than bufr_copy/bufr_filter
 *
 */

#include "grib_api_internal.h"

static int verbose                         = 0;
static const char* OUTPUT_FILENAME_DEFAULT = "split_rdbSubtype.undef.bufr";
static const char* OUTPUT_FILENAME_SUBTYPE = "split_rdbSubtype.%ld.bufr";

const char* tool_name = "bufr_split_by_rdbSubtype";

static void usage(const char* prog)
{
    printf("Usage: %s [-v] infile\n", prog);
    exit(1);
}

/* If rdbSubtype can be extracted, return GRIB_SUCCESS otherwise error code. */
/* If BUFR message does not have an ECMWF local section, set rdbSubtype to -1 */
static int decode_rdbSubtype(const void* msg, long* rdbSubtype)
{
    int err                         = GRIB_SUCCESS;
    long edition                    = 0;
    long pos_edition                = 7 * 8;
    const long nbits_edition        = 8;
    long section1Flags              = 0;
    long nbits_section1Flags        = 1 * 8;
    long pos_section1Flags          = 17 * 8;
    long section1Length             = 0;
    const long nbits_section1Length = 24;

    long bufrHeaderCentre       = 0;
    long nbits_bufrHeaderCentre = 2 * 8;
    long pos_bufrHeaderCentre   = 12 * 8;

    long pos_section1Length      = 8 * 8;
    int ecmwfLocalSectionPresent = 0;
    const unsigned char* message = (const unsigned char*)msg;

    ECCODES_ASSERT(message);
    *rdbSubtype = -1; /* default */

    edition = (long)grib_decode_unsigned_long(message, &pos_edition, nbits_edition);
    if (edition != 2 && edition != 3 && edition != 4) {
        fprintf(stderr, "%s: Unsupported BUFR edition: %ld", tool_name, edition);
        return GRIB_DECODING_ERROR;
    }
    section1Length = (long)grib_decode_unsigned_long(message, &pos_section1Length, nbits_section1Length);
    if (edition == 3) {
        pos_section1Flags      = 15 * 8;
        nbits_bufrHeaderCentre = 1 * 8;
        pos_bufrHeaderCentre   = 13 * 8;
    }
    if (edition == 2) {
        pos_section1Flags = 15 * 8;
    }

    bufrHeaderCentre = (long)grib_decode_unsigned_long(message, &pos_bufrHeaderCentre, nbits_bufrHeaderCentre);

    section1Flags = (long)grib_decode_unsigned_long(message, &pos_section1Flags, nbits_section1Flags);
    if (section1Flags != 0 && section1Flags != 128) {
        fprintf(stderr, "%s: Invalid BUFR section1 flags: %ld\n", tool_name, section1Flags);
        return GRIB_DECODING_ERROR;
    }
    ecmwfLocalSectionPresent = (bufrHeaderCentre == 98 && section1Flags != 0);

    /*printf("SPLT: edition=%ld section1Length=%ld bufrHeaderCentre=%ld section1Flags=%ld", edition,section1Length,bufrHeaderCentre,section1Flags);*/
    if (ecmwfLocalSectionPresent) {
        long oldSubtype           = 0;
        long newSubtype           = 0;
        long nbits_oldSubtype     = 1 * 8;
        long nbits_newSubtype     = 2 * 8;
        const long section0Length = 8;
        long pos_oldSubtype       = (section0Length + section1Length + 5) * 8;
        long pos_newSubtype       = (section0Length + section1Length + 49) * 8;

        oldSubtype = (long)grib_decode_unsigned_long(message, &pos_oldSubtype, nbits_oldSubtype);
        newSubtype = (long)grib_decode_unsigned_long(message, &pos_newSubtype, nbits_newSubtype);
        /*printf(" oldSubtype=%ld newSubtype=%ld\n", oldSubtype, newSubtype);*/

        if (oldSubtype < 255)
            *rdbSubtype = oldSubtype;
        else
            *rdbSubtype = newSubtype;
    }
    /*else printf(" oldSubtype=undef newSubtype=undef\n");*/
    return err;
}
static int split_file_by_subtype(FILE* in, const char* filename, unsigned long* count)
{
    void* mesg = NULL;
    FILE* out;
    size_t size          = 0;
    off_t offset         = 0;
    int err              = GRIB_SUCCESS;
    char ofilename[2048] = {0,};
    grib_context* c = grib_context_get_default();

    if (!in)
        return 1;
    snprintf(ofilename, 2048, "%s", OUTPUT_FILENAME_DEFAULT); /*default name*/

    while (err != GRIB_END_OF_FILE) {
        mesg = wmo_read_bufr_from_file_malloc(in, 0, &size, &offset, &err);
        if (mesg != NULL && err == 0) {
            /* Decode subtype from mesg */
            long rdbSubtype = 0;
            int status      = decode_rdbSubtype(mesg, &rdbSubtype);
            if (status != GRIB_SUCCESS) {
                fprintf(stderr, "%s: Failed to decode rdbSubtype from message %lu\n", tool_name, *count);
                return status;
            }

            snprintf(ofilename, 2048, "%s", OUTPUT_FILENAME_DEFAULT);
            if (rdbSubtype != -1)
                snprintf(ofilename, 2048, OUTPUT_FILENAME_SUBTYPE, rdbSubtype);

            if (verbose) {
                if (!path_is_regular_file(ofilename))
                    printf("Writing output to %s\n", ofilename);
            }
            out = fopen(ofilename, "ab");
            if (!out) {
                fprintf(stderr, "%s: Failed to open output file '%s'\n", tool_name, ofilename);
                perror(ofilename);
                return GRIB_IO_PROBLEM;
            }
            if (fwrite(mesg, 1, size, out) != size) {
                fprintf(stderr, "%s: Failed to append to file '%s'\n", tool_name, ofilename);
                perror(ofilename);
                fclose(out);
                return GRIB_IO_PROBLEM;
            }
            grib_context_free(c, mesg);
            fclose(out);
            (*count)++;
        }
    }

    if (err == GRIB_END_OF_FILE)
        err = GRIB_SUCCESS;

    return err;
}

int main(int argc, char* argv[])
{
    FILE* infh = NULL;
    char* filename;
    int i, status = 0;
    int err             = 0;
    unsigned long count = 0;

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

    i = 1;
    if (strcmp(argv[i], "-v") == 0) {
        i++;
        verbose = 1;
        if (argc != 3)
            usage(argv[0]);
    }

    filename = argv[i];
    if (path_is_directory(filename)) {
        fprintf(stderr, "%s: %s: Is a directory\n", tool_name, filename);
        return 1;
    }
    infh = fopen(filename, "rb");
    if (!infh) {
        perror(filename);
        return 1;
    }

    count = 0;
    err   = split_file_by_subtype(infh, filename, &count);
    if (err) {
        fprintf(stderr, "%s: Failed to split BUFR file %s\n", tool_name, filename);
        status = 1;
    }
    else {
        if (verbose)
            printf("%7lu %s\n", count, filename);
    }

    fclose(infh);

    return status;
}