2013-03-25 12:04:10 +00:00
|
|
|
/*
|
2020-01-28 14:32:34 +00:00
|
|
|
* (C) Copyright 2005- ECMWF.
|
2013-03-25 12:04:10 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2019-04-17 11:54:16 +00:00
|
|
|
* Description: Nearest neighbour functionality using multiple input points
|
2013-03-25 12:04:10 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-12-23 10:16:21 +00:00
|
|
|
#include "eccodes.h"
|
2013-03-25 12:04:10 +00:00
|
|
|
|
2020-01-22 14:57:43 +00:00
|
|
|
static void usage(const char* prog)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: %s latlon_file grib_orography grib_file grib_file ...\n", prog);
|
2019-04-17 11:54:16 +00:00
|
|
|
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");
|
2013-11-19 17:56:05 +00:00
|
|
|
exit(1);
|
2013-03-25 12:04:10 +00:00
|
|
|
}
|
|
|
|
|
2014-06-18 16:14:01 +00:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2020-01-22 14:57:43 +00:00
|
|
|
FILE* fin = 0;
|
|
|
|
int ret = 0;
|
|
|
|
char* fname = 0;
|
|
|
|
float lat, lon;
|
|
|
|
double *vlat, *vlon;
|
|
|
|
int npoints = 0, i = 0, n = 0;
|
2014-12-23 10:16:21 +00:00
|
|
|
codes_handle* h;
|
2020-01-22 14:57:43 +00:00
|
|
|
double *outlats, *outlons, *values, *lsm_values, *distances;
|
2013-11-19 17:56:05 +00:00
|
|
|
int* indexes;
|
2019-04-17 14:58:24 +00:00
|
|
|
/*long step=0;
|
2013-11-19 17:56:05 +00:00
|
|
|
char time[10]={0,};
|
|
|
|
char date[10]={0,};
|
|
|
|
long parameter=0;
|
2019-04-17 14:58:24 +00:00
|
|
|
size_t len=0;*/
|
2020-01-22 14:57:43 +00:00
|
|
|
long iid = 0;
|
|
|
|
long* id = NULL;
|
2019-04-17 11:54:16 +00:00
|
|
|
const int is_lsm = 1;
|
2013-11-19 17:56:05 +00:00
|
|
|
|
|
|
|
if (argc < 2) usage(argv[0]);
|
|
|
|
|
2019-04-17 11:54:16 +00:00
|
|
|
/* Input lat/lon file should have 3 columns:
|
|
|
|
* number latitude longitude
|
|
|
|
*/
|
2020-01-22 14:57:43 +00:00
|
|
|
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++;
|
2013-11-19 17:56:05 +00:00
|
|
|
fclose(fin);
|
|
|
|
|
2020-01-22 14:57:43 +00:00
|
|
|
id = (long*)malloc(npoints * sizeof(long));
|
|
|
|
if (!id) {
|
|
|
|
printf("unable to allocate %ld bytes\n", (long)(npoints * sizeof(long)));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
vlat = (double*)malloc(npoints * sizeof(double));
|
|
|
|
if (!vlat) {
|
|
|
|
printf("unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
vlon = (double*)malloc(npoints * sizeof(double));
|
|
|
|
if (!vlon) {
|
|
|
|
printf("unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
outlats = (double*)malloc(npoints * sizeof(double));
|
|
|
|
if (!outlats) {
|
|
|
|
printf("unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
outlons = (double*)malloc(npoints * sizeof(double));
|
|
|
|
if (!outlons) {
|
|
|
|
printf("unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
values = (double*)malloc(npoints * sizeof(double));
|
|
|
|
if (!values) {
|
|
|
|
printf("unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
lsm_values = (double*)malloc(npoints * sizeof(double));
|
|
|
|
if (!lsm_values) {
|
|
|
|
printf("unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
distances = (double*)malloc(npoints * sizeof(double));
|
|
|
|
if (!distances) {
|
|
|
|
printf("unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
indexes = (int*)malloc(npoints * sizeof(int));
|
|
|
|
if (!indexes) {
|
|
|
|
printf("unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fname = argv[1];
|
|
|
|
fin = fopen(fname, "r"); /* Open in text mode */
|
|
|
|
if (!fin) {
|
|
|
|
perror(fname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
while (fscanf(fin, "%ld %g %g", &iid, &lat, &lon) != EOF) {
|
|
|
|
id[i] = iid;
|
|
|
|
vlat[i] = lat;
|
|
|
|
while (lon < 0)
|
|
|
|
lon += 360;
|
|
|
|
vlon[i] = lon;
|
2013-11-19 17:56:05 +00:00
|
|
|
i++;
|
2013-03-25 12:04:10 +00:00
|
|
|
}
|
2013-11-19 17:56:05 +00:00
|
|
|
fclose(fin);
|
|
|
|
|
2019-04-17 11:54:16 +00:00
|
|
|
/* The first GRIB file on the arguments is treated as the land-sea mask file */
|
2020-01-22 14:57:43 +00:00
|
|
|
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) {
|
|
|
|
printf(" unable to create handle\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-11-19 17:56:05 +00:00
|
|
|
|
2020-01-22 14:57:43 +00:00
|
|
|
codes_grib_nearest_find_multiple(h, is_lsm, vlat, vlon, npoints,
|
|
|
|
outlats, outlons, lsm_values, distances, indexes);
|
2013-11-19 17:56:05 +00:00
|
|
|
|
2014-12-23 10:16:21 +00:00
|
|
|
codes_handle_delete(h);
|
2013-03-25 12:04:10 +00:00
|
|
|
|
|
|
|
fclose(fin);
|
|
|
|
|
2020-01-22 14:57:43 +00:00
|
|
|
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);
|
2013-11-19 17:56:05 +00:00
|
|
|
|
2019-04-17 14:58:24 +00:00
|
|
|
/*
|
2014-12-23 10:16:21 +00:00
|
|
|
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);
|
2013-11-19 17:56:05 +00:00
|
|
|
printf("# %s %s %ld %ld\n",date,time,step,parameter);
|
2019-04-17 14:58:24 +00:00
|
|
|
*/
|
2014-12-23 10:16:21 +00:00
|
|
|
codes_handle_delete(h);
|
2020-01-22 14:57:43 +00:00
|
|
|
for (i = 0; i < npoints; i++)
|
2019-04-17 14:58:24 +00:00
|
|
|
printf("%ld %.2f %.2f %.2f %.2f %d\n",
|
2020-01-22 14:57:43 +00:00
|
|
|
id[i], outlats[i], outlons[i],
|
|
|
|
lsm_values[i], values[i], indexes[i]);
|
2013-11-19 17:56:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fin);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2013-03-25 12:04:10 +00:00
|
|
|
}
|