From 9e66dd449467879de5dda9fc3484231d8a598e27 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Fri, 12 Jun 2020 22:14:02 +0100 Subject: [PATCH] Lambert azimuthal equal area: implement nearest --- definitions/grib2/template.3.140.def | 2 + src/CMakeLists.txt | 1 + src/Makefile.am | 1 + src/grib_nearest_class.h | 1 + ...arest_class_lambert_azimuthal_equal_area.c | 142 ++++++++++++++++++ src/grib_nearest_factory.h | 1 + tests/grib_lamb_az_eq_area.sh | 4 + 7 files changed, 152 insertions(+) create mode 100644 src/grib_nearest_class_lambert_azimuthal_equal_area.c diff --git a/definitions/grib2/template.3.140.def b/definitions/grib2/template.3.140.def index d04ba0764..094e624bd 100644 --- a/definitions/grib2/template.3.140.def +++ b/definitions/grib2/template.3.140.def @@ -60,6 +60,8 @@ iterator lambert_azimuthal_equal_area(numberOfPoints,missingValue,values, iScansNegatively, jScansPositively, jPointsAreConsecutive, alternativeRowScanning); +nearest lambert_azimuthal_equal_area(values,radius,Nx,Ny); + meta latLonValues latlonvalues(values); alias latitudeLongitudeValues=latLonValues; meta latitudes latitudes(values,0); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 682f92310..6dd7c3f51 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -346,6 +346,7 @@ list( APPEND grib_api_srcs grib_nearest_class_latlon_reduced.c grib_nearest_class_sh.c grib_nearest_class_lambert_conformal.c + grib_nearest_class_lambert_azimuthal_equal_area.c grib_nearest_class_mercator.c grib_nearest_class_polar_stereographic.c grib_iterator_class_polar_stereographic.c diff --git a/src/Makefile.am b/src/Makefile.am index 2008953f4..b5174a0c5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -361,6 +361,7 @@ libeccodes_la_prototypes= \ grib_nearest_class_latlon_reduced.c \ grib_nearest_class_sh.c \ grib_nearest_class_lambert_conformal.c \ + grib_nearest_class_lambert_azimuthal_equal_area.c \ grib_nearest_class_mercator.c \ grib_nearest_class_polar_stereographic.c \ grib_iterator_class_polar_stereographic.c \ diff --git a/src/grib_nearest_class.h b/src/grib_nearest_class.h index 5962393cb..a8998533c 100644 --- a/src/grib_nearest_class.h +++ b/src/grib_nearest_class.h @@ -1,5 +1,6 @@ /* This file is automatically generated by ./make_class.pl, do not edit */ extern grib_nearest_class* grib_nearest_class_gen; +extern grib_nearest_class* grib_nearest_class_lambert_azimuthal_equal_area; extern grib_nearest_class* grib_nearest_class_lambert_conformal; extern grib_nearest_class* grib_nearest_class_latlon_reduced; extern grib_nearest_class* grib_nearest_class_mercator; diff --git a/src/grib_nearest_class_lambert_azimuthal_equal_area.c b/src/grib_nearest_class_lambert_azimuthal_equal_area.c new file mode 100644 index 000000000..540bf1c1e --- /dev/null +++ b/src/grib_nearest_class_lambert_azimuthal_equal_area.c @@ -0,0 +1,142 @@ +/* + * (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. + */ + +#include "grib_api_internal.h" + +/* + This is used by make_class.pl + + START_CLASS_DEF + CLASS = nearest + SUPER = grib_nearest_class_gen + IMPLEMENTS = init + IMPLEMENTS = find;destroy + MEMBERS = double* lats + MEMBERS = int lats_count + MEMBERS = double* lons + MEMBERS = int lons_count + MEMBERS = double* distances + MEMBERS = int* k + MEMBERS = int* i + MEMBERS = int* j + MEMBERS = const char* Ni + MEMBERS = const char* Nj + END_CLASS_DEF + + */ + +/* START_CLASS_IMP */ + +/* + +Don't edit anything between START_CLASS_IMP and END_CLASS_IMP +Instead edit values between START_CLASS_DEF and END_CLASS_DEF +or edit "nearest.class" and rerun ./make_class.pl + +*/ + + +static void init_class(grib_nearest_class*); + +static int init(grib_nearest* nearest, grib_handle* h, grib_arguments* args); +static int find(grib_nearest* nearest, grib_handle* h, double inlat, double inlon, unsigned long flags, double* outlats, double* outlons, double* values, double* distances, int* indexes, size_t* len); +static int destroy(grib_nearest* nearest); + +typedef struct grib_nearest_lambert_azimuthal_equal_area +{ + grib_nearest nearest; + /* Members defined in gen */ + const char* values_key; + const char* radius; + int cargs; + /* Members defined in lambert_azimuthal_equal_area */ + double* lats; + int lats_count; + double* lons; + int lons_count; + double* distances; + int* k; + int* i; + int* j; + const char* Ni; + const char* Nj; +} grib_nearest_lambert_azimuthal_equal_area; + +extern grib_nearest_class* grib_nearest_class_gen; + +static grib_nearest_class _grib_nearest_class_lambert_azimuthal_equal_area = { + &grib_nearest_class_gen, /* super */ + "lambert_azimuthal_equal_area", /* name */ + sizeof(grib_nearest_lambert_azimuthal_equal_area), /* size of instance */ + 0, /* inited */ + &init_class, /* init_class */ + &init, /* constructor */ + &destroy, /* destructor */ + &find, /* find nearest */ +}; + +grib_nearest_class* grib_nearest_class_lambert_azimuthal_equal_area = &_grib_nearest_class_lambert_azimuthal_equal_area; + + +static void init_class(grib_nearest_class* c) +{ +} +/* END_CLASS_IMP */ + +static int init(grib_nearest* nearest, grib_handle* h, grib_arguments* args) +{ + grib_nearest_lambert_azimuthal_equal_area* self = (grib_nearest_lambert_azimuthal_equal_area*)nearest; + self->Ni = grib_arguments_get_name(h, args, self->cargs++); + self->Nj = grib_arguments_get_name(h, args, self->cargs++); + self->i = (int*)grib_context_malloc(h->context, 2 * sizeof(int)); + self->j = (int*)grib_context_malloc(h->context, 2 * sizeof(int)); + return 0; +} + +static int find(grib_nearest* nearest, grib_handle* h, + double inlat, double inlon, unsigned long flags, + double* outlats, double* outlons, + double* values, double* distances, int* indexes, size_t* len) +{ + grib_nearest_lambert_azimuthal_equal_area* self = (grib_nearest_lambert_azimuthal_equal_area*)nearest; + return grib_nearest_find_generic( + nearest, h, inlat, inlon, flags, /* inputs */ + + self->values_key, /* outputs to set the 'self' object */ + self->radius, + &(self->lats), + &(self->lats_count), + &(self->lons), + &(self->lons_count), + &(self->distances), + self->Ni, + self->Nj, + + outlats, outlons, /* outputs of the find function */ + values, distances, indexes, len); +} + +static int destroy(grib_nearest* nearest) +{ + grib_nearest_lambert_azimuthal_equal_area* self = (grib_nearest_lambert_azimuthal_equal_area*)nearest; + if (self->lats) + grib_context_free(nearest->context, self->lats); + if (self->lons) + grib_context_free(nearest->context, self->lons); + if (self->i) + grib_context_free(nearest->context, self->i); + if (self->j) + grib_context_free(nearest->context, self->j); + if (self->k) + grib_context_free(nearest->context, self->k); + if (self->distances) + grib_context_free(nearest->context, self->distances); + return GRIB_SUCCESS; +} diff --git a/src/grib_nearest_factory.h b/src/grib_nearest_factory.h index 8ec5cfedf..dca9dcdbb 100644 --- a/src/grib_nearest_factory.h +++ b/src/grib_nearest_factory.h @@ -1,5 +1,6 @@ /* This file is automatically generated by ./make_class.pl, do not edit */ { "gen", &grib_nearest_class_gen, }, +{ "lambert_azimuthal_equal_area", &grib_nearest_class_lambert_azimuthal_equal_area, }, { "lambert_conformal", &grib_nearest_class_lambert_conformal, }, { "latlon_reduced", &grib_nearest_class_latlon_reduced, }, { "mercator", &grib_nearest_class_mercator, }, diff --git a/tests/grib_lamb_az_eq_area.sh b/tests/grib_lamb_az_eq_area.sh index 0f7c753ec..71f048746 100755 --- a/tests/grib_lamb_az_eq_area.sh +++ b/tests/grib_lamb_az_eq_area.sh @@ -55,5 +55,9 @@ diff $DATA_OUTFILE $REF_FILE grib_check_key_equals $GRIB_OUTFILE standardParallelInDegrees,centralLongitudeInDegrees '48 9' grib_check_key_equals $GRIB_OUTFILE xDirectionGridLengthInMetres,yDirectionGridLengthInMetres '5000 5000' +# Nearest +${tools_dir}/grib_ls -l 67,-33,1 $GRIB_OUTFILE + + # Clean up rm -f $FILTER_FILE $GRIB_OUTFILE $DATA_OUTFILE