File: grib2ppm.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 (248 lines) | stat: -rw-r--r-- 7,502 bytes parent folder | download
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * (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 "grib_api_internal.h"

#define CMAP_MAX 20480

typedef struct map
{
    double min;
    double max;
    unsigned char r, g, b;
} map;

map cmap[CMAP_MAX];
unsigned long last[CMAP_MAX] = {0,};

/*
unsigned char unused(char c)
{
    if (c >= 'A' && c <= 'Z')
    {
        return c - 'A' + 10;
    }
    if (c >= 'a' && c <= 'z')
    {
        return c - 'a' + 10;
    }
    return c - '0';
}
*/

static int next(FILE* f, char* buf)
{
    int c = 0, i = 0;
    while ((c = fgetc(f)) != EOF) {
        if (c == ' ' || c == '\n') {
            if (i) break;
            i++;
        }
        else {
            buf[i++] = c;
        }
    }
    buf[i] = 0;
    return i;
}

int main(int argc, char* argv[])
{
    grib_handle* h = NULL;
    FILE* f        = NULL;
    int i          = 0;
    int err        = 0;
    int centred    = 0;
    unsigned r, g, b;
    int cmap_entries = 0;
    double min0, max0;
    int j;
    char buf[1024];
    double lcap = -1e+100, ucap = 1e+100;

    for (i = 1; i < argc; i++) {
        if (argv[i][0] == '-') {
            switch (argv[i][1]) {
                case 'c':
                    centred = 1;
                    break;

                case 'u':
                    ucap = atof(argv[i + 1]);
                    i++;
                    break;

                case 'l':
                    lcap = atof(argv[i + 1]);
                    i++;
                    break;

                case 'm':
                    f = fopen(argv[i + 1], "r");
                    if (!f) {
                        perror(argv[i + 1]);
                        exit(1);
                    }
                    while (next(f, buf)) {
                        min0 = atof(buf);
                        next(f, buf);
                        max0 = atof(buf);
                        next(f, buf);
                        r = atol(buf);
                        next(f, buf);
                        g = atol(buf);
                        next(f, buf);
                        b = atol(buf);

                        if (cmap_entries >= CMAP_MAX) {
                            fprintf(stderr, "%s\n", "colour map is too large");
                            exit(1);
                        }
                        cmap[cmap_entries].min = min0;
                        cmap[cmap_entries].max = max0;
                        cmap[cmap_entries].r   = r;
                        cmap[cmap_entries].g   = g;
                        cmap[cmap_entries].b   = b;
                        cmap_entries++;
                    }
                    fclose(f);
                    i++;
                    fprintf(stderr, "Number of colours: %d\n", cmap_entries);
            }
            continue;
        }

        f = fopen(argv[i], "rb");
        if (!f) {
            perror(argv[i]);
            exit(1);
        }

        while ((h = grib_handle_new_from_file(0, f, &err)) != NULL) {
            long width;
            long height;
            double max, min;
            double* values = NULL;
            unsigned long* indices;
            size_t count;

            GRIB_CHECK(grib_get_size(h, "values", &count), 0);
            values = (double*)malloc(sizeof(double) * count);
            if (!values) {
                fprintf(stderr, "Failed to allocate memory for values\n");
                exit(1);
            }
            indices = (unsigned long*)malloc(sizeof(unsigned long) * count);
            if (!indices) {
                fprintf(stderr, "Failed to allocate memory for indices\n");
                exit(1);
            }

            if (grib_is_missing(h, "Ni", &err)) {
                fprintf(stderr, "Key Ni cannot be missing. Reduced grids are not supported\n");
                exit(1);
            }
            GRIB_CHECK(grib_get_long(h, "Ni", &width), 0);
            GRIB_CHECK(grib_get_long(h, "Nj", &height), 0);
            GRIB_CHECK(grib_get_double_array(h, "values", values, &count), 0);

            max        = values[0];
            min        = values[0];
            indices[0] = 0;
            for (i = 1; i < count; ++i) {
                if (values[i] > max)
                    max = values[i];
                if (values[i] < min)
                    min = values[i];
                indices[i] = i;
            }

            if (max > ucap) {
                max = ucap;
            }

            if (min < lcap) {
                min = lcap;
            }


            fprintf(stderr, "width=%ld, height=%ld, min=%g, max=%g\n", width, height, min, max);
            if (centred) {
                /* assume first column in Greenwich meridian
                assume scanning mode
                 */
                int k = 0, jj;
                for (jj = 0; jj < height; jj++) {
                    for (i = 0; i < width; i++) {
                        int m = (i + width / 2) % width + jj * width;
                        ECCODES_ASSERT(k < count);
                        indices[k++] = m;
                    }
                }
            }

            if (cmap_entries) {
                printf("P6\n%ld %ld\n255\n", width, height);
                for (i = 0; i < count; ++i) {
                    double v       = values[indices[i]];
                    unsigned int p = ((unsigned long)v) % CMAP_MAX; /* kind of hashing */
                    unsigned int k = last[p];
                    for (j = 0; j < cmap_entries; j++) {
                        int m = (k + j) % cmap_entries;
                        if (v >= cmap[m].min && v < cmap[m].max) {
                            last[p] = m;
                            printf("%c", cmap[m].r);
                            printf("%c", cmap[m].g);
                            printf("%c", cmap[m].b);
                            break;
                        }
                    }
                    if (j == cmap_entries) {
                        fprintf(stderr, "Cannot find colour for %g\n", v);
                        exit(1);
                    }
                }
            }
            else {
                /* PPM header */
                printf("P5\n%ld %ld\n65535\n", width, height);
                for (i = 0; i < count; ++i) {
                    unsigned long c;
                    unsigned char hh, l;
                    double v = values[indices[i]];
                    if (v < lcap) {
                        v = lcap;
                    }
                    if (v > ucap) {
                        v = ucap;
                    }
                    double denom = 1;
                    if (max != min) denom = max - min;
                    c  = (v - min) * 65535 / denom;
                    hh = c >> 8;
                    l  = c & 0xff;
                    printf("%c", hh);
                    printf("%c", l);
                }
            }

            grib_handle_delete(h);
            free(values);
            free(indices);
            break;
        }
        fclose(f);
        if (err) {
            fprintf(stderr, "%s\n", grib_get_error_message(err));
            exit(1);
        }
    }
    return 0;
}