From 1afda19f17c186d5aebc2b1c8575c7d8554bda11 Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Sat, 21 Sep 2024 17:54:46 +0200 Subject: [PATCH 01/20] Modernisation: C++ iterators --- src/CMakeLists.txt | 26 +- src/eccodes_prototypes.h | 12 +- src/grib_api_internal.h | 36 +-- src/grib_iterator.cc | 168 ------------ src/grib_iterator_class.cc | 11 +- src/grib_iterator_class.h | 24 +- src/grib_iterator_class_latlon.cc | 224 ---------------- src/grib_iterator_class_latlon_reduced.cc | 205 --------------- src/grib_iterator_class_regular.cc | 246 ------------------ src/grib_iterator_factory.h | 26 +- src/iterator/grib_iterator.h | 40 +++ .../grib_iterator_class_gaussian.cc | 98 +------ src/iterator/grib_iterator_class_gaussian.h | 21 ++ .../grib_iterator_class_gaussian_reduced.cc | 212 +++++---------- .../grib_iterator_class_gaussian_reduced.h | 46 ++++ src/{ => iterator}/grib_iterator_class_gen.cc | 129 +++------ src/iterator/grib_iterator_class_gen.h | 33 +++ .../grib_iterator_class_healpix.cc | 149 +++-------- src/iterator/grib_iterator_class_healpix.h | 35 +++ ...ator_class_lambert_azimuthal_equal_area.cc | 169 ++++-------- ...rator_class_lambert_azimuthal_equal_area.h | 32 +++ .../grib_iterator_class_lambert_conformal.cc | 184 ++++--------- .../grib_iterator_class_lambert_conformal.h | 48 ++++ src/iterator/grib_iterator_class_latlon.cc | 152 +++++++++++ src/iterator/grib_iterator_class_latlon.h | 25 ++ .../grib_iterator_class_latlon_reduced.cc | 139 ++++++++++ .../grib_iterator_class_latlon_reduced.h | 30 +++ .../grib_iterator_class_mercator.cc | 167 ++++-------- src/iterator/grib_iterator_class_mercator.h | 38 +++ ...grib_iterator_class_polar_stereographic.cc | 160 ++++-------- .../grib_iterator_class_polar_stereographic.h | 32 +++ src/iterator/grib_iterator_class_regular.cc | 160 ++++++++++++ src/iterator/grib_iterator_class_regular.h | 40 +++ .../grib_iterator_class_space_view.cc | 166 ++++-------- src/iterator/grib_iterator_class_space_view.h | 32 +++ 35 files changed, 1366 insertions(+), 1949 deletions(-) delete mode 100644 src/grib_iterator.cc delete mode 100644 src/grib_iterator_class_latlon.cc delete mode 100644 src/grib_iterator_class_latlon_reduced.cc delete mode 100644 src/grib_iterator_class_regular.cc create mode 100644 src/iterator/grib_iterator.h rename src/{ => iterator}/grib_iterator_class_gaussian.cc (61%) create mode 100644 src/iterator/grib_iterator_class_gaussian.h rename src/{ => iterator}/grib_iterator_class_gaussian_reduced.cc (66%) create mode 100644 src/iterator/grib_iterator_class_gaussian_reduced.h rename src/{ => iterator}/grib_iterator_class_gen.cc (66%) create mode 100644 src/iterator/grib_iterator_class_gen.h rename src/{ => iterator}/grib_iterator_class_healpix.cc (71%) create mode 100644 src/iterator/grib_iterator_class_healpix.h rename src/{ => iterator}/grib_iterator_class_lambert_azimuthal_equal_area.cc (77%) create mode 100644 src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h rename src/{ => iterator}/grib_iterator_class_lambert_conformal.cc (78%) create mode 100644 src/iterator/grib_iterator_class_lambert_conformal.h create mode 100644 src/iterator/grib_iterator_class_latlon.cc create mode 100644 src/iterator/grib_iterator_class_latlon.h create mode 100644 src/iterator/grib_iterator_class_latlon_reduced.cc create mode 100644 src/iterator/grib_iterator_class_latlon_reduced.h rename src/{ => iterator}/grib_iterator_class_mercator.cc (71%) create mode 100644 src/iterator/grib_iterator_class_mercator.h rename src/{ => iterator}/grib_iterator_class_polar_stereographic.cc (72%) create mode 100644 src/iterator/grib_iterator_class_polar_stereographic.h create mode 100644 src/iterator/grib_iterator_class_regular.cc create mode 100644 src/iterator/grib_iterator_class_regular.h rename src/{ => iterator}/grib_iterator_class_space_view.cc (74%) create mode 100644 src/iterator/grib_iterator_class_space_view.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a6bf02848..6ad3fc89d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -331,20 +331,20 @@ list( APPEND eccodes_src_files grib_nearest_class_mercator.cc grib_nearest_class_polar_stereographic.cc grib_nearest_class_space_view.cc - grib_iterator_class_polar_stereographic.cc - grib_iterator_class_lambert_azimuthal_equal_area.cc - grib_iterator_class_lambert_conformal.cc - grib_iterator_class_mercator.cc - grib_iterator.cc + iterator/grib_iterator_class_polar_stereographic.cc + iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc + iterator/grib_iterator_class_lambert_conformal.cc + iterator/grib_iterator_class_mercator.cc + iterator/grib_iterator.cc grib_iterator_class.cc - grib_iterator_class_gaussian.cc - grib_iterator_class_gaussian_reduced.cc - grib_iterator_class_latlon_reduced.cc - grib_iterator_class_gen.cc - grib_iterator_class_latlon.cc - grib_iterator_class_regular.cc - grib_iterator_class_space_view.cc - grib_iterator_class_healpix.cc + iterator/grib_iterator_class_gaussian.cc + iterator/grib_iterator_class_gaussian_reduced.cc + iterator/grib_iterator_class_latlon_reduced.cc + iterator/grib_iterator_class_gen.cc + iterator/grib_iterator_class_latlon.cc + iterator/grib_iterator_class_regular.cc + iterator/grib_iterator_class_space_view.cc + iterator/grib_iterator_class_healpix.cc grib_expression.cc codes_util.cc grib_util.cc diff --git a/src/eccodes_prototypes.h b/src/eccodes_prototypes.h index 5039a36e7..a8a24ad33 100644 --- a/src/eccodes_prototypes.h +++ b/src/eccodes_prototypes.h @@ -811,12 +811,12 @@ grib_nearest* grib_nearest_factory(grib_handle* h, grib_arguments* args, int* er /* grib_iterator.cc */ int grib_get_data(const grib_handle* h, double* lats, double* lons, double* values); -int grib_iterator_next(grib_iterator* i, double* lat, double* lon, double* value); -int grib_iterator_has_next(grib_iterator* i); -int grib_iterator_previous(grib_iterator* i, double* lat, double* lon, double* value); -int grib_iterator_reset(grib_iterator* i); -int grib_iterator_init(grib_iterator* i, grib_handle* h, grib_arguments* args); -int grib_iterator_delete(grib_iterator* i); +//int grib_iterator_next(grib_iterator* i, double* lat, double* lon, double* value); +//int grib_iterator_has_next(grib_iterator* i); +//int grib_iterator_previous(grib_iterator* i, double* lat, double* lon, double* value); +//int grib_iterator_reset(grib_iterator* i); +//int grib_iterator_init(grib_iterator* i, grib_handle* h, grib_arguments* args); +//int grib_iterator_delete(grib_iterator* i); /* grib_iterator_class.cc */ grib_iterator* grib_iterator_factory(grib_handle* h, grib_arguments* args, unsigned long flags, int* error); diff --git a/src/grib_api_internal.h b/src/grib_api_internal.h index dc0742d8a..453ceee99 100644 --- a/src/grib_api_internal.h +++ b/src/grib_api_internal.h @@ -248,7 +248,7 @@ typedef struct grib_codetable grib_codetable; typedef struct grib_smart_table grib_smart_table; class grib_accessor; -typedef struct grib_iterator_class grib_iterator_class; +class grib_iterator; typedef struct grib_nearest_class grib_nearest_class; typedef struct grib_dumper grib_dumper; typedef struct grib_dumper_class grib_dumper_class; @@ -267,14 +267,6 @@ typedef int (*nearest_find_proc)(grib_nearest* nearest, grib_handle* h, double* distances, int* indexes, size_t* len); typedef int (*nearest_destroy_proc)(grib_nearest* nearest); -typedef void (*iterator_init_class_proc)(grib_iterator_class*); -typedef int (*iterator_init_proc)(grib_iterator* i, grib_handle*, grib_arguments*); - -typedef int (*iterator_next_proc)(grib_iterator* i, double* lat, double* lon, double* val); -typedef int (*iterator_previous_proc)(grib_iterator* i, double* lat, double* lon, double* val); -typedef int (*iterator_reset_proc)(grib_iterator* i); -typedef int (*iterator_destroy_proc)(grib_iterator* i); -typedef long (*iterator_has_next_proc)(grib_iterator* i); typedef int (*grib_pack_proc)(grib_handle* h, const double* in, size_t inlen, void* out, size_t* outlen); typedef int (*grib_unpack_proc)(grib_handle* h, const void* in, size_t inlen, double* out, size_t* outlen); @@ -459,20 +451,6 @@ struct grib_section size_t padding; }; -struct grib_iterator_class -{ - grib_iterator_class** super; - const char* name; - size_t size; - int inited; - iterator_init_class_proc init_class; - iterator_init_proc init; - iterator_destroy_proc destroy; - iterator_next_proc next; - iterator_previous_proc previous; - iterator_reset_proc reset; - iterator_has_next_proc has_next; -}; struct grib_nearest_class { @@ -529,17 +507,6 @@ struct grib_dumper_class dumper_footer_proc footer; }; -struct grib_iterator -{ - grib_arguments* args; /** args of iterator */ - grib_handle* h; - long e; /** current element */ - size_t nv; /** number of values */ - double* data; /** data values */ - grib_iterator_class* cclass; - unsigned long flags; -}; - struct grib_nearest { grib_arguments* args; /** args of iterator */ @@ -1287,6 +1254,7 @@ typedef struct j2k_encode_helper } #include "accessor/grib_accessor.h" #include "accessor/grib_accessors_list.h" +#include "iterator/grib_iterator.h" #endif #endif diff --git a/src/grib_iterator.cc b/src/grib_iterator.cc deleted file mode 100644 index 0d253bc68..000000000 --- a/src/grib_iterator.cc +++ /dev/null @@ -1,168 +0,0 @@ -/* - * (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. - */ - -/*************************************************************************** - * Jean Baptiste Filippi - 01.11.2005 * - ***************************************************************************/ -#include "grib_api_internal.h" - -#if GRIB_PTHREADS -static pthread_once_t once = PTHREAD_ONCE_INIT; -static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; - -static void init_mutex() -{ - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init(&mutex, &attr); - pthread_mutexattr_destroy(&attr); -} -#elif GRIB_OMP_THREADS -static int once = 0; -static omp_nest_lock_t mutex; - -static void init_mutex() -{ - GRIB_OMP_CRITICAL(lock_iterator_c) - { - if (once == 0) { - omp_init_nest_lock(&mutex); - once = 1; - } - } -} -#endif - -int grib_get_data(const grib_handle* h, double* lats, double* lons, double* values) -{ - int err = 0; - grib_iterator* iter = NULL; - double *lat, *lon, *val; - - iter = grib_iterator_new(h, 0, &err); - if (!iter || err != GRIB_SUCCESS) - return err; - - lat = lats; - lon = lons; - val = values; - while (grib_iterator_next(iter, lat++, lon++, val++)) {} - - grib_iterator_delete(iter); - - return err; -} - -int grib_iterator_next(grib_iterator* i, double* lat, double* lon, double* value) -{ - grib_iterator_class* c = i->cclass; - while (c) { - grib_iterator_class* s = c->super ? *(c->super) : NULL; - if (c->next) - return c->next(i, lat, lon, value); - c = s; - } - - grib_context_log(i->h->context, GRIB_LOG_FATAL, "%s: No next() function in iterator '%s'", __func__, i->cclass->name); - return 0; -} - -int grib_iterator_has_next(grib_iterator* i) -{ - grib_iterator_class* c = i->cclass; - while (c) { - grib_iterator_class* s = c->super ? *(c->super) : NULL; - if (c->has_next) - return c->has_next(i); - c = s; - } - grib_context_log(i->h->context, GRIB_LOG_FATAL, "%s: No has_next() function in iterator '%s'", __func__, i->cclass->name); - return 0; -} - -int grib_iterator_previous(grib_iterator* i, double* lat, double* lon, double* value) -{ - grib_iterator_class* c = i->cclass; - while (c) { - grib_iterator_class* s = c->super ? *(c->super) : NULL; - if (c->previous) - return c->previous(i, lat, lon, value); - c = s; - } - grib_context_log(i->h->context, GRIB_LOG_FATAL, "%s: No previous() function in iterator '%s'", __func__, i->cclass->name); - return 0; -} - -int grib_iterator_reset(grib_iterator* i) -{ - grib_iterator_class* c = i->cclass; - while (c) { - grib_iterator_class* s = c->super ? *(c->super) : NULL; - if (c->reset) - return c->reset(i); - c = s; - } - grib_context_log(i->h->context, GRIB_LOG_FATAL, "%s: No reset() function in iterator '%s'", __func__, i->cclass->name); - return 0; -} - -/* For this one, ALL init are called */ -static int init_iterator(grib_iterator_class* c, grib_iterator* i, grib_handle* h, grib_arguments* args) -{ - if (c) { - int ret = GRIB_SUCCESS; - grib_iterator_class* s = c->super ? *(c->super) : NULL; - if (!c->inited) { - if (c->init_class) - c->init_class(c); - c->inited = 1; - } - if (s) - ret = init_iterator(s, i, h, args); - - if (ret != GRIB_SUCCESS) - return ret; - - if (c->init) - return c->init(i, h, args); - } - return GRIB_INTERNAL_ERROR; -} - -int grib_iterator_init(grib_iterator* i, grib_handle* h, grib_arguments* args) -{ - int r = 0; - GRIB_MUTEX_INIT_ONCE(&once, &init_mutex); - GRIB_MUTEX_LOCK(&mutex); - r = init_iterator(i->cclass, i, h, args); - GRIB_MUTEX_UNLOCK(&mutex); - return r; -} - -/* For this one, ALL destroy are called */ -int grib_iterator_delete(grib_iterator* i) -{ - if (i) { - grib_iterator_class* c = i->cclass; - while (c) { - grib_iterator_class* s = c->super ? *(c->super) : NULL; - if (c->destroy) - c->destroy(i); - c = s; - } - /* This should go in a top class */ - grib_context_free(i->h->context, i); - } - else { - return GRIB_INVALID_ARGUMENT; - } - return 0; -} diff --git a/src/grib_iterator_class.cc b/src/grib_iterator_class.cc index 6e1ec78b7..38614cf1c 100644 --- a/src/grib_iterator_class.cc +++ b/src/grib_iterator_class.cc @@ -21,7 +21,7 @@ struct table_entry { const char* type; - grib_iterator_class** cclass; + grib_iterator** cclass; }; static const struct table_entry table[] = { @@ -38,11 +38,10 @@ grib_iterator* grib_iterator_factory(grib_handle* h, grib_arguments* args, unsig num_table_entries = sizeof(table) / sizeof(table[0]); for (i = 0; i < num_table_entries; i++) { if (strcmp(type, table[i].type) == 0) { - grib_iterator_class* c = *(table[i].cclass); - grib_iterator* it = (grib_iterator*)grib_context_malloc_clear(h->context, c->size); - it->cclass = c; - it->flags = flags; - *error = grib_iterator_init(it, h, args); + grib_iterator* c = *(table[i].cclass); + grib_iterator* it = (grib_iterator*)grib_context_malloc_clear(h->context, c->size_); + it->flags_ = flags; + *error = it->init(h, args); if (*error == GRIB_SUCCESS) return it; grib_context_log(h->context, GRIB_LOG_ERROR, "Geoiterator factory: Error instantiating iterator %s (%s)", diff --git a/src/grib_iterator_class.h b/src/grib_iterator_class.h index e9f95f5ec..7360818fc 100644 --- a/src/grib_iterator_class.h +++ b/src/grib_iterator_class.h @@ -1,13 +1,13 @@ /* This file is automatically generated by ./make_class.pl, do not edit */ -extern grib_iterator_class* grib_iterator_class_gaussian; -extern grib_iterator_class* grib_iterator_class_gaussian_reduced; -extern grib_iterator_class* grib_iterator_class_gen; -extern grib_iterator_class* grib_iterator_class_healpix; -extern grib_iterator_class* grib_iterator_class_lambert_azimuthal_equal_area; -extern grib_iterator_class* grib_iterator_class_lambert_conformal; -extern grib_iterator_class* grib_iterator_class_latlon; -extern grib_iterator_class* grib_iterator_class_latlon_reduced; -extern grib_iterator_class* grib_iterator_class_mercator; -extern grib_iterator_class* grib_iterator_class_polar_stereographic; -extern grib_iterator_class* grib_iterator_class_regular; -extern grib_iterator_class* grib_iterator_class_space_view; +extern grib_iterator* grib_iterator_gaussian; +extern grib_iterator* grib_iterator_gaussian_reduced; +extern grib_iterator* grib_iterator_gen; +extern grib_iterator* grib_iterator_healpix; +extern grib_iterator* grib_iterator_lambert_azimuthal_equal_area; +extern grib_iterator* grib_iterator_lambert_conformal; +extern grib_iterator* grib_iterator_latlon; +extern grib_iterator* grib_iterator_latlon_reduced; +extern grib_iterator* grib_iterator_mercator; +extern grib_iterator* grib_iterator_polar_stereographic; +extern grib_iterator* grib_iterator_regular; +extern grib_iterator* grib_iterator_space_view; diff --git a/src/grib_iterator_class_latlon.cc b/src/grib_iterator_class_latlon.cc deleted file mode 100644 index 2d68b8f4e..000000000 --- a/src/grib_iterator_class_latlon.cc +++ /dev/null @@ -1,224 +0,0 @@ -/* - * (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 = iterator - SUPER = grib_iterator_class_regular - IMPLEMENTS = init;next - 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 "iterator.class" and rerun ./make_class.pl - -*/ - - -static void init_class (grib_iterator_class*); - -static int init (grib_iterator* i,grib_handle*,grib_arguments*); -static int next (grib_iterator* i, double *lat, double *lon, double *val); - - -typedef struct grib_iterator_latlon{ - grib_iterator it; - /* Members defined in gen */ - int carg; - const char* missingValue; - /* Members defined in regular */ - double *las; - double *los; - long Ni; - long Nj; - long iScansNegatively; - long isRotated; - double angleOfRotation; - double southPoleLat; - double southPoleLon; - long jPointsAreConsecutive; - long disableUnrotate; - /* Members defined in latlon */ -} grib_iterator_latlon; - -extern grib_iterator_class* grib_iterator_class_regular; - -static grib_iterator_class _grib_iterator_class_latlon = { - &grib_iterator_class_regular, /* super */ - "latlon", /* name */ - sizeof(grib_iterator_latlon),/* size of instance */ - 0, /* inited */ - &init_class, /* init_class */ - &init, /* constructor */ - 0, /* destructor */ - &next, /* Next Value */ - 0, /* Previous Value */ - 0, /* Reset the counter */ - 0, /* has next values */ -}; - -grib_iterator_class* grib_iterator_class_latlon = &_grib_iterator_class_latlon; - - -static void init_class(grib_iterator_class* c) -{ - c->previous = (*(c->super))->previous; - c->reset = (*(c->super))->reset; - c->has_next = (*(c->super))->has_next; -} -/* END_CLASS_IMP */ - -static int next(grib_iterator* iter, double* lat, double* lon, double* val) -{ - /* GRIB-238: Support rotated lat/lon grids */ - double ret_lat, ret_lon, ret_val=0; - grib_iterator_latlon* self = (grib_iterator_latlon*)iter; - - if ((long)iter->e >= (long)(iter->nv - 1)) - return 0; - - iter->e++; - - /* Assumptions: - * All rows scan in the same direction (alternativeRowScanning==0) - */ - if (!self->jPointsAreConsecutive) { - /* Adjacent points in i (x) direction are consecutive */ - ret_lat = self->las[(long)floor(iter->e / self->Ni)]; - ret_lon = self->los[(long)iter->e % self->Ni]; - if (iter->data) - ret_val = iter->data[iter->e]; - } - else { - /* Adjacent points in j (y) direction is consecutive */ - ret_lon = self->los[(long)iter->e / self->Nj]; - ret_lat = self->las[(long)floor(iter->e % self->Nj)]; - if (iter->data) - ret_val = iter->data[iter->e]; - } - - /* See ECC-808: Some users want to disable the unrotate */ - if (self->isRotated && !self->disableUnrotate) { - double new_lat = 0, new_lon = 0; - unrotate(ret_lat, ret_lon, - self->angleOfRotation, self->southPoleLat, self->southPoleLon, - &new_lat, &new_lon); - ret_lat = new_lat; - ret_lon = new_lon; - } - - *lat = ret_lat; - *lon = ret_lon; - if (val && iter->data) { - *val = ret_val; - } - return 1; -} - -static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) -{ - grib_iterator_latlon* self = (grib_iterator_latlon*)iter; - int err = 0; - double jdir; - double lat1=0, lat2=0, north=0, south=0; - long jScansPositively; - long lai; - - const char* s_lat1 = grib_arguments_get_name(h, args, self->carg++); - const char* s_jdir = grib_arguments_get_name(h, args, self->carg++); - const char* s_jScansPos = grib_arguments_get_name(h, args, self->carg++); - const char* s_jPtsConsec = grib_arguments_get_name(h, args, self->carg++); - const char* s_isRotatedGrid = grib_arguments_get_name(h, args, self->carg++); - const char* s_angleOfRotation = grib_arguments_get_name(h, args, self->carg++); - const char* s_latSouthernPole = grib_arguments_get_name(h, args, self->carg++); - const char* s_lonSouthernPole = grib_arguments_get_name(h, args, self->carg++); - - self->angleOfRotation = 0; - self->isRotated = 0; - self->southPoleLat = 0; - self->southPoleLon = 0; - self->disableUnrotate = 0; /* unrotate enabled by default */ - - if ((err = grib_get_long(h, s_isRotatedGrid, &self->isRotated))) - return err; - if (self->isRotated) { - if ((err = grib_get_double_internal(h, s_angleOfRotation, &self->angleOfRotation))) - return err; - if ((err = grib_get_double_internal(h, s_latSouthernPole, &self->southPoleLat))) - return err; - if ((err = grib_get_double_internal(h, s_lonSouthernPole, &self->southPoleLon))) - return err; - } - - if ((err = grib_get_double_internal(h, s_lat1, &lat1))) - return err; - if ((err = grib_get_double_internal(h, "latitudeLastInDegrees", &lat2))) - return err; - if ((err = grib_get_double_internal(h, s_jdir, &jdir))) //can be GRIB_MISSING_DOUBLE - return err; - if ((err = grib_get_long_internal(h, s_jScansPos, &jScansPositively))) - return err; - if ((err = grib_get_long_internal(h, s_jPtsConsec, &self->jPointsAreConsecutive))) - return err; - if ((err = grib_get_long(h, "iteratorDisableUnrotate", &self->disableUnrotate))) - return err; - - /* ECC-984: If jDirectionIncrement is missing, then we cannot use it (See jDirectionIncrementGiven) */ - /* So try to compute the increment */ - if ( (grib_is_missing(h, s_jdir, &err) && err == GRIB_SUCCESS) || (jdir == GRIB_MISSING_DOUBLE) ) { - const long Nj = self->Nj; - Assert(Nj > 1); - if (lat1 > lat2) { - jdir = (lat1 - lat2) / (Nj - 1); - } - else { - jdir = (lat2 - lat1) / (Nj - 1); - } - grib_context_log(h->context, GRIB_LOG_DEBUG, - "Cannot use jDirectionIncrement. Using value of %.6f obtained from La1, La2 and Nj", jdir); - } - - if (jScansPositively) { - north = lat2; - south = lat1; - jdir = -jdir; - } else { - north = lat1; - south = lat2; - } - if (south > north) { - grib_context_log(h->context, GRIB_LOG_ERROR, - "Lat/Lon Geoiterator: First and last latitudes are inconsistent with scanning order: lat1=%g, lat2=%g jScansPositively=%ld", - lat1, lat2, jScansPositively); - return GRIB_WRONG_GRID; - } - - for (lai = 0; lai < self->Nj; lai++) { - self->las[lai] = lat1; - lat1 -= jdir; - } - /* ECC-1406: Due to rounding, errors can accumulate. - * So we ensure the last latitude is latitudeOfLastGridPointInDegrees - */ - self->las[self->Nj-1] = lat2; - - iter->e = -1; - return err; -} diff --git a/src/grib_iterator_class_latlon_reduced.cc b/src/grib_iterator_class_latlon_reduced.cc deleted file mode 100644 index 53252dd88..000000000 --- a/src/grib_iterator_class_latlon_reduced.cc +++ /dev/null @@ -1,205 +0,0 @@ -/* - * (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" -#include - -/* - This is used by make_class.pl - - START_CLASS_DEF - CLASS = iterator - SUPER = grib_iterator_class_gen - IMPLEMENTS = destroy - IMPLEMENTS = init;next - MEMBERS = double *las - MEMBERS = double *los - 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 "iterator.class" and rerun ./make_class.pl - -*/ - - -static void init_class (grib_iterator_class*); - -static int init (grib_iterator* i,grib_handle*,grib_arguments*); -static int next (grib_iterator* i, double *lat, double *lon, double *val); -static int destroy (grib_iterator* i); - - -typedef struct grib_iterator_latlon_reduced{ - grib_iterator it; - /* Members defined in gen */ - int carg; - const char* missingValue; - /* Members defined in latlon_reduced */ - double *las; - double *los; -} grib_iterator_latlon_reduced; - -extern grib_iterator_class* grib_iterator_class_gen; - -static grib_iterator_class _grib_iterator_class_latlon_reduced = { - &grib_iterator_class_gen, /* super */ - "latlon_reduced", /* name */ - sizeof(grib_iterator_latlon_reduced),/* size of instance */ - 0, /* inited */ - &init_class, /* init_class */ - &init, /* constructor */ - &destroy, /* destructor */ - &next, /* Next Value */ - 0, /* Previous Value */ - 0, /* Reset the counter */ - 0, /* has next values */ -}; - -grib_iterator_class* grib_iterator_class_latlon_reduced = &_grib_iterator_class_latlon_reduced; - - -static void init_class(grib_iterator_class* c) -{ - c->previous = (*(c->super))->previous; - c->reset = (*(c->super))->reset; - c->has_next = (*(c->super))->has_next; -} -/* END_CLASS_IMP */ - -static int next(grib_iterator* iter, double* lat, double* lon, double* val) -{ - grib_iterator_latlon_reduced* self = (grib_iterator_latlon_reduced*)iter; - - if ((long)iter->e >= (long)(iter->nv - 1)) - return 0; - iter->e++; - - *lat = self->las[iter->e]; - *lon = self->los[iter->e]; - if (val && iter->data) { - *val = iter->data[iter->e]; - } - return 1; -} - -static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) -{ - grib_iterator_latlon_reduced* self = (grib_iterator_latlon_reduced*)iter; - - int ret = GRIB_SUCCESS; - double laf; - double lal; - long nlats; - double lof, tlof; - double lol, dimin; - long* pl; - size_t plsize = 0; - long k, j, ii; - long nlons, plmax; - double jdirinc = 0; - double idirinc = 0; - double dlon = 0; - int islocal = 0; - long nlons2 = 0; /* adjusted num of longitudes */ - - const char* latofirst = grib_arguments_get_name(h, args, self->carg++); - const char* longoffirst = grib_arguments_get_name(h, args, self->carg++); - const char* latoflast = grib_arguments_get_name(h, args, self->carg++); - const char* longoflast = grib_arguments_get_name(h, args, self->carg++); - const char* nlats_name = grib_arguments_get_name(h, args, self->carg++); - const char* jdirec = grib_arguments_get_name(h, args, self->carg++); - const char* plac = grib_arguments_get_name(h, args, self->carg++); - - if ((ret = grib_get_double_internal(h, latofirst, &laf))) - return ret; - if ((ret = grib_get_double_internal(h, longoffirst, &lof))) - return ret; - - if ((ret = grib_get_double_internal(h, latoflast, &lal))) - return ret; - if ((ret = grib_get_double_internal(h, longoflast, &lol))) - return ret; - - if ((ret = grib_get_long_internal(h, nlats_name, &nlats))) - return ret; - - if ((ret = grib_get_double_internal(h, jdirec, &jdirinc))) - return ret; - - plsize = nlats; - pl = (long*)grib_context_malloc(h->context, plsize * sizeof(long)); - grib_get_long_array_internal(h, plac, pl, &plsize); - - self->las = (double*)grib_context_malloc(h->context, iter->nv * sizeof(double)); - self->los = (double*)grib_context_malloc(h->context, iter->nv * sizeof(double)); - - plmax = pl[0]; - for (j = 0; j < nlats; j++) - if (plmax < pl[j]) - plmax = pl[j]; - dimin = 360.0 / plmax; - - if (360 - fabs(lol - lof) < 2 * dimin) { - dlon = 360; - islocal = 0; - } - else if (lol < lof) { - /* handle something like 150 to -120 to treat as 150 to 240 */ - /* so that dlon is 90 (not -270) */ - dlon = lol + 360.0 - lof; - islocal = 1; - } - else { - dlon = lol - lof; - islocal = 1; - } - - if (laf > lal) - jdirinc = -jdirinc; - k = 0; - for (j = 0; j < nlats; j++) { - nlons = pl[j]; - tlof = lof; - nlons2 = nlons - islocal; - /*Sometimes there are no points on a latitude! Protect against div by zero*/ - if (nlons2 < 1) - nlons2 = 1; - idirinc = dlon / nlons2; - for (ii = 0; ii < nlons; ii++) { - self->las[k] = laf; - self->los[k] = tlof; - tlof += idirinc; - k++; - } - laf += jdirinc; - } - - iter->e = -1; - grib_context_free(h->context, pl); - - return ret; -} - -static int destroy(grib_iterator* iter) -{ - grib_iterator_latlon_reduced* self = (grib_iterator_latlon_reduced*)iter; - const grib_context* c = iter->h->context; - - grib_context_free(c, self->las); - grib_context_free(c, self->los); - return GRIB_SUCCESS; -} diff --git a/src/grib_iterator_class_regular.cc b/src/grib_iterator_class_regular.cc deleted file mode 100644 index 64033b8de..000000000 --- a/src/grib_iterator_class_regular.cc +++ /dev/null @@ -1,246 +0,0 @@ -/* - * (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" -#include - -/* - This is used by make_class.pl - - START_CLASS_DEF - CLASS = iterator - SUPER = grib_iterator_class_gen - IMPLEMENTS = previous;next - IMPLEMENTS = init;destroy - MEMBERS = double *las - MEMBERS = double *los - MEMBERS = long Ni - MEMBERS = long Nj - MEMBERS = long iScansNegatively - MEMBERS = long isRotated - MEMBERS = double angleOfRotation - MEMBERS = double southPoleLat - MEMBERS = double southPoleLon - MEMBERS = long jPointsAreConsecutive - MEMBERS = long disableUnrotate - 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 "iterator.class" and rerun ./make_class.pl - -*/ - - -static void init_class (grib_iterator_class*); - -static int init (grib_iterator* i,grib_handle*,grib_arguments*); -static int next (grib_iterator* i, double *lat, double *lon, double *val); -static int previous (grib_iterator* ei, double *lat, double *lon, double *val); -static int destroy (grib_iterator* i); - - -typedef struct grib_iterator_regular{ - grib_iterator it; - /* Members defined in gen */ - int carg; - const char* missingValue; - /* Members defined in regular */ - double *las; - double *los; - long Ni; - long Nj; - long iScansNegatively; - long isRotated; - double angleOfRotation; - double southPoleLat; - double southPoleLon; - long jPointsAreConsecutive; - long disableUnrotate; -} grib_iterator_regular; - -extern grib_iterator_class* grib_iterator_class_gen; - -static grib_iterator_class _grib_iterator_class_regular = { - &grib_iterator_class_gen, /* super */ - "regular", /* name */ - sizeof(grib_iterator_regular),/* size of instance */ - 0, /* inited */ - &init_class, /* init_class */ - &init, /* constructor */ - &destroy, /* destructor */ - &next, /* Next Value */ - &previous, /* Previous Value */ - 0, /* Reset the counter */ - 0, /* has next values */ -}; - -grib_iterator_class* grib_iterator_class_regular = &_grib_iterator_class_regular; - - -static void init_class(grib_iterator_class* c) -{ - c->reset = (*(c->super))->reset; - c->has_next = (*(c->super))->has_next; -} -/* END_CLASS_IMP */ - -#define ITER "Regular grid Geoiterator" - -static int next(grib_iterator* iter, double* lat, double* lon, double* val) -{ - grib_iterator_regular* self = (grib_iterator_regular*)iter; - - if ((long)iter->e >= (long)(iter->nv - 1)) - return 0; - - iter->e++; - - *lat = self->las[(long)floor(iter->e / self->Ni)]; - *lon = self->los[(long)iter->e % self->Ni]; - if (val && iter->data) { - *val = iter->data[iter->e]; - } - return 1; -} - -static int previous(grib_iterator* iter, double* lat, double* lon, double* val) -{ - grib_iterator_regular* self = (grib_iterator_regular*)iter; - - if (iter->e < 0) - return 0; - *lat = self->las[(long)floor(iter->e / self->Ni)]; - *lon = self->los[iter->e % self->Ni]; - if (val && iter->data) { - *val = iter->data[iter->e]; - } - iter->e--; - - return 1; -} - -static int destroy(grib_iterator* iter) -{ - grib_iterator_regular* self = (grib_iterator_regular*)iter; - const grib_context* c = iter->h->context; - grib_context_free(c, self->las); - grib_context_free(c, self->los); - return GRIB_SUCCESS; -} - -static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) -{ - grib_iterator_regular* self = (grib_iterator_regular*)iter; - int ret = GRIB_SUCCESS; - - long Ni; /* Number of points along a parallel = Nx */ - long Nj; /* Number of points along a meridian = Ny */ - double idir, idir_coded, lon1, lon2; - long loi; - - const char* s_lon1 = grib_arguments_get_name(h, args, self->carg++); - const char* s_idir = grib_arguments_get_name(h, args, self->carg++); - const char* s_Ni = grib_arguments_get_name(h, args, self->carg++); - const char* s_Nj = grib_arguments_get_name(h, args, self->carg++); - const char* s_iScansNeg = grib_arguments_get_name(h, args, self->carg++); - - if ((ret = grib_get_double_internal(h, s_lon1, &lon1))) - return ret; - if ((ret = grib_get_double_internal(h, "longitudeOfLastGridPointInDegrees", &lon2))) - return ret; - if ((ret = grib_get_double_internal(h, s_idir, &idir))) // can be GRIB_MISSING_DOUBLE - return ret; - idir_coded = idir; - if ((ret = grib_get_long_internal(h, s_Ni, &Ni))) - return ret; - if (grib_is_missing(h, s_Ni, &ret) && ret == GRIB_SUCCESS) { - grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Key %s cannot be 'missing' for a regular grid!", ITER, s_Ni); - return GRIB_WRONG_GRID; - } - - if ((ret = grib_get_long_internal(h, s_Nj, &Nj))) - return ret; - if (grib_is_missing(h, s_Nj, &ret) && ret == GRIB_SUCCESS) { - grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Key %s cannot be 'missing' for a regular grid!", ITER, s_Nj); - return GRIB_WRONG_GRID; - } - - if (Ni*Nj != iter->nv) { - grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Ni*Nj!=numberOfDataPoints (%ld*%ld!=%zu)", ITER, Ni, Nj, iter->nv); - return GRIB_WRONG_GRID; - } - - if ((ret = grib_get_long_internal(h, s_iScansNeg, &self->iScansNegatively))) - return ret; - - /* GRIB-801: Careful of case with a single point! Ni==1 */ - if (Ni > 1) { - /* Note: If first and last longitudes are equal I assume you wanna go round the globe */ - if (self->iScansNegatively) { - if (lon1 > lon2) { - idir = (lon1 - lon2) / (Ni - 1); - } - else { - idir = (lon1 + 360.0 - lon2) / (Ni - 1); - } - } - else { - if (lon2 > lon1) { - idir = (lon2 - lon1) / (Ni - 1); - } - else { - idir = (lon2 + 360.0 - lon1) / (Ni - 1); - } - } - } - if (self->iScansNegatively) { - idir = -idir; - } - else { - if (lon1 + (Ni - 2) * idir > 360) - lon1 -= 360; - /*See ECC-704, GRIB-396*/ - /*else if ( (lon1+(Ni-1)*idir)-360 > epsilon ){ - idir=360.0/(float)Ni; - }*/ - } - - self->Ni = Ni; - self->Nj = Nj; - - self->las = (double*)grib_context_malloc(h->context, Nj * sizeof(double)); - self->los = (double*)grib_context_malloc(h->context, Ni * sizeof(double)); - - if (idir != idir_coded) { - grib_context_log(h->context, GRIB_LOG_DEBUG, "%s: Using idir=%g (coded value=%g)", ITER, idir, idir_coded); - } - - for (loi = 0; loi < Ni; loi++) { - self->los[loi] = lon1; - lon1 += idir; - } - - // ECC-1406: Due to rounding, errors can accumulate. - // So we ensure the last longitude is longitudeOfLastGridPointInDegrees - // Also see ECC-1671, ECC-1708 - if (lon2 > 0) { - lon2 = normalise_longitude_in_degrees(lon2); - } - self->los[Ni-1] = lon2; - - return ret; -} diff --git a/src/grib_iterator_factory.h b/src/grib_iterator_factory.h index 206a6d140..e9bda157e 100644 --- a/src/grib_iterator_factory.h +++ b/src/grib_iterator_factory.h @@ -1,13 +1,13 @@ -/* This file is automatically generated by ./make_class.pl, do not edit */ -{ "gaussian", &grib_iterator_class_gaussian, }, -{ "gaussian_reduced", &grib_iterator_class_gaussian_reduced, }, -{ "gen", &grib_iterator_class_gen, }, -{ "healpix", &grib_iterator_class_healpix, }, -{ "lambert_azimuthal_equal_area", &grib_iterator_class_lambert_azimuthal_equal_area, }, -{ "lambert_conformal", &grib_iterator_class_lambert_conformal, }, -{ "latlon", &grib_iterator_class_latlon, }, -{ "latlon_reduced", &grib_iterator_class_latlon_reduced, }, -{ "mercator", &grib_iterator_class_mercator, }, -{ "polar_stereographic", &grib_iterator_class_polar_stereographic, }, -{ "regular", &grib_iterator_class_regular, }, -{ "space_view", &grib_iterator_class_space_view, }, +/* This file is automatically generated by ./make.pl, do not edit */ +{ "gaussian", &grib_iterator_gaussian, }, +{ "gaussian_reduced", &grib_iterator_gaussian_reduced, }, +{ "gen", &grib_iterator_gen, }, +{ "healpix", &grib_iterator_healpix, }, +{ "lambert_azimuthal_equal_area", &grib_iterator_lambert_azimuthal_equal_area, }, +{ "lambert_conformal", &grib_iterator_lambert_conformal, }, +{ "latlon", &grib_iterator_latlon, }, +{ "latlon_reduced", &grib_iterator_latlon_reduced, }, +{ "mercator", &grib_iterator_mercator, }, +{ "polar_stereographic", &grib_iterator_polar_stereographic, }, +{ "regular", &grib_iterator_regular, }, +{ "space_view", &grib_iterator_space_view, }, diff --git a/src/iterator/grib_iterator.h b/src/iterator/grib_iterator.h new file mode 100644 index 000000000..3307eabf4 --- /dev/null +++ b/src/iterator/grib_iterator.h @@ -0,0 +1,40 @@ +/* + * (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. + */ + +#pragma once + +#include "grib_api_internal.h" + +/*! Grib geoiterator, structure supporting a geographic iteration of values on a GRIB message. + \ingroup grib_iterator +*/ + +class grib_iterator +{ +public: + virtual int init(grib_handle*, grib_arguments*) = 0; + virtual int next(double*, double*, double*) = 0; + virtual int previous(double*, double*, double*) = 0; + virtual int reset() = 0; + virtual int destroy() = 0; + virtual long has_next() = 0; + +public: + grib_context* context_; + grib_arguments* args_; /** args of iterator */ + grib_handle* h_; + long e_; /** current element */ + size_t nv_; /** number of values */ + double* data_; /** data values */ + unsigned long flags_; + const char* class_name_; + size_t size_; + int inited_; +}; diff --git a/src/grib_iterator_class_gaussian.cc b/src/iterator/grib_iterator_class_gaussian.cc similarity index 61% rename from src/grib_iterator_class_gaussian.cc rename to src/iterator/grib_iterator_class_gaussian.cc index 714aa4d35..356f9fb5f 100644 --- a/src/grib_iterator_class_gaussian.cc +++ b/src/iterator/grib_iterator_class_gaussian.cc @@ -8,89 +8,17 @@ * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. */ -#include "grib_api_internal.h" +#include "grib_iterator_class_gaussian.h" #include -/* - This is used by make_class.pl - - START_CLASS_DEF - CLASS = iterator - SUPER = grib_iterator_class_regular - IMPLEMENTS = init - 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 "iterator.class" and rerun ./make_class.pl - -*/ - - -static void init_class (grib_iterator_class*); - -static int init (grib_iterator* i,grib_handle*,grib_arguments*); - - -typedef struct grib_iterator_gaussian{ - grib_iterator it; - /* Members defined in gen */ - int carg; - const char* missingValue; - /* Members defined in regular */ - double *las; - double *los; - long Ni; - long Nj; - long iScansNegatively; - long isRotated; - double angleOfRotation; - double southPoleLat; - double southPoleLon; - long jPointsAreConsecutive; - long disableUnrotate; - /* Members defined in gaussian */ -} grib_iterator_gaussian; - -extern grib_iterator_class* grib_iterator_class_regular; - -static grib_iterator_class _grib_iterator_class_gaussian = { - &grib_iterator_class_regular, /* super */ - "gaussian", /* name */ - sizeof(grib_iterator_gaussian),/* size of instance */ - 0, /* inited */ - &init_class, /* init_class */ - &init, /* constructor */ - 0, /* destructor */ - 0, /* Next Value */ - 0, /* Previous Value */ - 0, /* Reset the counter */ - 0, /* has next values */ -}; - -grib_iterator_class* grib_iterator_class_gaussian = &_grib_iterator_class_gaussian; - - -static void init_class(grib_iterator_class* c) -{ - c->next = (*(c->super))->next; - c->previous = (*(c->super))->previous; - c->reset = (*(c->super))->reset; - c->has_next = (*(c->super))->has_next; -} -/* END_CLASS_IMP */ +grib_iterator_gaussian_t _grib_iterator_gaussian{}; +grib_iterator* grib_iterator_gaussian = &_grib_iterator_gaussian; static void binary_search_gaussian_latitudes(const double xx[], const unsigned long n, double x, long* j); -static int init(grib_iterator* i, grib_handle* h, grib_arguments* args) +int grib_iterator_gaussian_t::init(grib_handle* h, grib_arguments* args) { - grib_iterator_gaussian* self = (grib_iterator_gaussian*)i; + grib_iterator_regular_t::init(h, args); double* lats; double laf; /* latitude of first point in degrees */ @@ -103,10 +31,10 @@ static int init(grib_iterator* i, grib_handle* h, grib_arguments* args) long istart = 0; int ret = GRIB_SUCCESS; - const char* latofirst = grib_arguments_get_name(h, args, self->carg++); - const char* latoflast = grib_arguments_get_name(h, args, self->carg++); - const char* numtrunc = grib_arguments_get_name(h, args, self->carg++); - const char* s_jScansPositively = grib_arguments_get_name(h, args, self->carg++); + const char* latofirst = grib_arguments_get_name(h, args, carg_++); + const char* latoflast = grib_arguments_get_name(h, args, carg_++); + const char* numtrunc = grib_arguments_get_name(h, args, carg_++); + const char* s_jScansPositively = grib_arguments_get_name(h, args, carg_++); if ((ret = grib_get_double_internal(h, latofirst, &laf))) return ret; @@ -144,15 +72,15 @@ static int init(grib_iterator* i, grib_handle* h, grib_arguments* args) } if (jScansPositively) { - for (lai = 0; lai < self->Nj; lai++) { + for (lai = 0; lai < Nj_; lai++) { DEBUG_ASSERT(istart >= 0); - self->las[lai] = lats[istart--]; + las_[lai] = lats[istart--]; if (istart<0) istart=size-1; } } else { - for (lai = 0; lai < self->Nj; lai++) { - self->las[lai] = lats[istart++]; + for (lai = 0; lai < Nj_; lai++) { + las_[lai] = lats[istart++]; if (istart > size - 1) istart = 0; } diff --git a/src/iterator/grib_iterator_class_gaussian.h b/src/iterator/grib_iterator_class_gaussian.h new file mode 100644 index 000000000..184c7f83d --- /dev/null +++ b/src/iterator/grib_iterator_class_gaussian.h @@ -0,0 +1,21 @@ +/* + * (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_iterator_class_regular.h" + +class grib_iterator_gaussian_t : public grib_iterator_regular_t +{ +public: + grib_iterator_gaussian_t() : grib_iterator_regular_t() + { + class_name_ = "gaussian"; + } + int init(grib_handle*, grib_arguments*); +}; diff --git a/src/grib_iterator_class_gaussian_reduced.cc b/src/iterator/grib_iterator_class_gaussian_reduced.cc similarity index 66% rename from src/grib_iterator_class_gaussian_reduced.cc rename to src/iterator/grib_iterator_class_gaussian_reduced.cc index b21f680b1..21a752afb 100644 --- a/src/grib_iterator_class_gaussian_reduced.cc +++ b/src/iterator/grib_iterator_class_gaussian_reduced.cc @@ -8,111 +8,33 @@ * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. */ -#include "grib_api_internal.h" +#include "grib_iterator_class_gaussian_reduced.h" #include -/* - This is used by make_class.pl - START_CLASS_DEF - CLASS = iterator - SUPER = grib_iterator_class_gen - IMPLEMENTS = destroy - IMPLEMENTS = init;next - MEMBERS = double *las - MEMBERS = double *los - MEMBERS = long Nj - MEMBERS = long isRotated - MEMBERS = double angleOfRotation - MEMBERS = double southPoleLat - MEMBERS = double southPoleLon - MEMBERS = long disableUnrotate - 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 "iterator.class" and rerun ./make_class.pl - -*/ - - -static void init_class (grib_iterator_class*); - -static int init (grib_iterator* i,grib_handle*,grib_arguments*); -static int next (grib_iterator* i, double *lat, double *lon, double *val); -static int destroy (grib_iterator* i); - - -typedef struct grib_iterator_gaussian_reduced{ - grib_iterator it; - /* Members defined in gen */ - int carg; - const char* missingValue; - /* Members defined in gaussian_reduced */ - double *las; - double *los; - long Nj; - long isRotated; - double angleOfRotation; - double southPoleLat; - double southPoleLon; - long disableUnrotate; -} grib_iterator_gaussian_reduced; - -extern grib_iterator_class* grib_iterator_class_gen; - -static grib_iterator_class _grib_iterator_class_gaussian_reduced = { - &grib_iterator_class_gen, /* super */ - "gaussian_reduced", /* name */ - sizeof(grib_iterator_gaussian_reduced),/* size of instance */ - 0, /* inited */ - &init_class, /* init_class */ - &init, /* constructor */ - &destroy, /* destructor */ - &next, /* Next Value */ - 0, /* Previous Value */ - 0, /* Reset the counter */ - 0, /* has next values */ -}; - -grib_iterator_class* grib_iterator_class_gaussian_reduced = &_grib_iterator_class_gaussian_reduced; - - -static void init_class(grib_iterator_class* c) -{ - c->previous = (*(c->super))->previous; - c->reset = (*(c->super))->reset; - c->has_next = (*(c->super))->has_next; -} -/* END_CLASS_IMP */ +grib_iterator_gaussian_reduced_t _grib_iterator_gaussian_reduced{}; +grib_iterator* grib_iterator_gaussian_reduced = &_grib_iterator_gaussian_reduced; #define ITER "Reduced Gaussian grid Geoiterator" -static int next(grib_iterator* iter, double* lat, double* lon, double* val) +int grib_iterator_gaussian_reduced_t::next(double* lat, double* lon, double* val) { - grib_iterator_gaussian_reduced* self = (grib_iterator_gaussian_reduced*)iter; double ret_lat=0, ret_lon=0; - if (iter->e >= (long)(iter->nv - 1)) + if (e_ >= (long)(nv_ - 1)) return 0; - iter->e++; + e_++; - ret_lat = self->las[iter->e]; - ret_lon = self->los[iter->e]; - if (val && iter->data) { - *val = iter->data[iter->e]; + ret_lat = las_[e_]; + ret_lon = los_[e_]; + if (val && data_) { + *val = data_[e_]; } - if (self->isRotated && !self->disableUnrotate) { + if (isRotated_ && !disableUnrotate_) { double new_lat = 0, new_lon = 0; unrotate(ret_lat, ret_lon, - self->angleOfRotation, self->southPoleLat, self->southPoleLon, + angleOfRotation_, southPoleLat_, southPoleLon_, &new_lat, &new_lon); ret_lat = new_lat; ret_lon = new_lon; @@ -172,7 +94,7 @@ static void binary_search(const double xx[], const unsigned long n, double x, lo } /* Use legacy way to compute the iterator latitude/longitude values */ -static int iterate_reduced_gaussian_subarea_legacy(grib_iterator* iter, grib_handle* h, +int grib_iterator_gaussian_reduced_t::iterate_reduced_gaussian_subarea_legacy(grib_handle* h, double lat_first, double lon_first, double lat_last, double lon_last, double* lats, long* pl, size_t plsize) @@ -183,7 +105,6 @@ static int iterate_reduced_gaussian_subarea_legacy(grib_iterator* iter, grib_han long row_count = 0; double d = 0; long ilon_first, ilon_last, i; - grib_iterator_gaussian_reduced* self = (grib_iterator_gaussian_reduced*)iter; /*get_reduced_row_proc get_reduced_row = &grib_get_reduced_row;*/ get_reduced_row_proc get_reduced_row = &grib_get_reduced_row_legacy; /* legacy algorithm */ @@ -198,7 +119,7 @@ static int iterate_reduced_gaussian_subarea_legacy(grib_iterator* iter, grib_han l++; } - iter->e = 0; + e_ = 0; for (j = 0; j < plsize; j++) { long k = 0; row_count = 0; @@ -207,16 +128,16 @@ static int iterate_reduced_gaussian_subarea_legacy(grib_iterator* iter, grib_han if (ilon_first > ilon_last) ilon_first -= pl[j]; for (i = ilon_first; i <= ilon_last; i++) { - if (iter->e >= iter->nv) { + if (e_ >= nv_) { size_t np = count_subarea_points(h, get_reduced_row, pl, plsize, lon_first, lon_last); grib_context_log(h->context, GRIB_LOG_ERROR, - "%s (sub-area legacy). Num points=%zu, size(values)=%zu", ITER, np, iter->nv); + "%s (sub-area legacy). Num points=%zu, size(values)=%zu", ITER, np, nv_); return GRIB_WRONG_GRID; } - self->los[iter->e] = ((i)*360.0) / pl[j]; - self->las[iter->e] = lats[j + l]; - iter->e++; + los_[e_] = ((i)*360.0) / pl[j]; + las_[e_] = lats[j + l]; + e_++; k++; if (k >= row_count) { /* Ensure we exit the loop and only process 'row_count' points */ @@ -235,7 +156,7 @@ static int iterate_reduced_gaussian_subarea_legacy(grib_iterator* iter, grib_han // err = iterate_reduced_gaussian_subarea_algorithm2(iter, h, lat_first, lon_first, lat_last, lon_last, lats, pl, plsize); // } // return err; -static int iterate_reduced_gaussian_subarea(grib_iterator* iter, grib_handle* h, +int grib_iterator_gaussian_reduced_t::iterate_reduced_gaussian_subarea(grib_handle* h, double lat_first, double lon_first, double lat_last, double lon_last, double* lats, long* pl, size_t plsize, size_t numlats) @@ -245,7 +166,6 @@ static int iterate_reduced_gaussian_subarea(grib_iterator* iter, grib_handle* h, size_t j = 0; long row_count = 0, i = 0; double olon_first, olon_last; - grib_iterator_gaussian_reduced* self = (grib_iterator_gaussian_reduced*)iter; get_reduced_row_proc get_reduced_row = &grib_get_reduced_row; if (h->context->debug) { @@ -265,33 +185,33 @@ static int iterate_reduced_gaussian_subarea(grib_iterator* iter, grib_handle* h, // } // } - iter->e = 0; + e_ = 0; for (j = 0; j < plsize; j++) { const double delta = 360.0 / pl[j]; row_count = 0; grib_get_reduced_row_p(pl[j], lon_first, lon_last, &row_count, &olon_first, &olon_last); for (i = 0; i < row_count; ++i) { double lon2 = olon_first + i * delta; - if (iter->e >= iter->nv) { + if (e_ >= nv_) { /* Only print error message on the second pass */ size_t np = count_subarea_points(h, get_reduced_row, pl, plsize, lon_first, lon_last); grib_context_log(h->context, GRIB_LOG_ERROR, - "%s (sub-area). Num points=%zu, size(values)=%zu", ITER, np, iter->nv); + "%s (sub-area). Num points=%zu, size(values)=%zu", ITER, np, nv_); return GRIB_WRONG_GRID; } - self->los[iter->e] = lon2; + los_[e_] = lon2; DEBUG_ASSERT(j + l < numlats); - self->las[iter->e] = lats[j + l]; - iter->e++; + las_[e_] = lats[j + l]; + e_++; } } - if (iter->e != iter->nv) { + if (e_ != nv_) { /* Fewer counted points in the sub-area than the number of data values */ const size_t legacy_count = count_subarea_points(h, grib_get_reduced_row_legacy, pl, plsize, lon_first, lon_last); - if (iter->nv == legacy_count) { + if (nv_ == legacy_count) { /* Legacy (produced by PRODGEN/LIBEMOS) */ - return iterate_reduced_gaussian_subarea_legacy(iter, h, lat_first, lon_first, lat_last, lon_last, lats, pl, plsize); + return iterate_reduced_gaussian_subarea_legacy(h, lat_first, lon_first, lat_last, lon_last, lats, pl, plsize); } else { /* TODO: A gap exists! Not all values can be mapped. Inconsistent grid or error in calculating num. points! */ @@ -300,8 +220,10 @@ static int iterate_reduced_gaussian_subarea(grib_iterator* iter, grib_handle* h, return err; } -static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) +int grib_iterator_gaussian_reduced_t::init(grib_handle* h, grib_arguments* args) { + grib_iterator_gen_t::init(h, args); + int ret = GRIB_SUCCESS, j, is_global = 0; double lat_first = 0, lon_first = 0, lat_last = 0, lon_last = 0; double angular_precision = 1.0 / 1000000.0; @@ -314,28 +236,27 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) long row_count = 0; long angleSubdivisions = 0; const grib_context* c = h->context; - grib_iterator_gaussian_reduced* self = (grib_iterator_gaussian_reduced*)iter; - const char* slat_first = grib_arguments_get_name(h, args, self->carg++); - const char* slon_first = grib_arguments_get_name(h, args, self->carg++); - const char* slat_last = grib_arguments_get_name(h, args, self->carg++); - const char* slon_last = grib_arguments_get_name(h, args, self->carg++); - const char* sorder = grib_arguments_get_name(h, args, self->carg++); - const char* spl = grib_arguments_get_name(h, args, self->carg++); - const char* snj = grib_arguments_get_name(h, args, self->carg++); + const char* slat_first = grib_arguments_get_name(h, args, carg_++); + const char* slon_first = grib_arguments_get_name(h, args, carg_++); + const char* slat_last = grib_arguments_get_name(h, args, carg_++); + const char* slon_last = grib_arguments_get_name(h, args, carg_++); + const char* sorder = grib_arguments_get_name(h, args, carg_++); + const char* spl = grib_arguments_get_name(h, args, carg_++); + const char* snj = grib_arguments_get_name(h, args, carg_++); - self->angleOfRotation = 0; - self->isRotated = 0; - self->southPoleLat = 0; - self->southPoleLon = 0; - self->disableUnrotate = 0; /* unrotate enabled by default */ + angleOfRotation_ = 0; + isRotated_ = 0; + southPoleLat_ = 0; + southPoleLon_ = 0; + disableUnrotate_ = 0; /* unrotate enabled by default */ - ret = grib_get_long(h, "isRotatedGrid", &self->isRotated); - if (ret == GRIB_SUCCESS && self->isRotated) { - if ((ret = grib_get_double_internal(h, "angleOfRotation", &self->angleOfRotation))) + ret = grib_get_long(h, "isRotatedGrid", &isRotated_); + if (ret == GRIB_SUCCESS && isRotated_) { + if ((ret = grib_get_double_internal(h, "angleOfRotation", &angleOfRotation_))) return ret; - if ((ret = grib_get_double_internal(h, "latitudeOfSouthernPoleInDegrees", &self->southPoleLat))) + if ((ret = grib_get_double_internal(h, "latitudeOfSouthernPoleInDegrees", &southPoleLat_))) return ret; - if ((ret = grib_get_double_internal(h, "longitudeOfSouthernPoleInDegrees", &self->southPoleLon))) + if ((ret = grib_get_double_internal(h, "longitudeOfSouthernPoleInDegrees", &southPoleLon_))) return ret; } @@ -379,11 +300,11 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) grib_get_long_array_internal(h, spl, pl, &plsize); - self->las = (double*)grib_context_malloc(h->context, iter->nv * sizeof(double)); - if (!self->las) + las_ = (double*)grib_context_malloc(h->context, nv_ * sizeof(double)); + if (!las_) return GRIB_OUT_OF_MEMORY; - self->los = (double*)grib_context_malloc(h->context, iter->nv * sizeof(double)); - if (!self->los) + los_ = (double*)grib_context_malloc(h->context, nv_ * sizeof(double)); + if (!los_) return GRIB_OUT_OF_MEMORY; while (lon_last < 0) @@ -402,11 +323,11 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) is_global = is_gaussian_global(lat_first, lat_last, lon_first, lon_last, max_pl, lats, angular_precision); if (!is_global) { /*sub area*/ - ret = iterate_reduced_gaussian_subarea(iter, h, lat_first, lon_first, lat_last, lon_last, lats, pl, plsize, numlats); + ret = iterate_reduced_gaussian_subarea(h, lat_first, lon_first, lat_last, lon_last, lats, pl, plsize, numlats); } else { /*global*/ - iter->e = 0; + e_ = 0; if (h->context->debug) { const size_t np = count_global_points(pl, plsize); fprintf(stderr, "ECCODES DEBUG grib_iterator_class_gaussian_reduced: global num points=%zu\n", np); @@ -415,37 +336,38 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) for (j = 0; j < plsize; j++) { row_count = pl[j]; for (i = 0; i < row_count; i++) { - if (iter->e >= iter->nv) { + if (e_ >= nv_) { /*grib_context_log(h->context,GRIB_LOG_ERROR, "Failed to initialise reduced Gaussian iterator (global)");*/ /*return GRIB_WRONG_GRID;*/ /*Try now as NON-global*/ - ret = iterate_reduced_gaussian_subarea(iter, h, lat_first, lon_first, lat_last, lon_last, lats, pl, plsize, numlats); + ret = iterate_reduced_gaussian_subarea(h, lat_first, lon_first, lat_last, lon_last, lats, pl, plsize, numlats); if (ret != GRIB_SUCCESS) grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Failed to initialise iterator (global)", ITER); goto finalise; } - self->los[iter->e] = (i * 360.0) / row_count; - self->las[iter->e] = lats[j]; - iter->e++; + los_[e_] = (i * 360.0) / row_count; + las_[e_] = lats[j]; + e_++; } } } finalise: - iter->e = -1; + e_ = -1; grib_context_free(h->context, lats); grib_context_free(h->context, pl); return ret; } -static int destroy(grib_iterator* iter) +int grib_iterator_gaussian_reduced_t::destroy() { - grib_iterator_gaussian_reduced* self = (grib_iterator_gaussian_reduced*)iter; - const grib_context* c = iter->h->context; + const grib_context* c = h_->context; - grib_context_free(c, self->las); - grib_context_free(c, self->los); + grib_context_free(c, las_); + grib_context_free(c, los_); + + grib_iterator_gen_t::destroy(); return GRIB_SUCCESS; } diff --git a/src/iterator/grib_iterator_class_gaussian_reduced.h b/src/iterator/grib_iterator_class_gaussian_reduced.h new file mode 100644 index 000000000..3580be66f --- /dev/null +++ b/src/iterator/grib_iterator_class_gaussian_reduced.h @@ -0,0 +1,46 @@ +/* + * (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. + */ + +#pragma once + +#include "grib_iterator_class_gen.h" + +class grib_iterator_gaussian_reduced_t : public grib_iterator_gen_t +{ +public: + grib_iterator_gaussian_reduced_t() : grib_iterator_gen_t() + { + class_name_ = "gaussian_reduced"; + } + + int init(grib_handle*,grib_arguments*) override; + int next(double *, double *, double *) override; + int destroy() override; + +private: + double *las_; + double *los_; + long Nj_; + long isRotated_; + double angleOfRotation_; + double southPoleLat_; + double southPoleLon_; + long disableUnrotate_; + +int iterate_reduced_gaussian_subarea_legacy(grib_handle*, + double, double, + double, double, + double*, long*, size_t); + +int iterate_reduced_gaussian_subarea(grib_handle*, + double, double, + double, double, + double*, long*, size_t, size_t); +}; diff --git a/src/grib_iterator_class_gen.cc b/src/iterator/grib_iterator_class_gen.cc similarity index 66% rename from src/grib_iterator_class_gen.cc rename to src/iterator/grib_iterator_class_gen.cc index 7a8ad1f7b..1c0ae0eeb 100644 --- a/src/grib_iterator_class_gen.cc +++ b/src/iterator/grib_iterator_class_gen.cc @@ -8,71 +8,11 @@ * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. */ -#include "grib_api_internal.h" +#include "grib_iterator_class_gen.h" -/* - This is used by make_class.pl +//grib_iterator_gen_t _grib_iterator_class_gen{}; +//grib_iterator* grib_iterator_class_gen = &_grib_iterator_class_gen; - START_CLASS_DEF - CLASS = iterator - IMPLEMENTS = destroy - IMPLEMENTS = has_next - IMPLEMENTS = init - IMPLEMENTS = reset - MEMBERS = int carg - MEMBERS = const char* missingValue; - 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 "iterator.class" and rerun ./make_class.pl - -*/ - - -static void init_class (grib_iterator_class*); - -static int init (grib_iterator* i,grib_handle*,grib_arguments*); -static int destroy (grib_iterator* i); -static int reset (grib_iterator* i); -static long has_next (grib_iterator* i); - - -typedef struct grib_iterator_gen{ - grib_iterator it; - /* Members defined in gen */ - int carg; - const char* missingValue; -} grib_iterator_gen; - - -static grib_iterator_class _grib_iterator_class_gen = { - 0, /* super */ - "gen", /* name */ - sizeof(grib_iterator_gen),/* size of instance */ - 0, /* inited */ - &init_class, /* init_class */ - &init, /* constructor */ - &destroy, /* destructor */ - 0, /* Next Value */ - 0, /* Previous Value */ - &reset, /* Reset the counter */ - &has_next, /* has next values */ -}; - -grib_iterator_class* grib_iterator_class_gen = &_grib_iterator_class_gen; - - -static void init_class(grib_iterator_class* c) -{ -} -/* END_CLASS_IMP */ /* * Return pointer to data at (i,j) (Fortran convention) @@ -170,23 +110,24 @@ int transform_iterator_data(grib_context* context, double* data, return GRIB_SUCCESS; } -static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) +int grib_iterator_gen_t::init(grib_handle* h, grib_arguments* args) { - grib_iterator_gen* self = (grib_iterator_gen*)iter; + grib_iterator::init(h, args); + size_t dli = 0; int err = GRIB_SUCCESS; const char* s_rawData = NULL; const char* s_numPoints = NULL; long numberOfPoints = 0; - self->carg = 1; + carg_ = 1; - s_numPoints = grib_arguments_get_name(h, args, self->carg++); - self->missingValue = grib_arguments_get_name(h, args, self->carg++); - s_rawData = grib_arguments_get_name(h, args, self->carg++); + s_numPoints = grib_arguments_get_name(h, args, carg_++); + missingValue_ = grib_arguments_get_name(h, args, carg_++); + s_rawData = grib_arguments_get_name(h, args, carg_++); - iter->data = NULL; - iter->h = h; /* We may not need to keep them */ - iter->args = args; + data_ = NULL; + h = h; /* We may not need to keep them */ + args = args; if ((err = grib_get_size(h, s_rawData, &dli)) != GRIB_SUCCESS) return err; @@ -195,9 +136,9 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) // See ECC-1792. If we don't want to decode the Data Section, we should not do a check // to see if it is consistent with the Grid Section - if (iter->flags & GRIB_GEOITERATOR_NO_VALUES) { + if (flags_ & GRIB_GEOITERATOR_NO_VALUES) { // Iterator's number of values taken from the Grid Section - iter->nv = numberOfPoints; + nv_ = numberOfPoints; } else { // Check for consistency between the Grid and Data Sections if (numberOfPoints != dli) { @@ -205,47 +146,59 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) s_numPoints, s_rawData, numberOfPoints, dli); return GRIB_WRONG_GRID; } - iter->nv = dli; + nv_ = dli; } - if (iter->nv == 0) { + if (nv_ == 0) { grib_context_log(h->context, GRIB_LOG_ERROR, "Geoiterator: size(%s) is %ld", s_rawData, dli); return GRIB_WRONG_GRID; } - if ((iter->flags & GRIB_GEOITERATOR_NO_VALUES) == 0) { + if ((flags_ & GRIB_GEOITERATOR_NO_VALUES) == 0) { // ECC-1525 // When the NO_VALUES flag is unset, decode the values and store them in the iterator. // By default (and legacy) flags==0, so we decode - iter->data = (double*)grib_context_malloc(h->context, (iter->nv) * sizeof(double)); + data_ = (double*)grib_context_malloc(h->context, (nv_) * sizeof(double)); - if ((err = grib_get_double_array_internal(h, s_rawData, iter->data, &(iter->nv)))) { + if ((err = grib_get_double_array_internal(h, s_rawData, data_, &(nv_)))) { return err; } } - iter->e = -1; + e_ = -1; return err; } -static int reset(grib_iterator* iter) +int grib_iterator_gen_t::reset() { - iter->e = -1; + e_ = -1; return 0; } -static int destroy(grib_iterator* iter) +int grib_iterator_gen_t::destroy() { - const grib_context* c = iter->h->context; - grib_context_free(c, iter->data); + const grib_context* c = h_->context; + grib_context_free(c, data_); + + grib_iterator::destroy(); return GRIB_SUCCESS; } -static long has_next(grib_iterator* iter) +long grib_iterator_gen_t::has_next() { - if (iter->flags == 0 && iter->data == NULL) + if (flags_ == 0 && data_ == NULL) return 0; - if (iter->e >= (long)(iter->nv - 1)) + if (e_ >= (long)(nv_ - 1)) return 0; return 1; } + + +int grib_iterator_gen_t::previous(double*, double*, double*) { + return GRIB_NOT_IMPLEMENTED; +} + +int grib_iterator_gen_t::next(double*, double*, double*) { + return GRIB_NOT_IMPLEMENTED; +} + diff --git a/src/iterator/grib_iterator_class_gen.h b/src/iterator/grib_iterator_class_gen.h new file mode 100644 index 000000000..6dc9c9fb3 --- /dev/null +++ b/src/iterator/grib_iterator_class_gen.h @@ -0,0 +1,33 @@ +/* + * (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. + */ + +#pragma once + +#include "grib_iterator.h" + +class grib_iterator_gen_t : public grib_iterator +{ +public: + grib_iterator_gen_t() : grib_iterator() + { + class_name_ = "abstract_long_vector"; + } + + int init(grib_handle*,grib_arguments*) override; + int next(double*, double*, double*) override; + int previous(double*, double*, double*) override; + int reset() override; + int destroy() override; + long has_next() override; + +public: + int carg_; + const char* missingValue_; +}; diff --git a/src/grib_iterator_class_healpix.cc b/src/iterator/grib_iterator_class_healpix.cc similarity index 71% rename from src/grib_iterator_class_healpix.cc rename to src/iterator/grib_iterator_class_healpix.cc index 6a545b6c5..abfe3695f 100644 --- a/src/grib_iterator_class_healpix.cc +++ b/src/iterator/grib_iterator_class_healpix.cc @@ -8,7 +8,7 @@ * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. */ -#include "grib_api_internal.h" +#include "grib_iterator_class_healpix.h" #include #include @@ -17,77 +17,8 @@ #include #include -/* - This is used by make_class.pl - - START_CLASS_DEF - CLASS = iterator - SUPER = grib_iterator_class_gen - IMPLEMENTS = destroy - IMPLEMENTS = init;next - MEMBERS = double *lats - MEMBERS = double *lons - MEMBERS = long Nsides - MEMBERS = bool nested - 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 "iterator.class" and rerun ./make_class.pl - -*/ - - -static void init_class (grib_iterator_class*); - -static int init (grib_iterator* i,grib_handle*,grib_arguments*); -static int next (grib_iterator* i, double *lat, double *lon, double *val); -static int destroy (grib_iterator* i); - - -typedef struct grib_iterator_healpix{ - grib_iterator it; - /* Members defined in gen */ - int carg; - const char* missingValue; - /* Members defined in healpix */ - double *lats; - double *lons; - long Nsides; - bool nested; -} grib_iterator_healpix; - -extern grib_iterator_class* grib_iterator_class_gen; - -static grib_iterator_class _grib_iterator_class_healpix = { - &grib_iterator_class_gen, /* super */ - "healpix", /* name */ - sizeof(grib_iterator_healpix),/* size of instance */ - 0, /* inited */ - &init_class, /* init_class */ - &init, /* constructor */ - &destroy, /* destructor */ - &next, /* Next Value */ - 0, /* Previous Value */ - 0, /* Reset the counter */ - 0, /* has next values */ -}; - -grib_iterator_class* grib_iterator_class_healpix = &_grib_iterator_class_healpix; - - -static void init_class(grib_iterator_class* c) -{ - c->previous = (*(c->super))->previous; - c->reset = (*(c->super))->reset; - c->has_next = (*(c->super))->has_next; -} -/* END_CLASS_IMP */ +grib_iterator_healpix_t _grib_iterator_healpix; +grib_iterator* grib_iterator_healpix = &_grib_iterator_healpix; #define ITER "HEALPix Geoiterator" constexpr double RAD2DEG = 57.29577951308232087684; // 180 over pi @@ -211,7 +142,9 @@ static std::vector HEALPix_longitudes(size_t N, size_t i) return longitudes; } -static int iterate_healpix(grib_iterator_healpix* self, long N) +} // anonymous namespace + +int grib_iterator_healpix_t::iterate_healpix(long N) { size_t Ny = 4 * static_cast(N) - 1; auto Nd = static_cast(N); @@ -241,7 +174,7 @@ static int iterate_healpix(grib_iterator_healpix* self, long N) // Equator latitudes[2 * N - 1] = 0.0; - if (self->nested) { + if (nested_) { if (!is_power_of_2(N)) { grib_context* c = grib_context_get_default(); grib_context_log(c, GRIB_LOG_ERROR, "%s: For nested ordering, Nside must be a power of 2", ITER); @@ -313,8 +246,8 @@ static int iterate_healpix(grib_iterator_healpix* self, long N) // Compute the longitudes at a given latitude for (double longitude : HEALPix_longitudes(N, i)) { Assert( ring_to_nest.at(j) < Npix ); - self->lons[ring_to_nest.at(j)] = longitude; - self->lats[ring_to_nest.at(j)] = latitudes[i]; + lons_[ring_to_nest.at(j)] = longitude; + lats_[ring_to_nest.at(j)] = latitudes[i]; ++j; } } @@ -322,8 +255,8 @@ static int iterate_healpix(grib_iterator_healpix* self, long N) for (size_t i = 0, j = 0; i < Ny; i++) { // Compute the longitudes at a given latitude for (double longitude : HEALPix_longitudes(N, i)) { - self->lons[j] = longitude; - self->lats[j] = latitudes[i]; + lons_[j] = longitude; + lats_[j] = latitudes[i]; ++j; } } @@ -332,15 +265,14 @@ static int iterate_healpix(grib_iterator_healpix* self, long N) return GRIB_SUCCESS; } -} // anonymous namespace - -static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) +int grib_iterator_healpix_t::init(grib_handle* h, grib_arguments* args) { - int err = 0; - grib_iterator_healpix* self = (grib_iterator_healpix*)iter; + grib_iterator_gen_t::init(h, args); - const char* snside = grib_arguments_get_name(h, args, self->carg++); - const char* sorder = grib_arguments_get_name(h, args, self->carg++); + int err = 0; + + const char* snside = grib_arguments_get_name(h, args, carg_++); + const char* sorder = grib_arguments_get_name(h, args, carg_++); long N = 0; if ((err = grib_get_long_internal(h, snside, &N)) != GRIB_SUCCESS) { @@ -357,12 +289,12 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) return err; } - self->nested = STR_EQUAL(ordering, "nested"); - if (!STR_EQUAL(ordering, "ring") && !self->nested) { + nested_ = STR_EQUAL(ordering, "nested"); + if (!STR_EQUAL(ordering, "ring") && !nested_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Only orderingConvention=(ring|nested) are supported", ITER); return GRIB_GEOCALCULUS_PROBLEM; } - // if (self->nested && N == 1) { + // if (nested && N == 1) { // grib_context_log(h->context, GRIB_LOG_ERROR, "%s: For orderingConvention=nested, N must be greater than 1", ITER); // return GRIB_GEOCALCULUS_PROBLEM; // } @@ -372,58 +304,55 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) return GRIB_WRONG_GRID; } - if (iter->nv != 12 * N * N) { + if (nv_ != 12 * N * N) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Wrong number of points (%zu!=12x%ldx%ld)", - ITER, iter->nv, N, N); + ITER, nv_, N, N); return GRIB_WRONG_GRID; } - self->lats = (double*)grib_context_malloc(h->context, iter->nv * sizeof(double)); - if (self->lats == nullptr) { + lats_ = (double*)grib_context_malloc(h->context, nv_ * sizeof(double)); + if (lats_ == nullptr) { return GRIB_OUT_OF_MEMORY; } - self->lons = (double*)grib_context_malloc(h->context, iter->nv * sizeof(double)); - if (self->lons == nullptr) { + lons_ = (double*)grib_context_malloc(h->context, nv_ * sizeof(double)); + if (lons_ == nullptr) { return GRIB_OUT_OF_MEMORY; } try { - err = iterate_healpix(self, N); + err = iterate_healpix(N); } catch (...) { return GRIB_INTERNAL_ERROR; } - iter->e = -1; + e_ = -1; return err; } -static int next(grib_iterator* iter, double* lat, double* lon, double* val) +int grib_iterator_healpix_t::next(double* lat, double* lon, double* val) { - auto* self = (grib_iterator_healpix*)iter; - - if (iter->e >= static_cast(iter->nv - 1)) { + if (e_ >= static_cast(nv_ - 1)) { return 0; } - iter->e++; + e_++; - *lat = self->lats[iter->e]; - *lon = self->lons[iter->e]; - if (val != nullptr && iter->data != nullptr) { - *val = iter->data[iter->e]; + *lat = lats_[e_]; + *lon = lons_[e_]; + if (val != nullptr && data_ != nullptr) { + *val = data_[e_]; } return 1; } -static int destroy(grib_iterator* iter) +int grib_iterator_healpix_t::destroy() { - auto* self = (grib_iterator_healpix*)iter; - const auto* c = iter->h->context; + grib_context_free(context_, lats_); + grib_context_free(context_, lons_); - grib_context_free(c, self->lats); - grib_context_free(c, self->lons); + grib_iterator_gen_t::destroy(); return GRIB_SUCCESS; } diff --git a/src/iterator/grib_iterator_class_healpix.h b/src/iterator/grib_iterator_class_healpix.h new file mode 100644 index 000000000..b179b27d7 --- /dev/null +++ b/src/iterator/grib_iterator_class_healpix.h @@ -0,0 +1,35 @@ +/* + * (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. + */ + +#pragma once + +#include "grib_iterator_class_gen.h" + +class grib_iterator_healpix_t : public grib_iterator_gen_t +{ +public: + grib_iterator_healpix_t() : grib_iterator_gen_t() + { + class_name_ = "healpix"; + } + + int init(grib_handle*, grib_arguments*) override; + int next(double *, double *, double *) override; + int destroy() override; + +public: + double *lats_; + double *lons_; + long Nsides_; + bool nested_; + +private: + int iterate_healpix(long N); +}; diff --git a/src/grib_iterator_class_lambert_azimuthal_equal_area.cc b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc similarity index 77% rename from src/grib_iterator_class_lambert_azimuthal_equal_area.cc rename to src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc index 24dba1877..daf28758c 100644 --- a/src/grib_iterator_class_lambert_azimuthal_equal_area.cc +++ b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc @@ -8,93 +8,26 @@ * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. */ -#include "grib_api_internal.h" +#include "grib_iterator_class_lambert_azimuthal_equal_area.h" #include -/* - This is used by make_class.pl - START_CLASS_DEF - CLASS = iterator - SUPER = grib_iterator_class_gen - IMPLEMENTS = destroy - IMPLEMENTS = init;next - MEMBERS = double *lats - MEMBERS = double *lons - MEMBERS = long Nj - END_CLASS_DEF -*/ +grib_iterator_lambert_azimuthal_equal_area_t _grib_iterator_lambert_azimuthal_equal_area{}; +grib_iterator* grib_iterator_lambert_azimuthal_equal_area = &_grib_iterator_lambert_azimuthal_equal_area; -/* 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 "iterator.class" and rerun ./make_class.pl - -*/ - - -static void init_class (grib_iterator_class*); - -static int init (grib_iterator* i,grib_handle*,grib_arguments*); -static int next (grib_iterator* i, double *lat, double *lon, double *val); -static int destroy (grib_iterator* i); - - -typedef struct grib_iterator_lambert_azimuthal_equal_area{ - grib_iterator it; - /* Members defined in gen */ - int carg; - const char* missingValue; - /* Members defined in lambert_azimuthal_equal_area */ - double *lats; - double *lons; - long Nj; -} grib_iterator_lambert_azimuthal_equal_area; - -extern grib_iterator_class* grib_iterator_class_gen; - -static grib_iterator_class _grib_iterator_class_lambert_azimuthal_equal_area = { - &grib_iterator_class_gen, /* super */ - "lambert_azimuthal_equal_area", /* name */ - sizeof(grib_iterator_lambert_azimuthal_equal_area),/* size of instance */ - 0, /* inited */ - &init_class, /* init_class */ - &init, /* constructor */ - &destroy, /* destructor */ - &next, /* Next Value */ - 0, /* Previous Value */ - 0, /* Reset the counter */ - 0, /* has next values */ -}; - -grib_iterator_class* grib_iterator_class_lambert_azimuthal_equal_area = &_grib_iterator_class_lambert_azimuthal_equal_area; - - -static void init_class(grib_iterator_class* c) -{ - c->previous = (*(c->super))->previous; - c->reset = (*(c->super))->reset; - c->has_next = (*(c->super))->has_next; -} -/* END_CLASS_IMP */ #define ITER "Lambert azimuthal equal area Geoiterator" -static int next(grib_iterator* iter, double* lat, double* lon, double* val) +int grib_iterator_lambert_azimuthal_equal_area_t::next(double* lat, double* lon, double* val) { - grib_iterator_lambert_azimuthal_equal_area* self = (grib_iterator_lambert_azimuthal_equal_area*)iter; - - if ((long)iter->e >= (long)(iter->nv - 1)) + if ((long)e_ >= (long)(nv_ - 1)) return 0; - iter->e++; + e_++; - *lat = self->lats[iter->e]; - *lon = self->lons[iter->e]; - if (val && iter->data) { - *val = iter->data[iter->e]; + *lat = lats_[e_]; + *lon = lons_[e_]; + if (val && data_) { + *val = data_[e_]; } return 1; } @@ -161,7 +94,7 @@ static double pj_qsfn(double sinphi, double e, double one_es) #define EPS10 1.e-10 static int init_oblate(grib_handle* h, - grib_iterator_lambert_azimuthal_equal_area* self, + grib_iterator_lambert_azimuthal_equal_area_t* self, size_t nv, long nx, long ny, double Dx, double Dy, double earthMinorAxisInMetres, double earthMajorAxisInMetres, double latFirstInRadians, double lonFirstInRadians, @@ -231,18 +164,18 @@ static int init_oblate(grib_handle* h, x0 = Q__xmf * b * cosb * sinlam; /* Allocate latitude and longitude arrays */ - self->lats = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!self->lats) { + self->lats_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); + if (!self->lats_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } - self->lons = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!self->lats) { + self->lons_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); + if (!self->lats_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } - lats = self->lats; - lons = self->lons; + lats = self->lats_; + lons = self->lons_; /* Populate the lat and lon arrays */ { @@ -292,7 +225,7 @@ static int init_oblate(grib_handle* h, } static int init_sphere(grib_handle* h, - grib_iterator_lambert_azimuthal_equal_area* self, + grib_iterator_lambert_azimuthal_equal_area_t* self, size_t nv, long nx, long ny, double Dx, double Dy, double radius, double latFirstInRadians, double lonFirstInRadians, @@ -317,18 +250,18 @@ static int init_sphere(grib_handle* h, Dx = iScansNegatively == 0 ? Dx / 1000 : -Dx / 1000; Dy = jScansPositively == 1 ? Dy / 1000 : -Dy / 1000; - self->lats = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!self->lats) { + self->lats_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); + if (!self->lats_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } - self->lons = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!self->lats) { + self->lons_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); + if (!self->lats_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } - lats = self->lats; - lons = self->lons; + lats = self->lats_; + lons = self->lons_; /* compute xFirst,yFirst in metres */ sinphi = sin(latFirstInRadians); @@ -398,8 +331,10 @@ static int init_sphere(grib_handle* h, return GRIB_SUCCESS; } -static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) +int grib_iterator_lambert_azimuthal_equal_area_t::init(grib_handle* h, grib_arguments* args) { + grib_iterator_gen_t::init(h, args); + int err = 0; int is_oblate = 0; double lonFirstInDegrees, latFirstInDegrees, lonFirstInRadians, latFirstInRadians, radius = 0; @@ -411,22 +346,19 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) long jScansPositively, jPointsAreConsecutive; double earthMajorAxisInMetres = 0, earthMinorAxisInMetres = 0; - grib_iterator_lambert_azimuthal_equal_area* self = - (grib_iterator_lambert_azimuthal_equal_area*)iter; - - const char* sradius = grib_arguments_get_name(h, args, self->carg++); - const char* snx = grib_arguments_get_name(h, args, self->carg++); - const char* sny = grib_arguments_get_name(h, args, self->carg++); - const char* slatFirstInDegrees = grib_arguments_get_name(h, args, self->carg++); - const char* slonFirstInDegrees = grib_arguments_get_name(h, args, self->carg++); - const char* sstandardParallel = grib_arguments_get_name(h, args, self->carg++); - const char* scentralLongitude = grib_arguments_get_name(h, args, self->carg++); - const char* sDx = grib_arguments_get_name(h, args, self->carg++); - const char* sDy = grib_arguments_get_name(h, args, self->carg++); - const char* siScansNegatively = grib_arguments_get_name(h, args, self->carg++); - const char* sjScansPositively = grib_arguments_get_name(h, args, self->carg++); - const char* sjPointsAreConsecutive = grib_arguments_get_name(h, args, self->carg++); - const char* salternativeRowScanning = grib_arguments_get_name(h, args, self->carg++); + const char* sradius = grib_arguments_get_name(h, args, carg_++); + const char* snx = grib_arguments_get_name(h, args, carg_++); + const char* sny = grib_arguments_get_name(h, args, carg_++); + const char* slatFirstInDegrees = grib_arguments_get_name(h, args, carg_++); + const char* slonFirstInDegrees = grib_arguments_get_name(h, args, carg_++); + const char* sstandardParallel = grib_arguments_get_name(h, args, carg_++); + const char* scentralLongitude = grib_arguments_get_name(h, args, carg_++); + const char* sDx = grib_arguments_get_name(h, args, carg_++); + const char* sDy = grib_arguments_get_name(h, args, carg_++); + const char* siScansNegatively = grib_arguments_get_name(h, args, carg_++); + const char* sjScansPositively = grib_arguments_get_name(h, args, carg_++); + const char* sjPointsAreConsecutive = grib_arguments_get_name(h, args, carg_++); + const char* salternativeRowScanning = grib_arguments_get_name(h, args, carg_++); const double d2r = acos(0.0) / 90.0; is_oblate = grib_is_earth_oblate(h); @@ -443,8 +375,8 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) if ((err = grib_get_long_internal(h, sny, &ny)) != GRIB_SUCCESS) return err; - if (iter->nv != nx * ny) { - grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Wrong number of points (%zu!=%ldx%ld)", ITER, iter->nv, nx, ny); + if (nv_ != nx * ny) { + grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Wrong number of points (%zu!=%ldx%ld)", ITER, nv_, nx, ny); return GRIB_WRONG_GRID; } if ((err = grib_get_double_internal(h, slatFirstInDegrees, &latFirstInDegrees)) != GRIB_SUCCESS) @@ -474,14 +406,14 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) standardParallelInRadians = standardParallelInDegrees * d2r; if (is_oblate) { - err = init_oblate(h, self, iter->nv, nx, ny, + err = init_oblate(h, this, nv_, nx, ny, Dx, Dy, earthMinorAxisInMetres, earthMajorAxisInMetres, latFirstInRadians, lonFirstInRadians, centralLongitudeInRadians, standardParallelInRadians, iScansNegatively, jScansPositively, jPointsAreConsecutive); } else { - err = init_sphere(h, self, iter->nv, nx, ny, + err = init_sphere(h, this, nv_, nx, ny, Dx, Dy, radius, latFirstInRadians, lonFirstInRadians, centralLongitudeInRadians, standardParallelInRadians, @@ -489,17 +421,18 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) } if (err) return err; - iter->e = -1; + e_ = -1; return GRIB_SUCCESS; } -static int destroy(grib_iterator* iter) +int grib_iterator_lambert_azimuthal_equal_area_t::destroy() { - grib_iterator_lambert_azimuthal_equal_area* self = (grib_iterator_lambert_azimuthal_equal_area*)iter; - const grib_context* c = iter->h->context; + const grib_context* c = h_->context; - grib_context_free(c, self->lats); - grib_context_free(c, self->lons); + grib_context_free(c, lats_); + grib_context_free(c, lons_); + + grib_iterator_gen_t::destroy(); return GRIB_SUCCESS; } diff --git a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h new file mode 100644 index 000000000..06e866dc5 --- /dev/null +++ b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h @@ -0,0 +1,32 @@ +/* + * (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. + */ + +#pragma once + +#include "grib_iterator_class_gen.h" + +class grib_iterator_lambert_azimuthal_equal_area_t : public grib_iterator_gen_t +{ +public: + grib_iterator_lambert_azimuthal_equal_area_t() : grib_iterator_gen_t() + { + class_name_ = "lambert_azimuthal_equal_area"; + } + + int init(grib_handle*, grib_arguments*) override; + int next(double*, double*, double*) override; + int previous(double*, double*, double*) override; + int destroy() override; + +public: + double *lats_; + double *lons_; + long Nj_; +}; diff --git a/src/grib_iterator_class_lambert_conformal.cc b/src/iterator/grib_iterator_class_lambert_conformal.cc similarity index 78% rename from src/grib_iterator_class_lambert_conformal.cc rename to src/iterator/grib_iterator_class_lambert_conformal.cc index fa002c18b..6b44a3d98 100644 --- a/src/grib_iterator_class_lambert_conformal.cc +++ b/src/iterator/grib_iterator_class_lambert_conformal.cc @@ -8,78 +8,11 @@ * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. */ -#include "grib_api_internal.h" +#include "grib_iterator_class_lambert_conformal.h" #include -/* - This is used by make_class.pl - - START_CLASS_DEF - CLASS = iterator - SUPER = grib_iterator_class_gen - IMPLEMENTS = destroy - IMPLEMENTS = init;next - MEMBERS = double *lats - MEMBERS = double *lons - MEMBERS = long 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 "iterator.class" and rerun ./make_class.pl - -*/ - - -static void init_class (grib_iterator_class*); - -static int init (grib_iterator* i,grib_handle*,grib_arguments*); -static int next (grib_iterator* i, double *lat, double *lon, double *val); -static int destroy (grib_iterator* i); - - -typedef struct grib_iterator_lambert_conformal{ - grib_iterator it; - /* Members defined in gen */ - int carg; - const char* missingValue; - /* Members defined in lambert_conformal */ - double *lats; - double *lons; - long Nj; -} grib_iterator_lambert_conformal; - -extern grib_iterator_class* grib_iterator_class_gen; - -static grib_iterator_class _grib_iterator_class_lambert_conformal = { - &grib_iterator_class_gen, /* super */ - "lambert_conformal", /* name */ - sizeof(grib_iterator_lambert_conformal),/* size of instance */ - 0, /* inited */ - &init_class, /* init_class */ - &init, /* constructor */ - &destroy, /* destructor */ - &next, /* Next Value */ - 0, /* Previous Value */ - 0, /* Reset the counter */ - 0, /* has next values */ -}; - -grib_iterator_class* grib_iterator_class_lambert_conformal = &_grib_iterator_class_lambert_conformal; - - -static void init_class(grib_iterator_class* c) -{ - c->previous = (*(c->super))->previous; - c->reset = (*(c->super))->reset; - c->has_next = (*(c->super))->has_next; -} -/* END_CLASS_IMP */ +grib_iterator_lambert_conformal_t _grib_iterator_lambert_conformal{}; +grib_iterator* grib_iterator_lambert_conformal = &_grib_iterator_lambert_conformal; #define ITER "Lambert conformal Geoiterator" #define EPSILON 1.0e-10 @@ -188,8 +121,7 @@ static void xy2lonlat(double radius, double n, double f, double rho0_bare, doubl } } -static int init_sphere(const grib_handle* h, - grib_iterator_lambert_conformal* self, +int grib_iterator_lambert_conformal_t::init_sphere(const grib_handle* h, size_t nv, long nx, long ny, double LoVInDegrees, double Dx, double Dy, double radius, @@ -225,13 +157,13 @@ static int init_sphere(const grib_handle* h, // Dy = jScansPositively == 1 ? Dy : -Dy; // Allocate latitude and longitude arrays - self->lats = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!self->lats) { + lats_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); + if (!lats_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } - self->lons = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!self->lats) { + lons_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); + if (!lats_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } @@ -244,8 +176,8 @@ static int init_sphere(const grib_handle* h, x = x0 + i * Dx; xy2lonlat(radius, n, f, rho0_bare, LoVInRadians, x, y, &lonDeg, &latDeg); lonDeg = normalise_longitude_in_degrees(lonDeg); - self->lons[index] = lonDeg; - self->lats[index] = latDeg; + lons_[index] = lonDeg; + lats_[index] = latDeg; } } @@ -278,8 +210,7 @@ static int init_sphere(const grib_handle* h, } // Oblate spheroid -static int init_oblate(const grib_handle* h, - grib_iterator_lambert_conformal* self, +int grib_iterator_lambert_conformal_t::init_oblate(const grib_handle* h, size_t nv, long nx, long ny, double LoVInDegrees, double Dx, double Dy, @@ -349,13 +280,13 @@ static int init_oblate(const grib_handle* h, y0 = -y0; // Allocate latitude and longitude arrays - self->lats = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!self->lats) { + lats_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); + if (!lats_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } - self->lons = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!self->lats) { + lons_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); + if (!lats_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } @@ -388,8 +319,8 @@ static int init_oblate(const grib_handle* h, if (err) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Failed to compute the latitude angle, phi2, for the inverse", ITER); - grib_context_free(h->context, self->lats); - grib_context_free(h->context, self->lons); + grib_context_free(h->context, lats_); + grib_context_free(h->context, lons_); return err; } } else { @@ -401,15 +332,17 @@ static int init_oblate(const grib_handle* h, } latDeg = latRad * RAD2DEG; // Convert to degrees lonDeg = normalise_longitude_in_degrees(lonRad * RAD2DEG); - self->lons[index] = lonDeg; - self->lats[index] = latDeg; + lons_[index] = lonDeg; + lats_[index] = latDeg; } } return GRIB_SUCCESS; } -static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) +int grib_iterator_lambert_conformal_t::init(grib_handle* h, grib_arguments* args) { + grib_iterator_gen_t::init(h, args); + int err = 0, is_oblate = 0; long nx, ny, iScansNegatively, jScansPositively, jPointsAreConsecutive, alternativeRowScanning; double LoVInDegrees, LaDInDegrees, Latin1InDegrees, Latin2InDegrees, latFirstInDegrees, @@ -418,24 +351,22 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) LaDInRadians; double earthMajorAxisInMetres=0, earthMinorAxisInMetres=0; - grib_iterator_lambert_conformal* self = (grib_iterator_lambert_conformal*)iter; - - const char* sradius = grib_arguments_get_name(h, args, self->carg++); - const char* snx = grib_arguments_get_name(h, args, self->carg++); - const char* sny = grib_arguments_get_name(h, args, self->carg++); - const char* sLoVInDegrees = grib_arguments_get_name(h, args, self->carg++); - const char* sLaDInDegrees = grib_arguments_get_name(h, args, self->carg++); - const char* sLatin1InDegrees = grib_arguments_get_name(h, args, self->carg++); - const char* sLatin2InDegrees = grib_arguments_get_name(h, args, self->carg++); - const char* slatFirstInDegrees = grib_arguments_get_name(h, args, self->carg++); - const char* slonFirstInDegrees = grib_arguments_get_name(h, args, self->carg++); + const char* sradius = grib_arguments_get_name(h, args, carg_++); + const char* snx = grib_arguments_get_name(h, args, carg_++); + const char* sny = grib_arguments_get_name(h, args, carg_++); + const char* sLoVInDegrees = grib_arguments_get_name(h, args, carg_++); + const char* sLaDInDegrees = grib_arguments_get_name(h, args, carg_++); + const char* sLatin1InDegrees = grib_arguments_get_name(h, args, carg_++); + const char* sLatin2InDegrees = grib_arguments_get_name(h, args, carg_++); + const char* slatFirstInDegrees = grib_arguments_get_name(h, args, carg_++); + const char* slonFirstInDegrees = grib_arguments_get_name(h, args, carg_++); // Dx and Dy are in Metres - const char* sDx = grib_arguments_get_name(h, args, self->carg++); - const char* sDy = grib_arguments_get_name(h, args, self->carg++); - const char* siScansNegatively = grib_arguments_get_name(h, args, self->carg++); - const char* sjScansPositively = grib_arguments_get_name(h, args, self->carg++); - const char* sjPointsAreConsecutive = grib_arguments_get_name(h, args, self->carg++); - const char* salternativeRowScanning = grib_arguments_get_name(h, args, self->carg++); + const char* sDx = grib_arguments_get_name(h, args, carg_++); + const char* sDy = grib_arguments_get_name(h, args, carg_++); + const char* siScansNegatively = grib_arguments_get_name(h, args, carg_++); + const char* sjScansPositively = grib_arguments_get_name(h, args, carg_++); + const char* sjPointsAreConsecutive = grib_arguments_get_name(h, args, carg_++); + const char* salternativeRowScanning = grib_arguments_get_name(h, args, carg_++); if ((err = grib_get_long_internal(h, snx, &nx)) != GRIB_SUCCESS) return err; if ((err = grib_get_long_internal(h, sny, &ny)) != GRIB_SUCCESS) return err; @@ -449,8 +380,8 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) if ((err = grib_get_double_internal(h, sradius, &radius)) != GRIB_SUCCESS) return err; } - if (iter->nv != nx * ny) { - grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Wrong number of points (%zu!=%ldx%ld)", ITER, iter->nv, nx, ny); + if (nv_ != nx * ny) { + grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Wrong number of points (%zu!=%ldx%ld)", ITER, nv_, nx, ny); return GRIB_WRONG_GRID; } @@ -497,14 +428,14 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) LoVInRadians = LoVInDegrees * DEG2RAD; if (is_oblate) { - err = init_oblate(h, self, iter->nv, nx, ny, + err = init_oblate(h, nv_, nx, ny, LoVInDegrees, Dx, Dy, earthMinorAxisInMetres, earthMajorAxisInMetres, latFirstInRadians, lonFirstInRadians, LoVInRadians, Latin1InRadians, Latin2InRadians, LaDInRadians); } else { - err = init_sphere(h, self, iter->nv, nx, ny, + err = init_sphere(h, nv_, nx, ny, LoVInDegrees, Dx, Dy, radius, latFirstInRadians, lonFirstInRadians, @@ -512,37 +443,36 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) } if (err) return err; - iter->e = -1; + e_ = -1; // Apply the scanning mode flags which may require data array to be transformed - err = transform_iterator_data(h->context, iter->data, + err = transform_iterator_data(h->context, data_, iScansNegatively, jScansPositively, jPointsAreConsecutive, alternativeRowScanning, - iter->nv, nx, ny); + nv_, nx, ny); return err; } -static int next(grib_iterator* iter, double* lat, double* lon, double* val) +int grib_iterator_lambert_conformal_t::next(double* lat, double* lon, double* val) { - grib_iterator_lambert_conformal* self = (grib_iterator_lambert_conformal*)iter; - - if ((long)iter->e >= (long)(iter->nv - 1)) + if ((long)e_ >= (long)(nv_ - 1)) return 0; - iter->e++; + e_++; - *lat = self->lats[iter->e]; - *lon = self->lons[iter->e]; - if (val && iter->data) { - *val = iter->data[iter->e]; + *lat = lats_[e_]; + *lon = lons_[e_]; + if (val && data_) { + *val = data_[e_]; } return 1; } -static int destroy(grib_iterator* i) +int grib_iterator_lambert_conformal_t::destroy() { - grib_iterator_lambert_conformal* self = (grib_iterator_lambert_conformal*)i; - const grib_context* c = i->h->context; + const grib_context* c = h_->context; - grib_context_free(c, self->lats); - grib_context_free(c, self->lons); + grib_context_free(c, lats_); + grib_context_free(c, lons_); + + grib_iterator_gen_t::destroy(); return GRIB_SUCCESS; } diff --git a/src/iterator/grib_iterator_class_lambert_conformal.h b/src/iterator/grib_iterator_class_lambert_conformal.h new file mode 100644 index 000000000..ffd8baa59 --- /dev/null +++ b/src/iterator/grib_iterator_class_lambert_conformal.h @@ -0,0 +1,48 @@ +/* + * (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. + */ + +#pragma once + +#include "grib_iterator_class_gen.h" + +class grib_iterator_lambert_conformal_t : public grib_iterator_gen_t +{ +public: + grib_iterator_lambert_conformal_t() : grib_iterator_gen_t() + { + class_name_ = "lambert_conformal"; + } + + int init(grib_handle*,grib_arguments*) override; + int next(double*, double*, double*) override; + int destroy() override; + +private: + double* lats_; + double* lons_; + long Nj_; + + int init_sphere(const grib_handle*, + size_t, long, long, + double, + double, double, double, + double, double, + double, double, double, + double); + + int init_oblate(const grib_handle*, + size_t, long, long, + double, + double, double, + double, double, + double, double, + double, double, double, + double); +}; diff --git a/src/iterator/grib_iterator_class_latlon.cc b/src/iterator/grib_iterator_class_latlon.cc new file mode 100644 index 000000000..f33c5111e --- /dev/null +++ b/src/iterator/grib_iterator_class_latlon.cc @@ -0,0 +1,152 @@ +/* + * (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_iterator_class_latlon.h" + +grib_iterator_latlon_t _grib_iterator_latlon{}; +grib_iterator* grib_iterator_latlon = &_grib_iterator_latlon; + + +int grib_iterator_latlon_t::next(double* lat, double* lon, double* val) +{ + /* GRIB-238: Support rotated lat/lon grids */ + double ret_lat, ret_lon, ret_val=0; + if ((long)e_ >= (long)(nv_ - 1)) + return 0; + + e_++; + + /* Assumptions: + * All rows scan in the same direction (alternativeRowScanning==0) + */ + if (!jPointsAreConsecutive_) { + /* Adjacent points in i (x) direction are consecutive */ + ret_lat = las_[(long)floor(e_ / Ni_)]; + ret_lon = los_[(long)e_ % Ni_]; + if (data_) + ret_val = data_[e_]; + } + else { + /* Adjacent points in j (y) direction is consecutive */ + ret_lon = los_[(long)e_ / Nj_]; + ret_lat = las_[(long)floor(e_ % Nj_)]; + if (data_) + ret_val = data_[e_]; + } + + /* See ECC-808: Some users want to disable the unrotate */ + if (isRotated_ && !disableUnrotate_) { + double new_lat = 0, new_lon = 0; + unrotate(ret_lat, ret_lon, + angleOfRotation_, southPoleLat_, southPoleLon_, + &new_lat, &new_lon); + ret_lat = new_lat; + ret_lon = new_lon; + } + + *lat = ret_lat; + *lon = ret_lon; + if (val && data_) { + *val = ret_val; + } + return 1; +} + +int grib_iterator_latlon_t::init(grib_handle* h, grib_arguments* args) +{ + grib_iterator_regular_t::init(h, args); + + int err = 0; + double jdir; + double lat1=0, lat2=0, north=0, south=0; + long jScansPositively; + long lai; + + const char* s_lat1 = grib_arguments_get_name(h, args, carg_++); + const char* s_jdir = grib_arguments_get_name(h, args, carg_++); + const char* s_jScansPos = grib_arguments_get_name(h, args, carg_++); + const char* s_jPtsConsec = grib_arguments_get_name(h, args, carg_++); + const char* s_isRotatedGrid = grib_arguments_get_name(h, args, carg_++); + const char* s_angleOfRotation = grib_arguments_get_name(h, args, carg_++); + const char* s_latSouthernPole = grib_arguments_get_name(h, args, carg_++); + const char* s_lonSouthernPole = grib_arguments_get_name(h, args, carg_++); + + angleOfRotation_ = 0; + isRotated_ = 0; + southPoleLat_ = 0; + southPoleLon_ = 0; + disableUnrotate_ = 0; /* unrotate enabled by default */ + + if ((err = grib_get_long(h, s_isRotatedGrid, &isRotated_))) + return err; + if (isRotated_) { + if ((err = grib_get_double_internal(h, s_angleOfRotation, &angleOfRotation_))) + return err; + if ((err = grib_get_double_internal(h, s_latSouthernPole, &southPoleLat_))) + return err; + if ((err = grib_get_double_internal(h, s_lonSouthernPole, &southPoleLon_))) + return err; + } + + if ((err = grib_get_double_internal(h, s_lat1, &lat1))) + return err; + if ((err = grib_get_double_internal(h, "latitudeLastInDegrees", &lat2))) + return err; + if ((err = grib_get_double_internal(h, s_jdir, &jdir))) //can be GRIB_MISSING_DOUBLE + return err; + if ((err = grib_get_long_internal(h, s_jScansPos, &jScansPositively))) + return err; + if ((err = grib_get_long_internal(h, s_jPtsConsec, &jPointsAreConsecutive_))) + return err; + if ((err = grib_get_long(h, "iteratorDisableUnrotate", &disableUnrotate_))) + return err; + + /* ECC-984: If jDirectionIncrement is missing, then we cannot use it (See jDirectionIncrementGiven) */ + /* So try to compute the increment */ + if ( (grib_is_missing(h, s_jdir, &err) && err == GRIB_SUCCESS) || (jdir == GRIB_MISSING_DOUBLE) ) { + const long Nj = Nj_; + Assert(Nj > 1); + if (lat1 > lat2) { + jdir = (lat1 - lat2) / (Nj - 1); + } + else { + jdir = (lat2 - lat1) / (Nj - 1); + } + grib_context_log(h->context, GRIB_LOG_DEBUG, + "Cannot use jDirectionIncrement. Using value of %.6f obtained from La1, La2 and Nj", jdir); + } + + if (jScansPositively) { + north = lat2; + south = lat1; + jdir = -jdir; + } else { + north = lat1; + south = lat2; + } + if (south > north) { + grib_context_log(h->context, GRIB_LOG_ERROR, + "Lat/Lon Geoiterator: First and last latitudes are inconsistent with scanning order: lat1=%g, lat2=%g jScansPositively=%ld", + lat1, lat2, jScansPositively); + return GRIB_WRONG_GRID; + } + + for (lai = 0; lai < Nj_; lai++) { + las_[lai] = lat1; + lat1 -= jdir; + } + /* ECC-1406: Due to rounding, errors can accumulate. + * So we ensure the last latitude is latitudeOfLastGridPointInDegrees + */ + las_[Nj_-1] = lat2; + + e_ = -1; + return err; +} diff --git a/src/iterator/grib_iterator_class_latlon.h b/src/iterator/grib_iterator_class_latlon.h new file mode 100644 index 000000000..3a3094220 --- /dev/null +++ b/src/iterator/grib_iterator_class_latlon.h @@ -0,0 +1,25 @@ +/* + * (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. + */ + +#pragma once + +#include "grib_iterator_class_regular.h" + +class grib_iterator_latlon_t : public grib_iterator_regular_t +{ +public: + grib_iterator_latlon_t() : grib_iterator_regular_t() + { + class_name_ = "latlon"; + } + + int init(grib_handle*, grib_arguments*) override; + int next(double*, double*, double*) override; +}; diff --git a/src/iterator/grib_iterator_class_latlon_reduced.cc b/src/iterator/grib_iterator_class_latlon_reduced.cc new file mode 100644 index 000000000..f2ef96281 --- /dev/null +++ b/src/iterator/grib_iterator_class_latlon_reduced.cc @@ -0,0 +1,139 @@ +/* + * (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_iterator_class_latlon_reduced.h" +#include + +grib_iterator_latlon_reduced_t _grib_iterator_latlon_reduced{}; +grib_iterator* grib_iterator_latlon_reduced = &_grib_iterator_latlon_reduced; + + +int grib_iterator_latlon_reduced_t::next(double* lat, double* lon, double* val) +{ + if ((long)e_ >= (long)(nv_ - 1)) + return 0; + e_++; + + *lat = las_[e_]; + *lon = los_[e_]; + if (val && data_) { + *val = data_[e_]; + } + return 1; +} + +int grib_iterator_latlon_reduced_t::init(grib_handle* h, grib_arguments* args) +{ + grib_iterator_gen_t::init(h, args); + + int ret = GRIB_SUCCESS; + double laf; + double lal; + long nlats; + double lof, tlof; + double lol, dimin; + long* pl; + size_t plsize = 0; + long k, j, ii; + long nlons, plmax; + double jdirinc = 0; + double idirinc = 0; + double dlon = 0; + int islocal = 0; + long nlons2 = 0; /* adjusted num of longitudes */ + + const char* latofirst = grib_arguments_get_name(h, args, carg_++); + const char* longoffirst = grib_arguments_get_name(h, args, carg_++); + const char* latoflast = grib_arguments_get_name(h, args, carg_++); + const char* longoflast = grib_arguments_get_name(h, args, carg_++); + const char* nlats_name = grib_arguments_get_name(h, args, carg_++); + const char* jdirec = grib_arguments_get_name(h, args, carg_++); + const char* plac = grib_arguments_get_name(h, args, carg_++); + + if ((ret = grib_get_double_internal(h, latofirst, &laf))) + return ret; + if ((ret = grib_get_double_internal(h, longoffirst, &lof))) + return ret; + + if ((ret = grib_get_double_internal(h, latoflast, &lal))) + return ret; + if ((ret = grib_get_double_internal(h, longoflast, &lol))) + return ret; + + if ((ret = grib_get_long_internal(h, nlats_name, &nlats))) + return ret; + + if ((ret = grib_get_double_internal(h, jdirec, &jdirinc))) + return ret; + + plsize = nlats; + pl = (long*)grib_context_malloc(h->context, plsize * sizeof(long)); + grib_get_long_array_internal(h, plac, pl, &plsize); + + las_ = (double*)grib_context_malloc(h->context, nv_ * sizeof(double)); + los_ = (double*)grib_context_malloc(h->context, nv_ * sizeof(double)); + + plmax = pl[0]; + for (j = 0; j < nlats; j++) + if (plmax < pl[j]) + plmax = pl[j]; + dimin = 360.0 / plmax; + + if (360 - fabs(lol - lof) < 2 * dimin) { + dlon = 360; + islocal = 0; + } + else if (lol < lof) { + /* handle something like 150 to -120 to treat as 150 to 240 */ + /* so that dlon is 90 (not -270) */ + dlon = lol + 360.0 - lof; + islocal = 1; + } + else { + dlon = lol - lof; + islocal = 1; + } + + if (laf > lal) + jdirinc = -jdirinc; + k = 0; + for (j = 0; j < nlats; j++) { + nlons = pl[j]; + tlof = lof; + nlons2 = nlons - islocal; + /*Sometimes there are no points on a latitude! Protect against div by zero*/ + if (nlons2 < 1) + nlons2 = 1; + idirinc = dlon / nlons2; + for (ii = 0; ii < nlons; ii++) { + las_[k] = laf; + los_[k] = tlof; + tlof += idirinc; + k++; + } + laf += jdirinc; + } + + e_ = -1; + grib_context_free(h->context, pl); + + return ret; +} + +int grib_iterator_latlon_reduced_t::destroy() +{ + const grib_context* c = h_->context; + + grib_context_free(c, las_); + grib_context_free(c, los_); + + grib_iterator_gen_t::destroy(); + return GRIB_SUCCESS; +} diff --git a/src/iterator/grib_iterator_class_latlon_reduced.h b/src/iterator/grib_iterator_class_latlon_reduced.h new file mode 100644 index 000000000..57511595b --- /dev/null +++ b/src/iterator/grib_iterator_class_latlon_reduced.h @@ -0,0 +1,30 @@ +/* + * (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. + */ + +#pragma once + +#include "grib_iterator_class_gen.h" + +class grib_iterator_latlon_reduced_t : public grib_iterator_gen_t +{ +public: + grib_iterator_latlon_reduced_t() : grib_iterator_gen_t() + { + class_name_ = "latlon_reduced"; + } + + int init(grib_handle*,grib_arguments*) override; + int next(double *lat, double *lon, double *val) override; + int destroy() override; + +private: + double* las_; + double* los_; +}; diff --git a/src/grib_iterator_class_mercator.cc b/src/iterator/grib_iterator_class_mercator.cc similarity index 71% rename from src/grib_iterator_class_mercator.cc rename to src/iterator/grib_iterator_class_mercator.cc index c4f668186..e31b2b059 100644 --- a/src/grib_iterator_class_mercator.cc +++ b/src/iterator/grib_iterator_class_mercator.cc @@ -8,78 +8,11 @@ * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. */ -#include "grib_api_internal.h" +#include "grib_iterator_class_mercator.h" #include -/* - This is used by make_class.pl - - START_CLASS_DEF - CLASS = iterator - SUPER = grib_iterator_class_gen - IMPLEMENTS = destroy - IMPLEMENTS = init;next - MEMBERS = double *lats - MEMBERS = double *lons - MEMBERS = long 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 "iterator.class" and rerun ./make_class.pl - -*/ - - -static void init_class (grib_iterator_class*); - -static int init (grib_iterator* i,grib_handle*,grib_arguments*); -static int next (grib_iterator* i, double *lat, double *lon, double *val); -static int destroy (grib_iterator* i); - - -typedef struct grib_iterator_mercator{ - grib_iterator it; - /* Members defined in gen */ - int carg; - const char* missingValue; - /* Members defined in mercator */ - double *lats; - double *lons; - long Nj; -} grib_iterator_mercator; - -extern grib_iterator_class* grib_iterator_class_gen; - -static grib_iterator_class _grib_iterator_class_mercator = { - &grib_iterator_class_gen, /* super */ - "mercator", /* name */ - sizeof(grib_iterator_mercator),/* size of instance */ - 0, /* inited */ - &init_class, /* init_class */ - &init, /* constructor */ - &destroy, /* destructor */ - &next, /* Next Value */ - 0, /* Previous Value */ - 0, /* Reset the counter */ - 0, /* has next values */ -}; - -grib_iterator_class* grib_iterator_class_mercator = &_grib_iterator_class_mercator; - - -static void init_class(grib_iterator_class* c) -{ - c->previous = (*(c->super))->previous; - c->reset = (*(c->super))->reset; - c->has_next = (*(c->super))->has_next; -} -/* END_CLASS_IMP */ +grib_iterator_mercator_t _grib_iterator_mercator{}; +grib_iterator* grib_iterator_mercator = &_grib_iterator_mercator; #define ITER "Mercator Geoiterator" #define EPSILON 1.0e-10 @@ -148,8 +81,7 @@ static double compute_t( return (tan(0.5 * (M_PI_2 - phi)) / con); } -static int init_mercator(grib_handle* h, - grib_iterator_mercator* self, +int grib_iterator_mercator_t::init_mercator(grib_handle* h, size_t nv, long nx, long ny, double DiInMetres, double DjInMetres, double earthMinorAxisInMetres, double earthMajorAxisInMetres, @@ -184,13 +116,13 @@ static int init_mercator(grib_handle* h, y0 = -y0; /* Allocate latitude and longitude arrays */ - self->lats = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!self->lats) { + lats_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); + if (!lats_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } - self->lons = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!self->lats) { + lons_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); + if (!lats_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } @@ -211,8 +143,8 @@ static int init_mercator(grib_handle* h, latRad = compute_phi(e, ts, &err); if (err) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Failed to compute the latitude angle, phi2, for the inverse", ITER); - grib_context_free(h->context, self->lats); - grib_context_free(h->context, self->lons); + grib_context_free(h->context, lats_); + grib_context_free(h->context, lons_); return err; } lonRad = adjust_lon_radians(orientationInRadians + _x / (earthMajorAxisInMetres * m1)); @@ -221,15 +153,17 @@ static int init_mercator(grib_handle* h, } latDeg = latRad * RAD2DEG; /* Convert to degrees */ lonDeg = normalise_longitude_in_degrees(lonRad * RAD2DEG); - self->lons[index] = lonDeg; - self->lats[index] = latDeg; + lons_[index] = lonDeg; + lats_[index] = latDeg; } } return GRIB_SUCCESS; } -static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) +int grib_iterator_mercator_t::init(grib_handle* h, grib_arguments* args) { + grib_iterator_gen_t::init(h, args); + int err = 0; long ni, nj, iScansNegatively, jScansPositively, jPointsAreConsecutive, alternativeRowScanning; double latFirstInDegrees, lonFirstInDegrees, LaDInDegrees; @@ -238,24 +172,22 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) LaDInRadians, orientationInRadians; double earthMajorAxisInMetres = 0, earthMinorAxisInMetres = 0; - grib_iterator_mercator* self = (grib_iterator_mercator*)iter; - - const char* sRadius = grib_arguments_get_name(h, args, self->carg++); - const char* sNi = grib_arguments_get_name(h, args, self->carg++); - const char* sNj = grib_arguments_get_name(h, args, self->carg++); - const char* sLatFirstInDegrees = grib_arguments_get_name(h, args, self->carg++); - const char* sLonFirstInDegrees = grib_arguments_get_name(h, args, self->carg++); - const char* sLaDInDegrees = grib_arguments_get_name(h, args, self->carg++); - const char* sLatLastInDegrees = grib_arguments_get_name(h, args, self->carg++); - const char* sLonLastInDegrees = grib_arguments_get_name(h, args, self->carg++); - const char* sOrientationInDegrees = grib_arguments_get_name(h, args, self->carg++); + const char* sRadius = grib_arguments_get_name(h, args, carg_++); + const char* sNi = grib_arguments_get_name(h, args, carg_++); + const char* sNj = grib_arguments_get_name(h, args, carg_++); + const char* sLatFirstInDegrees = grib_arguments_get_name(h, args, carg_++); + const char* sLonFirstInDegrees = grib_arguments_get_name(h, args, carg_++); + const char* sLaDInDegrees = grib_arguments_get_name(h, args, carg_++); + const char* sLatLastInDegrees = grib_arguments_get_name(h, args, carg_++); + const char* sLonLastInDegrees = grib_arguments_get_name(h, args, carg_++); + const char* sOrientationInDegrees = grib_arguments_get_name(h, args, carg_++); /* Dx and Dy are in Metres */ - const char* sDi = grib_arguments_get_name(h, args, self->carg++); - const char* sDj = grib_arguments_get_name(h, args, self->carg++); - const char* siScansNegatively = grib_arguments_get_name(h, args, self->carg++); - const char* sjScansPositively = grib_arguments_get_name(h, args, self->carg++); - const char* sjPointsAreConsecutive = grib_arguments_get_name(h, args, self->carg++); - const char* sAlternativeRowScanning = grib_arguments_get_name(h, args, self->carg++); + const char* sDi = grib_arguments_get_name(h, args, carg_++); + const char* sDj = grib_arguments_get_name(h, args, carg_++); + const char* siScansNegatively = grib_arguments_get_name(h, args, carg_++); + const char* sjScansPositively = grib_arguments_get_name(h, args, carg_++); + const char* sjPointsAreConsecutive = grib_arguments_get_name(h, args, carg_++); + const char* sAlternativeRowScanning = grib_arguments_get_name(h, args, carg_++); if ((err = grib_get_long_internal(h, sNi, &ni)) != GRIB_SUCCESS) return err; if ((err = grib_get_long_internal(h, sNj, &nj)) != GRIB_SUCCESS) return err; @@ -269,8 +201,8 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) earthMinorAxisInMetres = earthMajorAxisInMetres = radius; } - if (iter->nv != ni * nj) { - grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Wrong number of points (%zu!=%ldx%ld)", ITER, iter->nv, ni, nj); + if (nv_ != ni * nj) { + grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Wrong number of points (%zu!=%ldx%ld)", ITER, nv_, ni, nj); return GRIB_WRONG_GRID; } @@ -307,44 +239,43 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) LaDInRadians = LaDInDegrees * DEG2RAD; orientationInRadians = orientationInDegrees * DEG2RAD; - err = init_mercator(h, self, iter->nv, ni, nj, + err = init_mercator(h, nv_, ni, nj, DiInMetres, DjInMetres, earthMinorAxisInMetres, earthMajorAxisInMetres, latFirstInRadians, lonFirstInRadians, latLastInRadians, lonLastInRadians, LaDInRadians, orientationInRadians); if (err) return err; - iter->e = -1; + e_ = -1; /* Apply the scanning mode flags which may require data array to be transformed */ - err = transform_iterator_data(h->context, iter->data, + err = transform_iterator_data(h->context, data_, iScansNegatively, jScansPositively, jPointsAreConsecutive, alternativeRowScanning, - iter->nv, ni, nj); + nv_, ni, nj); return err; } -static int next(grib_iterator* iter, double* lat, double* lon, double* val) +int grib_iterator_mercator_t::next(double* lat, double* lon, double* val) { - grib_iterator_mercator* self = (grib_iterator_mercator*)iter; - - if ((long)iter->e >= (long)(iter->nv - 1)) + if ((long)e_ >= (long)(nv_ - 1)) return 0; - iter->e++; + e_++; - *lat = self->lats[iter->e]; - *lon = self->lons[iter->e]; - if (val && iter->data) { - *val = iter->data[iter->e]; + *lat = lats_[e_]; + *lon = lons_[e_]; + if (val && data_) { + *val = data_[e_]; } return 1; } -static int destroy(grib_iterator* iter) +int grib_iterator_mercator_t::destroy() { - grib_iterator_mercator* self = (grib_iterator_mercator*)iter; - const grib_context* c = iter->h->context; + const grib_context* c = h_->context; - grib_context_free(c, self->lats); - grib_context_free(c, self->lons); + grib_context_free(c, lats_); + grib_context_free(c, lons_); + + grib_iterator_gen_t::destroy(); return GRIB_SUCCESS; } diff --git a/src/iterator/grib_iterator_class_mercator.h b/src/iterator/grib_iterator_class_mercator.h new file mode 100644 index 000000000..61ca4dd51 --- /dev/null +++ b/src/iterator/grib_iterator_class_mercator.h @@ -0,0 +1,38 @@ +/* + * (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. + */ + +#pragma once + +#include "grib_iterator_class_gen.h" + +class grib_iterator_mercator_t : public grib_iterator_gen_t +{ +public: + grib_iterator_mercator_t() { + class_name_ = "mercator"; + } + int init(grib_handle*,grib_arguments*) override; + int next(double *lat, double *lon, double *val) override; + int destroy() override; + +private: + double *lats_; + double *lons_; + long Nj_; + + int init_mercator(grib_handle*, + size_t, long, long, + double, double, + double, double, + double, double, + double, double, + double, double); +}; + diff --git a/src/grib_iterator_class_polar_stereographic.cc b/src/iterator/grib_iterator_class_polar_stereographic.cc similarity index 72% rename from src/grib_iterator_class_polar_stereographic.cc rename to src/iterator/grib_iterator_class_polar_stereographic.cc index 1f0c59d69..a0fccafc5 100644 --- a/src/grib_iterator_class_polar_stereographic.cc +++ b/src/iterator/grib_iterator_class_polar_stereographic.cc @@ -8,93 +8,24 @@ * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. */ -#include "grib_api_internal.h" +#include "grib_iterator_class_polar_stereographic.h" #include -/* - This is used by make_class.pl - - START_CLASS_DEF - CLASS = iterator - SUPER = grib_iterator_class_gen - IMPLEMENTS = destroy - IMPLEMENTS = init;next - MEMBERS = double *lats - MEMBERS = double *lons - MEMBERS = long 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 "iterator.class" and rerun ./make_class.pl - -*/ - - -static void init_class (grib_iterator_class*); - -static int init (grib_iterator* i,grib_handle*,grib_arguments*); -static int next (grib_iterator* i, double *lat, double *lon, double *val); -static int destroy (grib_iterator* i); - - -typedef struct grib_iterator_polar_stereographic{ - grib_iterator it; - /* Members defined in gen */ - int carg; - const char* missingValue; - /* Members defined in polar_stereographic */ - double *lats; - double *lons; - long Nj; -} grib_iterator_polar_stereographic; - -extern grib_iterator_class* grib_iterator_class_gen; - -static grib_iterator_class _grib_iterator_class_polar_stereographic = { - &grib_iterator_class_gen, /* super */ - "polar_stereographic", /* name */ - sizeof(grib_iterator_polar_stereographic),/* size of instance */ - 0, /* inited */ - &init_class, /* init_class */ - &init, /* constructor */ - &destroy, /* destructor */ - &next, /* Next Value */ - 0, /* Previous Value */ - 0, /* Reset the counter */ - 0, /* has next values */ -}; - -grib_iterator_class* grib_iterator_class_polar_stereographic = &_grib_iterator_class_polar_stereographic; - - -static void init_class(grib_iterator_class* c) -{ - c->previous = (*(c->super))->previous; - c->reset = (*(c->super))->reset; - c->has_next = (*(c->super))->has_next; -} -/* END_CLASS_IMP */ +grib_iterator_polar_stereographic_t _grib_iterator_polar_stereographic{}; +grib_iterator* grib_iterator_polar_stereographic = &_grib_iterator_polar_stereographic; #define ITER "Polar stereographic Geoiterator" -static int next(grib_iterator* iter, double* lat, double* lon, double* val) +int grib_iterator_polar_stereographic_t::next(double* lat, double* lon, double* val) { - grib_iterator_polar_stereographic* self = (grib_iterator_polar_stereographic*)iter; - - if ((long)iter->e >= (long)(iter->nv - 1)) + if ((long)e_ >= (long)(nv_ - 1)) return 0; - iter->e++; + e_++; - *lat = self->lats[iter->e]; - *lon = self->lons[iter->e]; - if (val && iter->data) { - *val = iter->data[iter->e]; + *lat = lats_[e_]; + *lon = lons_[e_]; + if (val && data_) { + *val = data_[e_]; } return 1; } @@ -117,8 +48,10 @@ typedef struct proj_data_t #define PI_OVER_2 1.5707963267948966 /* half pi */ #define EPSILON 1.0e-10 -static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) +int grib_iterator_polar_stereographic_t::init(grib_handle* h, grib_arguments* args) { + grib_iterator_gen_t::init(h, args); + int ret = 0; double *lats, *lons; /* arrays for latitudes and longitudes */ double lonFirstInDegrees, latFirstInDegrees, radius; @@ -135,22 +68,20 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) proj_data_t fwd_proj_data = {0,}; proj_data_t inv_proj_data = {0,}; - grib_iterator_polar_stereographic* self = (grib_iterator_polar_stereographic*)iter; - - const char* s_radius = grib_arguments_get_name(h, args, self->carg++); - const char* s_nx = grib_arguments_get_name(h, args, self->carg++); - const char* s_ny = grib_arguments_get_name(h, args, self->carg++); - const char* s_latFirstInDegrees = grib_arguments_get_name(h, args, self->carg++); - const char* s_lonFirstInDegrees = grib_arguments_get_name(h, args, self->carg++); - const char* s_southPoleOnPlane = grib_arguments_get_name(h, args, self->carg++); - const char* s_centralLongitude = grib_arguments_get_name(h, args, self->carg++); - const char* s_centralLatitude = grib_arguments_get_name(h, args, self->carg++); - const char* s_Dx = grib_arguments_get_name(h, args, self->carg++); - const char* s_Dy = grib_arguments_get_name(h, args, self->carg++); - const char* s_iScansNegatively = grib_arguments_get_name(h, args, self->carg++); - const char* s_jScansPositively = grib_arguments_get_name(h, args, self->carg++); - const char* s_jPointsAreConsecutive = grib_arguments_get_name(h, args, self->carg++); - const char* s_alternativeRowScanning = grib_arguments_get_name(h, args, self->carg++); + const char* s_radius = grib_arguments_get_name(h, args, carg_++); + const char* s_nx = grib_arguments_get_name(h, args, carg_++); + const char* s_ny = grib_arguments_get_name(h, args, carg_++); + const char* s_latFirstInDegrees = grib_arguments_get_name(h, args, carg_++); + const char* s_lonFirstInDegrees = grib_arguments_get_name(h, args, carg_++); + const char* s_southPoleOnPlane = grib_arguments_get_name(h, args, carg_++); + const char* s_centralLongitude = grib_arguments_get_name(h, args, carg_++); + const char* s_centralLatitude = grib_arguments_get_name(h, args, carg_++); + const char* s_Dx = grib_arguments_get_name(h, args, carg_++); + const char* s_Dy = grib_arguments_get_name(h, args, carg_++); + const char* s_iScansNegatively = grib_arguments_get_name(h, args, carg_++); + const char* s_jScansPositively = grib_arguments_get_name(h, args, carg_++); + const char* s_jPointsAreConsecutive = grib_arguments_get_name(h, args, carg_++); + const char* s_alternativeRowScanning = grib_arguments_get_name(h, args, carg_++); if (grib_is_earth_oblate(h)) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Only supported for spherical earth.", ITER); @@ -164,8 +95,8 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) if ((ret = grib_get_long_internal(h, s_ny, &ny)) != GRIB_SUCCESS) return ret; - if (iter->nv != nx * ny) { - grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Wrong number of points (%zu!=%ldx%ld)", ITER, iter->nv, nx, ny); + if (nv_ != nx * ny) { + grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Wrong number of points (%zu!=%ldx%ld)", ITER, nv_, nx, ny); return GRIB_WRONG_GRID; } if ((ret = grib_get_double_internal(h, s_latFirstInDegrees, &latFirstInDegrees)) != GRIB_SUCCESS) @@ -243,18 +174,18 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) inv_proj_data.mcs = cos(con1); inv_proj_data.tcs = tan(0.5 * (PI_OVER_2 - con1)); } - self->lats = (double*)grib_context_malloc(h->context, iter->nv * sizeof(double)); - if (!self->lats) { - grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, iter->nv * sizeof(double)); + lats_ = (double*)grib_context_malloc(h->context, nv_ * sizeof(double)); + if (!lats_) { + grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv_ * sizeof(double)); return GRIB_OUT_OF_MEMORY; } - self->lons = (double*)grib_context_malloc(h->context, iter->nv * sizeof(double)); - if (!self->lats) { - grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, iter->nv * sizeof(double)); + lons_ = (double*)grib_context_malloc(h->context, nv_ * sizeof(double)); + if (!lats_) { + grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv_ * sizeof(double)); return GRIB_OUT_OF_MEMORY; } - lats = self->lats; - lons = self->lons; + lats = lats_; + lons = lons_; /* These will be processed later in transform_iterator_data() */ /* Dx = iScansNegatively == 0 ? Dx : -Dx; */ /* Dy = jScansPositively == 1 ? Dy : -Dy; */ @@ -353,22 +284,23 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) // } // } - iter->e = -1; + e_ = -1; /* Apply the scanning mode flags which may require data array to be transformed */ - ret = transform_iterator_data(h->context, iter->data, + ret = transform_iterator_data(h->context, data_, iScansNegatively, jScansPositively, jPointsAreConsecutive, alternativeRowScanning, - iter->nv, nx, ny); + nv_, nx, ny); return ret; } -static int destroy(grib_iterator* i) +int grib_iterator_polar_stereographic_t::destroy() { - grib_iterator_polar_stereographic* self = (grib_iterator_polar_stereographic*)i; - const grib_context* c = i->h->context; + const grib_context* c = h_->context; - grib_context_free(c, self->lats); - grib_context_free(c, self->lons); + grib_context_free(c, lats_); + grib_context_free(c, lons_); + + grib_iterator_gen_t::destroy(); return GRIB_SUCCESS; } diff --git a/src/iterator/grib_iterator_class_polar_stereographic.h b/src/iterator/grib_iterator_class_polar_stereographic.h new file mode 100644 index 000000000..cbc15b4a8 --- /dev/null +++ b/src/iterator/grib_iterator_class_polar_stereographic.h @@ -0,0 +1,32 @@ +/* + * (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. + */ + +#pragma once + +#include "grib_iterator_class_gen.h" + +class grib_iterator_polar_stereographic_t : public grib_iterator_gen_t +{ +public: + grib_iterator_polar_stereographic_t() : grib_iterator_gen_t() + { + class_name_ = "polar_stereographic"; + } + + int init(grib_handle*,grib_arguments*) override; + int next(double *, double *, double *) override; + int destroy() override; + +private: + double* lats_; + double* lons_; + long Nj_; +}; + diff --git a/src/iterator/grib_iterator_class_regular.cc b/src/iterator/grib_iterator_class_regular.cc new file mode 100644 index 000000000..334dab9a0 --- /dev/null +++ b/src/iterator/grib_iterator_class_regular.cc @@ -0,0 +1,160 @@ +/* + * (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_iterator_class_regular.h" +#include + +grib_iterator_regular_t _grib_iterator_regular{}; +grib_iterator_regular_t* grib_iterator_regular = &_grib_iterator_regular; + +#define ITER "Regular grid Geoiterator" + +int grib_iterator_regular_t::next(double* lat, double* lon, double* val) +{ + if ((long)e_ >= (long)(nv_ - 1)) + return 0; + + e_++; + + *lat = las_[(long)floor(e_ / Ni_)]; + *lon = los_[(long)e_ % Ni_]; + if (val && data_) { + *val = data_[e_]; + } + return 1; +} + +int grib_iterator_regular_t::previous(double* lat, double* lon, double* val) +{ + if (e_ < 0) + return 0; + *lat = las_[(long)floor(e_ / Ni_)]; + *lon = los_[e_ % Ni_]; + if (val && data_) { + *val = data_[e_]; + } + e_--; + + return 1; +} + +int grib_iterator_regular_t::destroy() +{ + const grib_context* c = h_->context; + grib_context_free(c, las_); + grib_context_free(c, los_); + + grib_iterator_gen_t::destroy(); + return GRIB_SUCCESS; +} + +int grib_iterator_regular_t::init(grib_handle* h, grib_arguments* args) +{ + grib_iterator_gen_t::init(h, args); + + int ret = GRIB_SUCCESS; + + long Ni; /* Number of points along a parallel = Nx */ + long Nj; /* Number of points along a meridian = Ny */ + double idir, idir_coded, lon1, lon2; + long loi; + + const char* s_lon1 = grib_arguments_get_name(h, args, carg_++); + const char* s_idir = grib_arguments_get_name(h, args, carg_++); + const char* s_Ni = grib_arguments_get_name(h, args, carg_++); + const char* s_Nj = grib_arguments_get_name(h, args, carg_++); + const char* s_iScansNeg = grib_arguments_get_name(h, args, carg_++); + + if ((ret = grib_get_double_internal(h, s_lon1, &lon1))) + return ret; + if ((ret = grib_get_double_internal(h, "longitudeOfLastGridPointInDegrees", &lon2))) + return ret; + if ((ret = grib_get_double_internal(h, s_idir, &idir))) // can be GRIB_MISSING_DOUBLE + return ret; + idir_coded = idir; + if ((ret = grib_get_long_internal(h, s_Ni, &Ni))) + return ret; + if (grib_is_missing(h, s_Ni, &ret) && ret == GRIB_SUCCESS) { + grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Key %s cannot be 'missing' for a regular grid!", ITER, s_Ni); + return GRIB_WRONG_GRID; + } + + if ((ret = grib_get_long_internal(h, s_Nj, &Nj))) + return ret; + if (grib_is_missing(h, s_Nj, &ret) && ret == GRIB_SUCCESS) { + grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Key %s cannot be 'missing' for a regular grid!", ITER, s_Nj); + return GRIB_WRONG_GRID; + } + + if (Ni*Nj != nv_) { + grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Ni*Nj!=numberOfDataPoints (%ld*%ld!=%zu)", ITER, Ni, Nj, nv_); + return GRIB_WRONG_GRID; + } + + if ((ret = grib_get_long_internal(h, s_iScansNeg, &iScansNegatively_))) + return ret; + + /* GRIB-801: Careful of case with a single point! Ni==1 */ + if (Ni > 1) { + /* Note: If first and last longitudes are equal I assume you wanna go round the globe */ + if (iScansNegatively_) { + if (lon1 > lon2) { + idir = (lon1 - lon2) / (Ni - 1); + } + else { + idir = (lon1 + 360.0 - lon2) / (Ni - 1); + } + } + else { + if (lon2 > lon1) { + idir = (lon2 - lon1) / (Ni - 1); + } + else { + idir = (lon2 + 360.0 - lon1) / (Ni - 1); + } + } + } + if (iScansNegatively_) { + idir = -idir; + } + else { + if (lon1 + (Ni - 2) * idir > 360) + lon1 -= 360; + /*See ECC-704, GRIB-396*/ + /*else if ( (lon1+(Ni-1)*idir)-360 > epsilon ){ + idir=360.0/(float)Ni; + }*/ + } + + Ni_ = Ni; + Nj_ = Nj; + + las_ = (double*)grib_context_malloc(h->context, Nj * sizeof(double)); + los_ = (double*)grib_context_malloc(h->context, Ni * sizeof(double)); + + if (idir != idir_coded) { + grib_context_log(h->context, GRIB_LOG_DEBUG, "%s: Using idir=%g (coded value=%g)", ITER, idir, idir_coded); + } + + for (loi = 0; loi < Ni; loi++) { + los_[loi] = lon1; + lon1 += idir; + } + + // ECC-1406: Due to rounding, errors can accumulate. + // So we ensure the last longitude is longitudeOfLastGridPointInDegrees + // Also see ECC-1671, ECC-1708 + if (lon2 > 0) { + lon2 = normalise_longitude_in_degrees(lon2); + } + los_[Ni-1] = lon2; + + return ret; +} diff --git a/src/iterator/grib_iterator_class_regular.h b/src/iterator/grib_iterator_class_regular.h new file mode 100644 index 000000000..a9b1478a2 --- /dev/null +++ b/src/iterator/grib_iterator_class_regular.h @@ -0,0 +1,40 @@ +/* + * (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. + */ + +#pragma once + +#include "grib_iterator_class_gen.h" + +class grib_iterator_regular_t : public grib_iterator_gen_t +{ +public: + grib_iterator_regular_t() + { + class_name_ = "regular"; + } + + int init(grib_handle*,grib_arguments*) override; + int next(double *lat, double *lon, double *val) override; + int previous(double *lat, double *lon, double *val) override; + int destroy() override; + +protected: + long Ni_; + long Nj_; + double *las_; + double *los_; + long iScansNegatively_; + long isRotated_; + double angleOfRotation_; + double southPoleLat_; + double southPoleLon_; + long jPointsAreConsecutive_; + long disableUnrotate_; +}; diff --git a/src/grib_iterator_class_space_view.cc b/src/iterator/grib_iterator_class_space_view.cc similarity index 74% rename from src/grib_iterator_class_space_view.cc rename to src/iterator/grib_iterator_class_space_view.cc index 1622f94a5..fbfed04a9 100644 --- a/src/grib_iterator_class_space_view.cc +++ b/src/iterator/grib_iterator_class_space_view.cc @@ -8,93 +8,24 @@ * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. */ -#include "grib_api_internal.h" +#include "grib_iterator_class_space_view.h" #include -/* - This is used by make_class.pl - - START_CLASS_DEF - CLASS = iterator - SUPER = grib_iterator_class_gen - IMPLEMENTS = destroy - IMPLEMENTS = init;next - MEMBERS = double *lats - MEMBERS = double *lons - MEMBERS = long 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 "iterator.class" and rerun ./make_class.pl - -*/ - - -static void init_class (grib_iterator_class*); - -static int init (grib_iterator* i,grib_handle*,grib_arguments*); -static int next (grib_iterator* i, double *lat, double *lon, double *val); -static int destroy (grib_iterator* i); - - -typedef struct grib_iterator_space_view{ - grib_iterator it; - /* Members defined in gen */ - int carg; - const char* missingValue; - /* Members defined in space_view */ - double *lats; - double *lons; - long Nj; -} grib_iterator_space_view; - -extern grib_iterator_class* grib_iterator_class_gen; - -static grib_iterator_class _grib_iterator_class_space_view = { - &grib_iterator_class_gen, /* super */ - "space_view", /* name */ - sizeof(grib_iterator_space_view),/* size of instance */ - 0, /* inited */ - &init_class, /* init_class */ - &init, /* constructor */ - &destroy, /* destructor */ - &next, /* Next Value */ - 0, /* Previous Value */ - 0, /* Reset the counter */ - 0, /* has next values */ -}; - -grib_iterator_class* grib_iterator_class_space_view = &_grib_iterator_class_space_view; - - -static void init_class(grib_iterator_class* c) -{ - c->previous = (*(c->super))->previous; - c->reset = (*(c->super))->reset; - c->has_next = (*(c->super))->has_next; -} -/* END_CLASS_IMP */ +grib_iterator_space_view_t _grib_iterator_space_view{}; +grib_iterator* grib_iterator_space_view = &_grib_iterator_space_view; #define ITER "Space view Geoiterator" -static int next(grib_iterator* iter, double* lat, double* lon, double* val) +int grib_iterator_space_view_t::next(double* lat, double* lon, double* val) { - grib_iterator_space_view* self = (grib_iterator_space_view*)iter; - - if ((long)iter->e >= (long)(iter->nv - 1)) + if ((long)e_ >= (long)(nv_ - 1)) return 0; - iter->e++; + e_++; - *lat = self->lats[iter->e]; - *lon = self->lons[iter->e]; - if (val && iter->data) { - *val = iter->data[iter->e]; + *lat = lats_[e_]; + *lon = lons_[e_]; + if (val && data_) { + *val = data_[e_]; } return 1; } @@ -142,8 +73,10 @@ static int next(grib_iterator* iter, double* lat, double* lon, double* val) #define RAD2DEG 57.29577951308232087684 /* 180 over pi */ #define DEG2RAD 0.01745329251994329576 /* pi over 180 */ -static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) +int grib_iterator_space_view_t::init(grib_handle* h, grib_arguments* args) { + grib_iterator_gen_t::init(h, args); + /* REFERENCE: * LRIT/HRIT Global Specification (CGMS 03, Issue 2.6, 12.08.1999) */ @@ -163,31 +96,29 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) double factor_1, factor_2, tmp1, Sd, Sn, Sxy, S1, S2, S3; int x0, y0, ix, iy; double *s_x, *c_x; /* arrays storing sin and cos values */ - size_t array_size = (iter->nv * sizeof(double)); + size_t array_size = (nv_ * sizeof(double)); - grib_iterator_space_view* self = (grib_iterator_space_view*)iter; + const char* sradius = grib_arguments_get_name(h, args, carg_++); + const char* sEarthIsOblate = grib_arguments_get_name(h, args, carg_++); + const char* sMajorAxisInMetres = grib_arguments_get_name(h, args, carg_++); + const char* sMinorAxisInMetres = grib_arguments_get_name(h, args, carg_++); + const char* snx = grib_arguments_get_name(h, args, carg_++); + const char* sny = grib_arguments_get_name(h, args, carg_++); + const char* sLatOfSubSatellitePointInDegrees = grib_arguments_get_name(h, args, carg_++); + const char* sLonOfSubSatellitePointInDegrees = grib_arguments_get_name(h, args, carg_++); + const char* sDx = grib_arguments_get_name(h, args, carg_++); + const char* sDy = grib_arguments_get_name(h, args, carg_++); + const char* sXpInGridLengths = grib_arguments_get_name(h, args, carg_++); + const char* sYpInGridLengths = grib_arguments_get_name(h, args, carg_++); + const char* sOrientationInDegrees = grib_arguments_get_name(h, args, carg_++); + const char* sNrInRadiusOfEarthScaled = grib_arguments_get_name(h, args, carg_++); + const char* sXo = grib_arguments_get_name(h, args, carg_++); + const char* sYo = grib_arguments_get_name(h, args, carg_++); - const char* sradius = grib_arguments_get_name(h, args, self->carg++); - const char* sEarthIsOblate = grib_arguments_get_name(h, args, self->carg++); - const char* sMajorAxisInMetres = grib_arguments_get_name(h, args, self->carg++); - const char* sMinorAxisInMetres = grib_arguments_get_name(h, args, self->carg++); - const char* snx = grib_arguments_get_name(h, args, self->carg++); - const char* sny = grib_arguments_get_name(h, args, self->carg++); - const char* sLatOfSubSatellitePointInDegrees = grib_arguments_get_name(h, args, self->carg++); - const char* sLonOfSubSatellitePointInDegrees = grib_arguments_get_name(h, args, self->carg++); - const char* sDx = grib_arguments_get_name(h, args, self->carg++); - const char* sDy = grib_arguments_get_name(h, args, self->carg++); - const char* sXpInGridLengths = grib_arguments_get_name(h, args, self->carg++); - const char* sYpInGridLengths = grib_arguments_get_name(h, args, self->carg++); - const char* sOrientationInDegrees = grib_arguments_get_name(h, args, self->carg++); - const char* sNrInRadiusOfEarthScaled = grib_arguments_get_name(h, args, self->carg++); - const char* sXo = grib_arguments_get_name(h, args, self->carg++); - const char* sYo = grib_arguments_get_name(h, args, self->carg++); - - const char* siScansNegatively = grib_arguments_get_name(h, args, self->carg++); - const char* sjScansPositively = grib_arguments_get_name(h, args, self->carg++); - const char* sjPointsAreConsecutive = grib_arguments_get_name(h, args, self->carg++); - const char* sAlternativeRowScanning = grib_arguments_get_name(h, args, self->carg++); + const char* siScansNegatively = grib_arguments_get_name(h, args, carg_++); + const char* sjScansPositively = grib_arguments_get_name(h, args, carg_++); + const char* sjPointsAreConsecutive = grib_arguments_get_name(h, args, carg_++); + const char* sAlternativeRowScanning = grib_arguments_get_name(h, args, carg_++); if ((ret = grib_get_long_internal(h, snx, &nx)) != GRIB_SUCCESS) return ret; @@ -207,8 +138,8 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) return ret; } - if (iter->nv != nx * ny) { - grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Wrong number of points (%zu!=%ldx%ld)", ITER, iter->nv, nx, ny); + if (nv_ != nx * ny) { + grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Wrong number of points (%zu!=%ldx%ld)", ITER, nv_, nx, ny); return GRIB_WRONG_GRID; } if ((ret = grib_get_double_internal(h, sLatOfSubSatellitePointInDegrees, &latOfSubSatellitePointInDegrees)) != GRIB_SUCCESS) @@ -288,18 +219,18 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) rx = angular_size / dx; ry = (r_pol / r_eq) * angular_size / dy; - self->lats = (double*)grib_context_malloc(h->context, array_size); - if (!self->lats) { + lats_ = (double*)grib_context_malloc(h->context, array_size); + if (!lats_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, array_size); return GRIB_OUT_OF_MEMORY; } - self->lons = (double*)grib_context_malloc(h->context, array_size); - if (!self->lats) { + lons_ = (double*)grib_context_malloc(h->context, array_size); + if (!lats_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, array_size); return GRIB_OUT_OF_MEMORY; } - lats = self->lats; - lons = self->lons; + lats = lats_; + lons = lons_; if (!iScansNegatively) { xp = xp - x0; @@ -373,17 +304,18 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) } grib_context_free(h->context, s_x); grib_context_free(h->context, c_x); - iter->e = -1; + e_ = -1; return ret; } -static int destroy(grib_iterator* iter) +int grib_iterator_space_view_t::destroy() { - grib_iterator_space_view* self = (grib_iterator_space_view*)iter; - const grib_context* c = iter->h->context; + const grib_context* c = h_->context; - grib_context_free(c, self->lats); - grib_context_free(c, self->lons); + grib_context_free(c, lats_); + grib_context_free(c, lons_); + + grib_iterator_gen_t::destroy(); return GRIB_SUCCESS; } diff --git a/src/iterator/grib_iterator_class_space_view.h b/src/iterator/grib_iterator_class_space_view.h new file mode 100644 index 000000000..b116f4ee1 --- /dev/null +++ b/src/iterator/grib_iterator_class_space_view.h @@ -0,0 +1,32 @@ +/* + * (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. + */ + +#pragma once + +#include "grib_iterator_class_gen.h" + +class grib_iterator_space_view_t : public grib_iterator_gen_t +{ +public: + grib_iterator_space_view_t() + { + class_name_ = "space_view"; + } + + int init(grib_handle*, grib_arguments*) override; + int next(double *, double *, double *) override; + int destroy() override; + +private: + double* lats_; + double* lons_; + long Nj_; +}; + From 2122cd6ef95476b625da86fcfb0d8c583fad4b5b Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Tue, 24 Sep 2024 11:04:11 +0200 Subject: [PATCH 02/20] Modernisation: Introduce namespaces and change class names --- src/iterator/grib_iterator.h | 10 ++++- src/iterator/grib_iterator_class_gaussian.cc | 17 +++++--- src/iterator/grib_iterator_class_gaussian.h | 16 ++++++-- .../grib_iterator_class_gaussian_reduced.cc | 25 +++++++---- .../grib_iterator_class_gaussian_reduced.h | 12 +++++- src/iterator/grib_iterator_class_gen.cc | 41 ++++++++++++++----- src/iterator/grib_iterator_class_gen.h | 14 +++++-- src/iterator/grib_iterator_class_healpix.cc | 31 +++++++++----- src/iterator/grib_iterator_class_healpix.h | 12 +++++- ...ator_class_lambert_azimuthal_equal_area.cc | 27 +++++++----- ...rator_class_lambert_azimuthal_equal_area.h | 12 +++++- .../grib_iterator_class_lambert_conformal.cc | 26 ++++++++---- .../grib_iterator_class_lambert_conformal.h | 12 +++++- src/iterator/grib_iterator_class_latlon.cc | 18 +++++--- src/iterator/grib_iterator_class_latlon.h | 12 +++++- .../grib_iterator_class_latlon_reduced.cc | 23 +++++++---- .../grib_iterator_class_latlon_reduced.h | 12 +++++- src/iterator/grib_iterator_class_mercator.cc | 25 +++++++---- src/iterator/grib_iterator_class_mercator.h | 11 ++++- ...grib_iterator_class_polar_stereographic.cc | 23 +++++++---- .../grib_iterator_class_polar_stereographic.h | 11 ++++- src/iterator/grib_iterator_class_regular.cc | 25 +++++++---- src/iterator/grib_iterator_class_regular.h | 12 +++++- .../grib_iterator_class_space_view.cc | 23 +++++++---- src/iterator/grib_iterator_class_space_view.h | 11 ++++- 25 files changed, 332 insertions(+), 129 deletions(-) diff --git a/src/iterator/grib_iterator.h b/src/iterator/grib_iterator.h index 3307eabf4..d9949072e 100644 --- a/src/iterator/grib_iterator.h +++ b/src/iterator/grib_iterator.h @@ -16,7 +16,11 @@ \ingroup grib_iterator */ -class grib_iterator +namespace eccodes { +namespace grib { +namespace geo { + +class Iterator { public: virtual int init(grib_handle*, grib_arguments*) = 0; @@ -38,3 +42,7 @@ public: size_t size_; int inited_; }; + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_gaussian.cc b/src/iterator/grib_iterator_class_gaussian.cc index 356f9fb5f..340329772 100644 --- a/src/iterator/grib_iterator_class_gaussian.cc +++ b/src/iterator/grib_iterator_class_gaussian.cc @@ -9,16 +9,19 @@ */ #include "grib_iterator_class_gaussian.h" -#include -grib_iterator_gaussian_t _grib_iterator_gaussian{}; -grib_iterator* grib_iterator_gaussian = &_grib_iterator_gaussian; +namespace eccodes { +namespace grib { +namespace geo { + +Gaussian _grib_iterator_gaussian{}; +Iterator* grib_iterator_gaussian = &_grib_iterator_gaussian; static void binary_search_gaussian_latitudes(const double xx[], const unsigned long n, double x, long* j); -int grib_iterator_gaussian_t::init(grib_handle* h, grib_arguments* args) +int Gaussian::init(grib_handle* h, grib_arguments* args) { - grib_iterator_regular_t::init(h, args); + Regular::init(h, args); double* lats; double laf; /* latitude of first point in degrees */ @@ -144,3 +147,7 @@ static void binary_search_gaussian_latitudes(const double array[], const unsigne // } // *j = jl; // } + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_gaussian.h b/src/iterator/grib_iterator_class_gaussian.h index 184c7f83d..2efb3fff3 100644 --- a/src/iterator/grib_iterator_class_gaussian.h +++ b/src/iterator/grib_iterator_class_gaussian.h @@ -8,14 +8,24 @@ * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. */ +#pragma once + #include "grib_iterator_class_regular.h" -class grib_iterator_gaussian_t : public grib_iterator_regular_t +namespace eccodes { +namespace grib { +namespace geo { + +class Gaussian : public Regular { public: - grib_iterator_gaussian_t() : grib_iterator_regular_t() + Gaussian() : Regular() { class_name_ = "gaussian"; } - int init(grib_handle*, grib_arguments*); + int init(grib_handle*, grib_arguments*) override; }; + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_gaussian_reduced.cc b/src/iterator/grib_iterator_class_gaussian_reduced.cc index 21a752afb..6cc1b5e0a 100644 --- a/src/iterator/grib_iterator_class_gaussian_reduced.cc +++ b/src/iterator/grib_iterator_class_gaussian_reduced.cc @@ -11,13 +11,16 @@ #include "grib_iterator_class_gaussian_reduced.h" #include +namespace eccodes { +namespace grib { +namespace geo { -grib_iterator_gaussian_reduced_t _grib_iterator_gaussian_reduced{}; -grib_iterator* grib_iterator_gaussian_reduced = &_grib_iterator_gaussian_reduced; +GaussianReduced _grib_iterator_gaussian_reduced{}; +Iterator* grib_iterator_gaussian_reduced = &_grib_iterator_gaussian_reduced; #define ITER "Reduced Gaussian grid Geoiterator" -int grib_iterator_gaussian_reduced_t::next(double* lat, double* lon, double* val) +int GaussianReduced::next(double* lat, double* lon, double* val) { double ret_lat=0, ret_lon=0; @@ -94,7 +97,7 @@ static void binary_search(const double xx[], const unsigned long n, double x, lo } /* Use legacy way to compute the iterator latitude/longitude values */ -int grib_iterator_gaussian_reduced_t::iterate_reduced_gaussian_subarea_legacy(grib_handle* h, +int GaussianReduced::iterate_reduced_gaussian_subarea_legacy(grib_handle* h, double lat_first, double lon_first, double lat_last, double lon_last, double* lats, long* pl, size_t plsize) @@ -156,7 +159,7 @@ int grib_iterator_gaussian_reduced_t::iterate_reduced_gaussian_subarea_legacy(gr // err = iterate_reduced_gaussian_subarea_algorithm2(iter, h, lat_first, lon_first, lat_last, lon_last, lats, pl, plsize); // } // return err; -int grib_iterator_gaussian_reduced_t::iterate_reduced_gaussian_subarea(grib_handle* h, +int GaussianReduced::iterate_reduced_gaussian_subarea(grib_handle* h, double lat_first, double lon_first, double lat_last, double lon_last, double* lats, long* pl, size_t plsize, size_t numlats) @@ -220,9 +223,9 @@ int grib_iterator_gaussian_reduced_t::iterate_reduced_gaussian_subarea(grib_hand return err; } -int grib_iterator_gaussian_reduced_t::init(grib_handle* h, grib_arguments* args) +int GaussianReduced::init(grib_handle* h, grib_arguments* args) { - grib_iterator_gen_t::init(h, args); + Gen::init(h, args); int ret = GRIB_SUCCESS, j, is_global = 0; double lat_first = 0, lon_first = 0, lat_last = 0, lon_last = 0; @@ -361,13 +364,17 @@ finalise: return ret; } -int grib_iterator_gaussian_reduced_t::destroy() +int GaussianReduced::destroy() { const grib_context* c = h_->context; grib_context_free(c, las_); grib_context_free(c, los_); - grib_iterator_gen_t::destroy(); + Gen::destroy(); return GRIB_SUCCESS; } + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_gaussian_reduced.h b/src/iterator/grib_iterator_class_gaussian_reduced.h index 3580be66f..547181f45 100644 --- a/src/iterator/grib_iterator_class_gaussian_reduced.h +++ b/src/iterator/grib_iterator_class_gaussian_reduced.h @@ -12,10 +12,14 @@ #include "grib_iterator_class_gen.h" -class grib_iterator_gaussian_reduced_t : public grib_iterator_gen_t +namespace eccodes { +namespace grib { +namespace geo { + +class GaussianReduced : public Gen { public: - grib_iterator_gaussian_reduced_t() : grib_iterator_gen_t() + GaussianReduced() : Gen() { class_name_ = "gaussian_reduced"; } @@ -44,3 +48,7 @@ int iterate_reduced_gaussian_subarea(grib_handle*, double, double, double*, long*, size_t, size_t); }; + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_gen.cc b/src/iterator/grib_iterator_class_gen.cc index 1c0ae0eeb..55589e143 100644 --- a/src/iterator/grib_iterator_class_gen.cc +++ b/src/iterator/grib_iterator_class_gen.cc @@ -10,8 +10,12 @@ #include "grib_iterator_class_gen.h" -//grib_iterator_gen_t _grib_iterator_class_gen{}; -//grib_iterator* grib_iterator_class_gen = &_grib_iterator_class_gen; +namespace eccodes { +namespace grib { +namespace geo { + +//Gen _grib_iterator_class_gen{}; +//Iterator* grib_iterator_class_gen = &_grib_iterator_class_gen; /* @@ -110,9 +114,9 @@ int transform_iterator_data(grib_context* context, double* data, return GRIB_SUCCESS; } -int grib_iterator_gen_t::init(grib_handle* h, grib_arguments* args) +int Gen::init(grib_handle* h, grib_arguments* args) { - grib_iterator::init(h, args); + Iterator::init(h, args); size_t dli = 0; int err = GRIB_SUCCESS; @@ -169,22 +173,22 @@ int grib_iterator_gen_t::init(grib_handle* h, grib_arguments* args) return err; } -int grib_iterator_gen_t::reset() +int Gen::reset() { e_ = -1; return 0; } -int grib_iterator_gen_t::destroy() +int Gen::destroy() { const grib_context* c = h_->context; grib_context_free(c, data_); - grib_iterator::destroy(); + Iterator::destroy(); return GRIB_SUCCESS; } -long grib_iterator_gen_t::has_next() +long Gen::has_next() { if (flags_ == 0 && data_ == NULL) return 0; @@ -194,11 +198,28 @@ long grib_iterator_gen_t::has_next() } -int grib_iterator_gen_t::previous(double*, double*, double*) { +int Gen::previous(double*, double*, double*) { return GRIB_NOT_IMPLEMENTED; } -int grib_iterator_gen_t::next(double*, double*, double*) { +int Gen::next(double*, double*, double*) { return GRIB_NOT_IMPLEMENTED; } + +int Gen::get(double* lat, double* lon, double* val) +{ + if (e_ >= (long)(nv_ - 1)) + return GRIB_END_OF_ITERATION; + + e_++; + if (lat) *lat = 0; + if (lon) *lon = 0; + if (val) *val = 0; + + return 1; +} + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_gen.h b/src/iterator/grib_iterator_class_gen.h index 6dc9c9fb3..450d49b9c 100644 --- a/src/iterator/grib_iterator_class_gen.h +++ b/src/iterator/grib_iterator_class_gen.h @@ -12,10 +12,14 @@ #include "grib_iterator.h" -class grib_iterator_gen_t : public grib_iterator +namespace eccodes { +namespace grib { +namespace geo { // TODO(maee): geo + +class Gen : public Iterator { public: - grib_iterator_gen_t() : grib_iterator() + Gen() : Iterator() { class_name_ = "abstract_long_vector"; } @@ -25,9 +29,13 @@ public: int previous(double*, double*, double*) override; int reset() override; int destroy() override; - long has_next() override; + long has_next() override; // TODO(maee/masn): return bool please! public: int carg_; const char* missingValue_; }; + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_healpix.cc b/src/iterator/grib_iterator_class_healpix.cc index abfe3695f..017b9e7a5 100644 --- a/src/iterator/grib_iterator_class_healpix.cc +++ b/src/iterator/grib_iterator_class_healpix.cc @@ -10,6 +10,10 @@ #include "grib_iterator_class_healpix.h" +namespace eccodes { +namespace grib { +namespace geo { + #include #include #include @@ -17,15 +21,16 @@ #include #include -grib_iterator_healpix_t _grib_iterator_healpix; -grib_iterator* grib_iterator_healpix = &_grib_iterator_healpix; +namespace eccodes { +namespace grib { +namespace geo { + +Healpix _grib_iterator_healpix; +Iterator* grib_iterator_healpix = &_grib_iterator_healpix; #define ITER "HEALPix Geoiterator" constexpr double RAD2DEG = 57.29577951308232087684; // 180 over pi -namespace -{ - struct CodecFijNest { static constexpr uint64_t __masks[] = { 0x00000000ffffffff, 0x0000ffff0000ffff, 0x00ff00ff00ff00ff, @@ -144,7 +149,7 @@ static std::vector HEALPix_longitudes(size_t N, size_t i) } // anonymous namespace -int grib_iterator_healpix_t::iterate_healpix(long N) +int Healpix::iterate_healpix(long N) { size_t Ny = 4 * static_cast(N) - 1; auto Nd = static_cast(N); @@ -265,9 +270,9 @@ int grib_iterator_healpix_t::iterate_healpix(long N) return GRIB_SUCCESS; } -int grib_iterator_healpix_t::init(grib_handle* h, grib_arguments* args) +int Healpix::init(grib_handle* h, grib_arguments* args) { - grib_iterator_gen_t::init(h, args); + Gen::init(h, args); int err = 0; @@ -332,7 +337,7 @@ int grib_iterator_healpix_t::init(grib_handle* h, grib_arguments* args) return err; } -int grib_iterator_healpix_t::next(double* lat, double* lon, double* val) +int Healpix::next(double* lat, double* lon, double* val) { if (e_ >= static_cast(nv_ - 1)) { return 0; @@ -348,11 +353,15 @@ int grib_iterator_healpix_t::next(double* lat, double* lon, double* val) return 1; } -int grib_iterator_healpix_t::destroy() +int Healpix::destroy() { grib_context_free(context_, lats_); grib_context_free(context_, lons_); - grib_iterator_gen_t::destroy(); + Gen::destroy(); return GRIB_SUCCESS; } + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_healpix.h b/src/iterator/grib_iterator_class_healpix.h index b179b27d7..008ac11f9 100644 --- a/src/iterator/grib_iterator_class_healpix.h +++ b/src/iterator/grib_iterator_class_healpix.h @@ -12,10 +12,14 @@ #include "grib_iterator_class_gen.h" -class grib_iterator_healpix_t : public grib_iterator_gen_t +namespace eccodes { +namespace grib { +namespace geo { + +class Healpix : public Gen { public: - grib_iterator_healpix_t() : grib_iterator_gen_t() + Healpix() : Gen() { class_name_ = "healpix"; } @@ -33,3 +37,7 @@ public: private: int iterate_healpix(long N); }; + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc index daf28758c..07fa2fcb1 100644 --- a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc +++ b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc @@ -9,16 +9,17 @@ */ #include "grib_iterator_class_lambert_azimuthal_equal_area.h" -#include +namespace eccodes { +namespace grib { +namespace geo { -grib_iterator_lambert_azimuthal_equal_area_t _grib_iterator_lambert_azimuthal_equal_area{}; -grib_iterator* grib_iterator_lambert_azimuthal_equal_area = &_grib_iterator_lambert_azimuthal_equal_area; - +LambertAzimuthalEqualArea _grib_iterator_lambert_azimuthal_equal_area{}; +Iterator* grib_iterator_lambert_azimuthal_equal_area = &_grib_iterator_lambert_azimuthal_equal_area; #define ITER "Lambert azimuthal equal area Geoiterator" -int grib_iterator_lambert_azimuthal_equal_area_t::next(double* lat, double* lon, double* val) +int LambertAzimuthalEqualArea::next(double* lat, double* lon, double* val) { if ((long)e_ >= (long)(nv_ - 1)) return 0; @@ -94,7 +95,7 @@ static double pj_qsfn(double sinphi, double e, double one_es) #define EPS10 1.e-10 static int init_oblate(grib_handle* h, - grib_iterator_lambert_azimuthal_equal_area_t* self, + LambertAzimuthalEqualArea* self, size_t nv, long nx, long ny, double Dx, double Dy, double earthMinorAxisInMetres, double earthMajorAxisInMetres, double latFirstInRadians, double lonFirstInRadians, @@ -225,7 +226,7 @@ static int init_oblate(grib_handle* h, } static int init_sphere(grib_handle* h, - grib_iterator_lambert_azimuthal_equal_area_t* self, + LambertAzimuthalEqualArea* self, size_t nv, long nx, long ny, double Dx, double Dy, double radius, double latFirstInRadians, double lonFirstInRadians, @@ -331,9 +332,9 @@ static int init_sphere(grib_handle* h, return GRIB_SUCCESS; } -int grib_iterator_lambert_azimuthal_equal_area_t::init(grib_handle* h, grib_arguments* args) +int LambertAzimuthalEqualArea::init(grib_handle* h, grib_arguments* args) { - grib_iterator_gen_t::init(h, args); + Gen::init(h, args); int err = 0; int is_oblate = 0; @@ -426,13 +427,17 @@ int grib_iterator_lambert_azimuthal_equal_area_t::init(grib_handle* h, grib_argu return GRIB_SUCCESS; } -int grib_iterator_lambert_azimuthal_equal_area_t::destroy() +int LambertAzimuthalEqualArea::destroy() { const grib_context* c = h_->context; grib_context_free(c, lats_); grib_context_free(c, lons_); - grib_iterator_gen_t::destroy(); + Gen::destroy(); return GRIB_SUCCESS; } + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h index 06e866dc5..694a93e67 100644 --- a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h +++ b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h @@ -12,10 +12,14 @@ #include "grib_iterator_class_gen.h" -class grib_iterator_lambert_azimuthal_equal_area_t : public grib_iterator_gen_t +namespace eccodes { +namespace grib { +namespace geo { + +class LambertAzimuthalEqualArea : public Gen { public: - grib_iterator_lambert_azimuthal_equal_area_t() : grib_iterator_gen_t() + LambertAzimuthalEqualArea() : Gen() { class_name_ = "lambert_azimuthal_equal_area"; } @@ -30,3 +34,7 @@ public: double *lons_; long Nj_; }; + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_lambert_conformal.cc b/src/iterator/grib_iterator_class_lambert_conformal.cc index 6b44a3d98..9da401312 100644 --- a/src/iterator/grib_iterator_class_lambert_conformal.cc +++ b/src/iterator/grib_iterator_class_lambert_conformal.cc @@ -11,8 +11,12 @@ #include "grib_iterator_class_lambert_conformal.h" #include -grib_iterator_lambert_conformal_t _grib_iterator_lambert_conformal{}; -grib_iterator* grib_iterator_lambert_conformal = &_grib_iterator_lambert_conformal; +namespace eccodes { +namespace grib { +namespace geo { + +LambertConformal _grib_iterator_lambert_conformal{}; +Iterator* grib_iterator_lambert_conformal = &_grib_iterator_lambert_conformal; #define ITER "Lambert conformal Geoiterator" #define EPSILON 1.0e-10 @@ -121,7 +125,7 @@ static void xy2lonlat(double radius, double n, double f, double rho0_bare, doubl } } -int grib_iterator_lambert_conformal_t::init_sphere(const grib_handle* h, +int LambertConformal::init_sphere(const grib_handle* h, size_t nv, long nx, long ny, double LoVInDegrees, double Dx, double Dy, double radius, @@ -210,7 +214,7 @@ int grib_iterator_lambert_conformal_t::init_sphere(const grib_handle* h, } // Oblate spheroid -int grib_iterator_lambert_conformal_t::init_oblate(const grib_handle* h, +int LambertConformal::init_oblate(const grib_handle* h, size_t nv, long nx, long ny, double LoVInDegrees, double Dx, double Dy, @@ -339,9 +343,9 @@ int grib_iterator_lambert_conformal_t::init_oblate(const grib_handle* h, return GRIB_SUCCESS; } -int grib_iterator_lambert_conformal_t::init(grib_handle* h, grib_arguments* args) +int LambertConformal::init(grib_handle* h, grib_arguments* args) { - grib_iterator_gen_t::init(h, args); + Gen::init(h, args); int err = 0, is_oblate = 0; long nx, ny, iScansNegatively, jScansPositively, jPointsAreConsecutive, alternativeRowScanning; @@ -452,7 +456,7 @@ int grib_iterator_lambert_conformal_t::init(grib_handle* h, grib_arguments* args return err; } -int grib_iterator_lambert_conformal_t::next(double* lat, double* lon, double* val) +int LambertConformal::next(double* lat, double* lon, double* val) { if ((long)e_ >= (long)(nv_ - 1)) return 0; @@ -466,13 +470,17 @@ int grib_iterator_lambert_conformal_t::next(double* lat, double* lon, double* va return 1; } -int grib_iterator_lambert_conformal_t::destroy() +int LambertConformal::destroy() { const grib_context* c = h_->context; grib_context_free(c, lats_); grib_context_free(c, lons_); - grib_iterator_gen_t::destroy(); + Gen::destroy(); return GRIB_SUCCESS; } + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_lambert_conformal.h b/src/iterator/grib_iterator_class_lambert_conformal.h index ffd8baa59..d02380c5d 100644 --- a/src/iterator/grib_iterator_class_lambert_conformal.h +++ b/src/iterator/grib_iterator_class_lambert_conformal.h @@ -12,10 +12,14 @@ #include "grib_iterator_class_gen.h" -class grib_iterator_lambert_conformal_t : public grib_iterator_gen_t +namespace eccodes { +namespace grib { +namespace geo { + +class LambertConformal : public Gen { public: - grib_iterator_lambert_conformal_t() : grib_iterator_gen_t() + LambertConformal() : Gen() { class_name_ = "lambert_conformal"; } @@ -46,3 +50,7 @@ private: double, double, double, double); }; + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_latlon.cc b/src/iterator/grib_iterator_class_latlon.cc index f33c5111e..9f697a511 100644 --- a/src/iterator/grib_iterator_class_latlon.cc +++ b/src/iterator/grib_iterator_class_latlon.cc @@ -10,11 +10,15 @@ #include "grib_iterator_class_latlon.h" -grib_iterator_latlon_t _grib_iterator_latlon{}; -grib_iterator* grib_iterator_latlon = &_grib_iterator_latlon; +namespace eccodes { +namespace grib { +namespace geo { + +Latlon _grib_iterator_latlon{}; +Iterator* grib_iterator_latlon = &_grib_iterator_latlon; -int grib_iterator_latlon_t::next(double* lat, double* lon, double* val) +int Latlon::next(double* lat, double* lon, double* val) { /* GRIB-238: Support rotated lat/lon grids */ double ret_lat, ret_lon, ret_val=0; @@ -59,9 +63,9 @@ int grib_iterator_latlon_t::next(double* lat, double* lon, double* val) return 1; } -int grib_iterator_latlon_t::init(grib_handle* h, grib_arguments* args) +int Latlon::init(grib_handle* h, grib_arguments* args) { - grib_iterator_regular_t::init(h, args); + Regular::init(h, args); int err = 0; double jdir; @@ -150,3 +154,7 @@ int grib_iterator_latlon_t::init(grib_handle* h, grib_arguments* args) e_ = -1; return err; } + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_latlon.h b/src/iterator/grib_iterator_class_latlon.h index 3a3094220..f825f3f53 100644 --- a/src/iterator/grib_iterator_class_latlon.h +++ b/src/iterator/grib_iterator_class_latlon.h @@ -12,10 +12,14 @@ #include "grib_iterator_class_regular.h" -class grib_iterator_latlon_t : public grib_iterator_regular_t +namespace eccodes { +namespace grib { +namespace geo { + +class Latlon : public Regular { public: - grib_iterator_latlon_t() : grib_iterator_regular_t() + Latlon() : Regular() { class_name_ = "latlon"; } @@ -23,3 +27,7 @@ public: int init(grib_handle*, grib_arguments*) override; int next(double*, double*, double*) override; }; + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_latlon_reduced.cc b/src/iterator/grib_iterator_class_latlon_reduced.cc index f2ef96281..0cb0244b6 100644 --- a/src/iterator/grib_iterator_class_latlon_reduced.cc +++ b/src/iterator/grib_iterator_class_latlon_reduced.cc @@ -9,13 +9,16 @@ */ #include "grib_iterator_class_latlon_reduced.h" -#include -grib_iterator_latlon_reduced_t _grib_iterator_latlon_reduced{}; -grib_iterator* grib_iterator_latlon_reduced = &_grib_iterator_latlon_reduced; +namespace eccodes { +namespace grib { +namespace geo { + +LatlonReduced _grib_iterator_latlon_reduced{}; +Iterator* grib_iterator_latlon_reduced = &_grib_iterator_latlon_reduced; -int grib_iterator_latlon_reduced_t::next(double* lat, double* lon, double* val) +int LatlonReduced::next(double* lat, double* lon, double* val) { if ((long)e_ >= (long)(nv_ - 1)) return 0; @@ -29,9 +32,9 @@ int grib_iterator_latlon_reduced_t::next(double* lat, double* lon, double* val) return 1; } -int grib_iterator_latlon_reduced_t::init(grib_handle* h, grib_arguments* args) +int LatlonReduced::init(grib_handle* h, grib_arguments* args) { - grib_iterator_gen_t::init(h, args); + Gen::init(h, args); int ret = GRIB_SUCCESS; double laf; @@ -127,13 +130,17 @@ int grib_iterator_latlon_reduced_t::init(grib_handle* h, grib_arguments* args) return ret; } -int grib_iterator_latlon_reduced_t::destroy() +int LatlonReduced::destroy() { const grib_context* c = h_->context; grib_context_free(c, las_); grib_context_free(c, los_); - grib_iterator_gen_t::destroy(); + Gen::destroy(); return GRIB_SUCCESS; } + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_latlon_reduced.h b/src/iterator/grib_iterator_class_latlon_reduced.h index 57511595b..de114c6d5 100644 --- a/src/iterator/grib_iterator_class_latlon_reduced.h +++ b/src/iterator/grib_iterator_class_latlon_reduced.h @@ -12,10 +12,14 @@ #include "grib_iterator_class_gen.h" -class grib_iterator_latlon_reduced_t : public grib_iterator_gen_t +namespace eccodes { +namespace grib { +namespace geo { + +class LatlonReduced : public Gen { public: - grib_iterator_latlon_reduced_t() : grib_iterator_gen_t() + LatlonReduced() : Gen() { class_name_ = "latlon_reduced"; } @@ -28,3 +32,7 @@ private: double* las_; double* los_; }; + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_mercator.cc b/src/iterator/grib_iterator_class_mercator.cc index e31b2b059..c4642ff9d 100644 --- a/src/iterator/grib_iterator_class_mercator.cc +++ b/src/iterator/grib_iterator_class_mercator.cc @@ -9,10 +9,13 @@ */ #include "grib_iterator_class_mercator.h" -#include -grib_iterator_mercator_t _grib_iterator_mercator{}; -grib_iterator* grib_iterator_mercator = &_grib_iterator_mercator; +namespace eccodes { +namespace grib { +namespace geo { + +Mercator _grib_iterator_mercator{}; +Iterator* grib_iterator_mercator = &_grib_iterator_mercator; #define ITER "Mercator Geoiterator" #define EPSILON 1.0e-10 @@ -81,7 +84,7 @@ static double compute_t( return (tan(0.5 * (M_PI_2 - phi)) / con); } -int grib_iterator_mercator_t::init_mercator(grib_handle* h, +int Mercator::init_mercator(grib_handle* h, size_t nv, long nx, long ny, double DiInMetres, double DjInMetres, double earthMinorAxisInMetres, double earthMajorAxisInMetres, @@ -160,9 +163,9 @@ int grib_iterator_mercator_t::init_mercator(grib_handle* h, return GRIB_SUCCESS; } -int grib_iterator_mercator_t::init(grib_handle* h, grib_arguments* args) +int Mercator::init(grib_handle* h, grib_arguments* args) { - grib_iterator_gen_t::init(h, args); + Gen::init(h, args); int err = 0; long ni, nj, iScansNegatively, jScansPositively, jPointsAreConsecutive, alternativeRowScanning; @@ -255,7 +258,7 @@ int grib_iterator_mercator_t::init(grib_handle* h, grib_arguments* args) return err; } -int grib_iterator_mercator_t::next(double* lat, double* lon, double* val) +int Mercator::next(double* lat, double* lon, double* val) { if ((long)e_ >= (long)(nv_ - 1)) return 0; @@ -269,13 +272,17 @@ int grib_iterator_mercator_t::next(double* lat, double* lon, double* val) return 1; } -int grib_iterator_mercator_t::destroy() +int Mercator::destroy() { const grib_context* c = h_->context; grib_context_free(c, lats_); grib_context_free(c, lons_); - grib_iterator_gen_t::destroy(); + Gen::destroy(); return GRIB_SUCCESS; } + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_mercator.h b/src/iterator/grib_iterator_class_mercator.h index 61ca4dd51..ea636c516 100644 --- a/src/iterator/grib_iterator_class_mercator.h +++ b/src/iterator/grib_iterator_class_mercator.h @@ -12,10 +12,14 @@ #include "grib_iterator_class_gen.h" -class grib_iterator_mercator_t : public grib_iterator_gen_t +namespace eccodes { +namespace grib { +namespace geo { + +class Mercator : public Gen { public: - grib_iterator_mercator_t() { + Mercator() { class_name_ = "mercator"; } int init(grib_handle*,grib_arguments*) override; @@ -36,3 +40,6 @@ private: double, double); }; +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_polar_stereographic.cc b/src/iterator/grib_iterator_class_polar_stereographic.cc index a0fccafc5..2da36a8c2 100644 --- a/src/iterator/grib_iterator_class_polar_stereographic.cc +++ b/src/iterator/grib_iterator_class_polar_stereographic.cc @@ -9,14 +9,17 @@ */ #include "grib_iterator_class_polar_stereographic.h" -#include -grib_iterator_polar_stereographic_t _grib_iterator_polar_stereographic{}; -grib_iterator* grib_iterator_polar_stereographic = &_grib_iterator_polar_stereographic; +namespace eccodes { +namespace grib { +namespace geo { + +PolarStereographic _grib_iterator_polar_stereographic{}; +Iterator* grib_iterator_polar_stereographic = &_grib_iterator_polar_stereographic; #define ITER "Polar stereographic Geoiterator" -int grib_iterator_polar_stereographic_t::next(double* lat, double* lon, double* val) +int PolarStereographic::next(double* lat, double* lon, double* val) { if ((long)e_ >= (long)(nv_ - 1)) return 0; @@ -48,9 +51,9 @@ typedef struct proj_data_t #define PI_OVER_2 1.5707963267948966 /* half pi */ #define EPSILON 1.0e-10 -int grib_iterator_polar_stereographic_t::init(grib_handle* h, grib_arguments* args) +int PolarStereographic::init(grib_handle* h, grib_arguments* args) { - grib_iterator_gen_t::init(h, args); + Gen::init(h, args); int ret = 0; double *lats, *lons; /* arrays for latitudes and longitudes */ @@ -294,13 +297,17 @@ int grib_iterator_polar_stereographic_t::init(grib_handle* h, grib_arguments* ar return ret; } -int grib_iterator_polar_stereographic_t::destroy() +int PolarStereographic::destroy() { const grib_context* c = h_->context; grib_context_free(c, lats_); grib_context_free(c, lons_); - grib_iterator_gen_t::destroy(); + Gen::destroy(); return GRIB_SUCCESS; } + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_polar_stereographic.h b/src/iterator/grib_iterator_class_polar_stereographic.h index cbc15b4a8..6920dfd40 100644 --- a/src/iterator/grib_iterator_class_polar_stereographic.h +++ b/src/iterator/grib_iterator_class_polar_stereographic.h @@ -12,10 +12,14 @@ #include "grib_iterator_class_gen.h" -class grib_iterator_polar_stereographic_t : public grib_iterator_gen_t +namespace eccodes { +namespace grib { +namespace geo { + +class PolarStereographic : public Gen { public: - grib_iterator_polar_stereographic_t() : grib_iterator_gen_t() + PolarStereographic() : Gen() { class_name_ = "polar_stereographic"; } @@ -30,3 +34,6 @@ private: long Nj_; }; +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_regular.cc b/src/iterator/grib_iterator_class_regular.cc index 334dab9a0..1dec29155 100644 --- a/src/iterator/grib_iterator_class_regular.cc +++ b/src/iterator/grib_iterator_class_regular.cc @@ -9,14 +9,17 @@ */ #include "grib_iterator_class_regular.h" -#include -grib_iterator_regular_t _grib_iterator_regular{}; -grib_iterator_regular_t* grib_iterator_regular = &_grib_iterator_regular; +namespace eccodes { +namespace grib { +namespace geo { + +Regular _grib_iterator_regular{}; +Iterator* grib_iterator_regular = &_grib_iterator_regular; #define ITER "Regular grid Geoiterator" -int grib_iterator_regular_t::next(double* lat, double* lon, double* val) +int Regular::next(double* lat, double* lon, double* val) { if ((long)e_ >= (long)(nv_ - 1)) return 0; @@ -31,7 +34,7 @@ int grib_iterator_regular_t::next(double* lat, double* lon, double* val) return 1; } -int grib_iterator_regular_t::previous(double* lat, double* lon, double* val) +int Regular::previous(double* lat, double* lon, double* val) { if (e_ < 0) return 0; @@ -45,19 +48,19 @@ int grib_iterator_regular_t::previous(double* lat, double* lon, double* val) return 1; } -int grib_iterator_regular_t::destroy() +int Regular::destroy() { const grib_context* c = h_->context; grib_context_free(c, las_); grib_context_free(c, los_); - grib_iterator_gen_t::destroy(); + Gen::destroy(); return GRIB_SUCCESS; } -int grib_iterator_regular_t::init(grib_handle* h, grib_arguments* args) +int Regular::init(grib_handle* h, grib_arguments* args) { - grib_iterator_gen_t::init(h, args); + Gen::init(h, args); int ret = GRIB_SUCCESS; @@ -158,3 +161,7 @@ int grib_iterator_regular_t::init(grib_handle* h, grib_arguments* args) return ret; } + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_regular.h b/src/iterator/grib_iterator_class_regular.h index a9b1478a2..77cbb1a00 100644 --- a/src/iterator/grib_iterator_class_regular.h +++ b/src/iterator/grib_iterator_class_regular.h @@ -12,10 +12,14 @@ #include "grib_iterator_class_gen.h" -class grib_iterator_regular_t : public grib_iterator_gen_t +namespace eccodes { +namespace grib { +namespace geo { + +class Regular : public Gen { public: - grib_iterator_regular_t() + Regular() { class_name_ = "regular"; } @@ -38,3 +42,7 @@ protected: long jPointsAreConsecutive_; long disableUnrotate_; }; + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_space_view.cc b/src/iterator/grib_iterator_class_space_view.cc index fbfed04a9..1badd7d8a 100644 --- a/src/iterator/grib_iterator_class_space_view.cc +++ b/src/iterator/grib_iterator_class_space_view.cc @@ -9,14 +9,17 @@ */ #include "grib_iterator_class_space_view.h" -#include -grib_iterator_space_view_t _grib_iterator_space_view{}; -grib_iterator* grib_iterator_space_view = &_grib_iterator_space_view; +namespace eccodes { +namespace grib { +namespace geo { + +SpaceView _grib_iterator_space_view{}; +Iterator* grib_iterator_space_view = &_grib_iterator_space_view; #define ITER "Space view Geoiterator" -int grib_iterator_space_view_t::next(double* lat, double* lon, double* val) +int SpaceView::next(double* lat, double* lon, double* val) { if ((long)e_ >= (long)(nv_ - 1)) return 0; @@ -73,9 +76,9 @@ int grib_iterator_space_view_t::next(double* lat, double* lon, double* val) #define RAD2DEG 57.29577951308232087684 /* 180 over pi */ #define DEG2RAD 0.01745329251994329576 /* pi over 180 */ -int grib_iterator_space_view_t::init(grib_handle* h, grib_arguments* args) +int SpaceView::init(grib_handle* h, grib_arguments* args) { - grib_iterator_gen_t::init(h, args); + Gen::init(h, args); /* REFERENCE: * LRIT/HRIT Global Specification (CGMS 03, Issue 2.6, 12.08.1999) @@ -309,13 +312,17 @@ int grib_iterator_space_view_t::init(grib_handle* h, grib_arguments* args) return ret; } -int grib_iterator_space_view_t::destroy() +int SpaceView::destroy() { const grib_context* c = h_->context; grib_context_free(c, lats_); grib_context_free(c, lons_); - grib_iterator_gen_t::destroy(); + Gen::destroy(); return GRIB_SUCCESS; } + +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_space_view.h b/src/iterator/grib_iterator_class_space_view.h index b116f4ee1..8af34d017 100644 --- a/src/iterator/grib_iterator_class_space_view.h +++ b/src/iterator/grib_iterator_class_space_view.h @@ -12,10 +12,14 @@ #include "grib_iterator_class_gen.h" -class grib_iterator_space_view_t : public grib_iterator_gen_t +namespace eccodes { +namespace grib { +namespace geo { + +class SpaceView : public Gen { public: - grib_iterator_space_view_t() + SpaceView() { class_name_ = "space_view"; } @@ -30,3 +34,6 @@ private: long Nj_; }; +} // namespace geo +} // namespace grib +} // namespace eccodes From 3940d7d84881a71aabe7669b6a2b2d5902ad83f0 Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Wed, 16 Oct 2024 14:09:07 +0200 Subject: [PATCH 03/20] Modernisation: Iterators (all ctests pass) --- src/accessor/grib_accessor_class_iterator.cc | 32 ---- src/accessor/grib_accessor_class_iterator.h | 5 +- src/eccodes_prototypes.h | 2 +- src/grib_api_internal.h | 9 +- src/grib_iterator_class.cc | 133 +++++++++++++- src/grib_iterator_class.h | 24 +-- src/grib_iterator_factory.h | 2 +- src/iterator/grib_iterator.cc | 171 ++++++++++++++++++ src/iterator/grib_iterator.h | 4 + src/iterator/grib_iterator_class_gaussian.cc | 11 +- src/iterator/grib_iterator_class_gaussian.h | 7 +- .../grib_iterator_class_gaussian_reduced.cc | 12 +- .../grib_iterator_class_gaussian_reduced.h | 6 +- src/iterator/grib_iterator_class_gen.cc | 133 ++------------ src/iterator/grib_iterator_class_gen.h | 9 +- src/iterator/grib_iterator_class_healpix.cc | 17 +- src/iterator/grib_iterator_class_healpix.h | 6 +- ...ator_class_lambert_azimuthal_equal_area.cc | 11 +- ...rator_class_lambert_azimuthal_equal_area.h | 7 +- .../grib_iterator_class_lambert_conformal.cc | 12 +- .../grib_iterator_class_lambert_conformal.h | 6 +- src/iterator/grib_iterator_class_latlon.cc | 12 +- src/iterator/grib_iterator_class_latlon.h | 6 +- .../grib_iterator_class_latlon_reduced.cc | 12 +- .../grib_iterator_class_latlon_reduced.h | 6 +- src/iterator/grib_iterator_class_mercator.cc | 11 +- src/iterator/grib_iterator_class_mercator.h | 6 +- ...grib_iterator_class_polar_stereographic.cc | 11 +- .../grib_iterator_class_polar_stereographic.h | 6 +- src/iterator/grib_iterator_class_regular.cc | 12 +- src/iterator/grib_iterator_class_regular.h | 6 +- .../grib_iterator_class_space_view.cc | 11 +- src/iterator/grib_iterator_class_space_view.h | 6 +- 33 files changed, 436 insertions(+), 288 deletions(-) create mode 100644 src/iterator/grib_iterator.cc diff --git a/src/accessor/grib_accessor_class_iterator.cc b/src/accessor/grib_accessor_class_iterator.cc index 96e490b62..308dc7da2 100644 --- a/src/accessor/grib_accessor_class_iterator.cc +++ b/src/accessor/grib_accessor_class_iterator.cc @@ -24,35 +24,3 @@ void grib_accessor_iterator_t::dump(grib_dumper* dumper) /* TODO: pass args */ grib_dump_label(dumper, this, NULL); } - -#if defined(HAVE_GEOGRAPHY) -grib_iterator* grib_iterator_new(const grib_handle* ch, unsigned long flags, int* error) -{ - grib_handle* h = (grib_handle*)ch; - grib_accessor* a = NULL; - grib_accessor_iterator_t* ita = NULL; - grib_iterator* iter = NULL; - *error = GRIB_NOT_IMPLEMENTED; - a = grib_find_accessor(h, "ITERATOR"); - ita = (grib_accessor_iterator_t*)a; - - if (!a) - return NULL; - - iter = grib_iterator_factory(h, ita->args_, flags, error); - - if (iter) - *error = GRIB_SUCCESS; - - return iter; -} -#else -grib_iterator* grib_iterator_new(const grib_handle* ch, unsigned long flags, int* error) -{ - *error = GRIB_FUNCTIONALITY_NOT_ENABLED; - grib_context_log(ch->context, GRIB_LOG_ERROR, - "Geoiterator functionality not enabled. Please rebuild with -DENABLE_GEOGRAPHY=ON"); - - return NULL; -} -#endif diff --git a/src/accessor/grib_accessor_class_iterator.h b/src/accessor/grib_accessor_class_iterator.h index 13aadfcd9..1b2186575 100644 --- a/src/accessor/grib_accessor_class_iterator.h +++ b/src/accessor/grib_accessor_class_iterator.h @@ -23,8 +23,5 @@ public: private: grib_arguments* args_; - - friend grib_iterator* grib_iterator_new(const grib_handle* ch, unsigned long flags, int* error); + friend eccodes::grib::geo::Iterator* eccodes::grib::geo::gribIteratorNew(const grib_handle*, unsigned long, int*); }; - -// grib_iterator* grib_iterator_new(const grib_handle* ch, unsigned long flags, int* error) diff --git a/src/eccodes_prototypes.h b/src/eccodes_prototypes.h index a8a24ad33..bd82a7ea2 100644 --- a/src/eccodes_prototypes.h +++ b/src/eccodes_prototypes.h @@ -819,7 +819,7 @@ int grib_get_data(const grib_handle* h, double* lats, double* lons, double* valu //int grib_iterator_delete(grib_iterator* i); /* grib_iterator_class.cc */ -grib_iterator* grib_iterator_factory(grib_handle* h, grib_arguments* args, unsigned long flags, int* error); +eccodes::grib::geo::Iterator* grib_iterator_factory(grib_handle* h, grib_arguments* args, unsigned long flags, int* error); /* grib_iterator_class_gen.cc */ int transform_iterator_data(grib_context* c, double* data, long iScansNegatively, long jScansPositively, long jPointsAreConsecutive, long alternativeRowScanning, size_t numPoints, long nx, long ny); diff --git a/src/grib_api_internal.h b/src/grib_api_internal.h index 453ceee99..9a930a560 100644 --- a/src/grib_api_internal.h +++ b/src/grib_api_internal.h @@ -248,7 +248,14 @@ typedef struct grib_codetable grib_codetable; typedef struct grib_smart_table grib_smart_table; class grib_accessor; -class grib_iterator; +namespace eccodes::grib::geo { +class Iterator; +} + +typedef struct grib_iterator { + eccodes::grib::geo::Iterator* iterator; +} grib_iterator; + typedef struct grib_nearest_class grib_nearest_class; typedef struct grib_dumper grib_dumper; typedef struct grib_dumper_class grib_dumper_class; diff --git a/src/grib_iterator_class.cc b/src/grib_iterator_class.cc index 38614cf1c..7f1bd2b3b 100644 --- a/src/grib_iterator_class.cc +++ b/src/grib_iterator_class.cc @@ -14,6 +14,8 @@ ***************************************************************************/ #include "grib_api_internal.h" +#include "./iterator/grib_iterator.h" +#include "./accessor/grib_accessor_class_iterator.h" /* This file is generated by ./make_class.pl */ #include "grib_iterator_class.h" @@ -21,15 +23,16 @@ struct table_entry { const char* type; - grib_iterator** cclass; + eccodes::grib::geo::Iterator** iterator; }; static const struct table_entry table[] = { -/* This file is generated by ./make_class.pl */ -#include "grib_iterator_factory.h" + /* This file is generated by ./make_class.pl */ + #include "grib_iterator_factory.h" }; -grib_iterator* grib_iterator_factory(grib_handle* h, grib_arguments* args, unsigned long flags, int* error) + +eccodes::grib::geo::Iterator* grib_iterator_factory(grib_handle* h, grib_arguments* args, unsigned long flags, int* error) { size_t i = 0, num_table_entries = 0; const char* type = (char*)grib_arguments_get_name(h, args, 0); @@ -38,15 +41,15 @@ grib_iterator* grib_iterator_factory(grib_handle* h, grib_arguments* args, unsig num_table_entries = sizeof(table) / sizeof(table[0]); for (i = 0; i < num_table_entries; i++) { if (strcmp(type, table[i].type) == 0) { - grib_iterator* c = *(table[i].cclass); - grib_iterator* it = (grib_iterator*)grib_context_malloc_clear(h->context, c->size_); + eccodes::grib::geo::Iterator* builder = *(table[i].iterator); + eccodes::grib::geo::Iterator* it = builder->create(); it->flags_ = flags; *error = it->init(h, args); if (*error == GRIB_SUCCESS) return it; grib_context_log(h->context, GRIB_LOG_ERROR, "Geoiterator factory: Error instantiating iterator %s (%s)", table[i].type, grib_get_error_message(*error)); - grib_iterator_delete(it); + gribIteratorDelete(it); return NULL; } } @@ -55,3 +58,119 @@ grib_iterator* grib_iterator_factory(grib_handle* h, grib_arguments* args, unsig return NULL; } + +int grib_get_data(const grib_handle* h, double* lats, double* lons, double* values) +{ + int err = 0; + eccodes::grib::geo::Iterator* iter = NULL; + double *lat, *lon, *val; + + iter = eccodes::grib::geo::gribIteratorNew(h, 0, &err); + if (!iter || err != GRIB_SUCCESS) + return err; + + lat = lats; + lon = lons; + val = values; + while (iter->next(lat++, lon++, val++)) {} + + gribIteratorDelete(iter); + + return err; +} + +/* + * Return pointer to data at (i,j) (Fortran convention) + */ +static double* pointer_to_data(unsigned int i, unsigned int j, + long iScansNegatively, long jScansPositively, + long jPointsAreConsecutive, long alternativeRowScanning, + unsigned int nx, unsigned int ny, double* data) +{ + /* Regular grid */ + if (nx > 0 && ny > 0) { + if (i >= nx || j >= ny) + return NULL; + j = (jScansPositively) ? j : ny - 1 - j; + i = ((alternativeRowScanning) && (j % 2 == 1)) ? nx - 1 - i : i; + i = (iScansNegatively) ? nx - 1 - i : i; + + return (jPointsAreConsecutive) ? data + j + i * ny : data + i + nx * j; + } + + /* Reduced or other data not on a grid */ + return NULL; +} + +/* Apply the scanning mode flags which may require data array to be transformed + * to standard west-to-east (+i) south-to-north (+j) mode. + * The data array passed in should have 'numPoints' elements. +*/ +int transform_iterator_data(grib_context* context, double* data, + long iScansNegatively, long jScansPositively, + long jPointsAreConsecutive, long alternativeRowScanning, + size_t numPoints, long nx, long ny) +{ + double* data2; + double *pData0, *pData1, *pData2; + long ix, iy; + + if (!iScansNegatively && jScansPositively && !jPointsAreConsecutive && !alternativeRowScanning) { + /* Already +i and +j. No need to change */ + return GRIB_SUCCESS; + } + if (!data) return GRIB_SUCCESS; + + if (!context) context = grib_context_get_default(); + + if (!iScansNegatively && !jScansPositively && !jPointsAreConsecutive && !alternativeRowScanning && + nx > 0 && ny > 0) { + /* Regular grid +i -j: convert from we:ns to we:sn */ + size_t row_size = ((size_t)nx) * sizeof(double); + data2 = (double*)grib_context_malloc(context, row_size); + if (!data2) { + grib_context_log(context, GRIB_LOG_ERROR, "Geoiterator data: Error allocating %ld bytes", row_size); + return GRIB_OUT_OF_MEMORY; + } + for (iy = 0; iy < ny / 2; iy++) { + memcpy(data2, data + ((size_t)iy) * nx, row_size); + memcpy(data + iy * nx, data + (ny - 1 - iy) * ((size_t)nx), row_size); + memcpy(data + (ny - 1 - iy) * ((size_t)nx), data2, row_size); + } + grib_context_free(context, data2); + return GRIB_SUCCESS; + } + + if (nx < 1 || ny < 1) { + grib_context_log(context, GRIB_LOG_ERROR, "Geoiterator data: Invalid values for Nx and/or Ny"); + return GRIB_GEOCALCULUS_PROBLEM; + } + data2 = (double*)grib_context_malloc(context, numPoints * sizeof(double)); + if (!data2) { + grib_context_log(context, GRIB_LOG_ERROR, "Geoiterator data: Error allocating %ld bytes", numPoints * sizeof(double)); + return GRIB_OUT_OF_MEMORY; + } + pData0 = data2; + for (iy = 0; iy < ny; iy++) { + long deltaX = 0; + pData1 = pointer_to_data(0, iy, iScansNegatively, jScansPositively, jPointsAreConsecutive, alternativeRowScanning, nx, ny, data); + if (!pData1) { + grib_context_free(context, data2); + return GRIB_GEOCALCULUS_PROBLEM; + } + pData2 = pointer_to_data(1, iy, iScansNegatively, jScansPositively, jPointsAreConsecutive, alternativeRowScanning, nx, ny, data); + if (!pData2) { + grib_context_free(context, data2); + return GRIB_GEOCALCULUS_PROBLEM; + } + deltaX = pData2 - pData1; + for (ix = 0; ix < nx; ix++) { + *pData0++ = *pData1; + pData1 += deltaX; + } + } + memcpy(data, data2, ((size_t)numPoints) * sizeof(double)); + grib_context_free(context, data2); + + return GRIB_SUCCESS; +} diff --git a/src/grib_iterator_class.h b/src/grib_iterator_class.h index 7360818fc..833f9b579 100644 --- a/src/grib_iterator_class.h +++ b/src/grib_iterator_class.h @@ -1,13 +1,13 @@ /* This file is automatically generated by ./make_class.pl, do not edit */ -extern grib_iterator* grib_iterator_gaussian; -extern grib_iterator* grib_iterator_gaussian_reduced; -extern grib_iterator* grib_iterator_gen; -extern grib_iterator* grib_iterator_healpix; -extern grib_iterator* grib_iterator_lambert_azimuthal_equal_area; -extern grib_iterator* grib_iterator_lambert_conformal; -extern grib_iterator* grib_iterator_latlon; -extern grib_iterator* grib_iterator_latlon_reduced; -extern grib_iterator* grib_iterator_mercator; -extern grib_iterator* grib_iterator_polar_stereographic; -extern grib_iterator* grib_iterator_regular; -extern grib_iterator* grib_iterator_space_view; +extern eccodes::grib::geo::Iterator* grib_iterator_gaussian; +extern eccodes::grib::geo::Iterator* grib_iterator_gaussian_reduced; +//extern eccodes::grib::geo::Iterator* grib_iterator_gen; +extern eccodes::grib::geo::Iterator* grib_iterator_healpix; +extern eccodes::grib::geo::Iterator* grib_iterator_lambert_azimuthal_equal_area; +extern eccodes::grib::geo::Iterator* grib_iterator_lambert_conformal; +extern eccodes::grib::geo::Iterator* grib_iterator_latlon; +extern eccodes::grib::geo::Iterator* grib_iterator_latlon_reduced; +extern eccodes::grib::geo::Iterator* grib_iterator_mercator; +extern eccodes::grib::geo::Iterator* grib_iterator_polar_stereographic; +extern eccodes::grib::geo::Iterator* grib_iterator_regular; +extern eccodes::grib::geo::Iterator* grib_iterator_space_view; diff --git a/src/grib_iterator_factory.h b/src/grib_iterator_factory.h index e9bda157e..ed247bebd 100644 --- a/src/grib_iterator_factory.h +++ b/src/grib_iterator_factory.h @@ -1,7 +1,7 @@ /* This file is automatically generated by ./make.pl, do not edit */ { "gaussian", &grib_iterator_gaussian, }, { "gaussian_reduced", &grib_iterator_gaussian_reduced, }, -{ "gen", &grib_iterator_gen, }, +//{ "gen", &grib_iterator_gen, }, { "healpix", &grib_iterator_healpix, }, { "lambert_azimuthal_equal_area", &grib_iterator_lambert_azimuthal_equal_area, }, { "lambert_conformal", &grib_iterator_lambert_conformal, }, diff --git a/src/iterator/grib_iterator.cc b/src/iterator/grib_iterator.cc new file mode 100644 index 000000000..dd2c741fa --- /dev/null +++ b/src/iterator/grib_iterator.cc @@ -0,0 +1,171 @@ +/* + * (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. + */ + +/*************************************************************************** + * Jean Baptiste Filippi - 01.11.2005 * + ***************************************************************************/ +#include "grib_iterator.h" +#include "../accessor/grib_accessor_class_iterator.h" + +namespace eccodes { +namespace grib { +namespace geo { + +#if GRIB_PTHREADS +static pthread_once_t once = PTHREAD_ONCE_INIT; +static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + +static void init_mutex() +{ + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&mutex, &attr); + pthread_mutexattr_destroy(&attr); +} +#elif GRIB_OMP_THREADS +static int once = 0; +static omp_nest_lock_t mutex; + +static void init_mutex() +{ + GRIB_OMP_CRITICAL(lock_iterator_c) + { + if (once == 0) { + omp_init_nest_lock(&mutex); + once = 1; + } + } +} +#endif + + +int Iterator::init(grib_handle* h, grib_arguments* args) +{ + int r = 0; + h_ = h; + //GRIB_MUTEX_INIT_ONCE(&once, &init_mutex); + //GRIB_MUTEX_LOCK(&mutex); + //r = init_iterator(this, i, h, args); + //GRIB_MUTEX_UNLOCK(&mutex); + return r; +} + +/* For this one, ALL destroy are called */ +int Iterator::destroy() +{ + grib_context_free(context_, this); + return 0; +} + +eccodes::grib::geo::Iterator* gribIteratorNew(const grib_handle* ch, unsigned long flags, int* error) +{ + grib_handle* h = (grib_handle*)ch; + grib_accessor* a = NULL; + grib_accessor_iterator_t* ita = NULL; + //grib_iterator* iter = NULL; + *error = GRIB_NOT_IMPLEMENTED; + a = grib_find_accessor(h, "ITERATOR"); + ita = (grib_accessor_iterator_t*)a; + + if (!a) + return NULL; + + eccodes::grib::geo::Iterator* iter = grib_iterator_factory(h, ita->args_, flags, error); + + if (iter) + *error = GRIB_SUCCESS; + + return iter; +} + +int gribIteratorDelete(eccodes::grib::geo::Iterator* i) +{ + if (i) + i->destroy(); + return 0; +} + + +} // namespace geo +} // namespace grib +} // namespace eccodes + + +/* + * C API Implementation + * codes_iterator_* wrappers are in eccodes.h and eccodes.cc + * grib_iterator_* declarations are in grib_api.h + */ + +int grib_iterator_reset(grib_iterator* i) +{ + return i->iterator->reset(); +} + +int grib_iterator_has_next(grib_iterator* i) +{ + return i->iterator->has_next(); +} + +int grib_iterator_next(grib_iterator* i, double* lat, double* lon, double* value) +{ + return i->iterator->next(lat, lon, value); +} + +int grib_iterator_previous(grib_iterator* i, double* lat, double* lon, double* value) +{ + return i->iterator->previous(lat, lon, value); +} + +int grib_iterator_destroy(grib_context* c, grib_iterator* i) +{ + return i->iterator->destroy(); +} + +#if defined(HAVE_GEOGRAPHY) +grib_iterator* grib_iterator_new(const grib_handle* ch, unsigned long flags, int* error) +{ + grib_iterator* i = (grib_iterator*)grib_context_malloc_clear(ch->context, sizeof(grib_iterator)); + i->iterator = eccodes::grib::geo::gribIteratorNew(ch, flags, error); + if (!i->iterator) { + grib_context_free(ch->context, i); + return NULL; + } + return i; +} + +int grib_iterator_delete(grib_iterator* i) +{ + if (i) { // TODO(maee): check if this is necessary + grib_context* c = i->iterator->context_; // TODO(maee): find a better solution + gribIteratorDelete(i->iterator); + grib_context_free(c, i); + } + return 0; +} + +#else +grib_iterator* grib_iterator_new(const grib_handle* ch, unsigned long flags, int* error) +{ + *error = GRIB_FUNCTIONALITY_NOT_ENABLED; + grib_context_log(ch->context, GRIB_LOG_ERROR, + "Geoiterator functionality not enabled. Please rebuild with -DENABLE_GEOGRAPHY=ON"); + + return NULL; +} + +int grib_iterator_delete(grib_iterator* i) +{ + grib_context_log(ch->context, GRIB_LOG_ERROR, + "Geoiterator functionality not enabled. Please rebuild with -DENABLE_GEOGRAPHY=ON"); + return GRIB_FUNCTIONALITY_NOT_ENABLED; +} +#endif diff --git a/src/iterator/grib_iterator.h b/src/iterator/grib_iterator.h index d9949072e..67ffa2ff9 100644 --- a/src/iterator/grib_iterator.h +++ b/src/iterator/grib_iterator.h @@ -29,6 +29,7 @@ public: virtual int reset() = 0; virtual int destroy() = 0; virtual long has_next() = 0; + virtual Iterator* create() const = 0; public: grib_context* context_; @@ -43,6 +44,9 @@ public: int inited_; }; +eccodes::grib::geo::Iterator* gribIteratorNew(const grib_handle*, unsigned long, int*); +int gribIteratorDelete(eccodes::grib::geo::Iterator*); + } // namespace geo } // namespace grib } // namespace eccodes diff --git a/src/iterator/grib_iterator_class_gaussian.cc b/src/iterator/grib_iterator_class_gaussian.cc index 340329772..35a2f47a8 100644 --- a/src/iterator/grib_iterator_class_gaussian.cc +++ b/src/iterator/grib_iterator_class_gaussian.cc @@ -10,18 +10,20 @@ #include "grib_iterator_class_gaussian.h" +eccodes::grib::geo::Gaussian _grib_iterator_gaussian{}; +eccodes::grib::geo::Iterator* grib_iterator_gaussian = &_grib_iterator_gaussian; + namespace eccodes { namespace grib { namespace geo { -Gaussian _grib_iterator_gaussian{}; -Iterator* grib_iterator_gaussian = &_grib_iterator_gaussian; - static void binary_search_gaussian_latitudes(const double xx[], const unsigned long n, double x, long* j); int Gaussian::init(grib_handle* h, grib_arguments* args) { - Regular::init(h, args); + int ret = GRIB_SUCCESS; + if((ret = Regular::init(h, args)) != GRIB_SUCCESS) + return ret; double* lats; double laf; /* latitude of first point in degrees */ @@ -32,7 +34,6 @@ int Gaussian::init(grib_handle* h, grib_arguments* args) int size = 0; double start; long istart = 0; - int ret = GRIB_SUCCESS; const char* latofirst = grib_arguments_get_name(h, args, carg_++); const char* latoflast = grib_arguments_get_name(h, args, carg_++); diff --git a/src/iterator/grib_iterator_class_gaussian.h b/src/iterator/grib_iterator_class_gaussian.h index 2efb3fff3..7420df8cf 100644 --- a/src/iterator/grib_iterator_class_gaussian.h +++ b/src/iterator/grib_iterator_class_gaussian.h @@ -19,10 +19,9 @@ namespace geo { class Gaussian : public Regular { public: - Gaussian() : Regular() - { - class_name_ = "gaussian"; - } + Gaussian() : Regular() { class_name_ = "gaussian"; } + Iterator* create() const override { return new Gaussian(); } + int init(grib_handle*, grib_arguments*) override; }; diff --git a/src/iterator/grib_iterator_class_gaussian_reduced.cc b/src/iterator/grib_iterator_class_gaussian_reduced.cc index 6cc1b5e0a..c8e2fe305 100644 --- a/src/iterator/grib_iterator_class_gaussian_reduced.cc +++ b/src/iterator/grib_iterator_class_gaussian_reduced.cc @@ -11,13 +11,13 @@ #include "grib_iterator_class_gaussian_reduced.h" #include +eccodes::grib::geo::GaussianReduced _grib_iterator_gaussian_reduced{}; +eccodes::grib::geo::Iterator* grib_iterator_gaussian_reduced = &_grib_iterator_gaussian_reduced; + namespace eccodes { namespace grib { namespace geo { -GaussianReduced _grib_iterator_gaussian_reduced{}; -Iterator* grib_iterator_gaussian_reduced = &_grib_iterator_gaussian_reduced; - #define ITER "Reduced Gaussian grid Geoiterator" int GaussianReduced::next(double* lat, double* lon, double* val) @@ -225,9 +225,11 @@ int GaussianReduced::iterate_reduced_gaussian_subarea(grib_handle* h, int GaussianReduced::init(grib_handle* h, grib_arguments* args) { - Gen::init(h, args); + int ret = GRIB_SUCCESS; + if ((ret = Gen::init(h, args)) != GRIB_SUCCESS) + return ret; - int ret = GRIB_SUCCESS, j, is_global = 0; + int j, is_global = 0; double lat_first = 0, lon_first = 0, lat_last = 0, lon_last = 0; double angular_precision = 1.0 / 1000000.0; double* lats; diff --git a/src/iterator/grib_iterator_class_gaussian_reduced.h b/src/iterator/grib_iterator_class_gaussian_reduced.h index 547181f45..46459e082 100644 --- a/src/iterator/grib_iterator_class_gaussian_reduced.h +++ b/src/iterator/grib_iterator_class_gaussian_reduced.h @@ -19,10 +19,8 @@ namespace geo { class GaussianReduced : public Gen { public: - GaussianReduced() : Gen() - { - class_name_ = "gaussian_reduced"; - } + GaussianReduced() : Gen() { class_name_ = "gaussian_reduced"; } + Iterator* create() const override { return new GaussianReduced(); } int init(grib_handle*,grib_arguments*) override; int next(double *, double *, double *) override; diff --git a/src/iterator/grib_iterator_class_gen.cc b/src/iterator/grib_iterator_class_gen.cc index 55589e143..13f5aca0e 100644 --- a/src/iterator/grib_iterator_class_gen.cc +++ b/src/iterator/grib_iterator_class_gen.cc @@ -14,120 +14,21 @@ namespace eccodes { namespace grib { namespace geo { -//Gen _grib_iterator_class_gen{}; -//Iterator* grib_iterator_class_gen = &_grib_iterator_class_gen; - - -/* - * Return pointer to data at (i,j) (Fortran convention) - */ -static double* pointer_to_data(unsigned int i, unsigned int j, - long iScansNegatively, long jScansPositively, - long jPointsAreConsecutive, long alternativeRowScanning, - unsigned int nx, unsigned int ny, double* data) -{ - /* Regular grid */ - if (nx > 0 && ny > 0) { - if (i >= nx || j >= ny) - return NULL; - j = (jScansPositively) ? j : ny - 1 - j; - i = ((alternativeRowScanning) && (j % 2 == 1)) ? nx - 1 - i : i; - i = (iScansNegatively) ? nx - 1 - i : i; - - return (jPointsAreConsecutive) ? data + j + i * ny : data + i + nx * j; - } - - /* Reduced or other data not on a grid */ - return NULL; -} - -/* Apply the scanning mode flags which may require data array to be transformed - * to standard west-to-east (+i) south-to-north (+j) mode. - * The data array passed in should have 'numPoints' elements. -*/ -int transform_iterator_data(grib_context* context, double* data, - long iScansNegatively, long jScansPositively, - long jPointsAreConsecutive, long alternativeRowScanning, - size_t numPoints, long nx, long ny) -{ - double* data2; - double *pData0, *pData1, *pData2; - long ix, iy; - - if (!iScansNegatively && jScansPositively && !jPointsAreConsecutive && !alternativeRowScanning) { - /* Already +i and +j. No need to change */ - return GRIB_SUCCESS; - } - if (!data) return GRIB_SUCCESS; - - if (!context) context = grib_context_get_default(); - - if (!iScansNegatively && !jScansPositively && !jPointsAreConsecutive && !alternativeRowScanning && - nx > 0 && ny > 0) { - /* Regular grid +i -j: convert from we:ns to we:sn */ - size_t row_size = ((size_t)nx) * sizeof(double); - data2 = (double*)grib_context_malloc(context, row_size); - if (!data2) { - grib_context_log(context, GRIB_LOG_ERROR, "Geoiterator data: Error allocating %ld bytes", row_size); - return GRIB_OUT_OF_MEMORY; - } - for (iy = 0; iy < ny / 2; iy++) { - memcpy(data2, data + ((size_t)iy) * nx, row_size); - memcpy(data + iy * nx, data + (ny - 1 - iy) * ((size_t)nx), row_size); - memcpy(data + (ny - 1 - iy) * ((size_t)nx), data2, row_size); - } - grib_context_free(context, data2); - return GRIB_SUCCESS; - } - - if (nx < 1 || ny < 1) { - grib_context_log(context, GRIB_LOG_ERROR, "Geoiterator data: Invalid values for Nx and/or Ny"); - return GRIB_GEOCALCULUS_PROBLEM; - } - data2 = (double*)grib_context_malloc(context, numPoints * sizeof(double)); - if (!data2) { - grib_context_log(context, GRIB_LOG_ERROR, "Geoiterator data: Error allocating %ld bytes", numPoints * sizeof(double)); - return GRIB_OUT_OF_MEMORY; - } - pData0 = data2; - for (iy = 0; iy < ny; iy++) { - long deltaX = 0; - pData1 = pointer_to_data(0, iy, iScansNegatively, jScansPositively, jPointsAreConsecutive, alternativeRowScanning, nx, ny, data); - if (!pData1) { - grib_context_free(context, data2); - return GRIB_GEOCALCULUS_PROBLEM; - } - pData2 = pointer_to_data(1, iy, iScansNegatively, jScansPositively, jPointsAreConsecutive, alternativeRowScanning, nx, ny, data); - if (!pData2) { - grib_context_free(context, data2); - return GRIB_GEOCALCULUS_PROBLEM; - } - deltaX = pData2 - pData1; - for (ix = 0; ix < nx; ix++) { - *pData0++ = *pData1; - pData1 += deltaX; - } - } - memcpy(data, data2, ((size_t)numPoints) * sizeof(double)); - grib_context_free(context, data2); - - return GRIB_SUCCESS; -} - int Gen::init(grib_handle* h, grib_arguments* args) { - Iterator::init(h, args); + int err = GRIB_SUCCESS; + if ((err = Iterator::init(h, args)) != GRIB_SUCCESS) + return err; size_t dli = 0; - int err = GRIB_SUCCESS; const char* s_rawData = NULL; const char* s_numPoints = NULL; long numberOfPoints = 0; - carg_ = 1; + carg_ = 1; - s_numPoints = grib_arguments_get_name(h, args, carg_++); + s_numPoints = grib_arguments_get_name(h, args, carg_++); missingValue_ = grib_arguments_get_name(h, args, carg_++); - s_rawData = grib_arguments_get_name(h, args, carg_++); + s_rawData = grib_arguments_get_name(h, args, carg_++); data_ = NULL; h = h; /* We may not need to keep them */ @@ -197,7 +98,6 @@ long Gen::has_next() return 1; } - int Gen::previous(double*, double*, double*) { return GRIB_NOT_IMPLEMENTED; } @@ -206,19 +106,18 @@ int Gen::next(double*, double*, double*) { return GRIB_NOT_IMPLEMENTED; } +//int Gen::get(double* lat, double* lon, double* val) +//{ +// if (e_ >= (long)(nv_ - 1)) +// return GRIB_END_OF_ITERATION; -int Gen::get(double* lat, double* lon, double* val) -{ - if (e_ >= (long)(nv_ - 1)) - return GRIB_END_OF_ITERATION; +// e_++; +// if (lat) *lat = 0; +// if (lon) *lon = 0; +// if (val) *val = 0; - e_++; - if (lat) *lat = 0; - if (lon) *lon = 0; - if (val) *val = 0; - - return 1; -} +// return 1; +//} } // namespace geo } // namespace grib diff --git a/src/iterator/grib_iterator_class_gen.h b/src/iterator/grib_iterator_class_gen.h index 450d49b9c..c7b349663 100644 --- a/src/iterator/grib_iterator_class_gen.h +++ b/src/iterator/grib_iterator_class_gen.h @@ -14,15 +14,13 @@ namespace eccodes { namespace grib { -namespace geo { // TODO(maee): geo +namespace geo { class Gen : public Iterator { public: - Gen() : Iterator() - { - class_name_ = "abstract_long_vector"; - } + Gen() : Iterator() { class_name_ = "gen"; } + Iterator* create() const override { return new Gen(); } int init(grib_handle*,grib_arguments*) override; int next(double*, double*, double*) override; @@ -32,6 +30,7 @@ public: long has_next() override; // TODO(maee/masn): return bool please! public: + //int get(double*, double*, double*); int carg_; const char* missingValue_; }; diff --git a/src/iterator/grib_iterator_class_healpix.cc b/src/iterator/grib_iterator_class_healpix.cc index 017b9e7a5..904acdae3 100644 --- a/src/iterator/grib_iterator_class_healpix.cc +++ b/src/iterator/grib_iterator_class_healpix.cc @@ -10,10 +10,6 @@ #include "grib_iterator_class_healpix.h" -namespace eccodes { -namespace grib { -namespace geo { - #include #include #include @@ -21,13 +17,13 @@ namespace geo { #include #include +eccodes::grib::geo::Healpix _grib_iterator_healpix; +eccodes::grib::geo::Iterator* grib_iterator_healpix = &_grib_iterator_healpix; + namespace eccodes { namespace grib { namespace geo { -Healpix _grib_iterator_healpix; -Iterator* grib_iterator_healpix = &_grib_iterator_healpix; - #define ITER "HEALPix Geoiterator" constexpr double RAD2DEG = 57.29577951308232087684; // 180 over pi @@ -147,7 +143,6 @@ static std::vector HEALPix_longitudes(size_t N, size_t i) return longitudes; } -} // anonymous namespace int Healpix::iterate_healpix(long N) { @@ -272,9 +267,9 @@ int Healpix::iterate_healpix(long N) int Healpix::init(grib_handle* h, grib_arguments* args) { - Gen::init(h, args); - - int err = 0; + int err = GRIB_SUCCESS; + if ((err = Gen::init(h, args)) != GRIB_SUCCESS) + return err; const char* snside = grib_arguments_get_name(h, args, carg_++); const char* sorder = grib_arguments_get_name(h, args, carg_++); diff --git a/src/iterator/grib_iterator_class_healpix.h b/src/iterator/grib_iterator_class_healpix.h index 008ac11f9..53ad2a703 100644 --- a/src/iterator/grib_iterator_class_healpix.h +++ b/src/iterator/grib_iterator_class_healpix.h @@ -19,10 +19,8 @@ namespace geo { class Healpix : public Gen { public: - Healpix() : Gen() - { - class_name_ = "healpix"; - } + Healpix() : Gen() { class_name_ = "healpix"; } + Iterator* create() const override { return new Healpix(); } int init(grib_handle*, grib_arguments*) override; int next(double *, double *, double *) override; diff --git a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc index 07fa2fcb1..b825fdad5 100644 --- a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc +++ b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc @@ -10,13 +10,13 @@ #include "grib_iterator_class_lambert_azimuthal_equal_area.h" +eccodes::grib::geo::LambertAzimuthalEqualArea _grib_iterator_lambert_azimuthal_equal_area{}; +eccodes::grib::geo::Iterator* grib_iterator_lambert_azimuthal_equal_area = &_grib_iterator_lambert_azimuthal_equal_area; + namespace eccodes { namespace grib { namespace geo { -LambertAzimuthalEqualArea _grib_iterator_lambert_azimuthal_equal_area{}; -Iterator* grib_iterator_lambert_azimuthal_equal_area = &_grib_iterator_lambert_azimuthal_equal_area; - #define ITER "Lambert azimuthal equal area Geoiterator" int LambertAzimuthalEqualArea::next(double* lat, double* lon, double* val) @@ -334,9 +334,10 @@ static int init_sphere(grib_handle* h, int LambertAzimuthalEqualArea::init(grib_handle* h, grib_arguments* args) { - Gen::init(h, args); + int err = 0; + if ((err = Gen::init(h, args)) != GRIB_SUCCESS) + return err; - int err = 0; int is_oblate = 0; double lonFirstInDegrees, latFirstInDegrees, lonFirstInRadians, latFirstInRadians, radius = 0; long nx, ny; diff --git a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h index 694a93e67..64c385e7b 100644 --- a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h +++ b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h @@ -19,14 +19,11 @@ namespace geo { class LambertAzimuthalEqualArea : public Gen { public: - LambertAzimuthalEqualArea() : Gen() - { - class_name_ = "lambert_azimuthal_equal_area"; - } + LambertAzimuthalEqualArea() : Gen() { class_name_ = "lambert_azimuthal_equal_area"; } + Iterator* create() const override { return new LambertAzimuthalEqualArea(); } int init(grib_handle*, grib_arguments*) override; int next(double*, double*, double*) override; - int previous(double*, double*, double*) override; int destroy() override; public: diff --git a/src/iterator/grib_iterator_class_lambert_conformal.cc b/src/iterator/grib_iterator_class_lambert_conformal.cc index 9da401312..469c5e3c3 100644 --- a/src/iterator/grib_iterator_class_lambert_conformal.cc +++ b/src/iterator/grib_iterator_class_lambert_conformal.cc @@ -11,13 +11,13 @@ #include "grib_iterator_class_lambert_conformal.h" #include +eccodes::grib::geo::LambertConformal _grib_iterator_lambert_conformal{}; +eccodes::grib::geo::Iterator* grib_iterator_lambert_conformal = &_grib_iterator_lambert_conformal; + namespace eccodes { namespace grib { namespace geo { -LambertConformal _grib_iterator_lambert_conformal{}; -Iterator* grib_iterator_lambert_conformal = &_grib_iterator_lambert_conformal; - #define ITER "Lambert conformal Geoiterator" #define EPSILON 1.0e-10 @@ -345,9 +345,11 @@ int LambertConformal::init_oblate(const grib_handle* h, int LambertConformal::init(grib_handle* h, grib_arguments* args) { - Gen::init(h, args); + int err = GRIB_SUCCESS; + if ((err = Gen::init(h, args)) != GRIB_SUCCESS) + return err; - int err = 0, is_oblate = 0; + int is_oblate = 0; long nx, ny, iScansNegatively, jScansPositively, jPointsAreConsecutive, alternativeRowScanning; double LoVInDegrees, LaDInDegrees, Latin1InDegrees, Latin2InDegrees, latFirstInDegrees, lonFirstInDegrees, Dx, Dy, radius = 0; diff --git a/src/iterator/grib_iterator_class_lambert_conformal.h b/src/iterator/grib_iterator_class_lambert_conformal.h index d02380c5d..41b8fe995 100644 --- a/src/iterator/grib_iterator_class_lambert_conformal.h +++ b/src/iterator/grib_iterator_class_lambert_conformal.h @@ -19,10 +19,8 @@ namespace geo { class LambertConformal : public Gen { public: - LambertConformal() : Gen() - { - class_name_ = "lambert_conformal"; - } + LambertConformal() : Gen() { class_name_ = "lambert_conformal"; } + Iterator* create() const override { return new LambertConformal(); } int init(grib_handle*,grib_arguments*) override; int next(double*, double*, double*) override; diff --git a/src/iterator/grib_iterator_class_latlon.cc b/src/iterator/grib_iterator_class_latlon.cc index 9f697a511..38eddad52 100644 --- a/src/iterator/grib_iterator_class_latlon.cc +++ b/src/iterator/grib_iterator_class_latlon.cc @@ -10,14 +10,13 @@ #include "grib_iterator_class_latlon.h" +eccodes::grib::geo::Latlon _grib_iterator_latlon{}; +eccodes::grib::geo::Iterator* grib_iterator_latlon = &_grib_iterator_latlon; + namespace eccodes { namespace grib { namespace geo { -Latlon _grib_iterator_latlon{}; -Iterator* grib_iterator_latlon = &_grib_iterator_latlon; - - int Latlon::next(double* lat, double* lon, double* val) { /* GRIB-238: Support rotated lat/lon grids */ @@ -65,9 +64,10 @@ int Latlon::next(double* lat, double* lon, double* val) int Latlon::init(grib_handle* h, grib_arguments* args) { - Regular::init(h, args); + int err = 0; + if ((err = Regular::init(h, args)) != GRIB_SUCCESS) + return err; - int err = 0; double jdir; double lat1=0, lat2=0, north=0, south=0; long jScansPositively; diff --git a/src/iterator/grib_iterator_class_latlon.h b/src/iterator/grib_iterator_class_latlon.h index f825f3f53..bc74d4509 100644 --- a/src/iterator/grib_iterator_class_latlon.h +++ b/src/iterator/grib_iterator_class_latlon.h @@ -19,10 +19,8 @@ namespace geo { class Latlon : public Regular { public: - Latlon() : Regular() - { - class_name_ = "latlon"; - } + Latlon() : Regular() { class_name_ = "latlon"; } + Iterator* create() const override { return new Latlon(); } int init(grib_handle*, grib_arguments*) override; int next(double*, double*, double*) override; diff --git a/src/iterator/grib_iterator_class_latlon_reduced.cc b/src/iterator/grib_iterator_class_latlon_reduced.cc index 0cb0244b6..90c4702a3 100644 --- a/src/iterator/grib_iterator_class_latlon_reduced.cc +++ b/src/iterator/grib_iterator_class_latlon_reduced.cc @@ -10,14 +10,13 @@ #include "grib_iterator_class_latlon_reduced.h" +eccodes::grib::geo::LatlonReduced _grib_iterator_latlon_reduced{}; +eccodes::grib::geo::Iterator* grib_iterator_latlon_reduced = &_grib_iterator_latlon_reduced; + namespace eccodes { namespace grib { namespace geo { -LatlonReduced _grib_iterator_latlon_reduced{}; -Iterator* grib_iterator_latlon_reduced = &_grib_iterator_latlon_reduced; - - int LatlonReduced::next(double* lat, double* lon, double* val) { if ((long)e_ >= (long)(nv_ - 1)) @@ -34,9 +33,10 @@ int LatlonReduced::next(double* lat, double* lon, double* val) int LatlonReduced::init(grib_handle* h, grib_arguments* args) { - Gen::init(h, args); - int ret = GRIB_SUCCESS; + if ((ret = Gen::init(h, args)) != GRIB_SUCCESS) + return ret; + double laf; double lal; long nlats; diff --git a/src/iterator/grib_iterator_class_latlon_reduced.h b/src/iterator/grib_iterator_class_latlon_reduced.h index de114c6d5..5c6537179 100644 --- a/src/iterator/grib_iterator_class_latlon_reduced.h +++ b/src/iterator/grib_iterator_class_latlon_reduced.h @@ -19,10 +19,8 @@ namespace geo { class LatlonReduced : public Gen { public: - LatlonReduced() : Gen() - { - class_name_ = "latlon_reduced"; - } + LatlonReduced() : Gen() { class_name_ = "latlon_reduced"; } + Iterator* create() const override { return new LatlonReduced(); } int init(grib_handle*,grib_arguments*) override; int next(double *lat, double *lon, double *val) override; diff --git a/src/iterator/grib_iterator_class_mercator.cc b/src/iterator/grib_iterator_class_mercator.cc index c4642ff9d..60fffc3eb 100644 --- a/src/iterator/grib_iterator_class_mercator.cc +++ b/src/iterator/grib_iterator_class_mercator.cc @@ -10,13 +10,13 @@ #include "grib_iterator_class_mercator.h" +eccodes::grib::geo::Mercator _grib_iterator_mercator{}; +eccodes::grib::geo::Iterator* grib_iterator_mercator = &_grib_iterator_mercator; + namespace eccodes { namespace grib { namespace geo { -Mercator _grib_iterator_mercator{}; -Iterator* grib_iterator_mercator = &_grib_iterator_mercator; - #define ITER "Mercator Geoiterator" #define EPSILON 1.0e-10 @@ -165,9 +165,10 @@ int Mercator::init_mercator(grib_handle* h, int Mercator::init(grib_handle* h, grib_arguments* args) { - Gen::init(h, args); + int err = GRIB_SUCCESS; + if ((err = Gen::init(h, args)) != GRIB_SUCCESS) + return err; - int err = 0; long ni, nj, iScansNegatively, jScansPositively, jPointsAreConsecutive, alternativeRowScanning; double latFirstInDegrees, lonFirstInDegrees, LaDInDegrees; double latLastInDegrees, lonLastInDegrees, orientationInDegrees, DiInMetres, DjInMetres, radius = 0; diff --git a/src/iterator/grib_iterator_class_mercator.h b/src/iterator/grib_iterator_class_mercator.h index ea636c516..80bebac8b 100644 --- a/src/iterator/grib_iterator_class_mercator.h +++ b/src/iterator/grib_iterator_class_mercator.h @@ -19,9 +19,9 @@ namespace geo { class Mercator : public Gen { public: - Mercator() { - class_name_ = "mercator"; - } + Mercator() { class_name_ = "mercator"; } + Iterator* create() const override { return new Mercator(); } + int init(grib_handle*,grib_arguments*) override; int next(double *lat, double *lon, double *val) override; int destroy() override; diff --git a/src/iterator/grib_iterator_class_polar_stereographic.cc b/src/iterator/grib_iterator_class_polar_stereographic.cc index 2da36a8c2..6729531f0 100644 --- a/src/iterator/grib_iterator_class_polar_stereographic.cc +++ b/src/iterator/grib_iterator_class_polar_stereographic.cc @@ -10,13 +10,13 @@ #include "grib_iterator_class_polar_stereographic.h" +eccodes::grib::geo::PolarStereographic _grib_iterator_polar_stereographic{}; +eccodes::grib::geo::Iterator* grib_iterator_polar_stereographic = &_grib_iterator_polar_stereographic; + namespace eccodes { namespace grib { namespace geo { -PolarStereographic _grib_iterator_polar_stereographic{}; -Iterator* grib_iterator_polar_stereographic = &_grib_iterator_polar_stereographic; - #define ITER "Polar stereographic Geoiterator" int PolarStereographic::next(double* lat, double* lon, double* val) @@ -53,9 +53,10 @@ typedef struct proj_data_t int PolarStereographic::init(grib_handle* h, grib_arguments* args) { - Gen::init(h, args); + int ret = GRIB_SUCCESS; + if ((ret = Gen::init(h, args)) != GRIB_SUCCESS) + return ret; - int ret = 0; double *lats, *lons; /* arrays for latitudes and longitudes */ double lonFirstInDegrees, latFirstInDegrees, radius; double x, y, Dx, Dy; diff --git a/src/iterator/grib_iterator_class_polar_stereographic.h b/src/iterator/grib_iterator_class_polar_stereographic.h index 6920dfd40..997918f90 100644 --- a/src/iterator/grib_iterator_class_polar_stereographic.h +++ b/src/iterator/grib_iterator_class_polar_stereographic.h @@ -19,10 +19,8 @@ namespace geo { class PolarStereographic : public Gen { public: - PolarStereographic() : Gen() - { - class_name_ = "polar_stereographic"; - } + PolarStereographic() : Gen() { class_name_ = "polar_stereographic"; } + Iterator* create() const override { return new PolarStereographic(); } int init(grib_handle*,grib_arguments*) override; int next(double *, double *, double *) override; diff --git a/src/iterator/grib_iterator_class_regular.cc b/src/iterator/grib_iterator_class_regular.cc index 1dec29155..ce04fcd45 100644 --- a/src/iterator/grib_iterator_class_regular.cc +++ b/src/iterator/grib_iterator_class_regular.cc @@ -10,13 +10,13 @@ #include "grib_iterator_class_regular.h" +eccodes::grib::geo::Regular _grib_iterator_regular{}; +eccodes::grib::geo::Iterator* grib_iterator_regular = &_grib_iterator_regular; + namespace eccodes { namespace grib { namespace geo { -Regular _grib_iterator_regular{}; -Iterator* grib_iterator_regular = &_grib_iterator_regular; - #define ITER "Regular grid Geoiterator" int Regular::next(double* lat, double* lon, double* val) @@ -60,9 +60,9 @@ int Regular::destroy() int Regular::init(grib_handle* h, grib_arguments* args) { - Gen::init(h, args); - - int ret = GRIB_SUCCESS; + int ret = GRIB_SUCCESS; + if ((ret = Gen::init(h, args)) != GRIB_SUCCESS) + return ret; long Ni; /* Number of points along a parallel = Nx */ long Nj; /* Number of points along a meridian = Ny */ diff --git a/src/iterator/grib_iterator_class_regular.h b/src/iterator/grib_iterator_class_regular.h index 77cbb1a00..c2f3afefa 100644 --- a/src/iterator/grib_iterator_class_regular.h +++ b/src/iterator/grib_iterator_class_regular.h @@ -19,10 +19,8 @@ namespace geo { class Regular : public Gen { public: - Regular() - { - class_name_ = "regular"; - } + Regular() { class_name_ = "regular"; } + Iterator* create() const override { return new Regular(); } int init(grib_handle*,grib_arguments*) override; int next(double *lat, double *lon, double *val) override; diff --git a/src/iterator/grib_iterator_class_space_view.cc b/src/iterator/grib_iterator_class_space_view.cc index 1badd7d8a..9b2c1d085 100644 --- a/src/iterator/grib_iterator_class_space_view.cc +++ b/src/iterator/grib_iterator_class_space_view.cc @@ -10,13 +10,13 @@ #include "grib_iterator_class_space_view.h" +eccodes::grib::geo::SpaceView _grib_iterator_space_view{}; +eccodes::grib::geo::Iterator* grib_iterator_space_view = &_grib_iterator_space_view; + namespace eccodes { namespace grib { namespace geo { -SpaceView _grib_iterator_space_view{}; -Iterator* grib_iterator_space_view = &_grib_iterator_space_view; - #define ITER "Space view Geoiterator" int SpaceView::next(double* lat, double* lon, double* val) @@ -78,12 +78,13 @@ int SpaceView::next(double* lat, double* lon, double* val) int SpaceView::init(grib_handle* h, grib_arguments* args) { - Gen::init(h, args); + int ret = GRIB_SUCCESS; + if ((ret = Gen::init(h, args)) != GRIB_SUCCESS) + return ret; /* REFERENCE: * LRIT/HRIT Global Specification (CGMS 03, Issue 2.6, 12.08.1999) */ - int ret = GRIB_SUCCESS; double *lats, *lons; /* arrays of latitudes and longitudes */ double latOfSubSatellitePointInDegrees, lonOfSubSatellitePointInDegrees; double orientationInDegrees, nrInRadiusOfEarth; diff --git a/src/iterator/grib_iterator_class_space_view.h b/src/iterator/grib_iterator_class_space_view.h index 8af34d017..e396e0549 100644 --- a/src/iterator/grib_iterator_class_space_view.h +++ b/src/iterator/grib_iterator_class_space_view.h @@ -19,10 +19,8 @@ namespace geo { class SpaceView : public Gen { public: - SpaceView() - { - class_name_ = "space_view"; - } + SpaceView() { class_name_ = "space_view"; } + Iterator* create() const override { return new SpaceView(); } int init(grib_handle*, grib_arguments*) override; int next(double *, double *, double *) override; From 30388789546db54c197fd5000c51371223a5cb78 Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Thu, 17 Oct 2024 11:04:59 +0200 Subject: [PATCH 04/20] Modernisation: Fix error code propagation in iterators destroy() --- src/iterator/grib_iterator.cc | 2 +- src/iterator/grib_iterator_class_gaussian_reduced.cc | 3 +-- src/iterator/grib_iterator_class_gen.cc | 3 +-- src/iterator/grib_iterator_class_healpix.cc | 3 +-- .../grib_iterator_class_lambert_azimuthal_equal_area.cc | 3 +-- src/iterator/grib_iterator_class_lambert_conformal.cc | 3 +-- src/iterator/grib_iterator_class_latlon_reduced.cc | 3 +-- src/iterator/grib_iterator_class_mercator.cc | 3 +-- src/iterator/grib_iterator_class_polar_stereographic.cc | 3 +-- src/iterator/grib_iterator_class_regular.cc | 3 +-- src/iterator/grib_iterator_class_space_view.cc | 3 +-- 11 files changed, 11 insertions(+), 21 deletions(-) diff --git a/src/iterator/grib_iterator.cc b/src/iterator/grib_iterator.cc index dd2c741fa..c045e1775 100644 --- a/src/iterator/grib_iterator.cc +++ b/src/iterator/grib_iterator.cc @@ -62,7 +62,7 @@ int Iterator::init(grib_handle* h, grib_arguments* args) int Iterator::destroy() { grib_context_free(context_, this); - return 0; + return GRIB_SUCCESS; } eccodes::grib::geo::Iterator* gribIteratorNew(const grib_handle* ch, unsigned long flags, int* error) diff --git a/src/iterator/grib_iterator_class_gaussian_reduced.cc b/src/iterator/grib_iterator_class_gaussian_reduced.cc index c8e2fe305..6a169da57 100644 --- a/src/iterator/grib_iterator_class_gaussian_reduced.cc +++ b/src/iterator/grib_iterator_class_gaussian_reduced.cc @@ -373,8 +373,7 @@ int GaussianReduced::destroy() grib_context_free(c, las_); grib_context_free(c, los_); - Gen::destroy(); - return GRIB_SUCCESS; + return Gen::destroy(); } } // namespace geo diff --git a/src/iterator/grib_iterator_class_gen.cc b/src/iterator/grib_iterator_class_gen.cc index 13f5aca0e..e5e7b727d 100644 --- a/src/iterator/grib_iterator_class_gen.cc +++ b/src/iterator/grib_iterator_class_gen.cc @@ -85,8 +85,7 @@ int Gen::destroy() const grib_context* c = h_->context; grib_context_free(c, data_); - Iterator::destroy(); - return GRIB_SUCCESS; + return Iterator::destroy(); } long Gen::has_next() diff --git a/src/iterator/grib_iterator_class_healpix.cc b/src/iterator/grib_iterator_class_healpix.cc index fda944c8c..7e3148f00 100644 --- a/src/iterator/grib_iterator_class_healpix.cc +++ b/src/iterator/grib_iterator_class_healpix.cc @@ -352,8 +352,7 @@ int Healpix::destroy() grib_context_free(context_, lats_); grib_context_free(context_, lons_); - Gen::destroy(); - return GRIB_SUCCESS; + return Gen::destroy(); } } // namespace geo diff --git a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc index b825fdad5..321e46c35 100644 --- a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc +++ b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc @@ -435,8 +435,7 @@ int LambertAzimuthalEqualArea::destroy() grib_context_free(c, lats_); grib_context_free(c, lons_); - Gen::destroy(); - return GRIB_SUCCESS; + return Gen::destroy(); } } // namespace geo diff --git a/src/iterator/grib_iterator_class_lambert_conformal.cc b/src/iterator/grib_iterator_class_lambert_conformal.cc index 469c5e3c3..672abd2ca 100644 --- a/src/iterator/grib_iterator_class_lambert_conformal.cc +++ b/src/iterator/grib_iterator_class_lambert_conformal.cc @@ -479,8 +479,7 @@ int LambertConformal::destroy() grib_context_free(c, lats_); grib_context_free(c, lons_); - Gen::destroy(); - return GRIB_SUCCESS; + return Gen::destroy(); } } // namespace geo diff --git a/src/iterator/grib_iterator_class_latlon_reduced.cc b/src/iterator/grib_iterator_class_latlon_reduced.cc index 90c4702a3..bffa842ec 100644 --- a/src/iterator/grib_iterator_class_latlon_reduced.cc +++ b/src/iterator/grib_iterator_class_latlon_reduced.cc @@ -137,8 +137,7 @@ int LatlonReduced::destroy() grib_context_free(c, las_); grib_context_free(c, los_); - Gen::destroy(); - return GRIB_SUCCESS; + return Gen::destroy(); } } // namespace geo diff --git a/src/iterator/grib_iterator_class_mercator.cc b/src/iterator/grib_iterator_class_mercator.cc index 60fffc3eb..0ddda997d 100644 --- a/src/iterator/grib_iterator_class_mercator.cc +++ b/src/iterator/grib_iterator_class_mercator.cc @@ -280,8 +280,7 @@ int Mercator::destroy() grib_context_free(c, lats_); grib_context_free(c, lons_); - Gen::destroy(); - return GRIB_SUCCESS; + return Gen::destroy(); } } // namespace geo diff --git a/src/iterator/grib_iterator_class_polar_stereographic.cc b/src/iterator/grib_iterator_class_polar_stereographic.cc index 6729531f0..498641c31 100644 --- a/src/iterator/grib_iterator_class_polar_stereographic.cc +++ b/src/iterator/grib_iterator_class_polar_stereographic.cc @@ -305,8 +305,7 @@ int PolarStereographic::destroy() grib_context_free(c, lats_); grib_context_free(c, lons_); - Gen::destroy(); - return GRIB_SUCCESS; + return Gen::destroy(); } } // namespace geo diff --git a/src/iterator/grib_iterator_class_regular.cc b/src/iterator/grib_iterator_class_regular.cc index ce04fcd45..2557223eb 100644 --- a/src/iterator/grib_iterator_class_regular.cc +++ b/src/iterator/grib_iterator_class_regular.cc @@ -54,8 +54,7 @@ int Regular::destroy() grib_context_free(c, las_); grib_context_free(c, los_); - Gen::destroy(); - return GRIB_SUCCESS; + return Gen::destroy(); } int Regular::init(grib_handle* h, grib_arguments* args) diff --git a/src/iterator/grib_iterator_class_space_view.cc b/src/iterator/grib_iterator_class_space_view.cc index 9b2c1d085..5f7957aaf 100644 --- a/src/iterator/grib_iterator_class_space_view.cc +++ b/src/iterator/grib_iterator_class_space_view.cc @@ -320,8 +320,7 @@ int SpaceView::destroy() grib_context_free(c, lats_); grib_context_free(c, lons_); - Gen::destroy(); - return GRIB_SUCCESS; + return Gen::destroy(); } } // namespace geo From 423521b9aac9d3a3e990ca5bfa737b1aace5c48c Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Thu, 17 Oct 2024 11:18:34 +0200 Subject: [PATCH 05/20] Modernisation: Fix memory leak and minor changes --- src/iterator/grib_iterator.cc | 7 ++++--- src/iterator/grib_iterator.h | 1 + src/iterator/grib_iterator_class_regular.h | 8 ++++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/iterator/grib_iterator.cc b/src/iterator/grib_iterator.cc index c045e1775..e6506c8c4 100644 --- a/src/iterator/grib_iterator.cc +++ b/src/iterator/grib_iterator.cc @@ -61,7 +61,9 @@ int Iterator::init(grib_handle* h, grib_arguments* args) /* For this one, ALL destroy are called */ int Iterator::destroy() { - grib_context_free(context_, this); + //grib_context_free(context_, this); + delete context_; + delete this; return GRIB_SUCCESS; } @@ -70,7 +72,6 @@ eccodes::grib::geo::Iterator* gribIteratorNew(const grib_handle* ch, unsigned lo grib_handle* h = (grib_handle*)ch; grib_accessor* a = NULL; grib_accessor_iterator_t* ita = NULL; - //grib_iterator* iter = NULL; *error = GRIB_NOT_IMPLEMENTED; a = grib_find_accessor(h, "ITERATOR"); ita = (grib_accessor_iterator_t*)a; @@ -90,7 +91,7 @@ int gribIteratorDelete(eccodes::grib::geo::Iterator* i) { if (i) i->destroy(); - return 0; + return GRIB_SUCCESS; } diff --git a/src/iterator/grib_iterator.h b/src/iterator/grib_iterator.h index 67ffa2ff9..5ac433f60 100644 --- a/src/iterator/grib_iterator.h +++ b/src/iterator/grib_iterator.h @@ -23,6 +23,7 @@ namespace geo { class Iterator { public: + virtual ~Iterator() {} virtual int init(grib_handle*, grib_arguments*) = 0; virtual int next(double*, double*, double*) = 0; virtual int previous(double*, double*, double*) = 0; diff --git a/src/iterator/grib_iterator_class_regular.h b/src/iterator/grib_iterator_class_regular.h index c2f3afefa..8659041b2 100644 --- a/src/iterator/grib_iterator_class_regular.h +++ b/src/iterator/grib_iterator_class_regular.h @@ -28,10 +28,10 @@ public: int destroy() override; protected: - long Ni_; - long Nj_; - double *las_; - double *los_; + long Ni_; + long Nj_; + double* las_; + double* los_; long iScansNegatively_; long isRotated_; double angleOfRotation_; From b490cfc3d37ff1cc38eabfa6d67c82a4fd173c08 Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Thu, 17 Oct 2024 11:55:01 +0200 Subject: [PATCH 06/20] Modernisation: Protect iterator's init() with a mutex --- src/grib_iterator_class.cc | 35 +++++++++++++++++++++++++++++++++- src/iterator/grib_iterator.cc | 36 +---------------------------------- 2 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/grib_iterator_class.cc b/src/grib_iterator_class.cc index 7f1bd2b3b..92129285e 100644 --- a/src/grib_iterator_class.cc +++ b/src/grib_iterator_class.cc @@ -20,6 +20,34 @@ /* This file is generated by ./make_class.pl */ #include "grib_iterator_class.h" +#if GRIB_PTHREADS +static pthread_once_t once = PTHREAD_ONCE_INIT; +static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + +static void init_mutex() +{ + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&mutex, &attr); + pthread_mutexattr_destroy(&attr); +} +#elif GRIB_OMP_THREADS +static int once = 0; +static omp_nest_lock_t mutex1; + +static void init_mutex() +{ + GRIB_OMP_CRITICAL(lock_grib_accessor_class_c) + { + if (once == 0) { + omp_init_nest_lock(&mutex1); + once = 1; + } + } +} +#endif + struct table_entry { const char* type; @@ -44,7 +72,12 @@ eccodes::grib::geo::Iterator* grib_iterator_factory(grib_handle* h, grib_argumen eccodes::grib::geo::Iterator* builder = *(table[i].iterator); eccodes::grib::geo::Iterator* it = builder->create(); it->flags_ = flags; - *error = it->init(h, args); + + GRIB_MUTEX_INIT_ONCE(&once, &init_mutex); + GRIB_MUTEX_LOCK(&mutex); + *error = it->init(h, args); + GRIB_MUTEX_UNLOCK(&mutex); + if (*error == GRIB_SUCCESS) return it; grib_context_log(h->context, GRIB_LOG_ERROR, "Geoiterator factory: Error instantiating iterator %s (%s)", diff --git a/src/iterator/grib_iterator.cc b/src/iterator/grib_iterator.cc index e6506c8c4..c9eb26281 100644 --- a/src/iterator/grib_iterator.cc +++ b/src/iterator/grib_iterator.cc @@ -18,50 +18,16 @@ namespace eccodes { namespace grib { namespace geo { -#if GRIB_PTHREADS -static pthread_once_t once = PTHREAD_ONCE_INIT; -static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; - -static void init_mutex() -{ - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init(&mutex, &attr); - pthread_mutexattr_destroy(&attr); -} -#elif GRIB_OMP_THREADS -static int once = 0; -static omp_nest_lock_t mutex; - -static void init_mutex() -{ - GRIB_OMP_CRITICAL(lock_iterator_c) - { - if (once == 0) { - omp_init_nest_lock(&mutex); - once = 1; - } - } -} -#endif - int Iterator::init(grib_handle* h, grib_arguments* args) { - int r = 0; h_ = h; - //GRIB_MUTEX_INIT_ONCE(&once, &init_mutex); - //GRIB_MUTEX_LOCK(&mutex); - //r = init_iterator(this, i, h, args); - //GRIB_MUTEX_UNLOCK(&mutex); - return r; + return GRIB_SUCCESS; } /* For this one, ALL destroy are called */ int Iterator::destroy() { - //grib_context_free(context_, this); delete context_; delete this; return GRIB_SUCCESS; From 2bd54826298b18eaa4a964e01356e5fd47cc3b1b Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Thu, 17 Oct 2024 12:06:14 +0200 Subject: [PATCH 07/20] Modernisation: has_next() returns bool instead of long --- src/iterator/grib_iterator.h | 2 +- src/iterator/grib_iterator_class_gen.cc | 8 ++++---- src/iterator/grib_iterator_class_gen.h | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/iterator/grib_iterator.h b/src/iterator/grib_iterator.h index 5ac433f60..0b98821ca 100644 --- a/src/iterator/grib_iterator.h +++ b/src/iterator/grib_iterator.h @@ -29,7 +29,7 @@ public: virtual int previous(double*, double*, double*) = 0; virtual int reset() = 0; virtual int destroy() = 0; - virtual long has_next() = 0; + virtual bool has_next() = 0; virtual Iterator* create() const = 0; public: diff --git a/src/iterator/grib_iterator_class_gen.cc b/src/iterator/grib_iterator_class_gen.cc index e5e7b727d..c2f87280b 100644 --- a/src/iterator/grib_iterator_class_gen.cc +++ b/src/iterator/grib_iterator_class_gen.cc @@ -88,13 +88,13 @@ int Gen::destroy() return Iterator::destroy(); } -long Gen::has_next() +bool Gen::has_next() { if (flags_ == 0 && data_ == NULL) - return 0; + return false; if (e_ >= (long)(nv_ - 1)) - return 0; - return 1; + return false; + return true; } int Gen::previous(double*, double*, double*) { diff --git a/src/iterator/grib_iterator_class_gen.h b/src/iterator/grib_iterator_class_gen.h index c7b349663..0fe7d4874 100644 --- a/src/iterator/grib_iterator_class_gen.h +++ b/src/iterator/grib_iterator_class_gen.h @@ -27,7 +27,7 @@ public: int previous(double*, double*, double*) override; int reset() override; int destroy() override; - long has_next() override; // TODO(maee/masn): return bool please! + bool has_next() override; public: //int get(double*, double*, double*); From a8f43e99b7185e9dfcfc028071478c3ef86bffd8 Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Thu, 17 Oct 2024 12:20:08 +0200 Subject: [PATCH 08/20] Modernisation: Fix grib_iterator_delete() --- src/iterator/grib_iterator.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/iterator/grib_iterator.cc b/src/iterator/grib_iterator.cc index c9eb26281..ae77c1274 100644 --- a/src/iterator/grib_iterator.cc +++ b/src/iterator/grib_iterator.cc @@ -111,12 +111,12 @@ grib_iterator* grib_iterator_new(const grib_handle* ch, unsigned long flags, int int grib_iterator_delete(grib_iterator* i) { - if (i) { // TODO(maee): check if this is necessary - grib_context* c = i->iterator->context_; // TODO(maee): find a better solution + if (i) { + grib_context* c = grib_context_get_default(); gribIteratorDelete(i->iterator); grib_context_free(c, i); } - return 0; + return GRIB_SUCCESS; } #else From c76108923a3f2b9d75e90a76401729b5f9e00654 Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Thu, 17 Oct 2024 12:22:32 +0200 Subject: [PATCH 09/20] Modernisation: clang-format --- src/iterator/grib_iterator.cc | 19 ++- src/iterator/grib_iterator.h | 25 +-- src/iterator/grib_iterator_class_gaussian.cc | 27 ++-- src/iterator/grib_iterator_class_gaussian.h | 20 ++- .../grib_iterator_class_gaussian_reduced.cc | 77 ++++----- .../grib_iterator_class_gaussian_reduced.h | 42 ++--- src/iterator/grib_iterator_class_gen.cc | 34 ++-- src/iterator/grib_iterator_class_gen.h | 22 +-- src/iterator/grib_iterator_class_healpix.cc | 20 ++- src/iterator/grib_iterator_class_healpix.h | 24 +-- ...ator_class_lambert_azimuthal_equal_area.cc | 38 +++-- ...rator_class_lambert_azimuthal_equal_area.h | 22 +-- .../grib_iterator_class_lambert_conformal.cc | 151 ++++++++++-------- .../grib_iterator_class_lambert_conformal.h | 46 +++--- src/iterator/grib_iterator_class_latlon.cc | 44 ++--- src/iterator/grib_iterator_class_latlon.h | 18 ++- .../grib_iterator_class_latlon_reduced.cc | 17 +- .../grib_iterator_class_latlon_reduced.h | 22 +-- src/iterator/grib_iterator_class_mercator.cc | 43 ++--- src/iterator/grib_iterator_class_mercator.h | 25 +-- ...grib_iterator_class_polar_stereographic.cc | 147 +++++++++-------- .../grib_iterator_class_polar_stereographic.h | 22 +-- src/iterator/grib_iterator_class_regular.cc | 23 +-- src/iterator/grib_iterator_class_regular.h | 21 +-- .../grib_iterator_class_space_view.cc | 21 +-- src/iterator/grib_iterator_class_space_view.h | 17 +- 26 files changed, 547 insertions(+), 440 deletions(-) diff --git a/src/iterator/grib_iterator.cc b/src/iterator/grib_iterator.cc index ae77c1274..a95070a79 100644 --- a/src/iterator/grib_iterator.cc +++ b/src/iterator/grib_iterator.cc @@ -14,9 +14,12 @@ #include "grib_iterator.h" #include "../accessor/grib_accessor_class_iterator.h" -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ int Iterator::init(grib_handle* h, grib_arguments* args) @@ -61,12 +64,12 @@ int gribIteratorDelete(eccodes::grib::geo::Iterator* i) } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes -/* +/* * C API Implementation * codes_iterator_* wrappers are in eccodes.h and eccodes.cc * grib_iterator_* declarations are in grib_api.h @@ -101,7 +104,7 @@ int grib_iterator_destroy(grib_context* c, grib_iterator* i) grib_iterator* grib_iterator_new(const grib_handle* ch, unsigned long flags, int* error) { grib_iterator* i = (grib_iterator*)grib_context_malloc_clear(ch->context, sizeof(grib_iterator)); - i->iterator = eccodes::grib::geo::gribIteratorNew(ch, flags, error); + i->iterator = eccodes::grib::geo::gribIteratorNew(ch, flags, error); if (!i->iterator) { grib_context_free(ch->context, i); return NULL; diff --git a/src/iterator/grib_iterator.h b/src/iterator/grib_iterator.h index 0b98821ca..9ac8d147d 100644 --- a/src/iterator/grib_iterator.h +++ b/src/iterator/grib_iterator.h @@ -16,21 +16,24 @@ \ingroup grib_iterator */ -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ class Iterator { public: virtual ~Iterator() {} virtual int init(grib_handle*, grib_arguments*) = 0; - virtual int next(double*, double*, double*) = 0; + virtual int next(double*, double*, double*) = 0; virtual int previous(double*, double*, double*) = 0; - virtual int reset() = 0; - virtual int destroy() = 0; - virtual bool has_next() = 0; - virtual Iterator* create() const = 0; + virtual int reset() = 0; + virtual int destroy() = 0; + virtual bool has_next() = 0; + virtual Iterator* create() const = 0; public: grib_context* context_; @@ -48,6 +51,6 @@ public: eccodes::grib::geo::Iterator* gribIteratorNew(const grib_handle*, unsigned long, int*); int gribIteratorDelete(eccodes::grib::geo::Iterator*); -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_gaussian.cc b/src/iterator/grib_iterator_class_gaussian.cc index 35a2f47a8..5ebbf9bd6 100644 --- a/src/iterator/grib_iterator_class_gaussian.cc +++ b/src/iterator/grib_iterator_class_gaussian.cc @@ -13,25 +13,28 @@ eccodes::grib::geo::Gaussian _grib_iterator_gaussian{}; eccodes::grib::geo::Iterator* grib_iterator_gaussian = &_grib_iterator_gaussian; -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ static void binary_search_gaussian_latitudes(const double xx[], const unsigned long n, double x, long* j); int Gaussian::init(grib_handle* h, grib_arguments* args) { int ret = GRIB_SUCCESS; - if((ret = Regular::init(h, args)) != GRIB_SUCCESS) + if ((ret = Regular::init(h, args)) != GRIB_SUCCESS) return ret; double* lats; double laf; /* latitude of first point in degrees */ double lal; /* latitude of last point in degrees */ long trunc; /* number of parallels between a pole and the equator */ - long lai = 0; + long lai = 0; long jScansPositively = 0; - int size = 0; + int size = 0; double start; long istart = 0; @@ -69,7 +72,7 @@ int Gaussian::init(grib_handle* h, grib_arguments* args) } */ - binary_search_gaussian_latitudes(lats, size-1, start, &istart); + binary_search_gaussian_latitudes(lats, size - 1, start, &istart); if (istart < 0 || istart >= size) { grib_context_log(h->context, GRIB_LOG_ERROR, "Failed to find index for latitude=%g", start); return GRIB_GEOCALCULUS_PROBLEM; @@ -79,7 +82,7 @@ int Gaussian::init(grib_handle* h, grib_arguments* args) for (lai = 0; lai < Nj_; lai++) { DEBUG_ASSERT(istart >= 0); las_[lai] = lats[istart--]; - if (istart<0) istart=size-1; + if (istart < 0) istart = size - 1; } } else { @@ -99,7 +102,7 @@ int Gaussian::init(grib_handle* h, grib_arguments* args) /* Note: the argument 'n' is NOT the size of the 'xx' array but its LAST index i.e. size of xx - 1 */ static void binary_search_gaussian_latitudes(const double array[], const unsigned long n, double x, long* j) { - unsigned long low = 0; + unsigned long low = 0; unsigned long high = n; unsigned long mid; const int descending = (array[n] < array[0]); @@ -149,6 +152,6 @@ static void binary_search_gaussian_latitudes(const double array[], const unsigne // *j = jl; // } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_gaussian.h b/src/iterator/grib_iterator_class_gaussian.h index 7420df8cf..1dd8a70ae 100644 --- a/src/iterator/grib_iterator_class_gaussian.h +++ b/src/iterator/grib_iterator_class_gaussian.h @@ -12,19 +12,23 @@ #include "grib_iterator_class_regular.h" -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ class Gaussian : public Regular { -public: - Gaussian() : Regular() { class_name_ = "gaussian"; } +public: + Gaussian() : + Regular() { class_name_ = "gaussian"; } Iterator* create() const override { return new Gaussian(); } int init(grib_handle*, grib_arguments*) override; }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_gaussian_reduced.cc b/src/iterator/grib_iterator_class_gaussian_reduced.cc index 6a169da57..1c3ee983c 100644 --- a/src/iterator/grib_iterator_class_gaussian_reduced.cc +++ b/src/iterator/grib_iterator_class_gaussian_reduced.cc @@ -14,15 +14,18 @@ eccodes::grib::geo::GaussianReduced _grib_iterator_gaussian_reduced{}; eccodes::grib::geo::Iterator* grib_iterator_gaussian_reduced = &_grib_iterator_gaussian_reduced; -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ #define ITER "Reduced Gaussian grid Geoiterator" int GaussianReduced::next(double* lat, double* lon, double* val) { - double ret_lat=0, ret_lon=0; + double ret_lat = 0, ret_lon = 0; if (e_ >= (long)(nv_ - 1)) return 0; @@ -98,9 +101,9 @@ static void binary_search(const double xx[], const unsigned long n, double x, lo /* Use legacy way to compute the iterator latitude/longitude values */ int GaussianReduced::iterate_reduced_gaussian_subarea_legacy(grib_handle* h, - double lat_first, double lon_first, - double lat_last, double lon_last, - double* lats, long* pl, size_t plsize) + double lat_first, double lon_first, + double lat_last, double lon_last, + double* lats, long* pl, size_t plsize) { int err = 0; int l = 0; @@ -109,7 +112,7 @@ int GaussianReduced::iterate_reduced_gaussian_subarea_legacy(grib_handle* h, double d = 0; long ilon_first, ilon_last, i; /*get_reduced_row_proc get_reduced_row = &grib_get_reduced_row;*/ - get_reduced_row_proc get_reduced_row = &grib_get_reduced_row_legacy; /* legacy algorithm */ + get_reduced_row_proc get_reduced_row = &grib_get_reduced_row_legacy; /* legacy algorithm */ if (h->context->debug) { const size_t np = count_subarea_points(h, get_reduced_row, pl, plsize, lon_first, lon_last); @@ -138,7 +141,7 @@ int GaussianReduced::iterate_reduced_gaussian_subarea_legacy(grib_handle* h, return GRIB_WRONG_GRID; } - los_[e_] = ((i)*360.0) / pl[j]; + los_[e_] = ((i) * 360.0) / pl[j]; las_[e_] = lats[j + l]; e_++; k++; @@ -160,9 +163,9 @@ int GaussianReduced::iterate_reduced_gaussian_subarea_legacy(grib_handle* h, // } // return err; int GaussianReduced::iterate_reduced_gaussian_subarea(grib_handle* h, - double lat_first, double lon_first, - double lat_last, double lon_last, - double* lats, long* pl, size_t plsize, size_t numlats) + double lat_first, double lon_first, + double lat_last, double lon_last, + double* lats, long* pl, size_t plsize, size_t numlats) { int err = 0; long l = 0; @@ -180,13 +183,13 @@ int GaussianReduced::iterate_reduced_gaussian_subarea(grib_handle* h, binary_search(lats, numlats - 1, lat_first, &l); Assert(l < numlats); -// for(il=0; ilcontext; - const char* slat_first = grib_arguments_get_name(h, args, carg_++); - const char* slon_first = grib_arguments_get_name(h, args, carg_++); - const char* slat_last = grib_arguments_get_name(h, args, carg_++); - const char* slon_last = grib_arguments_get_name(h, args, carg_++); - const char* sorder = grib_arguments_get_name(h, args, carg_++); - const char* spl = grib_arguments_get_name(h, args, carg_++); - const char* snj = grib_arguments_get_name(h, args, carg_++); + const grib_context* c = h->context; + const char* slat_first = grib_arguments_get_name(h, args, carg_++); + const char* slon_first = grib_arguments_get_name(h, args, carg_++); + const char* slat_last = grib_arguments_get_name(h, args, carg_++); + const char* slon_last = grib_arguments_get_name(h, args, carg_++); + const char* sorder = grib_arguments_get_name(h, args, carg_++); + const char* spl = grib_arguments_get_name(h, args, carg_++); + const char* snj = grib_arguments_get_name(h, args, carg_++); - angleOfRotation_ = 0; - isRotated_ = 0; - southPoleLat_ = 0; - southPoleLon_ = 0; - disableUnrotate_ = 0; /* unrotate enabled by default */ + angleOfRotation_ = 0; + isRotated_ = 0; + southPoleLat_ = 0; + southPoleLon_ = 0; + disableUnrotate_ = 0; /* unrotate enabled by default */ ret = grib_get_long(h, "isRotatedGrid", &isRotated_); if (ret == GRIB_SUCCESS && isRotated_) { @@ -368,7 +371,7 @@ finalise: int GaussianReduced::destroy() { - const grib_context* c = h_->context; + const grib_context* c = h_->context; grib_context_free(c, las_); grib_context_free(c, los_); @@ -376,6 +379,6 @@ int GaussianReduced::destroy() return Gen::destroy(); } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_gaussian_reduced.h b/src/iterator/grib_iterator_class_gaussian_reduced.h index 46459e082..7ffa0dc3d 100644 --- a/src/iterator/grib_iterator_class_gaussian_reduced.h +++ b/src/iterator/grib_iterator_class_gaussian_reduced.h @@ -12,23 +12,27 @@ #include "grib_iterator_class_gen.h" -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ class GaussianReduced : public Gen { public: - GaussianReduced() : Gen() { class_name_ = "gaussian_reduced"; } + GaussianReduced() : + Gen() { class_name_ = "gaussian_reduced"; } Iterator* create() const override { return new GaussianReduced(); } - int init(grib_handle*,grib_arguments*) override; - int next(double *, double *, double *) override; + int init(grib_handle*, grib_arguments*) override; + int next(double*, double*, double*) override; int destroy() override; private: - double *las_; - double *los_; + double* las_; + double* los_; long Nj_; long isRotated_; double angleOfRotation_; @@ -36,17 +40,17 @@ private: double southPoleLon_; long disableUnrotate_; -int iterate_reduced_gaussian_subarea_legacy(grib_handle*, - double, double, - double, double, - double*, long*, size_t); + int iterate_reduced_gaussian_subarea_legacy(grib_handle*, + double, double, + double, double, + double*, long*, size_t); -int iterate_reduced_gaussian_subarea(grib_handle*, - double, double, - double, double, - double*, long*, size_t, size_t); + int iterate_reduced_gaussian_subarea(grib_handle*, + double, double, + double, double, + double*, long*, size_t, size_t); }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_gen.cc b/src/iterator/grib_iterator_class_gen.cc index c2f87280b..8da4d693a 100644 --- a/src/iterator/grib_iterator_class_gen.cc +++ b/src/iterator/grib_iterator_class_gen.cc @@ -10,9 +10,12 @@ #include "grib_iterator_class_gen.h" -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ int Gen::init(grib_handle* h, grib_arguments* args) { @@ -31,8 +34,8 @@ int Gen::init(grib_handle* h, grib_arguments* args) s_rawData = grib_arguments_get_name(h, args, carg_++); data_ = NULL; - h = h; /* We may not need to keep them */ - args = args; + h = h; /* We may not need to keep them */ + args = args; if ((err = grib_get_size(h, s_rawData, &dli)) != GRIB_SUCCESS) return err; @@ -44,7 +47,8 @@ int Gen::init(grib_handle* h, grib_arguments* args) if (flags_ & GRIB_GEOITERATOR_NO_VALUES) { // Iterator's number of values taken from the Grid Section nv_ = numberOfPoints; - } else { + } + else { // Check for consistency between the Grid and Data Sections if (numberOfPoints != dli) { grib_context_log(h->context, GRIB_LOG_ERROR, "Geoiterator: %s != size(%s) (%ld!=%ld)", @@ -97,18 +101,20 @@ bool Gen::has_next() return true; } -int Gen::previous(double*, double*, double*) { +int Gen::previous(double*, double*, double*) +{ return GRIB_NOT_IMPLEMENTED; } -int Gen::next(double*, double*, double*) { +int Gen::next(double*, double*, double*) +{ return GRIB_NOT_IMPLEMENTED; } -//int Gen::get(double* lat, double* lon, double* val) +// int Gen::get(double* lat, double* lon, double* val) //{ -// if (e_ >= (long)(nv_ - 1)) -// return GRIB_END_OF_ITERATION; +// if (e_ >= (long)(nv_ - 1)) +// return GRIB_END_OF_ITERATION; // e_++; // if (lat) *lat = 0; @@ -118,6 +124,6 @@ int Gen::next(double*, double*, double*) { // return 1; //} -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_gen.h b/src/iterator/grib_iterator_class_gen.h index 0fe7d4874..711839d4a 100644 --- a/src/iterator/grib_iterator_class_gen.h +++ b/src/iterator/grib_iterator_class_gen.h @@ -12,17 +12,21 @@ #include "grib_iterator.h" -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ class Gen : public Iterator { public: - Gen() : Iterator() { class_name_ = "gen"; } + Gen() : + Iterator() { class_name_ = "gen"; } Iterator* create() const override { return new Gen(); } - int init(grib_handle*,grib_arguments*) override; + int init(grib_handle*, grib_arguments*) override; int next(double*, double*, double*) override; int previous(double*, double*, double*) override; int reset() override; @@ -30,11 +34,11 @@ public: bool has_next() override; public: - //int get(double*, double*, double*); + // int get(double*, double*, double*); int carg_; const char* missingValue_; }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_healpix.cc b/src/iterator/grib_iterator_class_healpix.cc index 7e3148f00..b90eb49a3 100644 --- a/src/iterator/grib_iterator_class_healpix.cc +++ b/src/iterator/grib_iterator_class_healpix.cc @@ -20,9 +20,12 @@ eccodes::grib::geo::Healpix _grib_iterator_healpix; eccodes::grib::geo::Iterator* grib_iterator_healpix = &_grib_iterator_healpix; -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ #define ITER "HEALPix Geoiterator" constexpr double RAD2DEG = 57.29577951308232087684; // 180 over pi @@ -241,16 +244,17 @@ int Healpix::iterate_healpix(long N) ring_to_nest[r] = to_nest(f, ring, Nside, phi, ring & 1); } - for (size_t i = 0, j=0; i < Ny; i++) { + for (size_t i = 0, j = 0; i < Ny; i++) { // Compute the longitudes at a given latitude for (double longitude : HEALPix_longitudes(N, i)) { - Assert( ring_to_nest.at(j) < Npix ); + Assert(ring_to_nest.at(j) < Npix); lons_[ring_to_nest.at(j)] = longitude; lats_[ring_to_nest.at(j)] = latitudes[i]; ++j; } } - } else { + } + else { for (size_t i = 0, j = 0; i < Ny; i++) { // Compute the longitudes at a given latitude for (double longitude : HEALPix_longitudes(N, i)) { @@ -282,7 +286,9 @@ int Healpix::init(grib_handle* h, grib_arguments* args) return GRIB_WRONG_GRID; } - char ordering[32] = {0,}; + char ordering[32] = { + 0, + }; size_t slen = sizeof(ordering); if ((err = grib_get_string_internal(h, sorder, ordering, &slen)) != GRIB_SUCCESS) { return err; diff --git a/src/iterator/grib_iterator_class_healpix.h b/src/iterator/grib_iterator_class_healpix.h index 53ad2a703..80ed53cc9 100644 --- a/src/iterator/grib_iterator_class_healpix.h +++ b/src/iterator/grib_iterator_class_healpix.h @@ -12,23 +12,27 @@ #include "grib_iterator_class_gen.h" -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ class Healpix : public Gen { public: - Healpix() : Gen() { class_name_ = "healpix"; } + Healpix() : + Gen() { class_name_ = "healpix"; } Iterator* create() const override { return new Healpix(); } int init(grib_handle*, grib_arguments*) override; - int next(double *, double *, double *) override; + int next(double*, double*, double*) override; int destroy() override; public: - double *lats_; - double *lons_; + double* lats_; + double* lons_; long Nsides_; bool nested_; @@ -36,6 +40,6 @@ private: int iterate_healpix(long N); }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc index 321e46c35..75863b808 100644 --- a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc +++ b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc @@ -13,9 +13,12 @@ eccodes::grib::geo::LambertAzimuthalEqualArea _grib_iterator_lambert_azimuthal_equal_area{}; eccodes::grib::geo::Iterator* grib_iterator_lambert_azimuthal_equal_area = &_grib_iterator_lambert_azimuthal_equal_area; -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ #define ITER "Lambert azimuthal equal area Geoiterator" @@ -34,15 +37,15 @@ int LambertAzimuthalEqualArea::next(double* lat, double* lon, double* val) } #ifndef M_PI -#define M_PI 3.14159265358979323846 /* Whole pie */ + #define M_PI 3.14159265358979323846 /* Whole pie */ #endif #ifndef M_PI_2 -#define M_PI_2 1.57079632679489661923 /* Half a pie */ + #define M_PI_2 1.57079632679489661923 /* Half a pie */ #endif #ifndef M_PI_4 -#define M_PI_4 0.78539816339744830962 /* Quarter of a pie */ + #define M_PI_4 0.78539816339744830962 /* Quarter of a pie */ #endif #define RAD2DEG 57.29577951308232087684 /* 180 over pi */ @@ -109,7 +112,9 @@ static int init_oblate(grib_handle* h, double Q__qp = 0, Q__rq = 0, Q__cosb1, Q__sinb1, Q__dd, Q__xmf, Q__ymf, t; /* double Q__mmf = 0; */ double e, es, temp, one_es; - double APA[3] = {0,}; + double APA[3] = { + 0, + }; double xFirst, yFirst; Dx = iScansNegatively == 0 ? Dx / 1000 : -Dx / 1000; @@ -136,7 +141,7 @@ static int init_oblate(grib_handle* h, else Q->mode = OBLIQ; */ - Q__qp = pj_qsfn(1.0, e, one_es); + Q__qp = pj_qsfn(1.0, e, one_es); /* Q__mmf = 0.5 / one_es; ---- TODO(masn): do I need this? */ pj_authset(es, APA); /* sets up APA array */ Q__rq = sqrt(0.5 * Q__qp); @@ -145,10 +150,11 @@ static int init_oblate(grib_handle* h, Q__cosb1 = sqrt(1.0 - Q__sinb1 * Q__sinb1); if (Q__cosb1 == 0) { Q__dd = 1.0; - } else { + } + else { Q__dd = cos(standardParallelInRadians) / (sqrt(1. - es * sinphi_ * sinphi_) * Q__rq * Q__cosb1); } - Q__ymf = (Q__xmf = Q__rq) / Q__dd; + Q__ymf = (Q__xmf = Q__rq) / Q__dd; Q__xmf *= Q__dd; sinb = q / Q__qp; @@ -249,8 +255,8 @@ static int init_sphere(grib_handle* h, cosphi1 = cos(phi1); sinphi1 = sin(phi1); - Dx = iScansNegatively == 0 ? Dx / 1000 : -Dx / 1000; - Dy = jScansPositively == 1 ? Dy / 1000 : -Dy / 1000; + Dx = iScansNegatively == 0 ? Dx / 1000 : -Dx / 1000; + Dy = jScansPositively == 1 ? Dy / 1000 : -Dy / 1000; self->lats_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); if (!self->lats_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); @@ -430,7 +436,7 @@ int LambertAzimuthalEqualArea::init(grib_handle* h, grib_arguments* args) int LambertAzimuthalEqualArea::destroy() { - const grib_context* c = h_->context; + const grib_context* c = h_->context; grib_context_free(c, lats_); grib_context_free(c, lons_); @@ -438,6 +444,6 @@ int LambertAzimuthalEqualArea::destroy() return Gen::destroy(); } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h index 64c385e7b..60f9b4228 100644 --- a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h +++ b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h @@ -12,14 +12,18 @@ #include "grib_iterator_class_gen.h" -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ class LambertAzimuthalEqualArea : public Gen { public: - LambertAzimuthalEqualArea() : Gen() { class_name_ = "lambert_azimuthal_equal_area"; } + LambertAzimuthalEqualArea() : + Gen() { class_name_ = "lambert_azimuthal_equal_area"; } Iterator* create() const override { return new LambertAzimuthalEqualArea(); } int init(grib_handle*, grib_arguments*) override; @@ -27,11 +31,11 @@ public: int destroy() override; public: - double *lats_; - double *lons_; + double* lats_; + double* lons_; long Nj_; }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_lambert_conformal.cc b/src/iterator/grib_iterator_class_lambert_conformal.cc index 672abd2ca..e85d8d634 100644 --- a/src/iterator/grib_iterator_class_lambert_conformal.cc +++ b/src/iterator/grib_iterator_class_lambert_conformal.cc @@ -14,32 +14,35 @@ eccodes::grib::geo::LambertConformal _grib_iterator_lambert_conformal{}; eccodes::grib::geo::Iterator* grib_iterator_lambert_conformal = &_grib_iterator_lambert_conformal; -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ -#define ITER "Lambert conformal Geoiterator" +#define ITER "Lambert conformal Geoiterator" #define EPSILON 1.0e-10 #ifndef M_PI -#define M_PI 3.14159265358979323846 // Whole pie + #define M_PI 3.14159265358979323846 // Whole pie #endif #ifndef M_PI_2 -#define M_PI_2 1.57079632679489661923 // Half a pie + #define M_PI_2 1.57079632679489661923 // Half a pie #endif #ifndef M_PI_4 -#define M_PI_4 0.78539816339744830962 // Quarter of a pie + #define M_PI_4 0.78539816339744830962 // Quarter of a pie #endif -#define RAD2DEG 57.29577951308232087684 // 180 over pi -#define DEG2RAD 0.01745329251994329576 // pi over 180 +#define RAD2DEG 57.29577951308232087684 // 180 over pi +#define DEG2RAD 0.01745329251994329576 // pi over 180 // Adjust longitude (in radians) to range -180 to 180 static double adjust_lon_radians(double lon) { - if (lon > M_PI) lon -= 2 * M_PI; + if (lon > M_PI) lon -= 2 * M_PI; if (lon < -M_PI) lon += 2 * M_PI; return lon; } @@ -51,8 +54,8 @@ static double adjust_lon_radians(double lon) // calculate phi on the left side. Substitute the calculated phi) into the right side, // calculate a new phi, etc., until phi does not change significantly from the preceding trial value of phi static double compute_phi( - double eccent, // Spheroid eccentricity - double ts, // Constant value t + double eccent, // Spheroid eccentricity + double ts, // Constant value t int* error) { double eccnth, phi, con, dphi, sinpi; @@ -82,9 +85,9 @@ static double compute_m(double eccent, double sinphi, double cosphi) // Compute the constant small t for use in the forward computations static double compute_t( - double eccent, // Eccentricity of the spheroid - double phi, // Latitude phi - double sinphi) // Sine of the latitude + double eccent, // Eccentricity of the spheroid + double phi, // Latitude phi + double sinphi) // Sine of the latitude { double con = eccent * sinphi; double com = 0.5 * eccent; @@ -99,25 +102,25 @@ static double calculate_eccentricity(double minor, double major) } static void xy2lonlat(double radius, double n, double f, double rho0_bare, double LoVInRadians, - double x, double y, - double* lonDeg, double* latDeg) + double x, double y, + double* lonDeg, double* latDeg) { DEBUG_ASSERT(radius > 0); DEBUG_ASSERT(n != 0.0); x /= radius; y /= radius; - y = rho0_bare - y; + y = rho0_bare - y; double rho = hypot(x, y); if (rho != 0.0) { if (n < 0.0) { rho = -rho; - x = -x; - y = -y; + x = -x; + y = -y; } - double latRadians = 2. * atan(pow(f / rho, 1.0/n)) - M_PI_2; + double latRadians = 2. * atan(pow(f / rho, 1.0 / n)) - M_PI_2; double lonRadians = atan2(x, y) / n; - *lonDeg = (lonRadians + LoVInRadians) * RAD2DEG; - *latDeg = latRadians * RAD2DEG; + *lonDeg = (lonRadians + LoVInRadians) * RAD2DEG; + *latDeg = latRadians * RAD2DEG; } else { *lonDeg = 0.0; @@ -126,27 +129,28 @@ static void xy2lonlat(double radius, double n, double f, double rho0_bare, doubl } int LambertConformal::init_sphere(const grib_handle* h, - size_t nv, long nx, long ny, - double LoVInDegrees, - double Dx, double Dy, double radius, - double latFirstInRadians, double lonFirstInRadians, - double LoVInRadians, double Latin1InRadians, double Latin2InRadians, - double LaDInRadians) + size_t nv, long nx, long ny, + double LoVInDegrees, + double Dx, double Dy, double radius, + double latFirstInRadians, double lonFirstInRadians, + double LoVInRadians, double Latin1InRadians, double Latin2InRadians, + double LaDInRadians) { double n, x, y; if (fabs(Latin1InRadians - Latin2InRadians) < 1E-09) { n = sin(Latin1InRadians); - } else { + } + else { n = log(cos(Latin1InRadians) / cos(Latin2InRadians)) / log(tan(M_PI_4 + Latin2InRadians / 2.0) / tan(M_PI_4 + Latin1InRadians / 2.0)); } - double f = (cos(Latin1InRadians) * pow(tan(M_PI_4 + Latin1InRadians / 2.0), n)) / n; - double rho = radius * f * pow(tan(M_PI_4 + latFirstInRadians / 2.0), -n); + double f = (cos(Latin1InRadians) * pow(tan(M_PI_4 + Latin1InRadians / 2.0), n)) / n; + double rho = radius * f * pow(tan(M_PI_4 + latFirstInRadians / 2.0), -n); double rho0_bare = f * pow(tan(M_PI_4 + LaDInRadians / 2.0), -n); - double rho0 = radius * rho0_bare; // scaled - double lonDiff = lonFirstInRadians - LoVInRadians; + double rho0 = radius * rho0_bare; // scaled + double lonDiff = lonFirstInRadians - LoVInRadians; // Adjust longitude to range -180 to 180 if (lonDiff > M_PI) @@ -177,9 +181,9 @@ int LambertConformal::init_sphere(const grib_handle* h, y = y0 + j * Dy; for (long i = 0; i < nx; i++) { const long index = i + j * nx; - x = x0 + i * Dx; + x = x0 + i * Dx; xy2lonlat(radius, n, f, rho0_bare, LoVInRadians, x, y, &lonDeg, &latDeg); - lonDeg = normalise_longitude_in_degrees(lonDeg); + lonDeg = normalise_longitude_in_degrees(lonDeg); lons_[index] = lonDeg; lats_[index] = latDeg; } @@ -215,30 +219,30 @@ int LambertConformal::init_sphere(const grib_handle* h, // Oblate spheroid int LambertConformal::init_oblate(const grib_handle* h, - size_t nv, long nx, long ny, - double LoVInDegrees, - double Dx, double Dy, - double earthMinorAxisInMetres, double earthMajorAxisInMetres, - double latFirstInRadians, double lonFirstInRadians, - double LoVInRadians, double Latin1InRadians, double Latin2InRadians, - double LaDInRadians) + size_t nv, long nx, long ny, + double LoVInDegrees, + double Dx, double Dy, + double earthMinorAxisInMetres, double earthMajorAxisInMetres, + double latFirstInRadians, double lonFirstInRadians, + double LoVInRadians, double Latin1InRadians, double Latin2InRadians, + double LaDInRadians) { int i, j, err = 0; double x0, y0, x, y, latRad, lonRad, latDeg, lonDeg, sinphi, ts, rh1, theta; - double false_easting; // x offset in meters - double false_northing; // y offset in meters + double false_easting; // x offset in meters + double false_northing; // y offset in meters - double ns; // ratio of angle between meridian - double F; // flattening of ellipsoid - double rh; // height above ellipsoid - double sin_po; // sin value - double cos_po; // cos value - double con; // temporary variable - double ms1; // small m 1 - double ms2; // small m 2 - double ts0; // small t 0 - double ts1; // small t 1 - double ts2; // small t 2 + double ns; // ratio of angle between meridian + double F; // flattening of ellipsoid + double rh; // height above ellipsoid + double sin_po; // sin value + double cos_po; // cos value + double con; // temporary variable + double ms1; // small m 1 + double ms2; // small m 2 + double ts0; // small t 0 + double ts1; // small t 1 + double ts2; // small t 2 double e = calculate_eccentricity(earthMinorAxisInMetres, earthMajorAxisInMetres); @@ -257,10 +261,11 @@ int LambertConformal::init_oblate(const grib_handle* h, if (fabs(Latin1InRadians - Latin2InRadians) > EPSILON) { ns = log(ms1 / ms2) / log(ts1 / ts2); - } else { + } + else { ns = con; } - F = ms1 / (ns * pow(ts1, ns)); + F = ms1 / (ns * pow(ts1, ns)); rh = earthMajorAxisInMetres * F * pow(ts0, ns); // Forward projection: convert lat,lon to x,y @@ -269,7 +274,8 @@ int LambertConformal::init_oblate(const grib_handle* h, sinphi = sin(latFirstInRadians); ts = compute_t(e, latFirstInRadians, sinphi); rh1 = earthMajorAxisInMetres * F * pow(ts, ns); - } else { + } + else { con = latFirstInRadians * ns; if (con <= 0) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Point cannot be projected: latFirstInRadians=%g", ITER, latFirstInRadians); @@ -305,8 +311,8 @@ int LambertConformal::init_oblate(const grib_handle* h, double _x, _y; x = i * Dx; // Inverse projection to convert from x,y to lat,lon - _x = x - false_easting; - _y = rh - y + false_northing; + _x = x - false_easting; + _y = rh - y + false_northing; rh1 = sqrt(_x * _x + _y * _y); con = 1.0; if (ns <= 0) { @@ -327,15 +333,16 @@ int LambertConformal::init_oblate(const grib_handle* h, grib_context_free(h->context, lons_); return err; } - } else { + } + else { latRad = -M_PI_2; } lonRad = adjust_lon_radians(theta / ns + LoVInRadians); if (i == 0 && j == 0) { DEBUG_ASSERT(fabs(latFirstInRadians - latRad) <= EPSILON); } - latDeg = latRad * RAD2DEG; // Convert to degrees - lonDeg = normalise_longitude_in_degrees(lonRad * RAD2DEG); + latDeg = latRad * RAD2DEG; // Convert to degrees + lonDeg = normalise_longitude_in_degrees(lonRad * RAD2DEG); lons_[index] = lonDeg; lats_[index] = latDeg; } @@ -355,7 +362,7 @@ int LambertConformal::init(grib_handle* h, grib_arguments* args) lonFirstInDegrees, Dx, Dy, radius = 0; double latFirstInRadians, lonFirstInRadians, LoVInRadians, Latin1InRadians, Latin2InRadians, LaDInRadians; - double earthMajorAxisInMetres=0, earthMinorAxisInMetres=0; + double earthMajorAxisInMetres = 0, earthMinorAxisInMetres = 0; const char* sradius = grib_arguments_get_name(h, args, carg_++); const char* snx = grib_arguments_get_name(h, args, carg_++); @@ -382,7 +389,8 @@ int LambertConformal::init(grib_handle* h, grib_arguments* args) if (is_oblate) { if ((err = grib_get_double_internal(h, "earthMinorAxisInMetres", &earthMinorAxisInMetres)) != GRIB_SUCCESS) return err; if ((err = grib_get_double_internal(h, "earthMajorAxisInMetres", &earthMajorAxisInMetres)) != GRIB_SUCCESS) return err; - } else { + } + else { if ((err = grib_get_double_internal(h, sradius, &radius)) != GRIB_SUCCESS) return err; } @@ -440,7 +448,8 @@ int LambertConformal::init(grib_handle* h, grib_arguments* args) latFirstInRadians, lonFirstInRadians, LoVInRadians, Latin1InRadians, Latin2InRadians, LaDInRadians); - } else { + } + else { err = init_sphere(h, nv_, nx, ny, LoVInDegrees, Dx, Dy, radius, @@ -474,7 +483,7 @@ int LambertConformal::next(double* lat, double* lon, double* val) int LambertConformal::destroy() { - const grib_context* c = h_->context; + const grib_context* c = h_->context; grib_context_free(c, lats_); grib_context_free(c, lons_); @@ -482,6 +491,6 @@ int LambertConformal::destroy() return Gen::destroy(); } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_lambert_conformal.h b/src/iterator/grib_iterator_class_lambert_conformal.h index 41b8fe995..2255a18aa 100644 --- a/src/iterator/grib_iterator_class_lambert_conformal.h +++ b/src/iterator/grib_iterator_class_lambert_conformal.h @@ -12,17 +12,21 @@ #include "grib_iterator_class_gen.h" -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ class LambertConformal : public Gen { public: - LambertConformal() : Gen() { class_name_ = "lambert_conformal"; } + LambertConformal() : + Gen() { class_name_ = "lambert_conformal"; } Iterator* create() const override { return new LambertConformal(); } - int init(grib_handle*,grib_arguments*) override; + int init(grib_handle*, grib_arguments*) override; int next(double*, double*, double*) override; int destroy() override; @@ -32,23 +36,23 @@ private: long Nj_; int init_sphere(const grib_handle*, - size_t, long, long, - double, - double, double, double, - double, double, - double, double, double, - double); + size_t, long, long, + double, + double, double, double, + double, double, + double, double, double, + double); int init_oblate(const grib_handle*, - size_t, long, long, - double, - double, double, - double, double, - double, double, - double, double, double, - double); + size_t, long, long, + double, + double, double, + double, double, + double, double, + double, double, double, + double); }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_latlon.cc b/src/iterator/grib_iterator_class_latlon.cc index 38eddad52..d931ad620 100644 --- a/src/iterator/grib_iterator_class_latlon.cc +++ b/src/iterator/grib_iterator_class_latlon.cc @@ -13,14 +13,17 @@ eccodes::grib::geo::Latlon _grib_iterator_latlon{}; eccodes::grib::geo::Iterator* grib_iterator_latlon = &_grib_iterator_latlon; -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ int Latlon::next(double* lat, double* lon, double* val) { /* GRIB-238: Support rotated lat/lon grids */ - double ret_lat, ret_lon, ret_val=0; + double ret_lat, ret_lon, ret_val = 0; if ((long)e_ >= (long)(nv_ - 1)) return 0; @@ -69,7 +72,7 @@ int Latlon::init(grib_handle* h, grib_arguments* args) return err; double jdir; - double lat1=0, lat2=0, north=0, south=0; + double lat1 = 0, lat2 = 0, north = 0, south = 0; long jScansPositively; long lai; @@ -82,11 +85,11 @@ int Latlon::init(grib_handle* h, grib_arguments* args) const char* s_latSouthernPole = grib_arguments_get_name(h, args, carg_++); const char* s_lonSouthernPole = grib_arguments_get_name(h, args, carg_++); - angleOfRotation_ = 0; - isRotated_ = 0; - southPoleLat_ = 0; - southPoleLon_ = 0; - disableUnrotate_ = 0; /* unrotate enabled by default */ + angleOfRotation_ = 0; + isRotated_ = 0; + southPoleLat_ = 0; + southPoleLon_ = 0; + disableUnrotate_ = 0; /* unrotate enabled by default */ if ((err = grib_get_long(h, s_isRotatedGrid, &isRotated_))) return err; @@ -103,7 +106,7 @@ int Latlon::init(grib_handle* h, grib_arguments* args) return err; if ((err = grib_get_double_internal(h, "latitudeLastInDegrees", &lat2))) return err; - if ((err = grib_get_double_internal(h, s_jdir, &jdir))) //can be GRIB_MISSING_DOUBLE + if ((err = grib_get_double_internal(h, s_jdir, &jdir))) // can be GRIB_MISSING_DOUBLE return err; if ((err = grib_get_long_internal(h, s_jScansPos, &jScansPositively))) return err; @@ -114,7 +117,7 @@ int Latlon::init(grib_handle* h, grib_arguments* args) /* ECC-984: If jDirectionIncrement is missing, then we cannot use it (See jDirectionIncrementGiven) */ /* So try to compute the increment */ - if ( (grib_is_missing(h, s_jdir, &err) && err == GRIB_SUCCESS) || (jdir == GRIB_MISSING_DOUBLE) ) { + if ((grib_is_missing(h, s_jdir, &err) && err == GRIB_SUCCESS) || (jdir == GRIB_MISSING_DOUBLE)) { const long Nj = Nj_; Assert(Nj > 1); if (lat1 > lat2) { @@ -124,14 +127,15 @@ int Latlon::init(grib_handle* h, grib_arguments* args) jdir = (lat2 - lat1) / (Nj - 1); } grib_context_log(h->context, GRIB_LOG_DEBUG, - "Cannot use jDirectionIncrement. Using value of %.6f obtained from La1, La2 and Nj", jdir); + "Cannot use jDirectionIncrement. Using value of %.6f obtained from La1, La2 and Nj", jdir); } if (jScansPositively) { north = lat2; south = lat1; - jdir = -jdir; - } else { + jdir = -jdir; + } + else { north = lat1; south = lat2; } @@ -148,13 +152,13 @@ int Latlon::init(grib_handle* h, grib_arguments* args) } /* ECC-1406: Due to rounding, errors can accumulate. * So we ensure the last latitude is latitudeOfLastGridPointInDegrees - */ - las_[Nj_-1] = lat2; + */ + las_[Nj_ - 1] = lat2; e_ = -1; return err; } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_latlon.h b/src/iterator/grib_iterator_class_latlon.h index bc74d4509..ca3121438 100644 --- a/src/iterator/grib_iterator_class_latlon.h +++ b/src/iterator/grib_iterator_class_latlon.h @@ -12,20 +12,24 @@ #include "grib_iterator_class_regular.h" -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ class Latlon : public Regular { public: - Latlon() : Regular() { class_name_ = "latlon"; } + Latlon() : + Regular() { class_name_ = "latlon"; } Iterator* create() const override { return new Latlon(); } int init(grib_handle*, grib_arguments*) override; int next(double*, double*, double*) override; }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_latlon_reduced.cc b/src/iterator/grib_iterator_class_latlon_reduced.cc index bffa842ec..9b3273f76 100644 --- a/src/iterator/grib_iterator_class_latlon_reduced.cc +++ b/src/iterator/grib_iterator_class_latlon_reduced.cc @@ -13,9 +13,12 @@ eccodes::grib::geo::LatlonReduced _grib_iterator_latlon_reduced{}; eccodes::grib::geo::Iterator* grib_iterator_latlon_reduced = &_grib_iterator_latlon_reduced; -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ int LatlonReduced::next(double* lat, double* lon, double* val) { @@ -132,7 +135,7 @@ int LatlonReduced::init(grib_handle* h, grib_arguments* args) int LatlonReduced::destroy() { - const grib_context* c = h_->context; + const grib_context* c = h_->context; grib_context_free(c, las_); grib_context_free(c, los_); @@ -140,6 +143,6 @@ int LatlonReduced::destroy() return Gen::destroy(); } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_latlon_reduced.h b/src/iterator/grib_iterator_class_latlon_reduced.h index 5c6537179..c4ab8f04f 100644 --- a/src/iterator/grib_iterator_class_latlon_reduced.h +++ b/src/iterator/grib_iterator_class_latlon_reduced.h @@ -12,18 +12,22 @@ #include "grib_iterator_class_gen.h" -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ class LatlonReduced : public Gen { public: - LatlonReduced() : Gen() { class_name_ = "latlon_reduced"; } + LatlonReduced() : + Gen() { class_name_ = "latlon_reduced"; } Iterator* create() const override { return new LatlonReduced(); } - int init(grib_handle*,grib_arguments*) override; - int next(double *lat, double *lon, double *val) override; + int init(grib_handle*, grib_arguments*) override; + int next(double* lat, double* lon, double* val) override; int destroy() override; private: @@ -31,6 +35,6 @@ private: double* los_; }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_mercator.cc b/src/iterator/grib_iterator_class_mercator.cc index 0ddda997d..abfde3dbd 100644 --- a/src/iterator/grib_iterator_class_mercator.cc +++ b/src/iterator/grib_iterator_class_mercator.cc @@ -13,23 +13,26 @@ eccodes::grib::geo::Mercator _grib_iterator_mercator{}; eccodes::grib::geo::Iterator* grib_iterator_mercator = &_grib_iterator_mercator; -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ -#define ITER "Mercator Geoiterator" +#define ITER "Mercator Geoiterator" #define EPSILON 1.0e-10 #ifndef M_PI -#define M_PI 3.14159265358979323846 /* Whole pie */ + #define M_PI 3.14159265358979323846 /* Whole pie */ #endif #ifndef M_PI_2 -#define M_PI_2 1.57079632679489661923 /* Half a pie */ + #define M_PI_2 1.57079632679489661923 /* Half a pie */ #endif #ifndef M_PI_4 -#define M_PI_4 0.78539816339744830962 /* Quarter of a pie */ + #define M_PI_4 0.78539816339744830962 /* Quarter of a pie */ #endif #define RAD2DEG 57.29577951308232087684 /* 180 over pi */ @@ -38,7 +41,7 @@ namespace geo { /* Adjust longitude (in radians) to range -180 to 180 */ static double adjust_lon_radians(double lon) { - if (lon > M_PI) lon -= 2 * M_PI; + if (lon > M_PI) lon -= 2 * M_PI; if (lon < -M_PI) lon += 2 * M_PI; return lon; } @@ -85,12 +88,12 @@ static double compute_t( } int Mercator::init_mercator(grib_handle* h, - size_t nv, long nx, long ny, - double DiInMetres, double DjInMetres, - double earthMinorAxisInMetres, double earthMajorAxisInMetres, - double latFirstInRadians, double lonFirstInRadians, - double latLastInRadians, double lonLastInRadians, - double LaDInRadians, double orientationInRadians) + size_t nv, long nx, long ny, + double DiInMetres, double DjInMetres, + double earthMinorAxisInMetres, double earthMajorAxisInMetres, + double latFirstInRadians, double lonFirstInRadians, + double latLastInRadians, double lonLastInRadians, + double LaDInRadians, double orientationInRadians) { int i, j, err = 0; double x0, y0, x, y, latRad, lonRad, latDeg, lonDeg, sinphi, ts; @@ -154,8 +157,8 @@ int Mercator::init_mercator(grib_handle* h, if (i == 0 && j == 0) { DEBUG_ASSERT(fabs(latFirstInRadians - latRad) <= EPSILON); } - latDeg = latRad * RAD2DEG; /* Convert to degrees */ - lonDeg = normalise_longitude_in_degrees(lonRad * RAD2DEG); + latDeg = latRad * RAD2DEG; /* Convert to degrees */ + lonDeg = normalise_longitude_in_degrees(lonRad * RAD2DEG); lons_[index] = lonDeg; lats_[index] = latDeg; } @@ -275,7 +278,7 @@ int Mercator::next(double* lat, double* lon, double* val) int Mercator::destroy() { - const grib_context* c = h_->context; + const grib_context* c = h_->context; grib_context_free(c, lats_); grib_context_free(c, lons_); @@ -283,6 +286,6 @@ int Mercator::destroy() return Gen::destroy(); } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_mercator.h b/src/iterator/grib_iterator_class_mercator.h index 80bebac8b..09b07b7f4 100644 --- a/src/iterator/grib_iterator_class_mercator.h +++ b/src/iterator/grib_iterator_class_mercator.h @@ -12,23 +12,26 @@ #include "grib_iterator_class_gen.h" -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ class Mercator : public Gen { public: Mercator() { class_name_ = "mercator"; } Iterator* create() const override { return new Mercator(); } - - int init(grib_handle*,grib_arguments*) override; - int next(double *lat, double *lon, double *val) override; + + int init(grib_handle*, grib_arguments*) override; + int next(double* lat, double* lon, double* val) override; int destroy() override; private: - double *lats_; - double *lons_; + double* lats_; + double* lons_; long Nj_; int init_mercator(grib_handle*, @@ -40,6 +43,6 @@ private: double, double); }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_polar_stereographic.cc b/src/iterator/grib_iterator_class_polar_stereographic.cc index 498641c31..6bc8a905a 100644 --- a/src/iterator/grib_iterator_class_polar_stereographic.cc +++ b/src/iterator/grib_iterator_class_polar_stereographic.cc @@ -13,9 +13,12 @@ eccodes::grib::geo::PolarStereographic _grib_iterator_polar_stereographic{}; eccodes::grib::geo::Iterator* grib_iterator_polar_stereographic = &_grib_iterator_polar_stereographic; -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ #define ITER "Polar stereographic Geoiterator" @@ -46,10 +49,10 @@ typedef struct proj_data_t double false_easting; /* x offset in meters */ } proj_data_t; -#define RAD2DEG 57.29577951308232087684 /* 180 over pi */ -#define DEG2RAD 0.01745329251994329576 /* pi over 180 */ -#define PI_OVER_2 1.5707963267948966 /* half pi */ -#define EPSILON 1.0e-10 +#define RAD2DEG 57.29577951308232087684 /* 180 over pi */ +#define DEG2RAD 0.01745329251994329576 /* pi over 180 */ +#define PI_OVER_2 1.5707963267948966 /* half pi */ +#define EPSILON 1.0e-10 int PolarStereographic::init(grib_handle* h, grib_arguments* args) { @@ -69,8 +72,12 @@ int PolarStereographic::init(grib_handle* h, grib_arguments* args) double ts; /* value of small t */ double height; /* height above ellipsoid */ double x0, y0, lonFirst, latFirst; - proj_data_t fwd_proj_data = {0,}; - proj_data_t inv_proj_data = {0,}; + proj_data_t fwd_proj_data = { + 0, + }; + proj_data_t inv_proj_data = { + 0, + }; const char* s_radius = grib_arguments_get_name(h, args, carg_++); const char* s_nx = grib_arguments_get_name(h, args, carg_++); @@ -229,64 +236,64 @@ int PolarStereographic::init(grib_handle* h, grib_arguments* args) y += Dy; } -// /*standardParallel = (southPoleOnPlane == 1) ? -90 : +90;*/ -// if (jPointsAreConsecutive) -// { -// x=xFirst; -// for (i=0;i360) *lons -= 360; -// lons++; -// lats++; -// y+=Dy; -// } -// x+=Dx; -// } -// } -// else -// { -// y=yFirst; -// for (j=0;j360) *lons -= 360; -// lons++; -// lats++; -// x+=Dx; -// } -// y+=Dy; -// } -// } + // /*standardParallel = (southPoleOnPlane == 1) ? -90 : +90;*/ + // if (jPointsAreConsecutive) + // { + // x=xFirst; + // for (i=0;i360) *lons -= 360; + // lons++; + // lats++; + // y+=Dy; + // } + // x+=Dx; + // } + // } + // else + // { + // y=yFirst; + // for (j=0;j360) *lons -= 360; + // lons++; + // lats++; + // x+=Dx; + // } + // y+=Dy; + // } + // } e_ = -1; @@ -308,6 +315,6 @@ int PolarStereographic::destroy() return Gen::destroy(); } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_polar_stereographic.h b/src/iterator/grib_iterator_class_polar_stereographic.h index 997918f90..6d7f4c0e7 100644 --- a/src/iterator/grib_iterator_class_polar_stereographic.h +++ b/src/iterator/grib_iterator_class_polar_stereographic.h @@ -12,18 +12,22 @@ #include "grib_iterator_class_gen.h" -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ class PolarStereographic : public Gen { public: - PolarStereographic() : Gen() { class_name_ = "polar_stereographic"; } + PolarStereographic() : + Gen() { class_name_ = "polar_stereographic"; } Iterator* create() const override { return new PolarStereographic(); } - int init(grib_handle*,grib_arguments*) override; - int next(double *, double *, double *) override; + int init(grib_handle*, grib_arguments*) override; + int next(double*, double*, double*) override; int destroy() override; private: @@ -32,6 +36,6 @@ private: long Nj_; }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_regular.cc b/src/iterator/grib_iterator_class_regular.cc index 2557223eb..4f6419ccb 100644 --- a/src/iterator/grib_iterator_class_regular.cc +++ b/src/iterator/grib_iterator_class_regular.cc @@ -13,9 +13,12 @@ eccodes::grib::geo::Regular _grib_iterator_regular{}; eccodes::grib::geo::Iterator* grib_iterator_regular = &_grib_iterator_regular; -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ #define ITER "Regular grid Geoiterator" @@ -50,7 +53,7 @@ int Regular::previous(double* lat, double* lon, double* val) int Regular::destroy() { - const grib_context* c = h_->context; + const grib_context* c = h_->context; grib_context_free(c, las_); grib_context_free(c, los_); @@ -78,7 +81,7 @@ int Regular::init(grib_handle* h, grib_arguments* args) return ret; if ((ret = grib_get_double_internal(h, "longitudeOfLastGridPointInDegrees", &lon2))) return ret; - if ((ret = grib_get_double_internal(h, s_idir, &idir))) // can be GRIB_MISSING_DOUBLE + if ((ret = grib_get_double_internal(h, s_idir, &idir))) // can be GRIB_MISSING_DOUBLE return ret; idir_coded = idir; if ((ret = grib_get_long_internal(h, s_Ni, &Ni))) @@ -95,7 +98,7 @@ int Regular::init(grib_handle* h, grib_arguments* args) return GRIB_WRONG_GRID; } - if (Ni*Nj != nv_) { + if (Ni * Nj != nv_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Ni*Nj!=numberOfDataPoints (%ld*%ld!=%zu)", ITER, Ni, Nj, nv_); return GRIB_WRONG_GRID; } @@ -156,11 +159,11 @@ int Regular::init(grib_handle* h, grib_arguments* args) if (lon2 > 0) { lon2 = normalise_longitude_in_degrees(lon2); } - los_[Ni-1] = lon2; + los_[Ni - 1] = lon2; return ret; } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_regular.h b/src/iterator/grib_iterator_class_regular.h index 8659041b2..5ad3f1b46 100644 --- a/src/iterator/grib_iterator_class_regular.h +++ b/src/iterator/grib_iterator_class_regular.h @@ -12,9 +12,12 @@ #include "grib_iterator_class_gen.h" -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ class Regular : public Gen { @@ -22,9 +25,9 @@ public: Regular() { class_name_ = "regular"; } Iterator* create() const override { return new Regular(); } - int init(grib_handle*,grib_arguments*) override; - int next(double *lat, double *lon, double *val) override; - int previous(double *lat, double *lon, double *val) override; + int init(grib_handle*, grib_arguments*) override; + int next(double* lat, double* lon, double* val) override; + int previous(double* lat, double* lon, double* val) override; int destroy() override; protected: @@ -41,6 +44,6 @@ protected: long disableUnrotate_; }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_space_view.cc b/src/iterator/grib_iterator_class_space_view.cc index 5f7957aaf..2207a3841 100644 --- a/src/iterator/grib_iterator_class_space_view.cc +++ b/src/iterator/grib_iterator_class_space_view.cc @@ -13,9 +13,12 @@ eccodes::grib::geo::SpaceView _grib_iterator_space_view{}; eccodes::grib::geo::Iterator* grib_iterator_space_view = &_grib_iterator_space_view; -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ #define ITER "Space view Geoiterator" @@ -83,8 +86,8 @@ int SpaceView::init(grib_handle* h, grib_arguments* args) return ret; /* REFERENCE: - * LRIT/HRIT Global Specification (CGMS 03, Issue 2.6, 12.08.1999) - */ + * LRIT/HRIT Global Specification (CGMS 03, Issue 2.6, 12.08.1999) + */ double *lats, *lons; /* arrays of latitudes and longitudes */ double latOfSubSatellitePointInDegrees, lonOfSubSatellitePointInDegrees; double orientationInDegrees, nrInRadiusOfEarth; @@ -315,7 +318,7 @@ int SpaceView::init(grib_handle* h, grib_arguments* args) int SpaceView::destroy() { - const grib_context* c = h_->context; + const grib_context* c = h_->context; grib_context_free(c, lats_); grib_context_free(c, lons_); @@ -323,6 +326,6 @@ int SpaceView::destroy() return Gen::destroy(); } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes diff --git a/src/iterator/grib_iterator_class_space_view.h b/src/iterator/grib_iterator_class_space_view.h index e396e0549..efd93c56f 100644 --- a/src/iterator/grib_iterator_class_space_view.h +++ b/src/iterator/grib_iterator_class_space_view.h @@ -12,9 +12,12 @@ #include "grib_iterator_class_gen.h" -namespace eccodes { -namespace grib { -namespace geo { +namespace eccodes +{ +namespace grib +{ +namespace geo +{ class SpaceView : public Gen { @@ -23,7 +26,7 @@ public: Iterator* create() const override { return new SpaceView(); } int init(grib_handle*, grib_arguments*) override; - int next(double *, double *, double *) override; + int next(double*, double*, double*) override; int destroy() override; private: @@ -32,6 +35,6 @@ private: long Nj_; }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace geo +} // namespace grib +} // namespace eccodes From 408066a9537ff20fdf867e7a5a4ae64210da3483 Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Thu, 17 Oct 2024 13:15:24 +0200 Subject: [PATCH 10/20] Modernisation: Make init_sphere() and init_oblate() member functions --- ...ator_class_lambert_azimuthal_equal_area.cc | 34 +++++++++---------- ...rator_class_lambert_azimuthal_equal_area.h | 15 ++++++++ .../grib_iterator_class_lambert_conformal.cc | 4 +-- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc index 75863b808..bf69dfcd2 100644 --- a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc +++ b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc @@ -97,8 +97,7 @@ static double pj_qsfn(double sinphi, double e, double one_es) } #define EPS10 1.e-10 -static int init_oblate(grib_handle* h, - LambertAzimuthalEqualArea* self, +int LambertAzimuthalEqualArea::init_oblate(grib_handle* h, size_t nv, long nx, long ny, double Dx, double Dy, double earthMinorAxisInMetres, double earthMajorAxisInMetres, double latFirstInRadians, double lonFirstInRadians, @@ -171,18 +170,18 @@ static int init_oblate(grib_handle* h, x0 = Q__xmf * b * cosb * sinlam; /* Allocate latitude and longitude arrays */ - self->lats_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!self->lats_) { + lats_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); + if (!lats_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } - self->lons_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!self->lats_) { + lons_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); + if (!lats_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } - lats = self->lats_; - lons = self->lons_; + lats = lats_; + lons = lons_; /* Populate the lat and lon arrays */ { @@ -231,8 +230,7 @@ static int init_oblate(grib_handle* h, return GRIB_SUCCESS; } -static int init_sphere(grib_handle* h, - LambertAzimuthalEqualArea* self, +int LambertAzimuthalEqualArea::init_sphere(grib_handle* h, size_t nv, long nx, long ny, double Dx, double Dy, double radius, double latFirstInRadians, double lonFirstInRadians, @@ -257,18 +255,18 @@ static int init_sphere(grib_handle* h, Dx = iScansNegatively == 0 ? Dx / 1000 : -Dx / 1000; Dy = jScansPositively == 1 ? Dy / 1000 : -Dy / 1000; - self->lats_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!self->lats_) { + lats_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); + if (!lats_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } - self->lons_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!self->lats_) { + lons_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); + if (!lats_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } - lats = self->lats_; - lons = self->lons_; + lats = lats_; + lons = lons_; /* compute xFirst,yFirst in metres */ sinphi = sin(latFirstInRadians); @@ -414,14 +412,14 @@ int LambertAzimuthalEqualArea::init(grib_handle* h, grib_arguments* args) standardParallelInRadians = standardParallelInDegrees * d2r; if (is_oblate) { - err = init_oblate(h, this, nv_, nx, ny, + err = init_oblate(h, nv_, nx, ny, Dx, Dy, earthMinorAxisInMetres, earthMajorAxisInMetres, latFirstInRadians, lonFirstInRadians, centralLongitudeInRadians, standardParallelInRadians, iScansNegatively, jScansPositively, jPointsAreConsecutive); } else { - err = init_sphere(h, this, nv_, nx, ny, + err = init_sphere(h, nv_, nx, ny, Dx, Dy, radius, latFirstInRadians, lonFirstInRadians, centralLongitudeInRadians, standardParallelInRadians, diff --git a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h index 60f9b4228..442034a49 100644 --- a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h +++ b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h @@ -34,6 +34,21 @@ public: double* lats_; double* lons_; long Nj_; + +private: +int init_sphere(grib_handle*, + size_t, long, long, + double, double, double, + double, double, + double, double, + long, long, long); + +int init_oblate(grib_handle*, + size_t, long, long, + double, double, double, double, + double, double, + double, double, + long, long, long); }; } // namespace geo diff --git a/src/iterator/grib_iterator_class_lambert_conformal.cc b/src/iterator/grib_iterator_class_lambert_conformal.cc index e85d8d634..4785a1679 100644 --- a/src/iterator/grib_iterator_class_lambert_conformal.cc +++ b/src/iterator/grib_iterator_class_lambert_conformal.cc @@ -209,8 +209,8 @@ int LambertConformal::init_sphere(const grib_handle* h, lonDeg = LoVInDegrees + (angle / n) * RAD2DEG; latDeg = (2.0 * atan(pow(radius * f / rho, 1.0 / n)) - M_PI_2) * RAD2DEG; lonDeg = normalise_longitude_in_degrees(lonDeg); - self->lons[index] = lonDeg; - self->lats[index] = latDeg; + lons_[index] = lonDeg; + lats_[index] = latDeg; } } #endif From e4df2db1a1c5af9076c32638556a1bb454414635 Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Thu, 17 Oct 2024 13:32:00 +0200 Subject: [PATCH 11/20] Modernisation: Make members private --- src/iterator/grib_iterator.h | 11 +++++--- src/iterator/grib_iterator_class_gen.h | 6 +++-- src/iterator/grib_iterator_class_healpix.h | 3 +-- ...rator_class_lambert_azimuthal_equal_area.h | 27 +++++++++---------- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/iterator/grib_iterator.h b/src/iterator/grib_iterator.h index 9ac8d147d..187c29efa 100644 --- a/src/iterator/grib_iterator.h +++ b/src/iterator/grib_iterator.h @@ -35,15 +35,18 @@ public: virtual bool has_next() = 0; virtual Iterator* create() const = 0; -public: + unsigned long flags_; + +protected: grib_context* context_; - grib_arguments* args_; /** args of iterator */ grib_handle* h_; + double* data_; /** data values */ long e_; /** current element */ size_t nv_; /** number of values */ - double* data_; /** data values */ - unsigned long flags_; const char* class_name_; + +private: + grib_arguments* args_; /** args of iterator */ size_t size_; int inited_; }; diff --git a/src/iterator/grib_iterator_class_gen.h b/src/iterator/grib_iterator_class_gen.h index 711839d4a..648d3686c 100644 --- a/src/iterator/grib_iterator_class_gen.h +++ b/src/iterator/grib_iterator_class_gen.h @@ -33,10 +33,12 @@ public: int destroy() override; bool has_next() override; -public: - // int get(double*, double*, double*); +protected: int carg_; + +private: const char* missingValue_; + // int get(double*, double*, double*); }; } // namespace geo diff --git a/src/iterator/grib_iterator_class_healpix.h b/src/iterator/grib_iterator_class_healpix.h index 80ed53cc9..69f08b888 100644 --- a/src/iterator/grib_iterator_class_healpix.h +++ b/src/iterator/grib_iterator_class_healpix.h @@ -30,13 +30,12 @@ public: int next(double*, double*, double*) override; int destroy() override; -public: +private: double* lats_; double* lons_; long Nsides_; bool nested_; -private: int iterate_healpix(long N); }; diff --git a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h index 442034a49..fddfa36ee 100644 --- a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h +++ b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h @@ -30,25 +30,24 @@ public: int next(double*, double*, double*) override; int destroy() override; -public: +private: double* lats_; double* lons_; long Nj_; -private: -int init_sphere(grib_handle*, - size_t, long, long, - double, double, double, - double, double, - double, double, - long, long, long); + int init_sphere(grib_handle*, + size_t, long, long, + double, double, double, + double, double, + double, double, + long, long, long); -int init_oblate(grib_handle*, - size_t, long, long, - double, double, double, double, - double, double, - double, double, - long, long, long); + int init_oblate(grib_handle*, + size_t, long, long, + double, double, double, double, + double, double, + double, double, + long, long, long); }; } // namespace geo From f63f70f95ec57c127ed2802effa7a9f163ced3ae Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Thu, 17 Oct 2024 13:41:51 +0200 Subject: [PATCH 12/20] Modernisation: Make next(), previous() and has_next() const members --- src/iterator/grib_iterator.h | 20 +++++++++---------- .../grib_iterator_class_gaussian_reduced.cc | 2 +- .../grib_iterator_class_gaussian_reduced.h | 2 +- src/iterator/grib_iterator_class_gen.cc | 6 +++--- src/iterator/grib_iterator_class_gen.h | 6 +++--- src/iterator/grib_iterator_class_healpix.cc | 2 +- src/iterator/grib_iterator_class_healpix.h | 2 +- ...ator_class_lambert_azimuthal_equal_area.cc | 2 +- ...rator_class_lambert_azimuthal_equal_area.h | 2 +- .../grib_iterator_class_lambert_conformal.cc | 2 +- .../grib_iterator_class_lambert_conformal.h | 2 +- src/iterator/grib_iterator_class_latlon.cc | 2 +- src/iterator/grib_iterator_class_latlon.h | 2 +- .../grib_iterator_class_latlon_reduced.cc | 2 +- .../grib_iterator_class_latlon_reduced.h | 2 +- src/iterator/grib_iterator_class_mercator.cc | 2 +- src/iterator/grib_iterator_class_mercator.h | 2 +- ...grib_iterator_class_polar_stereographic.cc | 2 +- .../grib_iterator_class_polar_stereographic.h | 2 +- src/iterator/grib_iterator_class_regular.cc | 4 ++-- src/iterator/grib_iterator_class_regular.h | 4 ++-- .../grib_iterator_class_space_view.cc | 2 +- src/iterator/grib_iterator_class_space_view.h | 2 +- 23 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/iterator/grib_iterator.h b/src/iterator/grib_iterator.h index 187c29efa..91ecda4dd 100644 --- a/src/iterator/grib_iterator.h +++ b/src/iterator/grib_iterator.h @@ -27,22 +27,22 @@ class Iterator { public: virtual ~Iterator() {} - virtual int init(grib_handle*, grib_arguments*) = 0; - virtual int next(double*, double*, double*) = 0; - virtual int previous(double*, double*, double*) = 0; - virtual int reset() = 0; - virtual int destroy() = 0; - virtual bool has_next() = 0; - virtual Iterator* create() const = 0; + virtual int init(grib_handle*, grib_arguments*) = 0; + virtual int next(double*, double*, double*) const = 0; + virtual int previous(double*, double*, double*) const = 0; + virtual int reset() = 0; + virtual int destroy() = 0; + virtual bool has_next() const = 0; + virtual Iterator* create() const = 0; unsigned long flags_; protected: grib_context* context_; grib_handle* h_; - double* data_; /** data values */ - long e_; /** current element */ - size_t nv_; /** number of values */ + double* data_; /** data values */ + mutable long e_; /** current element */ + size_t nv_; /** number of values */ const char* class_name_; private: diff --git a/src/iterator/grib_iterator_class_gaussian_reduced.cc b/src/iterator/grib_iterator_class_gaussian_reduced.cc index 1c3ee983c..29f896ce8 100644 --- a/src/iterator/grib_iterator_class_gaussian_reduced.cc +++ b/src/iterator/grib_iterator_class_gaussian_reduced.cc @@ -23,7 +23,7 @@ namespace geo #define ITER "Reduced Gaussian grid Geoiterator" -int GaussianReduced::next(double* lat, double* lon, double* val) +int GaussianReduced::next(double* lat, double* lon, double* val) const { double ret_lat = 0, ret_lon = 0; diff --git a/src/iterator/grib_iterator_class_gaussian_reduced.h b/src/iterator/grib_iterator_class_gaussian_reduced.h index 7ffa0dc3d..db3d58457 100644 --- a/src/iterator/grib_iterator_class_gaussian_reduced.h +++ b/src/iterator/grib_iterator_class_gaussian_reduced.h @@ -27,7 +27,7 @@ public: Iterator* create() const override { return new GaussianReduced(); } int init(grib_handle*, grib_arguments*) override; - int next(double*, double*, double*) override; + int next(double*, double*, double*) const override; int destroy() override; private: diff --git a/src/iterator/grib_iterator_class_gen.cc b/src/iterator/grib_iterator_class_gen.cc index 8da4d693a..ff78e9412 100644 --- a/src/iterator/grib_iterator_class_gen.cc +++ b/src/iterator/grib_iterator_class_gen.cc @@ -92,7 +92,7 @@ int Gen::destroy() return Iterator::destroy(); } -bool Gen::has_next() +bool Gen::has_next() const { if (flags_ == 0 && data_ == NULL) return false; @@ -101,12 +101,12 @@ bool Gen::has_next() return true; } -int Gen::previous(double*, double*, double*) +int Gen::previous(double*, double*, double*) const { return GRIB_NOT_IMPLEMENTED; } -int Gen::next(double*, double*, double*) +int Gen::next(double*, double*, double*) const { return GRIB_NOT_IMPLEMENTED; } diff --git a/src/iterator/grib_iterator_class_gen.h b/src/iterator/grib_iterator_class_gen.h index 648d3686c..a2a6da7c2 100644 --- a/src/iterator/grib_iterator_class_gen.h +++ b/src/iterator/grib_iterator_class_gen.h @@ -27,11 +27,11 @@ public: Iterator* create() const override { return new Gen(); } int init(grib_handle*, grib_arguments*) override; - int next(double*, double*, double*) override; - int previous(double*, double*, double*) override; + int next(double*, double*, double*) const override; + int previous(double*, double*, double*) const override; int reset() override; int destroy() override; - bool has_next() override; + bool has_next() const override; protected: int carg_; diff --git a/src/iterator/grib_iterator_class_healpix.cc b/src/iterator/grib_iterator_class_healpix.cc index b90eb49a3..e0710eb89 100644 --- a/src/iterator/grib_iterator_class_healpix.cc +++ b/src/iterator/grib_iterator_class_healpix.cc @@ -337,7 +337,7 @@ int Healpix::init(grib_handle* h, grib_arguments* args) return err; } -int Healpix::next(double* lat, double* lon, double* val) +int Healpix::next(double* lat, double* lon, double* val) const { if (e_ >= static_cast(nv_ - 1)) { return 0; diff --git a/src/iterator/grib_iterator_class_healpix.h b/src/iterator/grib_iterator_class_healpix.h index 69f08b888..ba241fbb4 100644 --- a/src/iterator/grib_iterator_class_healpix.h +++ b/src/iterator/grib_iterator_class_healpix.h @@ -27,7 +27,7 @@ public: Iterator* create() const override { return new Healpix(); } int init(grib_handle*, grib_arguments*) override; - int next(double*, double*, double*) override; + int next(double*, double*, double*) const override; int destroy() override; private: diff --git a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc index bf69dfcd2..ea7d21d31 100644 --- a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc +++ b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc @@ -22,7 +22,7 @@ namespace geo #define ITER "Lambert azimuthal equal area Geoiterator" -int LambertAzimuthalEqualArea::next(double* lat, double* lon, double* val) +int LambertAzimuthalEqualArea::next(double* lat, double* lon, double* val) const { if ((long)e_ >= (long)(nv_ - 1)) return 0; diff --git a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h index fddfa36ee..137b4722d 100644 --- a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h +++ b/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h @@ -27,7 +27,7 @@ public: Iterator* create() const override { return new LambertAzimuthalEqualArea(); } int init(grib_handle*, grib_arguments*) override; - int next(double*, double*, double*) override; + int next(double*, double*, double*) const override; int destroy() override; private: diff --git a/src/iterator/grib_iterator_class_lambert_conformal.cc b/src/iterator/grib_iterator_class_lambert_conformal.cc index 4785a1679..bcd14b57b 100644 --- a/src/iterator/grib_iterator_class_lambert_conformal.cc +++ b/src/iterator/grib_iterator_class_lambert_conformal.cc @@ -467,7 +467,7 @@ int LambertConformal::init(grib_handle* h, grib_arguments* args) return err; } -int LambertConformal::next(double* lat, double* lon, double* val) +int LambertConformal::next(double* lat, double* lon, double* val) const { if ((long)e_ >= (long)(nv_ - 1)) return 0; diff --git a/src/iterator/grib_iterator_class_lambert_conformal.h b/src/iterator/grib_iterator_class_lambert_conformal.h index 2255a18aa..ba3b1d4aa 100644 --- a/src/iterator/grib_iterator_class_lambert_conformal.h +++ b/src/iterator/grib_iterator_class_lambert_conformal.h @@ -27,7 +27,7 @@ public: Iterator* create() const override { return new LambertConformal(); } int init(grib_handle*, grib_arguments*) override; - int next(double*, double*, double*) override; + int next(double*, double*, double*) const override; int destroy() override; private: diff --git a/src/iterator/grib_iterator_class_latlon.cc b/src/iterator/grib_iterator_class_latlon.cc index d931ad620..fb44b551e 100644 --- a/src/iterator/grib_iterator_class_latlon.cc +++ b/src/iterator/grib_iterator_class_latlon.cc @@ -20,7 +20,7 @@ namespace grib namespace geo { -int Latlon::next(double* lat, double* lon, double* val) +int Latlon::next(double* lat, double* lon, double* val) const { /* GRIB-238: Support rotated lat/lon grids */ double ret_lat, ret_lon, ret_val = 0; diff --git a/src/iterator/grib_iterator_class_latlon.h b/src/iterator/grib_iterator_class_latlon.h index ca3121438..28cd297e0 100644 --- a/src/iterator/grib_iterator_class_latlon.h +++ b/src/iterator/grib_iterator_class_latlon.h @@ -27,7 +27,7 @@ public: Iterator* create() const override { return new Latlon(); } int init(grib_handle*, grib_arguments*) override; - int next(double*, double*, double*) override; + int next(double*, double*, double*) const override; }; } // namespace geo diff --git a/src/iterator/grib_iterator_class_latlon_reduced.cc b/src/iterator/grib_iterator_class_latlon_reduced.cc index 9b3273f76..ea79c9e77 100644 --- a/src/iterator/grib_iterator_class_latlon_reduced.cc +++ b/src/iterator/grib_iterator_class_latlon_reduced.cc @@ -20,7 +20,7 @@ namespace grib namespace geo { -int LatlonReduced::next(double* lat, double* lon, double* val) +int LatlonReduced::next(double* lat, double* lon, double* val) const { if ((long)e_ >= (long)(nv_ - 1)) return 0; diff --git a/src/iterator/grib_iterator_class_latlon_reduced.h b/src/iterator/grib_iterator_class_latlon_reduced.h index c4ab8f04f..992cba840 100644 --- a/src/iterator/grib_iterator_class_latlon_reduced.h +++ b/src/iterator/grib_iterator_class_latlon_reduced.h @@ -27,7 +27,7 @@ public: Iterator* create() const override { return new LatlonReduced(); } int init(grib_handle*, grib_arguments*) override; - int next(double* lat, double* lon, double* val) override; + int next(double* lat, double* lon, double* val) const override; int destroy() override; private: diff --git a/src/iterator/grib_iterator_class_mercator.cc b/src/iterator/grib_iterator_class_mercator.cc index abfde3dbd..0ef438c45 100644 --- a/src/iterator/grib_iterator_class_mercator.cc +++ b/src/iterator/grib_iterator_class_mercator.cc @@ -262,7 +262,7 @@ int Mercator::init(grib_handle* h, grib_arguments* args) return err; } -int Mercator::next(double* lat, double* lon, double* val) +int Mercator::next(double* lat, double* lon, double* val) const { if ((long)e_ >= (long)(nv_ - 1)) return 0; diff --git a/src/iterator/grib_iterator_class_mercator.h b/src/iterator/grib_iterator_class_mercator.h index 09b07b7f4..580e26e30 100644 --- a/src/iterator/grib_iterator_class_mercator.h +++ b/src/iterator/grib_iterator_class_mercator.h @@ -26,7 +26,7 @@ public: Iterator* create() const override { return new Mercator(); } int init(grib_handle*, grib_arguments*) override; - int next(double* lat, double* lon, double* val) override; + int next(double* lat, double* lon, double* val) const override; int destroy() override; private: diff --git a/src/iterator/grib_iterator_class_polar_stereographic.cc b/src/iterator/grib_iterator_class_polar_stereographic.cc index 6bc8a905a..2a4a14f5b 100644 --- a/src/iterator/grib_iterator_class_polar_stereographic.cc +++ b/src/iterator/grib_iterator_class_polar_stereographic.cc @@ -22,7 +22,7 @@ namespace geo #define ITER "Polar stereographic Geoiterator" -int PolarStereographic::next(double* lat, double* lon, double* val) +int PolarStereographic::next(double* lat, double* lon, double* val) const { if ((long)e_ >= (long)(nv_ - 1)) return 0; diff --git a/src/iterator/grib_iterator_class_polar_stereographic.h b/src/iterator/grib_iterator_class_polar_stereographic.h index 6d7f4c0e7..d786f4ee9 100644 --- a/src/iterator/grib_iterator_class_polar_stereographic.h +++ b/src/iterator/grib_iterator_class_polar_stereographic.h @@ -27,7 +27,7 @@ public: Iterator* create() const override { return new PolarStereographic(); } int init(grib_handle*, grib_arguments*) override; - int next(double*, double*, double*) override; + int next(double*, double*, double*) const override; int destroy() override; private: diff --git a/src/iterator/grib_iterator_class_regular.cc b/src/iterator/grib_iterator_class_regular.cc index 4f6419ccb..28148e94b 100644 --- a/src/iterator/grib_iterator_class_regular.cc +++ b/src/iterator/grib_iterator_class_regular.cc @@ -22,7 +22,7 @@ namespace geo #define ITER "Regular grid Geoiterator" -int Regular::next(double* lat, double* lon, double* val) +int Regular::next(double* lat, double* lon, double* val) const { if ((long)e_ >= (long)(nv_ - 1)) return 0; @@ -37,7 +37,7 @@ int Regular::next(double* lat, double* lon, double* val) return 1; } -int Regular::previous(double* lat, double* lon, double* val) +int Regular::previous(double* lat, double* lon, double* val) const { if (e_ < 0) return 0; diff --git a/src/iterator/grib_iterator_class_regular.h b/src/iterator/grib_iterator_class_regular.h index 5ad3f1b46..a0301bef3 100644 --- a/src/iterator/grib_iterator_class_regular.h +++ b/src/iterator/grib_iterator_class_regular.h @@ -26,8 +26,8 @@ public: Iterator* create() const override { return new Regular(); } int init(grib_handle*, grib_arguments*) override; - int next(double* lat, double* lon, double* val) override; - int previous(double* lat, double* lon, double* val) override; + int next(double* lat, double* lon, double* val) const override; + int previous(double* lat, double* lon, double* val) const override; int destroy() override; protected: diff --git a/src/iterator/grib_iterator_class_space_view.cc b/src/iterator/grib_iterator_class_space_view.cc index 2207a3841..0d488be05 100644 --- a/src/iterator/grib_iterator_class_space_view.cc +++ b/src/iterator/grib_iterator_class_space_view.cc @@ -22,7 +22,7 @@ namespace geo #define ITER "Space view Geoiterator" -int SpaceView::next(double* lat, double* lon, double* val) +int SpaceView::next(double* lat, double* lon, double* val) const { if ((long)e_ >= (long)(nv_ - 1)) return 0; diff --git a/src/iterator/grib_iterator_class_space_view.h b/src/iterator/grib_iterator_class_space_view.h index efd93c56f..7ed40c853 100644 --- a/src/iterator/grib_iterator_class_space_view.h +++ b/src/iterator/grib_iterator_class_space_view.h @@ -26,7 +26,7 @@ public: Iterator* create() const override { return new SpaceView(); } int init(grib_handle*, grib_arguments*) override; - int next(double*, double*, double*) override; + int next(double*, double*, double*) const override; int destroy() override; private: From f2c1a83c2be613bd07b8c4b1f54e7134288cfb9c Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Mon, 21 Oct 2024 17:19:39 +0200 Subject: [PATCH 13/20] Modernisation: Change namespace and move lats_ and lons_ to base class --- CMakeLists.txt | 7 ++- src/CMakeLists.txt | 26 +++++------ src/accessor/grib_accessor_class_iterator.h | 2 +- src/eccodes_prototypes.h | 2 +- .../grib_iterator.cc | 22 +++------- .../grib_iterator.h | 15 ++----- .../grib_iterator_class_gaussian.cc | 19 +++----- .../grib_iterator_class_gaussian.h | 12 ++---- .../grib_iterator_class_gaussian_reduced.cc | 43 ++++++++----------- .../grib_iterator_class_gaussian_reduced.h | 13 +----- .../grib_iterator_class_gen.cc | 11 +---- .../grib_iterator_class_gen.h | 13 ++---- .../grib_iterator_class_healpix.cc | 16 ++----- .../grib_iterator_class_healpix.h | 13 +----- ...ator_class_lambert_azimuthal_equal_area.cc | 15 ++----- ...rator_class_lambert_azimuthal_equal_area.h | 13 +----- .../grib_iterator_class_lambert_conformal.cc | 15 ++----- .../grib_iterator_class_lambert_conformal.h | 13 +----- .../grib_iterator_class_latlon.cc | 27 +++++------- .../grib_iterator_class_latlon.h | 11 +---- .../grib_iterator_class_latlon_reduced.cc | 31 ++++++------- .../grib_iterator_class_latlon_reduced.h | 17 ++------ .../grib_iterator_class_mercator.cc | 15 ++----- .../grib_iterator_class_mercator.h | 15 ++----- ...grib_iterator_class_polar_stereographic.cc | 15 ++----- .../grib_iterator_class_polar_stereographic.h | 13 +----- .../grib_iterator_class_regular.cc | 35 ++++++--------- .../grib_iterator_class_regular.h | 17 ++------ .../grib_iterator_class_space_view.cc | 15 ++----- .../grib_iterator_class_space_view.h | 13 +----- src/grib_api_internal.h | 6 +-- src/grib_iterator_class.cc | 16 +++---- src/grib_iterator_class.h | 24 +++++------ 33 files changed, 172 insertions(+), 368 deletions(-) rename src/{iterator => geo_iterator}/grib_iterator.cc (86%) rename src/{iterator => geo_iterator}/grib_iterator.h (84%) rename src/{iterator => geo_iterator}/grib_iterator_class_gaussian.cc (92%) rename src/{iterator => geo_iterator}/grib_iterator_class_gaussian.h (85%) rename src/{iterator => geo_iterator}/grib_iterator_class_gaussian_reduced.cc (94%) rename src/{iterator => geo_iterator}/grib_iterator_class_gaussian_reduced.h (90%) rename src/{iterator => geo_iterator}/grib_iterator_class_gen.cc (96%) rename src/{iterator => geo_iterator}/grib_iterator_class_gen.h (89%) rename src/{iterator => geo_iterator}/grib_iterator_class_healpix.cc (97%) rename src/{iterator => geo_iterator}/grib_iterator_class_healpix.h (84%) rename src/{iterator => geo_iterator}/grib_iterator_class_lambert_azimuthal_equal_area.cc (97%) rename src/{iterator => geo_iterator}/grib_iterator_class_lambert_azimuthal_equal_area.h (89%) rename src/{iterator => geo_iterator}/grib_iterator_class_lambert_conformal.cc (98%) rename src/{iterator => geo_iterator}/grib_iterator_class_lambert_conformal.h (89%) rename src/{iterator => geo_iterator}/grib_iterator_class_latlon.cc (91%) rename src/{iterator => geo_iterator}/grib_iterator_class_latlon.h (86%) rename src/{iterator => geo_iterator}/grib_iterator_class_latlon_reduced.cc (85%) rename src/{iterator => geo_iterator}/grib_iterator_class_latlon_reduced.h (75%) rename src/{iterator => geo_iterator}/grib_iterator_class_mercator.cc (97%) rename src/{iterator => geo_iterator}/grib_iterator_class_mercator.h (81%) rename src/{iterator => geo_iterator}/grib_iterator_class_polar_stereographic.cc (97%) rename src/{iterator => geo_iterator}/grib_iterator_class_polar_stereographic.h (83%) rename src/{iterator => geo_iterator}/grib_iterator_class_regular.cc (86%) rename src/{iterator => geo_iterator}/grib_iterator_class_regular.h (75%) rename src/{iterator => geo_iterator}/grib_iterator_class_space_view.cc (98%) rename src/{iterator => geo_iterator}/grib_iterator_class_space_view.h (83%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 84f1ab2dc..1b002b1b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -434,7 +434,12 @@ endforeach() ### include directories -include_directories( ${ECCODES_INCLUDE_DIRS} ${ECCODES_EXTRA_INCLUDE_DIRS} ) +include_directories( + "accessor" + "geo_iterator" + ${ECCODES_INCLUDE_DIRS} + ${ECCODES_EXTRA_INCLUDE_DIRS} + ) add_subdirectory( definitions ) # must be before memfs add_subdirectory( memfs ) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6ad3fc89d..a012d66e7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -331,20 +331,20 @@ list( APPEND eccodes_src_files grib_nearest_class_mercator.cc grib_nearest_class_polar_stereographic.cc grib_nearest_class_space_view.cc - iterator/grib_iterator_class_polar_stereographic.cc - iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc - iterator/grib_iterator_class_lambert_conformal.cc - iterator/grib_iterator_class_mercator.cc - iterator/grib_iterator.cc + geo_iterator/grib_iterator_class_polar_stereographic.cc + geo_iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc + geo_iterator/grib_iterator_class_lambert_conformal.cc + geo_iterator/grib_iterator_class_mercator.cc + geo_iterator/grib_iterator.cc grib_iterator_class.cc - iterator/grib_iterator_class_gaussian.cc - iterator/grib_iterator_class_gaussian_reduced.cc - iterator/grib_iterator_class_latlon_reduced.cc - iterator/grib_iterator_class_gen.cc - iterator/grib_iterator_class_latlon.cc - iterator/grib_iterator_class_regular.cc - iterator/grib_iterator_class_space_view.cc - iterator/grib_iterator_class_healpix.cc + geo_iterator/grib_iterator_class_gaussian.cc + geo_iterator/grib_iterator_class_gaussian_reduced.cc + geo_iterator/grib_iterator_class_latlon_reduced.cc + geo_iterator/grib_iterator_class_gen.cc + geo_iterator/grib_iterator_class_latlon.cc + geo_iterator/grib_iterator_class_regular.cc + geo_iterator/grib_iterator_class_space_view.cc + geo_iterator/grib_iterator_class_healpix.cc grib_expression.cc codes_util.cc grib_util.cc diff --git a/src/accessor/grib_accessor_class_iterator.h b/src/accessor/grib_accessor_class_iterator.h index 1b2186575..8fd950570 100644 --- a/src/accessor/grib_accessor_class_iterator.h +++ b/src/accessor/grib_accessor_class_iterator.h @@ -23,5 +23,5 @@ public: private: grib_arguments* args_; - friend eccodes::grib::geo::Iterator* eccodes::grib::geo::gribIteratorNew(const grib_handle*, unsigned long, int*); + friend eccodes::geo_iterator::Iterator* eccodes::geo_iterator::gribIteratorNew(const grib_handle*, unsigned long, int*); }; diff --git a/src/eccodes_prototypes.h b/src/eccodes_prototypes.h index 454403874..90592dd72 100644 --- a/src/eccodes_prototypes.h +++ b/src/eccodes_prototypes.h @@ -820,7 +820,7 @@ int grib_get_data(const grib_handle* h, double* lats, double* lons, double* valu //int grib_iterator_delete(grib_iterator* i); /* grib_iterator_class.cc */ -eccodes::grib::geo::Iterator* grib_iterator_factory(grib_handle* h, grib_arguments* args, unsigned long flags, int* error); +eccodes::geo_iterator::Iterator* grib_iterator_factory(grib_handle* h, grib_arguments* args, unsigned long flags, int* error); /* grib_iterator_class_gen.cc */ int transform_iterator_data(grib_context* c, double* data, long iScansNegatively, long jScansPositively, long jPointsAreConsecutive, long alternativeRowScanning, size_t numPoints, long nx, long ny); diff --git a/src/iterator/grib_iterator.cc b/src/geo_iterator/grib_iterator.cc similarity index 86% rename from src/iterator/grib_iterator.cc rename to src/geo_iterator/grib_iterator.cc index a95070a79..75938146e 100644 --- a/src/iterator/grib_iterator.cc +++ b/src/geo_iterator/grib_iterator.cc @@ -12,15 +12,9 @@ * Jean Baptiste Filippi - 01.11.2005 * ***************************************************************************/ #include "grib_iterator.h" -#include "../accessor/grib_accessor_class_iterator.h" - -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +#include "accessor/grib_accessor_class_iterator.h" +namespace eccodes::geo_iterator { int Iterator::init(grib_handle* h, grib_arguments* args) { @@ -36,7 +30,7 @@ int Iterator::destroy() return GRIB_SUCCESS; } -eccodes::grib::geo::Iterator* gribIteratorNew(const grib_handle* ch, unsigned long flags, int* error) +eccodes::geo_iterator::Iterator* gribIteratorNew(const grib_handle* ch, unsigned long flags, int* error) { grib_handle* h = (grib_handle*)ch; grib_accessor* a = NULL; @@ -48,7 +42,7 @@ eccodes::grib::geo::Iterator* gribIteratorNew(const grib_handle* ch, unsigned lo if (!a) return NULL; - eccodes::grib::geo::Iterator* iter = grib_iterator_factory(h, ita->args_, flags, error); + eccodes::geo_iterator::Iterator* iter = grib_iterator_factory(h, ita->args_, flags, error); if (iter) *error = GRIB_SUCCESS; @@ -56,7 +50,7 @@ eccodes::grib::geo::Iterator* gribIteratorNew(const grib_handle* ch, unsigned lo return iter; } -int gribIteratorDelete(eccodes::grib::geo::Iterator* i) +int gribIteratorDelete(eccodes::geo_iterator::Iterator* i) { if (i) i->destroy(); @@ -64,9 +58,7 @@ int gribIteratorDelete(eccodes::grib::geo::Iterator* i) } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator /* @@ -104,7 +96,7 @@ int grib_iterator_destroy(grib_context* c, grib_iterator* i) grib_iterator* grib_iterator_new(const grib_handle* ch, unsigned long flags, int* error) { grib_iterator* i = (grib_iterator*)grib_context_malloc_clear(ch->context, sizeof(grib_iterator)); - i->iterator = eccodes::grib::geo::gribIteratorNew(ch, flags, error); + i->iterator = eccodes::geo_iterator::gribIteratorNew(ch, flags, error); if (!i->iterator) { grib_context_free(ch->context, i); return NULL; diff --git a/src/iterator/grib_iterator.h b/src/geo_iterator/grib_iterator.h similarity index 84% rename from src/iterator/grib_iterator.h rename to src/geo_iterator/grib_iterator.h index 91ecda4dd..948bb5fe9 100644 --- a/src/iterator/grib_iterator.h +++ b/src/geo_iterator/grib_iterator.h @@ -16,12 +16,7 @@ \ingroup grib_iterator */ -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { class Iterator { @@ -51,9 +46,7 @@ private: int inited_; }; -eccodes::grib::geo::Iterator* gribIteratorNew(const grib_handle*, unsigned long, int*); -int gribIteratorDelete(eccodes::grib::geo::Iterator*); +eccodes::geo_iterator::Iterator* gribIteratorNew(const grib_handle*, unsigned long, int*); +int gribIteratorDelete(eccodes::geo_iterator::Iterator*); -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_gaussian.cc b/src/geo_iterator/grib_iterator_class_gaussian.cc similarity index 92% rename from src/iterator/grib_iterator_class_gaussian.cc rename to src/geo_iterator/grib_iterator_class_gaussian.cc index 5ebbf9bd6..da50560ef 100644 --- a/src/iterator/grib_iterator_class_gaussian.cc +++ b/src/geo_iterator/grib_iterator_class_gaussian.cc @@ -10,15 +10,10 @@ #include "grib_iterator_class_gaussian.h" -eccodes::grib::geo::Gaussian _grib_iterator_gaussian{}; -eccodes::grib::geo::Iterator* grib_iterator_gaussian = &_grib_iterator_gaussian; +eccodes::geo_iterator::Gaussian _grib_iterator_gaussian{}; +eccodes::geo_iterator::Iterator* grib_iterator_gaussian = &_grib_iterator_gaussian; -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { static void binary_search_gaussian_latitudes(const double xx[], const unsigned long n, double x, long* j); @@ -81,13 +76,13 @@ int Gaussian::init(grib_handle* h, grib_arguments* args) if (jScansPositively) { for (lai = 0; lai < Nj_; lai++) { DEBUG_ASSERT(istart >= 0); - las_[lai] = lats[istart--]; + lats_[lai] = lats[istart--]; if (istart < 0) istart = size - 1; } } else { for (lai = 0; lai < Nj_; lai++) { - las_[lai] = lats[istart++]; + lats_[lai] = lats[istart++]; if (istart > size - 1) istart = 0; } @@ -152,6 +147,4 @@ static void binary_search_gaussian_latitudes(const double array[], const unsigne // *j = jl; // } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_gaussian.h b/src/geo_iterator/grib_iterator_class_gaussian.h similarity index 85% rename from src/iterator/grib_iterator_class_gaussian.h rename to src/geo_iterator/grib_iterator_class_gaussian.h index 1dd8a70ae..5c0696f13 100644 --- a/src/iterator/grib_iterator_class_gaussian.h +++ b/src/geo_iterator/grib_iterator_class_gaussian.h @@ -12,12 +12,7 @@ #include "grib_iterator_class_regular.h" -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { class Gaussian : public Regular { @@ -29,6 +24,5 @@ public: int init(grib_handle*, grib_arguments*) override; }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator + diff --git a/src/iterator/grib_iterator_class_gaussian_reduced.cc b/src/geo_iterator/grib_iterator_class_gaussian_reduced.cc similarity index 94% rename from src/iterator/grib_iterator_class_gaussian_reduced.cc rename to src/geo_iterator/grib_iterator_class_gaussian_reduced.cc index 29f896ce8..eb90c8420 100644 --- a/src/iterator/grib_iterator_class_gaussian_reduced.cc +++ b/src/geo_iterator/grib_iterator_class_gaussian_reduced.cc @@ -11,15 +11,10 @@ #include "grib_iterator_class_gaussian_reduced.h" #include -eccodes::grib::geo::GaussianReduced _grib_iterator_gaussian_reduced{}; -eccodes::grib::geo::Iterator* grib_iterator_gaussian_reduced = &_grib_iterator_gaussian_reduced; +eccodes::geo_iterator::GaussianReduced _grib_iterator_gaussian_reduced{}; +eccodes::geo_iterator::Iterator* grib_iterator_gaussian_reduced = &_grib_iterator_gaussian_reduced; -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { #define ITER "Reduced Gaussian grid Geoiterator" @@ -31,8 +26,8 @@ int GaussianReduced::next(double* lat, double* lon, double* val) const return 0; e_++; - ret_lat = las_[e_]; - ret_lon = los_[e_]; + ret_lat = lats_[e_]; + ret_lon = lons_[e_]; if (val && data_) { *val = data_[e_]; } @@ -141,8 +136,8 @@ int GaussianReduced::iterate_reduced_gaussian_subarea_legacy(grib_handle* h, return GRIB_WRONG_GRID; } - los_[e_] = ((i) * 360.0) / pl[j]; - las_[e_] = lats[j + l]; + lons_[e_] = ((i) * 360.0) / pl[j]; + lats_[e_] = lats[j + l]; e_++; k++; if (k >= row_count) { @@ -205,9 +200,9 @@ int GaussianReduced::iterate_reduced_gaussian_subarea(grib_handle* h, "%s (sub-area). Num points=%zu, size(values)=%zu", ITER, np, nv_); return GRIB_WRONG_GRID; } - los_[e_] = lon2; + lons_[e_] = lon2; DEBUG_ASSERT(j + l < numlats); - las_[e_] = lats[j + l]; + lats_[e_] = lats[j + l]; e_++; } } @@ -308,11 +303,11 @@ int GaussianReduced::init(grib_handle* h, grib_arguments* args) grib_get_long_array_internal(h, spl, pl, &plsize); - las_ = (double*)grib_context_malloc(h->context, nv_ * sizeof(double)); - if (!las_) + lats_ = (double*)grib_context_malloc(h->context, nv_ * sizeof(double)); + if (!lats_) return GRIB_OUT_OF_MEMORY; - los_ = (double*)grib_context_malloc(h->context, nv_ * sizeof(double)); - if (!los_) + lons_ = (double*)grib_context_malloc(h->context, nv_ * sizeof(double)); + if (!lons_) return GRIB_OUT_OF_MEMORY; while (lon_last < 0) @@ -354,8 +349,8 @@ int GaussianReduced::init(grib_handle* h, grib_arguments* args) goto finalise; } - los_[e_] = (i * 360.0) / row_count; - las_[e_] = lats[j]; + lons_[e_] = (i * 360.0) / row_count; + lats_[e_] = lats[j]; e_++; } } @@ -373,12 +368,10 @@ int GaussianReduced::destroy() { const grib_context* c = h_->context; - grib_context_free(c, las_); - grib_context_free(c, los_); + grib_context_free(c, lats_); + grib_context_free(c, lons_); return Gen::destroy(); } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_gaussian_reduced.h b/src/geo_iterator/grib_iterator_class_gaussian_reduced.h similarity index 90% rename from src/iterator/grib_iterator_class_gaussian_reduced.h rename to src/geo_iterator/grib_iterator_class_gaussian_reduced.h index db3d58457..c67c6afb7 100644 --- a/src/iterator/grib_iterator_class_gaussian_reduced.h +++ b/src/geo_iterator/grib_iterator_class_gaussian_reduced.h @@ -12,12 +12,7 @@ #include "grib_iterator_class_gen.h" -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { class GaussianReduced : public Gen { @@ -31,8 +26,6 @@ public: int destroy() override; private: - double* las_; - double* los_; long Nj_; long isRotated_; double angleOfRotation_; @@ -51,6 +44,4 @@ private: double*, long*, size_t, size_t); }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_gen.cc b/src/geo_iterator/grib_iterator_class_gen.cc similarity index 96% rename from src/iterator/grib_iterator_class_gen.cc rename to src/geo_iterator/grib_iterator_class_gen.cc index ff78e9412..e2c9d825e 100644 --- a/src/iterator/grib_iterator_class_gen.cc +++ b/src/geo_iterator/grib_iterator_class_gen.cc @@ -10,12 +10,7 @@ #include "grib_iterator_class_gen.h" -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { int Gen::init(grib_handle* h, grib_arguments* args) { @@ -124,6 +119,4 @@ int Gen::next(double*, double*, double*) const // return 1; //} -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_gen.h b/src/geo_iterator/grib_iterator_class_gen.h similarity index 89% rename from src/iterator/grib_iterator_class_gen.h rename to src/geo_iterator/grib_iterator_class_gen.h index a2a6da7c2..0cca14c08 100644 --- a/src/iterator/grib_iterator_class_gen.h +++ b/src/geo_iterator/grib_iterator_class_gen.h @@ -12,12 +12,7 @@ #include "grib_iterator.h" -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { class Gen : public Iterator { @@ -35,12 +30,12 @@ public: protected: int carg_; + double* lats_; + double* lons_; private: const char* missingValue_; // int get(double*, double*, double*); }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_healpix.cc b/src/geo_iterator/grib_iterator_class_healpix.cc similarity index 97% rename from src/iterator/grib_iterator_class_healpix.cc rename to src/geo_iterator/grib_iterator_class_healpix.cc index e0710eb89..fcba61678 100644 --- a/src/iterator/grib_iterator_class_healpix.cc +++ b/src/geo_iterator/grib_iterator_class_healpix.cc @@ -14,18 +14,12 @@ #include #include #include -#include #include -eccodes::grib::geo::Healpix _grib_iterator_healpix; -eccodes::grib::geo::Iterator* grib_iterator_healpix = &_grib_iterator_healpix; +eccodes::geo_iterator::Healpix _grib_iterator_healpix; +eccodes::geo_iterator::Iterator* grib_iterator_healpix = &_grib_iterator_healpix; -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { #define ITER "HEALPix Geoiterator" constexpr double RAD2DEG = 57.29577951308232087684; // 180 over pi @@ -361,6 +355,4 @@ int Healpix::destroy() return Gen::destroy(); } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_healpix.h b/src/geo_iterator/grib_iterator_class_healpix.h similarity index 84% rename from src/iterator/grib_iterator_class_healpix.h rename to src/geo_iterator/grib_iterator_class_healpix.h index ba241fbb4..74ebdcec3 100644 --- a/src/iterator/grib_iterator_class_healpix.h +++ b/src/geo_iterator/grib_iterator_class_healpix.h @@ -12,12 +12,7 @@ #include "grib_iterator_class_gen.h" -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { class Healpix : public Gen { @@ -31,14 +26,10 @@ public: int destroy() override; private: - double* lats_; - double* lons_; long Nsides_; bool nested_; int iterate_healpix(long N); }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc b/src/geo_iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc similarity index 97% rename from src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc rename to src/geo_iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc index ea7d21d31..15eff4bef 100644 --- a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc +++ b/src/geo_iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc @@ -10,15 +10,10 @@ #include "grib_iterator_class_lambert_azimuthal_equal_area.h" -eccodes::grib::geo::LambertAzimuthalEqualArea _grib_iterator_lambert_azimuthal_equal_area{}; -eccodes::grib::geo::Iterator* grib_iterator_lambert_azimuthal_equal_area = &_grib_iterator_lambert_azimuthal_equal_area; +eccodes::geo_iterator::LambertAzimuthalEqualArea _grib_iterator_lambert_azimuthal_equal_area{}; +eccodes::geo_iterator::Iterator* grib_iterator_lambert_azimuthal_equal_area = &_grib_iterator_lambert_azimuthal_equal_area; -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { #define ITER "Lambert azimuthal equal area Geoiterator" @@ -442,6 +437,4 @@ int LambertAzimuthalEqualArea::destroy() return Gen::destroy(); } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h b/src/geo_iterator/grib_iterator_class_lambert_azimuthal_equal_area.h similarity index 89% rename from src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h rename to src/geo_iterator/grib_iterator_class_lambert_azimuthal_equal_area.h index 137b4722d..17cafff53 100644 --- a/src/iterator/grib_iterator_class_lambert_azimuthal_equal_area.h +++ b/src/geo_iterator/grib_iterator_class_lambert_azimuthal_equal_area.h @@ -12,12 +12,7 @@ #include "grib_iterator_class_gen.h" -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { class LambertAzimuthalEqualArea : public Gen { @@ -31,8 +26,6 @@ public: int destroy() override; private: - double* lats_; - double* lons_; long Nj_; int init_sphere(grib_handle*, @@ -50,6 +43,4 @@ private: long, long, long); }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_lambert_conformal.cc b/src/geo_iterator/grib_iterator_class_lambert_conformal.cc similarity index 98% rename from src/iterator/grib_iterator_class_lambert_conformal.cc rename to src/geo_iterator/grib_iterator_class_lambert_conformal.cc index bcd14b57b..5204d7638 100644 --- a/src/iterator/grib_iterator_class_lambert_conformal.cc +++ b/src/geo_iterator/grib_iterator_class_lambert_conformal.cc @@ -11,15 +11,10 @@ #include "grib_iterator_class_lambert_conformal.h" #include -eccodes::grib::geo::LambertConformal _grib_iterator_lambert_conformal{}; -eccodes::grib::geo::Iterator* grib_iterator_lambert_conformal = &_grib_iterator_lambert_conformal; +eccodes::geo_iterator::LambertConformal _grib_iterator_lambert_conformal{}; +eccodes::geo_iterator::Iterator* grib_iterator_lambert_conformal = &_grib_iterator_lambert_conformal; -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { #define ITER "Lambert conformal Geoiterator" #define EPSILON 1.0e-10 @@ -491,6 +486,4 @@ int LambertConformal::destroy() return Gen::destroy(); } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_lambert_conformal.h b/src/geo_iterator/grib_iterator_class_lambert_conformal.h similarity index 89% rename from src/iterator/grib_iterator_class_lambert_conformal.h rename to src/geo_iterator/grib_iterator_class_lambert_conformal.h index ba3b1d4aa..6a70b536d 100644 --- a/src/iterator/grib_iterator_class_lambert_conformal.h +++ b/src/geo_iterator/grib_iterator_class_lambert_conformal.h @@ -12,12 +12,7 @@ #include "grib_iterator_class_gen.h" -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { class LambertConformal : public Gen { @@ -31,8 +26,6 @@ public: int destroy() override; private: - double* lats_; - double* lons_; long Nj_; int init_sphere(const grib_handle*, @@ -53,6 +46,4 @@ private: double); }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_latlon.cc b/src/geo_iterator/grib_iterator_class_latlon.cc similarity index 91% rename from src/iterator/grib_iterator_class_latlon.cc rename to src/geo_iterator/grib_iterator_class_latlon.cc index fb44b551e..603640130 100644 --- a/src/iterator/grib_iterator_class_latlon.cc +++ b/src/geo_iterator/grib_iterator_class_latlon.cc @@ -10,15 +10,10 @@ #include "grib_iterator_class_latlon.h" -eccodes::grib::geo::Latlon _grib_iterator_latlon{}; -eccodes::grib::geo::Iterator* grib_iterator_latlon = &_grib_iterator_latlon; +eccodes::geo_iterator::Latlon _grib_iterator_latlon{}; +eccodes::geo_iterator::Iterator* grib_iterator_latlon = &_grib_iterator_latlon; -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { int Latlon::next(double* lat, double* lon, double* val) const { @@ -34,15 +29,15 @@ int Latlon::next(double* lat, double* lon, double* val) const */ if (!jPointsAreConsecutive_) { /* Adjacent points in i (x) direction are consecutive */ - ret_lat = las_[(long)floor(e_ / Ni_)]; - ret_lon = los_[(long)e_ % Ni_]; + ret_lat = lats_[(long)floor(e_ / Ni_)]; + ret_lon = lons_[(long)e_ % Ni_]; if (data_) ret_val = data_[e_]; } else { /* Adjacent points in j (y) direction is consecutive */ - ret_lon = los_[(long)e_ / Nj_]; - ret_lat = las_[(long)floor(e_ % Nj_)]; + ret_lon = lons_[(long)e_ / Nj_]; + ret_lat = lats_[(long)floor(e_ % Nj_)]; if (data_) ret_val = data_[e_]; } @@ -147,18 +142,16 @@ int Latlon::init(grib_handle* h, grib_arguments* args) } for (lai = 0; lai < Nj_; lai++) { - las_[lai] = lat1; + lats_[lai] = lat1; lat1 -= jdir; } /* ECC-1406: Due to rounding, errors can accumulate. * So we ensure the last latitude is latitudeOfLastGridPointInDegrees */ - las_[Nj_ - 1] = lat2; + lats_[Nj_ - 1] = lat2; e_ = -1; return err; } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_latlon.h b/src/geo_iterator/grib_iterator_class_latlon.h similarity index 86% rename from src/iterator/grib_iterator_class_latlon.h rename to src/geo_iterator/grib_iterator_class_latlon.h index 28cd297e0..8d2f5ef90 100644 --- a/src/iterator/grib_iterator_class_latlon.h +++ b/src/geo_iterator/grib_iterator_class_latlon.h @@ -12,12 +12,7 @@ #include "grib_iterator_class_regular.h" -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { class Latlon : public Regular { @@ -30,6 +25,4 @@ public: int next(double*, double*, double*) const override; }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_latlon_reduced.cc b/src/geo_iterator/grib_iterator_class_latlon_reduced.cc similarity index 85% rename from src/iterator/grib_iterator_class_latlon_reduced.cc rename to src/geo_iterator/grib_iterator_class_latlon_reduced.cc index ea79c9e77..ba0396912 100644 --- a/src/iterator/grib_iterator_class_latlon_reduced.cc +++ b/src/geo_iterator/grib_iterator_class_latlon_reduced.cc @@ -10,15 +10,10 @@ #include "grib_iterator_class_latlon_reduced.h" -eccodes::grib::geo::LatlonReduced _grib_iterator_latlon_reduced{}; -eccodes::grib::geo::Iterator* grib_iterator_latlon_reduced = &_grib_iterator_latlon_reduced; +eccodes::geo_iterator::LatlonReduced _grib_iterator_latlon_reduced{}; +eccodes::geo_iterator::Iterator* grib_iterator_latlon_reduced = &_grib_iterator_latlon_reduced; -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { int LatlonReduced::next(double* lat, double* lon, double* val) const { @@ -26,8 +21,8 @@ int LatlonReduced::next(double* lat, double* lon, double* val) const return 0; e_++; - *lat = las_[e_]; - *lon = los_[e_]; + *lat = lats_[e_]; + *lon = lons_[e_]; if (val && data_) { *val = data_[e_]; } @@ -83,8 +78,8 @@ int LatlonReduced::init(grib_handle* h, grib_arguments* args) pl = (long*)grib_context_malloc(h->context, plsize * sizeof(long)); grib_get_long_array_internal(h, plac, pl, &plsize); - las_ = (double*)grib_context_malloc(h->context, nv_ * sizeof(double)); - los_ = (double*)grib_context_malloc(h->context, nv_ * sizeof(double)); + lats_ = (double*)grib_context_malloc(h->context, nv_ * sizeof(double)); + lons_ = (double*)grib_context_malloc(h->context, nv_ * sizeof(double)); plmax = pl[0]; for (j = 0; j < nlats; j++) @@ -119,8 +114,8 @@ int LatlonReduced::init(grib_handle* h, grib_arguments* args) nlons2 = 1; idirinc = dlon / nlons2; for (ii = 0; ii < nlons; ii++) { - las_[k] = laf; - los_[k] = tlof; + lats_[k] = laf; + lons_[k] = tlof; tlof += idirinc; k++; } @@ -137,12 +132,10 @@ int LatlonReduced::destroy() { const grib_context* c = h_->context; - grib_context_free(c, las_); - grib_context_free(c, los_); + grib_context_free(c, lats_); + grib_context_free(c, lons_); return Gen::destroy(); } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_latlon_reduced.h b/src/geo_iterator/grib_iterator_class_latlon_reduced.h similarity index 75% rename from src/iterator/grib_iterator_class_latlon_reduced.h rename to src/geo_iterator/grib_iterator_class_latlon_reduced.h index 992cba840..cbfdefd4f 100644 --- a/src/iterator/grib_iterator_class_latlon_reduced.h +++ b/src/geo_iterator/grib_iterator_class_latlon_reduced.h @@ -12,12 +12,7 @@ #include "grib_iterator_class_gen.h" -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { class LatlonReduced : public Gen { @@ -27,14 +22,8 @@ public: Iterator* create() const override { return new LatlonReduced(); } int init(grib_handle*, grib_arguments*) override; - int next(double* lat, double* lon, double* val) const override; + int next(double*, double*, double*) const override; int destroy() override; - -private: - double* las_; - double* los_; }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_mercator.cc b/src/geo_iterator/grib_iterator_class_mercator.cc similarity index 97% rename from src/iterator/grib_iterator_class_mercator.cc rename to src/geo_iterator/grib_iterator_class_mercator.cc index 0ef438c45..4d86164b1 100644 --- a/src/iterator/grib_iterator_class_mercator.cc +++ b/src/geo_iterator/grib_iterator_class_mercator.cc @@ -10,15 +10,10 @@ #include "grib_iterator_class_mercator.h" -eccodes::grib::geo::Mercator _grib_iterator_mercator{}; -eccodes::grib::geo::Iterator* grib_iterator_mercator = &_grib_iterator_mercator; +eccodes::geo_iterator::Mercator _grib_iterator_mercator{}; +eccodes::geo_iterator::Iterator* grib_iterator_mercator = &_grib_iterator_mercator; -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { #define ITER "Mercator Geoiterator" #define EPSILON 1.0e-10 @@ -286,6 +281,4 @@ int Mercator::destroy() return Gen::destroy(); } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_mercator.h b/src/geo_iterator/grib_iterator_class_mercator.h similarity index 81% rename from src/iterator/grib_iterator_class_mercator.h rename to src/geo_iterator/grib_iterator_class_mercator.h index 580e26e30..ce4e867b7 100644 --- a/src/iterator/grib_iterator_class_mercator.h +++ b/src/geo_iterator/grib_iterator_class_mercator.h @@ -12,12 +12,7 @@ #include "grib_iterator_class_gen.h" -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { class Mercator : public Gen { @@ -26,12 +21,10 @@ public: Iterator* create() const override { return new Mercator(); } int init(grib_handle*, grib_arguments*) override; - int next(double* lat, double* lon, double* val) const override; + int next(double*, double*, double*) const override; int destroy() override; private: - double* lats_; - double* lons_; long Nj_; int init_mercator(grib_handle*, @@ -43,6 +36,4 @@ private: double, double); }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_polar_stereographic.cc b/src/geo_iterator/grib_iterator_class_polar_stereographic.cc similarity index 97% rename from src/iterator/grib_iterator_class_polar_stereographic.cc rename to src/geo_iterator/grib_iterator_class_polar_stereographic.cc index 2a4a14f5b..f912374e0 100644 --- a/src/iterator/grib_iterator_class_polar_stereographic.cc +++ b/src/geo_iterator/grib_iterator_class_polar_stereographic.cc @@ -10,15 +10,10 @@ #include "grib_iterator_class_polar_stereographic.h" -eccodes::grib::geo::PolarStereographic _grib_iterator_polar_stereographic{}; -eccodes::grib::geo::Iterator* grib_iterator_polar_stereographic = &_grib_iterator_polar_stereographic; +eccodes::geo_iterator::PolarStereographic _grib_iterator_polar_stereographic{}; +eccodes::geo_iterator::Iterator* grib_iterator_polar_stereographic = &_grib_iterator_polar_stereographic; -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { #define ITER "Polar stereographic Geoiterator" @@ -315,6 +310,4 @@ int PolarStereographic::destroy() return Gen::destroy(); } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_polar_stereographic.h b/src/geo_iterator/grib_iterator_class_polar_stereographic.h similarity index 83% rename from src/iterator/grib_iterator_class_polar_stereographic.h rename to src/geo_iterator/grib_iterator_class_polar_stereographic.h index d786f4ee9..6b35c9040 100644 --- a/src/iterator/grib_iterator_class_polar_stereographic.h +++ b/src/geo_iterator/grib_iterator_class_polar_stereographic.h @@ -12,12 +12,7 @@ #include "grib_iterator_class_gen.h" -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { class PolarStereographic : public Gen { @@ -31,11 +26,7 @@ public: int destroy() override; private: - double* lats_; - double* lons_; long Nj_; }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_regular.cc b/src/geo_iterator/grib_iterator_class_regular.cc similarity index 86% rename from src/iterator/grib_iterator_class_regular.cc rename to src/geo_iterator/grib_iterator_class_regular.cc index 28148e94b..0b1865c7c 100644 --- a/src/iterator/grib_iterator_class_regular.cc +++ b/src/geo_iterator/grib_iterator_class_regular.cc @@ -10,15 +10,10 @@ #include "grib_iterator_class_regular.h" -eccodes::grib::geo::Regular _grib_iterator_regular{}; -eccodes::grib::geo::Iterator* grib_iterator_regular = &_grib_iterator_regular; +eccodes::geo_iterator::Regular _grib_iterator_regular{}; +eccodes::geo_iterator::Iterator* grib_iterator_regular = &_grib_iterator_regular; -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { #define ITER "Regular grid Geoiterator" @@ -29,8 +24,8 @@ int Regular::next(double* lat, double* lon, double* val) const e_++; - *lat = las_[(long)floor(e_ / Ni_)]; - *lon = los_[(long)e_ % Ni_]; + *lat = lats_[(long)floor(e_ / Ni_)]; + *lon = lons_[(long)e_ % Ni_]; if (val && data_) { *val = data_[e_]; } @@ -41,8 +36,8 @@ int Regular::previous(double* lat, double* lon, double* val) const { if (e_ < 0) return 0; - *lat = las_[(long)floor(e_ / Ni_)]; - *lon = los_[e_ % Ni_]; + *lat = lats_[(long)floor(e_ / Ni_)]; + *lon = lons_[e_ % Ni_]; if (val && data_) { *val = data_[e_]; } @@ -54,8 +49,8 @@ int Regular::previous(double* lat, double* lon, double* val) const int Regular::destroy() { const grib_context* c = h_->context; - grib_context_free(c, las_); - grib_context_free(c, los_); + grib_context_free(c, lats_); + grib_context_free(c, lons_); return Gen::destroy(); } @@ -141,15 +136,15 @@ int Regular::init(grib_handle* h, grib_arguments* args) Ni_ = Ni; Nj_ = Nj; - las_ = (double*)grib_context_malloc(h->context, Nj * sizeof(double)); - los_ = (double*)grib_context_malloc(h->context, Ni * sizeof(double)); + lats_ = (double*)grib_context_malloc(h->context, Nj * sizeof(double)); + lons_ = (double*)grib_context_malloc(h->context, Ni * sizeof(double)); if (idir != idir_coded) { grib_context_log(h->context, GRIB_LOG_DEBUG, "%s: Using idir=%g (coded value=%g)", ITER, idir, idir_coded); } for (loi = 0; loi < Ni; loi++) { - los_[loi] = lon1; + lons_[loi] = lon1; lon1 += idir; } @@ -159,11 +154,9 @@ int Regular::init(grib_handle* h, grib_arguments* args) if (lon2 > 0) { lon2 = normalise_longitude_in_degrees(lon2); } - los_[Ni - 1] = lon2; + lons_[Ni - 1] = lon2; return ret; } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_regular.h b/src/geo_iterator/grib_iterator_class_regular.h similarity index 75% rename from src/iterator/grib_iterator_class_regular.h rename to src/geo_iterator/grib_iterator_class_regular.h index a0301bef3..b7d5bfd34 100644 --- a/src/iterator/grib_iterator_class_regular.h +++ b/src/geo_iterator/grib_iterator_class_regular.h @@ -12,12 +12,7 @@ #include "grib_iterator_class_gen.h" -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { class Regular : public Gen { @@ -26,15 +21,13 @@ public: Iterator* create() const override { return new Regular(); } int init(grib_handle*, grib_arguments*) override; - int next(double* lat, double* lon, double* val) const override; - int previous(double* lat, double* lon, double* val) const override; + int next(double*, double*, double*) const override; + int previous(double*, double*, double*) const override; int destroy() override; protected: long Ni_; long Nj_; - double* las_; - double* los_; long iScansNegatively_; long isRotated_; double angleOfRotation_; @@ -44,6 +37,4 @@ protected: long disableUnrotate_; }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_space_view.cc b/src/geo_iterator/grib_iterator_class_space_view.cc similarity index 98% rename from src/iterator/grib_iterator_class_space_view.cc rename to src/geo_iterator/grib_iterator_class_space_view.cc index 0d488be05..ddf8700ed 100644 --- a/src/iterator/grib_iterator_class_space_view.cc +++ b/src/geo_iterator/grib_iterator_class_space_view.cc @@ -10,15 +10,10 @@ #include "grib_iterator_class_space_view.h" -eccodes::grib::geo::SpaceView _grib_iterator_space_view{}; -eccodes::grib::geo::Iterator* grib_iterator_space_view = &_grib_iterator_space_view; +eccodes::geo_iterator::SpaceView _grib_iterator_space_view{}; +eccodes::geo_iterator::Iterator* grib_iterator_space_view = &_grib_iterator_space_view; -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { #define ITER "Space view Geoiterator" @@ -326,6 +321,4 @@ int SpaceView::destroy() return Gen::destroy(); } -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/iterator/grib_iterator_class_space_view.h b/src/geo_iterator/grib_iterator_class_space_view.h similarity index 83% rename from src/iterator/grib_iterator_class_space_view.h rename to src/geo_iterator/grib_iterator_class_space_view.h index 7ed40c853..72a11e68f 100644 --- a/src/iterator/grib_iterator_class_space_view.h +++ b/src/geo_iterator/grib_iterator_class_space_view.h @@ -12,12 +12,7 @@ #include "grib_iterator_class_gen.h" -namespace eccodes -{ -namespace grib -{ -namespace geo -{ +namespace eccodes::geo_iterator { class SpaceView : public Gen { @@ -30,11 +25,7 @@ public: int destroy() override; private: - double* lats_; - double* lons_; long Nj_; }; -} // namespace geo -} // namespace grib -} // namespace eccodes +} // namespace eccodes::geo_iterator diff --git a/src/grib_api_internal.h b/src/grib_api_internal.h index baa1e6af7..d17ffb83f 100644 --- a/src/grib_api_internal.h +++ b/src/grib_api_internal.h @@ -248,12 +248,12 @@ typedef struct grib_codetable grib_codetable; typedef struct grib_smart_table grib_smart_table; class grib_accessor; -namespace eccodes::grib::geo { +namespace eccodes::geo_iterator { class Iterator; } typedef struct grib_iterator { - eccodes::grib::geo::Iterator* iterator; + eccodes::geo_iterator::Iterator* iterator; } grib_iterator; typedef struct grib_nearest_class grib_nearest_class; @@ -1254,7 +1254,7 @@ typedef struct j2k_encode_helper } #include "accessor/grib_accessor.h" #include "accessor/grib_accessors_list.h" -#include "iterator/grib_iterator.h" +#include "geo_iterator/grib_iterator.h" #endif #endif diff --git a/src/grib_iterator_class.cc b/src/grib_iterator_class.cc index 92129285e..0c0e3d6b3 100644 --- a/src/grib_iterator_class.cc +++ b/src/grib_iterator_class.cc @@ -14,8 +14,8 @@ ***************************************************************************/ #include "grib_api_internal.h" -#include "./iterator/grib_iterator.h" -#include "./accessor/grib_accessor_class_iterator.h" +#include "geo_iterator/grib_iterator.h" +#include "accessor/grib_accessor_class_iterator.h" /* This file is generated by ./make_class.pl */ #include "grib_iterator_class.h" @@ -51,7 +51,7 @@ static void init_mutex() struct table_entry { const char* type; - eccodes::grib::geo::Iterator** iterator; + eccodes::geo_iterator::Iterator** iterator; }; static const struct table_entry table[] = { @@ -60,7 +60,7 @@ static const struct table_entry table[] = { }; -eccodes::grib::geo::Iterator* grib_iterator_factory(grib_handle* h, grib_arguments* args, unsigned long flags, int* error) +eccodes::geo_iterator::Iterator* grib_iterator_factory(grib_handle* h, grib_arguments* args, unsigned long flags, int* error) { size_t i = 0, num_table_entries = 0; const char* type = (char*)grib_arguments_get_name(h, args, 0); @@ -69,8 +69,8 @@ eccodes::grib::geo::Iterator* grib_iterator_factory(grib_handle* h, grib_argumen num_table_entries = sizeof(table) / sizeof(table[0]); for (i = 0; i < num_table_entries; i++) { if (strcmp(type, table[i].type) == 0) { - eccodes::grib::geo::Iterator* builder = *(table[i].iterator); - eccodes::grib::geo::Iterator* it = builder->create(); + eccodes::geo_iterator::Iterator* builder = *(table[i].iterator); + eccodes::geo_iterator::Iterator* it = builder->create(); it->flags_ = flags; GRIB_MUTEX_INIT_ONCE(&once, &init_mutex); @@ -95,10 +95,10 @@ eccodes::grib::geo::Iterator* grib_iterator_factory(grib_handle* h, grib_argumen int grib_get_data(const grib_handle* h, double* lats, double* lons, double* values) { int err = 0; - eccodes::grib::geo::Iterator* iter = NULL; + eccodes::geo_iterator::Iterator* iter = NULL; double *lat, *lon, *val; - iter = eccodes::grib::geo::gribIteratorNew(h, 0, &err); + iter = eccodes::geo_iterator::gribIteratorNew(h, 0, &err); if (!iter || err != GRIB_SUCCESS) return err; diff --git a/src/grib_iterator_class.h b/src/grib_iterator_class.h index 833f9b579..31aabe985 100644 --- a/src/grib_iterator_class.h +++ b/src/grib_iterator_class.h @@ -1,13 +1,13 @@ /* This file is automatically generated by ./make_class.pl, do not edit */ -extern eccodes::grib::geo::Iterator* grib_iterator_gaussian; -extern eccodes::grib::geo::Iterator* grib_iterator_gaussian_reduced; -//extern eccodes::grib::geo::Iterator* grib_iterator_gen; -extern eccodes::grib::geo::Iterator* grib_iterator_healpix; -extern eccodes::grib::geo::Iterator* grib_iterator_lambert_azimuthal_equal_area; -extern eccodes::grib::geo::Iterator* grib_iterator_lambert_conformal; -extern eccodes::grib::geo::Iterator* grib_iterator_latlon; -extern eccodes::grib::geo::Iterator* grib_iterator_latlon_reduced; -extern eccodes::grib::geo::Iterator* grib_iterator_mercator; -extern eccodes::grib::geo::Iterator* grib_iterator_polar_stereographic; -extern eccodes::grib::geo::Iterator* grib_iterator_regular; -extern eccodes::grib::geo::Iterator* grib_iterator_space_view; +extern eccodes::geo_iterator::Iterator* grib_iterator_gaussian; +extern eccodes::geo_iterator::Iterator* grib_iterator_gaussian_reduced; +//extern eccodes::geo_iterator::Iterator* grib_iterator_gen; +extern eccodes::geo_iterator::Iterator* grib_iterator_healpix; +extern eccodes::geo_iterator::Iterator* grib_iterator_lambert_azimuthal_equal_area; +extern eccodes::geo_iterator::Iterator* grib_iterator_lambert_conformal; +extern eccodes::geo_iterator::Iterator* grib_iterator_latlon; +extern eccodes::geo_iterator::Iterator* grib_iterator_latlon_reduced; +extern eccodes::geo_iterator::Iterator* grib_iterator_mercator; +extern eccodes::geo_iterator::Iterator* grib_iterator_polar_stereographic; +extern eccodes::geo_iterator::Iterator* grib_iterator_regular; +extern eccodes::geo_iterator::Iterator* grib_iterator_space_view; From b7187d77b2bda661d553fc3f8cd9815cfc13de76 Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Mon, 21 Oct 2024 17:37:29 +0200 Subject: [PATCH 14/20] Modernisation: Fix mutex name --- src/grib_iterator_class.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/grib_iterator_class.cc b/src/grib_iterator_class.cc index 0c0e3d6b3..fbf208818 100644 --- a/src/grib_iterator_class.cc +++ b/src/grib_iterator_class.cc @@ -34,14 +34,14 @@ static void init_mutex() } #elif GRIB_OMP_THREADS static int once = 0; -static omp_nest_lock_t mutex1; +static omp_nest_lock_t mutex; static void init_mutex() { GRIB_OMP_CRITICAL(lock_grib_accessor_class_c) { if (once == 0) { - omp_init_nest_lock(&mutex1); + omp_init_nest_lock(&mutex); once = 1; } } From 25835a2e23a7066b17f7eae1e820b3aeecb7819b Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Mon, 21 Oct 2024 18:09:39 +0200 Subject: [PATCH 15/20] Modernisation: Fix include dirs --- CMakeLists.txt | 2 -- src/CMakeLists.txt | 6 ++++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b002b1b2..05bd33688 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -435,8 +435,6 @@ endforeach() ### include directories include_directories( - "accessor" - "geo_iterator" ${ECCODES_INCLUDE_DIRS} ${ECCODES_EXTRA_INCLUDE_DIRS} ) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a012d66e7..aa38b6ad3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,6 +8,12 @@ # granted to it by virtue of its status as an intergovernmental organisation # nor does it submit to any jurisdiction. # + +include_directories( + "${CMAKE_CURRENT_SOURCE_DIR}/accessor" + "${CMAKE_CURRENT_SOURCE_DIR}/geo_iterator" + ) + list( APPEND eccodes_src_files accessor/grib_accessor.cc accessor/grib_accessors_list.cc From 6b343e279a9cea8266af3ab62412260e832f6f9f Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Mon, 21 Oct 2024 18:24:45 +0200 Subject: [PATCH 16/20] Modernisation: Eliminate unnecessary variables --- src/geo_iterator/grib_iterator.h | 2 +- src/geo_iterator/grib_iterator_class_gen.cc | 10 ++++------ src/geo_iterator/grib_iterator_class_gen.h | 1 - 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/geo_iterator/grib_iterator.h b/src/geo_iterator/grib_iterator.h index 948bb5fe9..d97b6974b 100644 --- a/src/geo_iterator/grib_iterator.h +++ b/src/geo_iterator/grib_iterator.h @@ -41,7 +41,7 @@ protected: const char* class_name_; private: - grib_arguments* args_; /** args of iterator */ + //grib_arguments* args_; [>* args of iterator <] size_t size_; int inited_; }; diff --git a/src/geo_iterator/grib_iterator_class_gen.cc b/src/geo_iterator/grib_iterator_class_gen.cc index e2c9d825e..ea813dd8b 100644 --- a/src/geo_iterator/grib_iterator_class_gen.cc +++ b/src/geo_iterator/grib_iterator_class_gen.cc @@ -22,15 +22,13 @@ int Gen::init(grib_handle* h, grib_arguments* args) const char* s_rawData = NULL; const char* s_numPoints = NULL; long numberOfPoints = 0; - carg_ = 1; + int carg = 1; - s_numPoints = grib_arguments_get_name(h, args, carg_++); - missingValue_ = grib_arguments_get_name(h, args, carg_++); - s_rawData = grib_arguments_get_name(h, args, carg_++); + s_numPoints = grib_arguments_get_name(h, args, carg++); + missingValue_ = grib_arguments_get_name(h, args, carg++); + s_rawData = grib_arguments_get_name(h, args, carg++); data_ = NULL; - h = h; /* We may not need to keep them */ - args = args; if ((err = grib_get_size(h, s_rawData, &dli)) != GRIB_SUCCESS) return err; diff --git a/src/geo_iterator/grib_iterator_class_gen.h b/src/geo_iterator/grib_iterator_class_gen.h index 0cca14c08..ace2c0232 100644 --- a/src/geo_iterator/grib_iterator_class_gen.h +++ b/src/geo_iterator/grib_iterator_class_gen.h @@ -29,7 +29,6 @@ public: bool has_next() const override; protected: - int carg_; double* lats_; double* lons_; From 60a37e36d0f1991f80deb5f3c26cf3585914d765 Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Mon, 21 Oct 2024 18:27:00 +0200 Subject: [PATCH 17/20] Modernisation: Revert deletion of the cargs_ --- src/geo_iterator/grib_iterator_class_gen.cc | 8 ++++---- src/geo_iterator/grib_iterator_class_gen.h | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/geo_iterator/grib_iterator_class_gen.cc b/src/geo_iterator/grib_iterator_class_gen.cc index ea813dd8b..3f8fadc02 100644 --- a/src/geo_iterator/grib_iterator_class_gen.cc +++ b/src/geo_iterator/grib_iterator_class_gen.cc @@ -22,11 +22,11 @@ int Gen::init(grib_handle* h, grib_arguments* args) const char* s_rawData = NULL; const char* s_numPoints = NULL; long numberOfPoints = 0; - int carg = 1; + int carg_ = 1; - s_numPoints = grib_arguments_get_name(h, args, carg++); - missingValue_ = grib_arguments_get_name(h, args, carg++); - s_rawData = grib_arguments_get_name(h, args, carg++); + s_numPoints = grib_arguments_get_name(h, args, carg_++); + missingValue_ = grib_arguments_get_name(h, args, carg_++); + s_rawData = grib_arguments_get_name(h, args, carg_++); data_ = NULL; if ((err = grib_get_size(h, s_rawData, &dli)) != GRIB_SUCCESS) diff --git a/src/geo_iterator/grib_iterator_class_gen.h b/src/geo_iterator/grib_iterator_class_gen.h index ace2c0232..0cca14c08 100644 --- a/src/geo_iterator/grib_iterator_class_gen.h +++ b/src/geo_iterator/grib_iterator_class_gen.h @@ -29,6 +29,7 @@ public: bool has_next() const override; protected: + int carg_; double* lats_; double* lons_; From c42c772f5e7073fe980a15ab45a54c0ba0122834 Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Mon, 21 Oct 2024 18:27:50 +0200 Subject: [PATCH 18/20] Modernisation: Revert deletion of cargs_ --- src/geo_iterator/grib_iterator_class_gen.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/geo_iterator/grib_iterator_class_gen.cc b/src/geo_iterator/grib_iterator_class_gen.cc index 3f8fadc02..cf63d0ce4 100644 --- a/src/geo_iterator/grib_iterator_class_gen.cc +++ b/src/geo_iterator/grib_iterator_class_gen.cc @@ -22,7 +22,7 @@ int Gen::init(grib_handle* h, grib_arguments* args) const char* s_rawData = NULL; const char* s_numPoints = NULL; long numberOfPoints = 0; - int carg_ = 1; + carg_ = 1; s_numPoints = grib_arguments_get_name(h, args, carg_++); missingValue_ = grib_arguments_get_name(h, args, carg_++); From 2479a8670c6cdcf4f7570c98677b68c2e0dc1068 Mon Sep 17 00:00:00 2001 From: Eugen Betke Date: Mon, 21 Oct 2024 18:30:44 +0200 Subject: [PATCH 19/20] Modernisatino: Fix error handling --- .../grib_iterator_class_lambert_azimuthal_equal_area.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/geo_iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc b/src/geo_iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc index 15eff4bef..a3f2f32f8 100644 --- a/src/geo_iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc +++ b/src/geo_iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc @@ -171,7 +171,7 @@ int LambertAzimuthalEqualArea::init_oblate(grib_handle* h, return GRIB_OUT_OF_MEMORY; } lons_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!lats_) { + if (!lons_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } From ce006ec2269e8a648f38b4e840795210f3171e7f Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Mon, 21 Oct 2024 16:38:52 +0000 Subject: [PATCH 20/20] Fix cppcheck warnings --- .../grib_iterator_class_lambert_azimuthal_equal_area.cc | 2 +- src/geo_iterator/grib_iterator_class_lambert_conformal.cc | 4 ++-- src/geo_iterator/grib_iterator_class_mercator.cc | 2 +- src/geo_iterator/grib_iterator_class_polar_stereographic.cc | 2 +- src/geo_iterator/grib_iterator_class_space_view.cc | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/geo_iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc b/src/geo_iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc index a3f2f32f8..009665ea4 100644 --- a/src/geo_iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc +++ b/src/geo_iterator/grib_iterator_class_lambert_azimuthal_equal_area.cc @@ -256,7 +256,7 @@ int LambertAzimuthalEqualArea::init_sphere(grib_handle* h, return GRIB_OUT_OF_MEMORY; } lons_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!lats_) { + if (!lons_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } diff --git a/src/geo_iterator/grib_iterator_class_lambert_conformal.cc b/src/geo_iterator/grib_iterator_class_lambert_conformal.cc index 5204d7638..6b8a6fe93 100644 --- a/src/geo_iterator/grib_iterator_class_lambert_conformal.cc +++ b/src/geo_iterator/grib_iterator_class_lambert_conformal.cc @@ -166,7 +166,7 @@ int LambertConformal::init_sphere(const grib_handle* h, return GRIB_OUT_OF_MEMORY; } lons_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!lats_) { + if (!lons_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } @@ -291,7 +291,7 @@ int LambertConformal::init_oblate(const grib_handle* h, return GRIB_OUT_OF_MEMORY; } lons_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!lats_) { + if (!lons_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } diff --git a/src/geo_iterator/grib_iterator_class_mercator.cc b/src/geo_iterator/grib_iterator_class_mercator.cc index 4d86164b1..e4a5496be 100644 --- a/src/geo_iterator/grib_iterator_class_mercator.cc +++ b/src/geo_iterator/grib_iterator_class_mercator.cc @@ -123,7 +123,7 @@ int Mercator::init_mercator(grib_handle* h, return GRIB_OUT_OF_MEMORY; } lons_ = (double*)grib_context_malloc(h->context, nv * sizeof(double)); - if (!lats_) { + if (!lons_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv * sizeof(double)); return GRIB_OUT_OF_MEMORY; } diff --git a/src/geo_iterator/grib_iterator_class_polar_stereographic.cc b/src/geo_iterator/grib_iterator_class_polar_stereographic.cc index f912374e0..181becee8 100644 --- a/src/geo_iterator/grib_iterator_class_polar_stereographic.cc +++ b/src/geo_iterator/grib_iterator_class_polar_stereographic.cc @@ -186,7 +186,7 @@ int PolarStereographic::init(grib_handle* h, grib_arguments* args) return GRIB_OUT_OF_MEMORY; } lons_ = (double*)grib_context_malloc(h->context, nv_ * sizeof(double)); - if (!lats_) { + if (!lons_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv_ * sizeof(double)); return GRIB_OUT_OF_MEMORY; } diff --git a/src/geo_iterator/grib_iterator_class_space_view.cc b/src/geo_iterator/grib_iterator_class_space_view.cc index ddf8700ed..15b57156b 100644 --- a/src/geo_iterator/grib_iterator_class_space_view.cc +++ b/src/geo_iterator/grib_iterator_class_space_view.cc @@ -227,7 +227,7 @@ int SpaceView::init(grib_handle* h, grib_arguments* args) return GRIB_OUT_OF_MEMORY; } lons_ = (double*)grib_context_malloc(h->context, array_size); - if (!lats_) { + if (!lons_) { grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, array_size); return GRIB_OUT_OF_MEMORY; }