2013-12-20 16:17:19 +00:00
|
|
|
/*
|
2020-01-28 14:32:34 +00:00
|
|
|
* (C) Copyright 2005- ECMWF.
|
2013-12-20 16:17:19 +00:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
typedef void (*getproc)(FILE*, grib_handle*, const char*, const char*);
|
|
|
|
|
2022-03-12 18:58:24 +00:00
|
|
|
static void get_long(FILE* f, grib_handle* h, const char* name, const char* arg)
|
2013-12-20 16:17:19 +00:00
|
|
|
{
|
|
|
|
const char* key = arg ? arg : name;
|
|
|
|
long value;
|
|
|
|
GRIB_CHECK(grib_get_long(h, key, &value), key);
|
|
|
|
fprintf(f, "\"%s\":%ld\n", name, value);
|
|
|
|
}
|
|
|
|
|
2022-03-12 18:58:24 +00:00
|
|
|
static void get_param(FILE* f, grib_handle* h, const char* name, const char* arg)
|
2013-12-20 16:17:19 +00:00
|
|
|
{
|
|
|
|
const char* key = "indicatorOfParameter";
|
|
|
|
long value;
|
|
|
|
GRIB_CHECK(grib_get_long(h, key, &value), key);
|
|
|
|
fprintf(f, "\"%s\":%ld\n", name, value - 131 + 2);
|
|
|
|
}
|
|
|
|
|
2022-03-12 18:58:24 +00:00
|
|
|
static void get_iso_ref_date(FILE* f, grib_handle* h, const char* name, const char* arg)
|
2013-12-20 16:17:19 +00:00
|
|
|
{
|
|
|
|
long year;
|
2020-01-22 13:10:59 +00:00
|
|
|
long month;
|
2013-12-20 16:17:19 +00:00
|
|
|
long day;
|
2020-01-22 13:10:59 +00:00
|
|
|
long hour;
|
|
|
|
long minute;
|
2013-12-20 16:17:19 +00:00
|
|
|
long second;
|
|
|
|
|
|
|
|
GRIB_CHECK(grib_get_long(h, "year", &year), "year");
|
|
|
|
GRIB_CHECK(grib_get_long(h, "month", &month), "month");
|
|
|
|
GRIB_CHECK(grib_get_long(h, "day", &day), "day");
|
|
|
|
GRIB_CHECK(grib_get_long(h, "hour", &hour), "hour");
|
|
|
|
GRIB_CHECK(grib_get_long(h, "minute", &minute), "minute");
|
|
|
|
GRIB_CHECK(grib_get_long(h, "second", &second), "second");
|
|
|
|
|
|
|
|
fprintf(f, "\"%s\":\"%04ld-%02ld-%02ldT%02ld:%02ld:%02ld.000Z\"", name, year, month, day, hour, minute, second);
|
|
|
|
}
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
typedef struct header_keys
|
|
|
|
{
|
2013-12-20 16:17:19 +00:00
|
|
|
const char* name;
|
|
|
|
getproc proc;
|
|
|
|
const char* arg;
|
|
|
|
|
|
|
|
} header_keys;
|
|
|
|
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
* "header": {
|
|
|
|
* "refTime": "2013-11-30T18:00:00.000Z",
|
|
|
|
* "parameterNumber": 2,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* },
|
|
|
|
*/
|
|
|
|
header_keys header[] = {
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
{
|
|
|
|
"scanMode",
|
|
|
|
&get_long,
|
|
|
|
"scanningMode",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"la1",
|
|
|
|
&get_long,
|
|
|
|
"latitudeOfFirstGridPointInDegrees",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"lo1",
|
|
|
|
&get_long,
|
|
|
|
"longitudeOfFirstGridPointInDegrees",
|
|
|
|
},
|
2013-12-20 16:17:19 +00:00
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
{
|
|
|
|
"la2",
|
|
|
|
&get_long,
|
|
|
|
"latitudeOfLastGridPointInDegrees",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"lo2",
|
|
|
|
&get_long,
|
|
|
|
"longitudeOfLastGridPointInDegrees",
|
|
|
|
},
|
2013-12-20 16:17:19 +00:00
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
{
|
|
|
|
"forecastTime",
|
|
|
|
&get_long,
|
|
|
|
"step",
|
|
|
|
},
|
2013-12-20 16:17:19 +00:00
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
{
|
|
|
|
"nx",
|
|
|
|
&get_long,
|
|
|
|
"Nx",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ny",
|
|
|
|
&get_long,
|
|
|
|
"Ny",
|
|
|
|
},
|
2013-12-20 16:17:19 +00:00
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
{
|
|
|
|
"dx",
|
|
|
|
&get_long,
|
|
|
|
"DxInDegrees",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"dy",
|
|
|
|
&get_long,
|
|
|
|
"DyInDegrees",
|
|
|
|
},
|
2013-12-20 16:17:19 +00:00
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
{
|
|
|
|
"parameterNumber",
|
|
|
|
&get_param,
|
|
|
|
"indicatorOfParameter",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"refTime",
|
|
|
|
&get_iso_ref_date,
|
|
|
|
NULL,
|
|
|
|
},
|
2013-12-20 16:17:19 +00:00
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
{
|
|
|
|
NULL,
|
|
|
|
}
|
2013-12-20 16:17:19 +00:00
|
|
|
};
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
int main(int argc, char* argv[])
|
2013-12-20 16:17:19 +00:00
|
|
|
{
|
|
|
|
grib_handle* h = NULL;
|
2020-01-22 13:10:59 +00:00
|
|
|
FILE* f = NULL;
|
2013-12-20 16:17:19 +00:00
|
|
|
size_t i, j = 0;
|
|
|
|
int err = 0;
|
|
|
|
double* values;
|
|
|
|
size_t size, count;
|
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
printf("[");
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
f = fopen(argv[i], "r");
|
|
|
|
if (!f) {
|
2013-12-20 16:17:19 +00:00
|
|
|
perror(argv[i]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
while ((h = grib_handle_new_from_file(0, f, &err)) != NULL) {
|
|
|
|
if (++n > 1) {
|
2013-12-20 16:17:19 +00:00
|
|
|
printf(",");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("{");
|
|
|
|
printf("\"header\":{\n");
|
|
|
|
|
|
|
|
j = 0;
|
2020-01-22 13:10:59 +00:00
|
|
|
while (header[j].name) {
|
|
|
|
if (j)
|
|
|
|
printf(",");
|
2013-12-20 16:17:19 +00:00
|
|
|
header[j].proc(stdout, h, header[j].name, header[j].arg);
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("},\n");
|
|
|
|
printf("\"meta\":{\n");
|
|
|
|
printf("},\n");
|
|
|
|
/*========== data *============*/
|
2020-01-22 13:10:59 +00:00
|
|
|
size = 0;
|
|
|
|
GRIB_CHECK(grib_get_size(h, "values", &size), 0);
|
|
|
|
values = (double*)malloc(sizeof(double) * size);
|
2013-12-20 16:17:19 +00:00
|
|
|
|
|
|
|
if (!values) {
|
2020-01-22 13:10:59 +00:00
|
|
|
fprintf(stderr, "%s: out of memory\n", argv[0]);
|
2013-12-20 16:17:19 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2020-01-22 13:10:59 +00:00
|
|
|
count = size;
|
|
|
|
GRIB_CHECK(grib_get_double_array(h, "values", values, &count), 0);
|
2013-12-20 16:17:19 +00:00
|
|
|
if (count != size) {
|
2020-01-22 13:10:59 +00:00
|
|
|
printf("%s: wrong values count %lu %lu\n", argv[0], count, size);
|
2013-12-20 16:17:19 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
printf("\"data\":[\n");
|
|
|
|
|
2020-01-22 13:10:59 +00:00
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
if (j)
|
|
|
|
printf(",");
|
|
|
|
printf("%g", (long)(values[j] * 10 + 0.5) / 10.0);
|
|
|
|
if (((j + 1) % 20) == 0) {
|
2013-12-20 16:17:19 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(values);
|
|
|
|
|
|
|
|
printf("]");
|
|
|
|
|
|
|
|
printf("}\n");
|
|
|
|
|
|
|
|
grib_handle_delete(h);
|
|
|
|
}
|
|
|
|
fclose(f);
|
2020-01-22 13:10:59 +00:00
|
|
|
if (err) {
|
|
|
|
fprintf(stderr, "%s\n", grib_get_error_message(err));
|
2013-12-20 16:17:19 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("]\n");
|
|
|
|
return 0;
|
|
|
|
}
|