Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ECC-1948: Conversion of geo-iterators to C++ #256

Merged
merged 24 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1afda19
Modernisation: C++ iterators
joobog Sep 21, 2024
2122cd6
Modernisation: Introduce namespaces and change class names
joobog Sep 24, 2024
3940d7d
Modernisation: Iterators (all ctests pass)
joobog Oct 16, 2024
fff9e47
Merge branch 'develop' into feature/modernisation_iterators
joobog Oct 16, 2024
3038878
Modernisation: Fix error code propagation in iterators destroy()
joobog Oct 17, 2024
423521b
Modernisation: Fix memory leak and minor changes
joobog Oct 17, 2024
b490cfc
Modernisation: Protect iterator's init() with a mutex
joobog Oct 17, 2024
2bd5482
Modernisation: has_next() returns bool instead of long
joobog Oct 17, 2024
a8f43e9
Modernisation: Fix grib_iterator_delete()
joobog Oct 17, 2024
c41dd80
Merge branch 'develop' into feature/modernisation_iterators
joobog Oct 17, 2024
c761089
Modernisation: clang-format
joobog Oct 17, 2024
408066a
Modernisation: Make init_sphere() and init_oblate() member functions
joobog Oct 17, 2024
e4df2db
Modernisation: Make members private
joobog Oct 17, 2024
f63f70f
Modernisation: Make next(), previous() and has_next() const members
joobog Oct 17, 2024
fe7ff3a
Merge branch 'develop' into feature/modernisation_iterators
joobog Oct 17, 2024
80cc5a4
Merge branch 'develop' into feature/modernisation_iterators
joobog Oct 21, 2024
f2c1a83
Modernisation: Change namespace and move lats_ and lons_ to base class
joobog Oct 21, 2024
b7187d7
Modernisation: Fix mutex name
joobog Oct 21, 2024
25835a2
Modernisation: Fix include dirs
joobog Oct 21, 2024
6b343e2
Modernisation: Eliminate unnecessary variables
joobog Oct 21, 2024
60a37e3
Modernisation: Revert deletion of the cargs_
joobog Oct 21, 2024
c42c772
Modernisation: Revert deletion of cargs_
joobog Oct 21, 2024
2479a86
Modernisatino: Fix error handling
joobog Oct 21, 2024
ce006ec
Fix cppcheck warnings
shahramn Oct 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,10 @@ endforeach()

### include directories

include_directories( ${ECCODES_INCLUDE_DIRS} ${ECCODES_EXTRA_INCLUDE_DIRS} )
include_directories(
${ECCODES_INCLUDE_DIRS}
${ECCODES_EXTRA_INCLUDE_DIRS}
)

add_subdirectory( definitions ) # must be before memfs
add_subdirectory( memfs )
Expand Down
32 changes: 19 additions & 13 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -331,20 +337,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
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
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
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
Expand Down
32 changes: 0 additions & 32 deletions src/accessor/grib_accessor_class_iterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 1 addition & 4 deletions src/accessor/grib_accessor_class_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,5 @@ class grib_accessor_iterator_t : public grib_accessor_gen_t

private:
grib_arguments* args_;

friend grib_iterator* grib_iterator_new(const grib_handle* ch, unsigned long flags, int* error);
friend eccodes::geo_iterator::Iterator* eccodes::geo_iterator::gribIteratorNew(const grib_handle*, unsigned long, int*);
};

// grib_iterator* grib_iterator_new(const grib_handle* ch, unsigned long flags, int* error)
14 changes: 7 additions & 7 deletions src/eccodes_prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -812,15 +812,15 @@ 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);
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);
Expand Down
133 changes: 133 additions & 0 deletions src/geo_iterator/grib_iterator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* (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::geo_iterator {

int Iterator::init(grib_handle* h, grib_arguments* args)
{
h_ = h;
return GRIB_SUCCESS;
}

/* For this one, ALL destroy are called */
int Iterator::destroy()
{
delete context_;
delete this;
return GRIB_SUCCESS;
}

eccodes::geo_iterator::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;
*error = GRIB_NOT_IMPLEMENTED;
a = grib_find_accessor(h, "ITERATOR");
ita = (grib_accessor_iterator_t*)a;

if (!a)
return NULL;

eccodes::geo_iterator::Iterator* iter = grib_iterator_factory(h, ita->args_, flags, error);

if (iter)
*error = GRIB_SUCCESS;

return iter;
}

int gribIteratorDelete(eccodes::geo_iterator::Iterator* i)
{
if (i)
i->destroy();
return GRIB_SUCCESS;
}


} // namespace eccodes::geo_iterator


/*
* 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::geo_iterator::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) {
grib_context* c = grib_context_get_default();
gribIteratorDelete(i->iterator);
grib_context_free(c, i);
}
return GRIB_SUCCESS;
}

#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
52 changes: 52 additions & 0 deletions src/geo_iterator/grib_iterator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* (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
*/

namespace eccodes::geo_iterator {

class Iterator
{
public:
virtual ~Iterator() {}
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 */
mutable long e_; /** current element */
size_t nv_; /** number of values */
const char* class_name_;

private:
//grib_arguments* args_; [>* args of iterator <]
size_t size_;
int inited_;
};

eccodes::geo_iterator::Iterator* gribIteratorNew(const grib_handle*, unsigned long, int*);
int gribIteratorDelete(eccodes::geo_iterator::Iterator*);

} // namespace eccodes::geo_iterator
Loading
Loading