1 /*
2 * (C) Copyright 2005- ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 *
7 * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
8 * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
9 */
10
11 /*
12 * C Implementation: points
13 *
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #include "grib_api_internal.h"
21
usage(char * prog)22 static void usage(char* prog)
23 {
24 printf("Usage: %s latlon_file grib_orography grib_file grib_file ...\n", prog);
25 exit(1);
26 }
27
main(int argc,char ** argv)28 int main(int argc, char** argv)
29 {
30 FILE* fin = 0;
31 int ret = 0;
32 char* fname = 0;
33 float lat, lon;
34 double *vlat, *vlon;
35 int npoints = 0, i = 0, n = 0;
36 grib_handle* h;
37 double *outlats, *outlons, *values, *lsm_values, *distances;
38 size_t* indexes;
39
40 if (argc < 2)
41 usage(argv[0]);
42
43 fname = argv[1];
44 fin = fopen(fname, "r");
45 if (!fin) {
46 perror(fname);
47 exit(1);
48 }
49 npoints = 0;
50 while (fscanf(fin, "%g %g", &lat, &lon) != EOF)
51 npoints++;
52 fclose(fin);
53
54 vlat = (double*)malloc(npoints * sizeof(double));
55 if (!vlat) {
56 printf("unable to allocate %d bytes\n", npoints * sizeof(double));
57 exit(1);
58 }
59 vlon = (double*)malloc(npoints * sizeof(double));
60 if (!vlon) {
61 printf("unable to allocate %d bytes\n", npoints * sizeof(double));
62 exit(1);
63 }
64 outlats = (double*)malloc(npoints * sizeof(double));
65 if (!outlats) {
66 printf("unable to allocate %d bytes\n", npoints * sizeof(double));
67 exit(1);
68 }
69 outlons = (double*)malloc(npoints * sizeof(double));
70 if (!outlons) {
71 printf("unable to allocate %d bytes\n", npoints * sizeof(double));
72 exit(1);
73 }
74 values = (double*)malloc(npoints * sizeof(double));
75 if (!values) {
76 printf("unable to allocate %d bytes\n", npoints * sizeof(double));
77 exit(1);
78 }
79 lsm_values = (double*)malloc(npoints * sizeof(double));
80 if (!lsm_values) {
81 printf("unable to allocate %d bytes\n", npoints * sizeof(double));
82 exit(1);
83 }
84 distances = (double*)malloc(npoints * sizeof(double));
85 if (!distances) {
86 printf("unable to allocate %d bytes\n", npoints * sizeof(double));
87 exit(1);
88 }
89 indexes = (size_t*)malloc(npoints * sizeof(double));
90 if (!indexes) {
91 printf("unable to allocate %d bytes\n", npoints * sizeof(double));
92 exit(1);
93 }
94
95 fname = argv[1];
96 fin = fopen(fname, "r");
97 if (!fin) {
98 perror(fname);
99 exit(1);
100 }
101 i = 0;
102 while (fscanf(fin, "%g %g", &lat, &lon) != EOF) {
103 vlat[i] = lat;
104 vlon[i] = lon > 0 ? lon : lon + 360;
105 i++;
106 }
107 fclose(fin);
108
109 fname = argv[2];
110 fin = fopen(fname, "r");
111 if (!fin) {
112 perror(fname);
113 exit(1);
114 }
115 h = grib_handle_new_from_file(0, fin, &ret);
116 if (!h || ret != GRIB_SUCCESS) {
117 printf(" unable to create handle\n");
118 exit(1);
119 }
120
121 grib_nearest_find_multiple(h, 1, vlat, vlon, npoints,
122 outlats, outlons, lsm_values, distances, indexes);
123
124 grib_handle_delete(h);
125
126 fclose(fin);
127
128 for (n = 3; n <= argc - 1; n++) {
129 fname = argv[n];
130 fin = fopen(fname, "r");
131 if (!fin) {
132 perror(fname);
133 exit(1);
134 }
135 while ((h = grib_handle_new_from_file(0, fin, &ret)) != NULL) {
136 grib_get_double_elements(h, "values", indexes, npoints, values);
137
138 grib_handle_delete(h);
139 for (i = 0; i < npoints; i++)
140 printf("%g %g %g %g %g %g\n",
141 vlat[i], vlon[i],
142 outlats[i], outlons[i],
143 lsm_values[i], values[i]);
144 }
145
146
147 fclose(fin);
148 }
149
150
151 return ret;
152 }
153