get.c

get.c How to get values through the key names.

00001 
00010 /*
00011  * C Implementation: get
00012  *
00013  * Description: how to get values using keys.
00014  *
00015  * Author: Enrico Fucile <enrico.fucile@ecmwf.int>
00016  *
00017  *
00018  */
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 
00022 #include "grib_api.h"
00023 
00024 int main(int argc, char** argv) {
00025   int err = 0;
00026   double *values = NULL;
00027   size_t values_len= 0;
00028 
00029   size_t i = 0;
00030 
00031   double latitudeOfFirstGridPointInDegrees;
00032   double longitudeOfFirstGridPointInDegrees;
00033   double latitudeOfLastGridPointInDegrees;
00034   double longitudeOfLastGridPointInDegrees;
00035 
00036   double jDirectionIncrementInDegrees;
00037   double iDirectionIncrementInDegrees;
00038 
00039   long numberOfPointsAlongAParallel;
00040   long numberOfPointsAlongAMeridian;
00041 
00042   double average = 0;
00043 
00044   FILE* in = NULL;
00045   char* filename = "../../data/regular_latlon_surface.grib1";
00046   grib_handle *h = NULL;
00047 
00048   in = fopen(t">/* ge the number of coded value from a key, if several keys of the same name are present...">grib_get_size(h,"values",&size),0);
00068 
00069   values1 = malloc(size*sizeof(double));
00070   /* get data values before changing the packing parameters*/
00071   GRIB_CHECK(grib_get_double_array(h,"values",values1,&size),0);
00072 
00073   /* changing decimal precition to 2 means that 2 decimal digits
00074      are preserved when packing.  */
00075   decimalPrecision=2;
00076   GRIB_CHECK(grib_set_long(h,"changeDecimalPrecision",decimalPrecision),0);
00077    
00078   /* bitsPerValue after changing the packing parameters */
00079   GRIB_CHECK(grib_get_long(h,"bitsPerValue",&bitsPerValue2),0);
00080 
00081   values2 = malloc(size*sizeof(double));
00082   /* get data values after changing the packing parameters*/
00083   GRIB_CHECK(grib_get_double_array(h,"values",values2,&size),0);
00084 
00085   /* computing error */
00086   maxa=0;
00087   maxr=0;
00088   maxv=values2[0];
00089   minv=maxv;
00090   for (i=0;i<size;i++) {
00091      a=fabs(values2[i]-values1[i]);
00092      if ( values2[i] > maxv ) maxv=values2[i];
00093      if ( values2[i] < maxv ) minv=values2[i];
00094      if ( values2[i] !=0 ) r=fabs((values2[i]-values1[i])/values2[i]);
00095      if ( a > maxa ) maxa=a;
00096      if ( r > maxr ) maxr=r;
00097   }
00098   printf("max absolute error = %g\n",maxa);
00099   printf("max relative error = %g\n",maxr);
00100   printf("min value = %g\n",minv);
00101   printf("max value = %g\n",maxv);
00102 
00103   printf("old number of bits per value=%ld\n",(long)bitsPerValue1);
00104   printf("new number of bits per value=%ld\n",(long)bitsPerValue2);
00105 
00106   /* get the coded message in a buffer */
00107   GRIB_CHECK(grib_get_message(h,&buffer,&size),0);
00108 
00109   /* write the buffer in a file*/
00110   if(fwrite(buffer,1,size,out) != size) 
00111   {
00112      perror(argv[1]);
00113      exit(1);
00114   }
00115 
00116   /* delete handle */
00117   grib_handle_delete(h);
00118 
00119   fclose(in);
00120   fclose(out);
00121 
00122   return 0;
00123 }
00124 

Generated on Wed Jul 22 16:10:29 2009 for grib_api by  doxygen 1.5.3
./usr/share/doc/libgrib-api-dev/html/clone_8f90-example.html0000644000000000000000000001276211232053415022463 0ustar rootroot grib_api: clone.f90

clone.f90

How to clone a message.

00001 ! Copyright 2005-2007 ECMWF
00002 ! 
00003 ! Licensed under the GNU Lesser General Public License which
00004 ! incorporates the terms and conditions of version 3 of the GNU
00005 ! General Public License.
00006 ! See LICENSE and gpl-3.0.txt for details.
00007 !
00008 !
00009 !  Description: how to create a new GRIB message by cloning 
00010 !               an existing message.
00011 !
00012 !
00013 !  Author: Anne Fouilloux
00014 !