00001 00010 /* 00011 * C Implementation: multi 00012 * 00013 * Description: How to encode grib messages containing multiple 00014 * fields. 00015 * 00016 * Author: Enrico Fucile <enrico.fucile@ecmwf.int> 00017 * 00018 * 00019 */ 00020 #include <stdio.h> 00021 #include <stdlib.h> 00022 00023 #include "grib_api.h" 00024 00025 void usage(char* prog) { 00026 printf("usage: %s in.grib out.grib\n",prog); 00027 exit(1); 00028 } 00029 00030 int main(int argc, char** argv) { 00031 int err = 0; 00032 FILE* in = NULL; 00033 FILE* of = NULL; 00034 long step; 00035 char* filename=NULL; 00036 char* ofilename=NULL; 00037 grib_handle *h = NULL; 00038 grib_multi_handle *mh=NULL; 00039 00040 if (argc < 3) usage(argv[0]); 00041 filename=argv[1]; 00042 ofilename=argv[2]; 00043 00044 /* open input file */ 00045 in = fopen(filename,"r"); 00046 if(!in) { 00047 printf("ERROR: unable to open file %s\n",filename); 00048 return 1; 00049 } 00050 00051 /* new grib handle from input file */ 00052 h = grib_handle_new_from_file(0,in,&err); 00053 GRIB_CHECK(err,0); 00054 /* create a new empty multi field handle */ 00055 mh=grib_multi_handle_new(0); 00056 if (!mh) { 00057 printf("unable to create multi field handle\n"); 00058 exit(1); 00059 } 00060 00061 for (step=12;step<=120;step+=12) { 00062 /* set step */ 00063 grib_set_long(h,"step",step); 00064 /* append h to mh repeating the sections preceding section 4 */ 00065 grib_multi_handle_append(h,4,mh); 00066 } 00067 00068 /* open output file */ 00069 of=fopen(ofilename,"w"); 00070 if(!of) { 00071 printf("ERROR: unable to open file %s\n",ofilename); 00072 exit(1); 00073 } 00074 00075 /* write multi fields handle to output file */ 00076 grib_multi_handle_write(mh,of); 00077 fclose(of); 00078 00079 /* clean memory */ 00080 grib_handle_delete(h); 00081 grib_multi_handle_delete(mh); 00082 00083 fclose(in); 00084 return 0; 00085 }