00001 00010 /* 00011 * C Implementation: set 00012 * 00013 * Description: how to set key values. 00014 * 00015 * 00016 * Author: Enrico Fucile 00017 * 00018 * 00019 */ 00020 #include <stdio.h> 00021 #include <stdlib.h> 00022 00023 #include "grib_api.h" 00024 00025 int main(int argc, char** argv) { 00026 int err = 0; 00027 long centre=80; 00028 long long_value=0; 00029 char string_value[100]; 00030 size_t len = sizeof(string_value)/sizeof(char); 00031 size_t size=0; 00032 00033 FILE* in = NULL; 00034 char* infile = "../../data/regular_latlon_surface.grib1"; 00035 FILE* out = NULL; 00036 char* outfile = "out.grib1"; 00037 grib_handle *h = NULL; 00038 const void* buffer = NULL; 00039 00040 in = fopen(infile,"r"); 00041 if(!in) { 00042 printf("ERROR: unable to open file %s\n",infile); 00043 return 1; 00044 } 00045 00046 out = fopen(outfile,"w"); 00047 if(!in) { 00048 printf("ERROR: unable to open file %s\n",outfile); 00049 return 1; 00050 } 00051 00052 /* create a new handle from a message in a file */ 00053 h = grib_handle_new_from_file(0,in,&err); 00054 if (h == NULL) { 00055 printf("Error: unable to create handle from file %s\n",infile); 00056 } 00057 00058 /* set centre as a long */ 00059 GRIB_CHECK(grib_set_long(h,"centre",centre),0); 00060 00061 /* get centre as a long */ 00062 GRIB_CHECK(grib_get_long(h,"centre",&long_value),0); 00063 printf("centre long value=%ld\n",long_value); 00064 00065 /* get centre as a string */ 00066 GRIB_CHECK(grib_get_string(h,"centre",string_value,&len),0); 00067 printf("centre string value=%s\n",string_value); 00068 00069 /* get the coded message in a buffer */ 00070 GRIB_CHECK(grib_get_message(h,&buffer,&size),0); 00071 00072 /* write the buffer in a file*/ 00073 if(fwrite(buffer,1,size,out) != size) 00074 { 00075 perror(argv[1]); 00076 exit(1); 00077 } 00078 00079 /* delete handle */ 00080 grib_handle_delete(h); 00081 00082 fclose(in); 00083 fclose(out); 00084 00085 return 0; 00086 }