Skip to content

Commit

Permalink
Geoiterator for unstructured grids (disabled)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahramn committed Nov 8, 2024
1 parent 2b0efc1 commit 562cf2a
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
2 changes: 2 additions & 0 deletions definitions/grib2/templates/template.3.101.def
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ unsigned[1] numberOfGridInReference : dump;
byte[16] uuidOfHGrid : dump;

template_nofail unstructuredGrid "grib2/localConcepts/[centre:s]/unstructuredGrid.def";

# iterator unstructured(numberOfPoints, missingValue, values, uuidOfHGrid);
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ list( APPEND eccodes_src_files
geo_iterator/grib_iterator_class_polar_stereographic.cc
geo_iterator/grib_iterator_class_regular.cc
geo_iterator/grib_iterator_class_space_view.cc
geo_iterator/grib_iterator_class_unstructured.cc
grib_expression.cc
codes_util.cc
grib_util.cc
Expand Down
73 changes: 73 additions & 0 deletions src/geo_iterator/grib_iterator_class_unstructured.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* (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_unstructured.h"

eccodes::geo_iterator::Unstructured _grib_iterator_unstructured{};
eccodes::geo_iterator::Iterator* grib_iterator_unstructured = &_grib_iterator_unstructured;

namespace eccodes::geo_iterator {

#define ITER "Unstructured grid Geoiterator"

int Unstructured::next(double* lat, double* lon, double* val) const
{
if ((long)e_ >= (long)(nv_ - 1))
return 0;
e_++;

*lat = lats_[e_];
*lon = lons_[e_];
if (val && data_) {
*val = data_[e_];
}
return 1;
}

int Unstructured::init(grib_handle* h, grib_arguments* args)
{
int ret = GRIB_SUCCESS;
if ((ret = Gen::init(h, args)) != GRIB_SUCCESS)
return ret;

const char* s_uuidOfHGrid = grib_arguments_get_name(h, args, carg_++);
char uuidOfHGrid[32] = {0,};
auto slen = sizeof(uuidOfHGrid);
if ((ret = grib_get_string_internal(h, s_uuidOfHGrid, uuidOfHGrid, &slen)) != GRIB_SUCCESS) {
return ret;
}

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;
}
lons_ = (double*)grib_context_malloc(h->context, nv_ * sizeof(double));
if (!lons_) {
grib_context_log(h->context, GRIB_LOG_ERROR, "%s: Error allocating %zu bytes", ITER, nv_ * sizeof(double));
return GRIB_OUT_OF_MEMORY;
}

e_ = -1;

return ret;
}

int Unstructured::destroy()
{
DEBUG_ASSERT(h_);
const grib_context* c = h_->context;
grib_context_free(c, lats_);
grib_context_free(c, lons_);

return Gen::destroy();
}

} // namespace eccodes::geo_iterator
29 changes: 29 additions & 0 deletions src/geo_iterator/grib_iterator_class_unstructured.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* (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"

namespace eccodes::geo_iterator {

class Unstructured : public Gen
{
public:
Unstructured() :
Gen() { class_name_ = "unstructured"; }
Iterator* create() const override { return new Unstructured(); }

int init(grib_handle*, grib_arguments*) override;
int next(double*, double*, double*) const override;
int destroy() override;
};

} // namespace eccodes::geo_iterator
1 change: 1 addition & 0 deletions src/grib_iterator_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ static const struct table_entry table[] = {
{ "polar_stereographic", &grib_iterator_polar_stereographic, },
{ "regular", &grib_iterator_regular, },
{ "space_view", &grib_iterator_space_view, },
{ "unstructured", &grib_iterator_unstructured, },
};

eccodes::geo_iterator::Iterator* grib_iterator_factory(grib_handle* h, grib_arguments* args, unsigned long flags, int* error)
Expand Down
1 change: 1 addition & 0 deletions src/grib_iterator_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ 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;
extern eccodes::geo_iterator::Iterator* grib_iterator_unstructured;


eccodes::geo_iterator::Iterator* grib_iterator_factory(grib_handle* h, grib_arguments* args, unsigned long flags, int* error);

0 comments on commit 562cf2a

Please sign in to comment.