1 /*
2 * Copyright 2005-2018 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 *
7 * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
8 * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
9 */
10
11 /*
12 * C Implementation: set
13 *
14 * Description: how to set key values.
15 *
16 *
17 *
18 *
19 */
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include "grib_api.h"
24
main(int argc,char ** argv)25 int main(int argc, char** argv) {
26 int err = 0;
27 long centre=80;
28 long long_value=0;
29 char string_value[100];
30 size_t len = sizeof(string_value)/sizeof(char);
31 size_t size=0;
32
33 FILE* in = NULL;
34 char* infile = "../data/regular_latlon_surface.grib1";
35 FILE* out = NULL;
36 char* outfile = "out.grib1";
37 grib_handle *h = NULL;
38 const void* buffer = NULL;
39
40 in = fopen(infile,"r");
41 if(!in) {
42 printf("ERROR: unable to open file %s\n",infile);
43 return 1;
44 }
45
46 out = fopen(outfile,"w");
47 if(!out) {
48 printf("ERROR: unable to open file %s\n",outfile);
49 return 1;
50 }
51
52 /* create a new handle from a message in a file */
53 h = grib_handle_new_from_file(0,in,&err);
54 if (h == NULL) {
55 printf("Error: unable to create handle from file %s\n",infile);
56 }
57
58 /* set centre as a long */
59 GRIB_CHECK(grib_set_long(h,"centre",centre),0);
60
61 /* get centre as a long */
62 GRIB_CHECK(grib_get_long(h,"centre",&long_value),0);
63 printf("centre long value=%ld\n",long_value);
64
65 /* get centre as a string */
66 GRIB_CHECK(grib_get_string(h,"centre",string_value,&len),0);
67 printf("centre string value=%s\n",string_value);
68
69 /* get the coded message in a buffer */
70 GRIB_CHECK(grib_get_message(h,&buffer,&size),0);
71
72 /* write the buffer in a file*/
73 if(fwrite(buffer,1,size,out) != size)
74 {
75 perror(argv[1]);
76 exit(1);
77 }
78
79 /* delete handle */
80 grib_handle_delete(h);
81
82 fclose(in);
83 fclose(out);
84
85 return 0;
86 }
87