grib_api: get.c

- Main Page
- Modules
- Files
- Related Pages
- Examples

# 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
    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(filename,"r");
    00049   if(!in) {
    00050     printf("ERROR: unable to open file %s\n",filename);
    00051     return 1;
    00052   }
    00053
    00054   /* create new handle from a message in a file*/
    00055   h = grib_handle_new_from_file(0,in,&err);
    00056   if (h == NULL) {
    00057     printf("Error: unable to create handle from file %s\n",filename);
    00058   }
    00059
    00060   /* get as a long*/
    00061   GRIB_CHECK(grib_get_long(h,"numberOfPointsAlongAParallel",&numberOfPointsAlongAParallel),0);
    00062   printf("numberOfPointsAlongAParallel=%ld\n",numberOfPointsAlongAParallel);
    00063
    00064   /* get as a long*/
    00065   GRIB_CHECK(grib_get_long(h,"numberOfPointsAlongAMeridian",&numberOfPointsAlongAMeridian),0);
    00066   printf("numberOfPointsAlongAMeridian=%ld\n",numberOfPointsAlongAMeridian);
    00067
    00068   /* get as a double*/
    00069   GRIB_CHECK(grib_get_double(h,"latitudeOfFirstGridPointInDegrees",&latitudeOfFirstGridPointInDegrees),0);
    00070   printf("latitudeOfFirstGridPointInDegrees=%g\n",latitudeOfFirstGridPointInDegrees);
    00071
    00072   /* get as a double*/
    00073   GRIB_CHECK(grib_get_double(h,"longitudeOfFirstGridPointInDegrees",&longitudeOfFirstGridPointInDegrees),0);
    00074   printf("longitudeOfFirstGridPointInDegrees=%g\n",longitudeOfFirstGridPointInDegrees);
    00075
    00076   /* get as a double*/
    00077   GRIB_CHECK(grib_get_double(h,"latitudeOfLastGridPointInDegrees",&latitudeOfLastGridPointInDegrees),0);
    00078   printf("latitudeOfLastGridPointInDegrees=%g\n",latitudeOfLastGridPointInDegrees);
    00079
    00080   /* get as a double*/
    00081   GRIB_CHECK(grib_get_double(h,"longitudeOfLastGridPointInDegrees",&longitudeOfLastGridPointInDegrees),0);
    00082   printf("longitudeOfLastGridPointInDegrees=%g\n",longitudeOfLastGridPointInDegrees);
    00083
    00084   /* get as a double*/
    00085   GRIB_CHECK(grib_get_double(h,"jDirectionIncrementInDegrees",&jDirectionIncrementInDegrees),0);
    00086   printf("jDirectionIncrementInDegrees=%g\n",jDirectionIncrementInDegrees);
    00087
    00088   /* get as a double*/
    00089   GRIB_CHECK(grib_get_double(h,"iDirectionIncrementInDegrees",&iDirectionIncrementInDegrees),0);
    00090   printf("iDirectionIncrementInDegrees=%g\n",iDirectionIncrementInDegrees);
    00091
    00092   /* get the size of the values array*/
    00093   GRIB_CHECK(grib_get_size(h,"values",&values_len),0);
    00094
    00095   values = malloc(values_len*sizeof(double));
    00096
    00097   /* get data values*/
    00098   GRIB_CHECK(grib_get_double_array(h,"values",values,&values_len),0);
    00099
    00100   average = 0;
    00101   for(i = 0; i < values_len; i++)
    00102     average += values[i];
    00103
    00104   average /=(double)values_len;
    00105
    00106   free(values);
    00107
    00108   printf("There are %d values, average is %g\n",(int)values_len,average);
    00109
    00110   grib_handle_delete(h);
    00111
    00112   fclose(in);
    00113   return 0;
    00114 }