ECC-499: example

This commit is contained in:
Shahram Najm 2019-12-12 18:00:56 +00:00
parent b2a1cc813a
commit 381ea98379
1 changed files with 19 additions and 18 deletions

View File

@ -11,7 +11,8 @@
/* /*
* C Implementation: nearest * C Implementation: nearest
* *
* Description: how to get nearest point(s) * Description: How to get nearest point(s).
* Assumes all input GRIB messages have the same grid geometry
*/ */
#include <stdio.h> #include <stdio.h>
@ -29,10 +30,10 @@ int main(int argc, char** argv)
{ {
int err = 0; int err = 0;
long step=0; long step=0;
size_t nfiles; size_t nfiles=0;
int i=0; size_t i=0, j=0;
int no_values = 0; /* Default: decode the values */
codes_fieldset* set=NULL; codes_fieldset* set=NULL;
const size_t max_numfiles = 100;
codes_handle* h=NULL; codes_handle* h=NULL;
char param[20]={0,}; char param[20]={0,};
size_t len=20; size_t len=20;
@ -42,29 +43,29 @@ int main(int argc, char** argv)
double distances[4]={0,}; double distances[4]={0,};
int indexes[4]={0,}; int indexes[4]={0,};
char* order_by="param,step"; char* order_by="param,step";
double* pValues = values; double* pValues = values; /* Default: decode the values */
size_t size=4; size_t size=4;
double lat=-40,lon=15; double lat=-40,lon=15;
int mode=0; int mode=0;
int count; int count;
int filearg_offset = 1;
char** filenames; char** filenames;
codes_nearest* nearest=NULL; codes_nearest* nearest=NULL;
if (argc < 2) usage(argv[0]); if (argc < 2) usage(argv[0]);
nfiles=argc-1;
if (strcmp(argv[1],"-n")==0) { filenames=(char**)malloc(sizeof(char*)*max_numfiles);
no_values = 1; /* No values decoding. Just indexes, distances etc */
nfiles -= 1; for(i=1; i<argc; ++i) {
pValues = NULL; if (strcmp(argv[i],"-n")==0) {
filearg_offset++; pValues = NULL; /* Do not decode the values */
} else {
if (j >= max_numfiles)
break;
filenames[j++]=(char*)strdup(argv[i]);
}
} }
nfiles = j;
filenames=(char**)malloc(sizeof(char*)*nfiles);
for (i=0;i<nfiles;i++)
filenames[i]=(char*)strdup(argv[i+filearg_offset]);
set=codes_fieldset_new_from_files(0,filenames,nfiles,0,0,0,order_by,&err); set=codes_fieldset_new_from_files(0,filenames,nfiles,0,0,0,order_by,&err);
CODES_CHECK(err,0); CODES_CHECK(err,0);
@ -85,14 +86,14 @@ int main(int argc, char** argv)
if (!nearest) nearest=codes_grib_nearest_new(h,&err); if (!nearest) nearest=codes_grib_nearest_new(h,&err);
CODES_CHECK(err,0); CODES_CHECK(err,0);
CODES_CHECK(codes_grib_nearest_find(nearest,h,lat,lon,mode,lats,lons,pValues,distances,indexes,&size),0); CODES_CHECK(codes_grib_nearest_find(nearest,h,lat,lon,mode,lats,lons,pValues,distances,indexes,&size),0);
if (no_values) { if (!pValues) {
printf("\nIdx\tlat\tlon\tdist\n"); printf("\nIdx\tlat\tlon\tdist\n");
for (i=0;i<4;i++) printf("%d\t%.2f\t%.2f\t%g\n", for (i=0;i<4;i++) printf("%d\t%.2f\t%.2f\t%g\n",
(int)indexes[i],lats[i],lons[i],distances[i]); (int)indexes[i],lats[i],lons[i],distances[i]);
} else { } else {
printf("\nIdx\tlat\tlon\tdist\tval\n"); printf("\nIdx\tlat\tlon\tdist\tval\n");
for (i=0;i<4;i++) printf("%d\t%.2f\t%.2f\t%g\t%g\n", for (i=0;i<4;i++) printf("%d\t%.2f\t%.2f\t%g\t%g\n",
(int)indexes[i],lats[i],lons[i],distances[i],values[i]); (int)indexes[i],lats[i],lons[i],distances[i],pValues[i]);
} }
printf("\n"); printf("\n");