00001 00010 /* 00011 * C Implementation: fieldset 00012 * 00013 * Description: how to use a fieldset. 00014 * 00015 * 00016 * Author: Enrico Fucile 00017 * 00018 * 00019 */ 00020 00021 #include <stdio.h> 00022 #include <stdlib.h> 00023 #include <string.h> 00024 00025 #include "grib_api.h" 00026 00027 void usage(char* prog) { 00028 printf("Usage: %s grib_file grib_file ...\n",prog); 00029 exit(1); 00030 } 00031 00032 int main(int argc, char** argv) { 00033 int err = 0; 00034 long step=0; 00035 size_t nfiles; 00036 int i=0; 00037 grib_fieldset* set=NULL; 00038 grib_handle* h=NULL; 00039 char param[20]={0,}; 00040 size_t len=20; 00041 double lats[4]={0,}; 00042 double lons[4]={0,}; 00043 double values[4]={0,}; 00044 double distances[4]={0,}; 00045 int indexes[4]={0,}; 00046 char* order_by="param,step"; 00047 00048 size_t size=4; 00049 double lat=-40,lon=15; 00050 int mode=0; 00051 int count; 00052 char** filenames; 00053 grib_nearest* nearest=NULL; 00054 00055 if (argc < 2) usage(argv[0]); 00056 00057 nfiles=argc-1; 00058 filenames=(char**)malloc(sizeof(char*)*nfiles); 00059 for (i=0;i<nfiles;i++) 00060 filenames[i]=(char*)strdup(argv[i+1]); 00061 00062 set=grib_fieldset_new_from_files(0,filenames,nfiles,0,0,0,order_by,&err); 00063 GRIB_CHECK(err,0); 00064 00065 printf("\nordering by %s\n",order_by); 00066 printf("\n%d fields in the fieldset\n",grib_fieldset_count(set)); 00067 printf("n,step,param\n"); 00068 00069 mode=GRIB_NEAREST_SAME_GRID | GRIB_NEAREST_SAME_POINT; 00070 count=1; 00071 while ((h=grib_fieldset_next_handle(set,&err))!=NULL) { 00072 GRIB_CHECK(grib_get_long(h,"step",&step),0); 00073 len=20; 00074 GRIB_CHECK(grib_get_string(h,"param",param,&len),0); 00075 00076 printf("%d %ld %s ",count,step,param); 00077 if (!nearest) nearest=grib_nearest_new(h,&err); 00078 GRIB_CHECK(err,0); 00079 GRIB_CHECK(grib_nearest_find(nearest,h,lat,lon,mode,lats,lons,values,distances,indexes,&size),0); 00080 for (i=0;i<4;i++) printf("%d %.2f %.2f %g %g - ", 00081 (int)indexes[i],lats[i],lons[i],distances[i],values[i]); 00082 printf("\n"); 00083 00084 grib_handle_delete(h); 00085 count++; 00086 } 00087 00088 if (nearest) grib_nearest_delete(nearest); 00089 00090 if (set) grib_fieldset_delete(set); 00091 00092 return 0; 00093 }