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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
/*
* (C) Copyright 2005- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
*/
/*
* Description: Nearest neighbour functionality using multiple input points
*
*/
#include "eccodes.h"
static void usage(const char* prog)
{
fprintf(stderr, "Usage: %s latlon_file grib_orography grib_file grib_file ...\n", prog);
fprintf(stderr, " The latlon_file should have 3 columns: num lat lon\n");
fprintf(stderr, " The grib_orography file is treated as the land-sea mask\n");
fprintf(stderr, "\n");
exit(1);
}
int main(int argc, char** argv)
{
FILE* fin = 0;
int ret = 0;
char* fname = 0;
float lat, lon;
double *vlat, *vlon;
int npoints = 0, i = 0, n = 0;
codes_handle* h;
double *outlats, *outlons, *values, *lsm_values, *distances;
int* indexes;
/*long step=0;
char time[10]={0,};
char date[10]={0,};
long parameter=0;
size_t len=0;*/
long iid = 0;
long* id = NULL;
const int is_lsm = 1;
if (argc < 2) usage(argv[0]);
/* input lat/lon file should have 3 columns:
* number latitude longitude
*/
fname = argv[1];
fin = fopen(fname, "r"); /* open in text mode */
if (!fin) {
perror(fname);
exit(1);
}
npoints = 0;
while (fscanf(fin, "%ld %g %g", &iid, &lat, &lon) != EOF)
npoints++;
fclose(fin);
if (npoints==0) {
fprintf(stderr, "No input points found!\n");
exit(1);
}
id = (long*)calloc(npoints, sizeof(long));
if (!id) {
fprintf(stderr, "unable to allocate %ld bytes\n", (long)(npoints * sizeof(long)));
exit(1);
}
vlat = (double*)calloc(npoints, sizeof(double));
if (!vlat) {
fprintf(stderr, "unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
exit(1);
}
vlon = (double*)calloc(npoints, sizeof(double));
if (!vlon) {
fprintf(stderr, "unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
exit(1);
}
outlats = (double*)calloc(npoints, sizeof(double));
if (!outlats) {
fprintf(stderr, "unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
exit(1);
}
outlons = (double*)calloc(npoints, sizeof(double));
if (!outlons) {
fprintf(stderr, "unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
exit(1);
}
values = (double*)calloc(npoints, sizeof(double));
if (!values) {
fprintf(stderr, "unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
exit(1);
}
lsm_values = (double*)calloc(npoints, sizeof(double));
if (!lsm_values) {
fprintf(stderr, "unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
exit(1);
}
distances = (double*)calloc(npoints, sizeof(double));
if (!distances) {
fprintf(stderr, "unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
exit(1);
}
indexes = (int*)calloc(npoints, sizeof(int));
if (!indexes) {
fprintf(stderr, "unable to allocate %ld bytes\n", (long)(npoints * sizeof(int)));
exit(1);
}
fname = argv[1];
fin = fopen(fname, "r"); /* open in text mode */
if (!fin) {
perror(fname);
exit(1);
}
i = 0;
while (i < npoints && fscanf(fin, "%ld %g %g", &iid, &lat, &lon) != EOF) {
id[i] = iid;
vlat[i] = lat;
while (lon < 0)
lon += 360;
vlon[i] = lon;
i++;
}
fclose(fin);
/* the first GRIB file on the arguments is treated as the land-sea mask file */
fname = argv[2];
fin = fopen(fname, "rb"); /* open GRIB in binary mode */
if (!fin) {
perror(fname);
exit(1);
}
h = codes_handle_new_from_file(0, fin, PRODUCT_GRIB, &ret);
if (!h || ret != CODES_SUCCESS) {
fprintf(stderr, "unable to create handle\n");
exit(1);
}
codes_grib_nearest_find_multiple(h, is_lsm, vlat, vlon, npoints,
outlats, outlons, lsm_values, distances, indexes);
codes_handle_delete(h);
fclose(fin);
for (n = 3; n <= argc - 1; n++) {
fname = argv[n];
fin = fopen(fname, "r");
if (!fin) {
perror(fname);
exit(1);
}
while ((h = codes_handle_new_from_file(0, fin, PRODUCT_GRIB, &ret)) != NULL) {
codes_get_double_elements(h, "values", indexes, npoints, values);
/*
CODES_CHECK(codes_get_length(h, "date", &len),0);
codes_get_string(h,"date",date,&len);
CODES_CHECK(codes_get_length(h, "time", &len),0);
codes_get_string(h,"time",time,&len);
codes_get_long(h,"step",&step);
codes_get_long(h,"parameter",¶meter);
printf("# %s %s %ld %ld\n",date,time,step,parameter);
*/
codes_handle_delete(h);
for (i = 0; i < npoints; i++)
printf("%ld %.2f %.2f %.2f %.2f %d\n",
id[i], outlats[i], outlons[i],
lsm_values[i], values[i], indexes[i]);
}
fclose(fin);
}
free(id);
free(vlat);
free(vlon);
free(outlats);
free(outlons);
free(values);
free(lsm_values);
free(distances);
free(indexes);
return ret;
}
|