print_data.c

print_data.c How to print all the data from a grib message.

00001 
00010 /*
00011  * C Implementation: print_data
00012  *
00013  * Description: prints all the data contained in a grib file
00014  *
00015  * Author: Enrico Fucile
00016  *
00017  *
00018  */
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 
00022 #include "grib_api.h"
00023 
00024 void usage(char* prog) {
00025   printf("usage: %s filename\n",prog);
00026   exit(1);
00027 }
00028 
00029 int main(int argc, char** argv) {
00030   int err = 0,i;
00031   double *values = NULL;
00032   double max,min,average;
00033   size_t values_len= 0;
00034 
00035   FILE* in = NULL;
00036   char* filename ;
00037   grib_handle *h = NULL;
00038 
00039   if (argc<2) usage(argv[0]);
00040   filename=argv[1];
00041 
00042   in = fopen(filename,"r");
00043   if(!in) {
00044     printf("ERROR: unable to open file %s\n",filename);
00045     return 1;
00046   }
00047 
00048   /* create new handle from a message in a file*/
00049   h = grib_handle_new_from_file(0,in,&err);
00050   if (h == NULL) {
00051     printf("Error: unable to create handle from file %s\n",filename);
00052   }
00053 
00054 
00055   /* get the size of the values array*/
00056   GRIB_CHECK(grib_get_size(h,"values",&values_len),0);
00057 
00058   values = malloc(values_len*sizeof(double));
00059 
00060   /* get data values*/
00061   GRIB_CHECK(grib_get_double_array(h,"values",values,&values_len),0);
00062 
00063   for(i = 0; i < values_len; i++)
00064     printf("%d  %.10e\n",i+1,values[i]);
00065 
00066   free(values);
00067 
00068 
00069   GRIB_CHECK(grib_get_double(h,"max",&max),0);
00070   GRIB_CHECK(grib_get_double(h,"min",&min),0);
00071   GRIB_CHECK(grib_get_double(h,"average",&average),0);
00072 
00073   printf("%d values found in %s\n",(int)values_len,filename);
00074   printf("max=%.10e min=%.10e average=%.10e\n",max,min,average);
00075 
00076   grib_handle_delete(h);
00077 
00078   fclose(in);
00079   return 0;
00080 }

Generated on Tue Sep 22 15:18:21 2009 for grib_api by  doxygen 1.5.3