File: nearest.c

package info (click to toggle)
eccodes 2.44.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 150,248 kB
  • sloc: cpp: 163,056; ansic: 26,308; sh: 21,602; f90: 6,854; perl: 6,363; python: 5,087; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 285; xml: 183; awk: 66
file content (92 lines) | stat: -rw-r--r-- 2,043 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
*
*  Name: nearest
*
*  Description:
*    Get the nearest for all the messages in the input grib file.
*    No land-sea mask applied yet. Print the nearest values to 
*    stdout with high precision.
*
*  Author: Cristian D Codorean
*/

#include <stdio.h>
#include <stdlib.h>

#include "grib_api.h"

void usage(char* name) {
    printf("Usage is: %s lat lon input1 [input2 ...]\n",name);
}

int main(int argc, char** argv) {

    grib_handle* h = NULL;
    FILE* file_handle = NULL;
    int err = 0;
    int i = 0;
    int nfiles = 0;
    int shift = 3;

    /* results */
    double outlat=0;
    double outlon=0;
    double value=0;
    double distance=0;
    int index=0;
    size_t size=1;

    /* program inputs */
    double lat =0,lon = 0;
    char** file_names  = NULL;
    
    /* just some input validation */
    if (argc < 4) {
        usage(argv[0]);
        exit(EXIT_FAILURE);
    }

    lat = atof(argv[1]);
    lon = atof(argv[2]);
    nfiles = argc - shift;

    file_names = (char**)malloc(nfiles*sizeof(char*));
    for (i = 0; i < nfiles; i++) {
        file_names[i] = strdup(argv[i+shift]);
    }

    

    for (i = 0; i < nfiles; i++) {

        /* open the input file */
        file_handle = fopen(file_names[i],"r");
        if (!file_handle) {
            printf("Could not open file %s\n",file_names[i]);
            exit(EXIT_FAILURE);
        }

        /* loop over all messages */
        while (h = grib_handle_new_from_file(0,file_handle,&err)) {

            if (err != GRIB_SUCCESS) GRIB_CHECK(err,0);
            
            GRIB_CHECK(grib_nearest_find_multiple(h,0,&lat,&lon,1,&outlat,&outlon,&value,&distance,&index),0);

            /*printf("outlat: %lf outlon: %lf value: %.10lf distance: %lf index: %d\n",
                outlat, outlon, value, distance, index);*/
            
            printf("%.10lf\n",value);

            GRIB_CHECK(grib_handle_delete(h),0);

        }

        fclose(file_handle);
    }

    for (i = 0; i < nfiles; i++)
        free(file_names[i]);

    return 0;
}