From 61a110b86cd3e5949c2f4d1f9e95aa9e72948a40 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Fri, 20 Dec 2013 16:17:19 +0000 Subject: [PATCH] Merge in Baudouin's change: added grib_to_json tool --- tools/CMakeLists.txt | 2 +- tools/Makefile.am | 81 ++++++++++----------- tools/grib_to_json.c | 170 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 211 insertions(+), 42 deletions(-) create mode 100644 tools/grib_to_json.c diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 59b632970..ca4d18b1e 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -14,7 +14,7 @@ list( APPEND grib_tools_bins grib2ppm grib_set grib_get grib_get_data grib_copy grib_repair grib_packing grib_cmp grib_convert grib_distance grib_corruption_check grib_compare parser grib_count grib_index_build - gg_sub_area_check grib_moments grib_to_netcdf ) + gg_sub_area_check grib_moments grib_to_netcdf grib_to_json) foreach( tool ${grib_tools_bins} ) diff --git a/tools/Makefile.am b/tools/Makefile.am index b0f723603..30a4f19f1 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -14,10 +14,10 @@ libgrib_tools_la_SOURCES = grib_tools.c \ bin_PROGRAMS = grib_keys grib_histogram grib_error grib_add big2gribex \ grib_debug grib_info grib_filter grib_ls grib_dump \ - grib2ppm grib_set grib_get grib_get_data grib_copy grib_repair \ - grib_packing grib_cmp grib_convert grib_distance grib_corruption_check\ - grib_compare grib_list_keys parser grib_count grib_index_build grib1to2 \ - gg_sub_area_check grib_moments grib_to_netcdf + grib2ppm grib_set grib_get grib_get_data grib_copy grib_repair \ + grib_packing grib_cmp grib_convert grib_distance grib_corruption_check\ + grib_compare grib_list_keys parser grib_count grib_index_build grib1to2 \ + gg_sub_area_check grib_moments grib_to_netcdf grib_to_json noinst_PROGRAMS = gaussian grib_diff mars_request xref all_keys #compile #dumpload grib_compare_old @@ -25,44 +25,43 @@ noinst_PROGRAMS = gaussian grib_diff mars_request xref all_keys #compile #dumpl #dumpload_SOURCES = dumpload.c loady.y loadl.l load.c dump.c load.h -grib_corruption_check_SOURCES = grib_corruption_check.c -grib_moments_SOURCES = grib_moments.c -grib_to_netcdf_SOURCES = grib_to_netcdf.c -grib_dump_SOURCES = grib_dump.c -grib_count_SOURCES = grib_count.c -grib_cmp_SOURCES = grib_cmp.c -grib_packing_SOURCES = grib_packing.c -grib_histogram_SOURCES = grib_histogram.c -grib_distance_SOURCES = grib_distance.c -grib_debug_SOURCES = grib_debug.c -parser_SOURCES = parser.c -grib_list_keys_SOURCES = list_keys.c -all_keys_SOURCES = all_keys.c -grib2ppm_SOURCES = grib2ppm.c -grib_info_SOURCES = grib_info.c -grib_keys_SOURCES = grib_keys.c - -grib_ls_SOURCES = grib_ls.c -grib_copy_SOURCES = grib_copy.c -grib_repair_SOURCES = grib_repair.c -grib_convert_SOURCES = grib_convert.c -grib_filter_SOURCES = grib_filter.c -grib_set_SOURCES = grib_set.c -grib_index_build_SOURCES = grib_index_build.c -grib_get_SOURCES = grib_get.c -grib_get_data_SOURCES = grib_get_data.c - -grib_add_SOURCES = grib_add.c -grib_error_SOURCES = grib_error.c -grib_diff_SOURCES = grib_diff.c -grib_compare_SOURCES = grib_compare.c -big2gribex_SOURCES = big2gribex.c -mars_request_SOURCES = mars_request.c -xref_SOURCES = xref.c -#compile_SOURCES = compile.c -gaussian_SOURCES = gaussian.c +grib_corruption_check_SOURCES = grib_corruption_check.c +grib_moments_SOURCES = grib_moments.c +grib_to_netcdf_SOURCES = grib_to_netcdf.c +grib_to_json_SOURCES = grib_to_json.c +grib_dump_SOURCES = grib_dump.c +grib_count_SOURCES = grib_count.c +grib_cmp_SOURCES = grib_cmp.c +grib_packing_SOURCES = grib_packing.c +grib_histogram_SOURCES = grib_histogram.c +grib_distance_SOURCES = grib_distance.c +grib_debug_SOURCES = grib_debug.c +parser_SOURCES = parser.c +grib_list_keys_SOURCES = list_keys.c +all_keys_SOURCES = all_keys.c +grib2ppm_SOURCES = grib2ppm.c +grib_info_SOURCES = grib_info.c +grib_keys_SOURCES = grib_keys.c +grib_ls_SOURCES = grib_ls.c +grib_copy_SOURCES = grib_copy.c +grib_repair_SOURCES = grib_repair.c +grib_convert_SOURCES = grib_convert.c +grib_filter_SOURCES = grib_filter.c +grib_set_SOURCES = grib_set.c +grib_index_build_SOURCES = grib_index_build.c +grib_get_SOURCES = grib_get.c +grib_get_data_SOURCES = grib_get_data.c +grib_add_SOURCES = grib_add.c +grib_error_SOURCES = grib_error.c +grib_diff_SOURCES = grib_diff.c +grib_compare_SOURCES = grib_compare.c +big2gribex_SOURCES = big2gribex.c +mars_request_SOURCES = mars_request.c +xref_SOURCES = xref.c +#compile_SOURCES = compile.c +gaussian_SOURCES = gaussian.c gg_sub_area_check_SOURCES = gg_sub_area_check.c -grib1to2_SOURCES = grib1to2.txt +grib1to2_SOURCES = grib1to2.txt INCLUDES = -I$(top_builddir)/src diff --git a/tools/grib_to_json.c b/tools/grib_to_json.c new file mode 100644 index 000000000..29ccb3b78 --- /dev/null +++ b/tools/grib_to_json.c @@ -0,0 +1,170 @@ +/* + * Copyright 2005-2013 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" + +typedef void (*getproc)(FILE*, grib_handle*, const char*, const char*); + +void get_long(FILE* f, grib_handle* h, const char* name, const char* arg) +{ + const char* key = arg ? arg : name; + long value; + GRIB_CHECK(grib_get_long(h, key, &value), key); + fprintf(f, "\"%s\":%ld\n", name, value); +} + +void get_param(FILE* f, grib_handle* h, const char* name, const char* arg) +{ + const char* key = "indicatorOfParameter"; + long value; + GRIB_CHECK(grib_get_long(h, key, &value), key); + fprintf(f, "\"%s\":%ld\n", name, value - 131 + 2); +} + +void get_iso_ref_date(FILE* f, grib_handle* h, const char* name, const char* arg) +{ + long year; + long month ; + long day; + long hour ; + long minute ; + 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); +} + +typedef struct header_keys { + const char* name; + getproc proc; + const char* arg; + +} header_keys; + +/* +{ + * "header": { + * "refTime": "2013-11-30T18:00:00.000Z", + * "parameterNumber": 2, + + + + * }, + */ +header_keys header[] = { + + {"scanMode",&get_long, "scanningMode",}, + {"la1",&get_long, "latitudeOfFirstGridPointInDegrees",}, + {"lo1",&get_long, "longitudeOfFirstGridPointInDegrees",}, + + {"la2",&get_long, "latitudeOfLastGridPointInDegrees",}, + {"lo2",&get_long, "longitudeOfLastGridPointInDegrees",}, + + {"forecastTime",&get_long, "step",}, + + {"nx",&get_long, "Nx",}, + {"ny",&get_long, "Ny",}, + + {"dx",&get_long, "DxInDegrees",}, + {"dy",&get_long, "DyInDegrees",}, + + {"parameterNumber", &get_param, "indicatorOfParameter",}, + {"refTime", &get_iso_ref_date, NULL,}, + + {NULL,} +}; + +int main(int argc, char *argv[]) +{ + grib_handle* h = NULL; + FILE* f = NULL; + size_t i, j = 0; + int err = 0; + double* values; + size_t size, count; + size_t n = 0; + + printf("["); + + for(i = 1; i < argc; i++) + { + f = fopen(argv[i],"r"); + if(!f) { + perror(argv[i]); + exit(1); + } + + while((h = grib_handle_new_from_file(0,f,&err)) != NULL) + { + if(++n>1) { + printf(","); + } + + printf("{"); + printf("\"header\":{\n"); + + j = 0; + while(header[j].name) { + if(j) printf(","); + header[j].proc(stdout, h, header[j].name, header[j].arg); + j++; + } + + printf("},\n"); + printf("\"meta\":{\n"); + printf("},\n"); + /*========== data *============*/ + size=0; + GRIB_CHECK(grib_get_size(h,"values",&size),0); + values = malloc(sizeof(double)*size); + + if (!values) { + fprintf(stderr,"%s: out of memory\n", argv[0]); + exit(1); + } + count=size; + GRIB_CHECK(grib_get_double_array(h,"values",values,&count),0); + if (count != size) { + printf("%s: wrong values count %lu %lu\n",argv[0],count,size); + exit(1); + } + printf("\"data\":[\n"); + + for(j = 0; j < count; j ++ ) { + if(j) printf(","); + printf("%g", (long)(values[j]*10+0.5)/10.0); + if(((j+1) % 20) == 0) { + printf("\n"); + } + } + free(values); + + printf("]"); + + printf("}\n"); + + grib_handle_delete(h); + } + fclose(f); + if(err) + { + fprintf(stderr,"%s\n",grib_get_error_message(err)); + exit(1); + } + } + printf("]\n"); + return 0; +}