Skip to content

Commit

Permalink
Work on eckit_geo
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaciel committed Mar 14, 2024
1 parent aeb4d71 commit 8b3fdc7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 57 deletions.
62 changes: 12 additions & 50 deletions src/eccodes/geo/GribSpec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,10 @@
#include <sstream>
#include <vector>

#define ECKIT_THREADS
#if defined(ECKIT_THREADS)
#include "eckit/thread/AutoLock.h"
#include "eckit/thread/Mutex.h"
#else
#include <mutex>
#endif

#include "eckit/config/Resource.h"
#include "eckit/exception/Exceptions.h"
#include "eckit/geo/PointLonLat.h"
#include "eckit/geo/util/Mutex.h"
#include "eckit/log/JSON.h"
#include "eckit/types/FloatCompare.h"
#include "eckit/types/Fraction.h"
Expand Down Expand Up @@ -561,46 +554,15 @@ bool get_value(const std::string& name, codes_handle* h, T& value, const Process
}


} // namespace


namespace util {

eckit::geo::util::recursive_mutex MUTEX;

#if defined(ECKIT_THREADS)


using recursive_mutex = eckit::Mutex;

template <typename T>
using lock_guard = typename eckit::AutoLock<T>;

struct once_flag {
pthread_once_t once_ = PTHREAD_ONCE_INIT;
class lock_type {
eckit::geo::util::lock_guard<eckit::geo::util::recursive_mutex> lock_guard_{MUTEX};
};

template <class Callable>
inline void call_once(once_flag& flag, Callable&& fun) {
pthread_once(&(flag.once_), fun);
}


#else


using std::call_once;
using std::lock_guard;
using std::once_flag;
using std::recursive_mutex;


#endif


} // namespace util


static util::recursive_mutex MUTEX;
} // namespace


GribSpec::GribSpec(codes_handle* h) : handle_(h) {
Expand All @@ -609,7 +571,7 @@ GribSpec::GribSpec(codes_handle* h) : handle_(h) {


bool GribSpec::has(const std::string& name) const {
util::lock_guard<util::recursive_mutex> lock(MUTEX);
lock_type lock;

if (cache_.has(name)) {
return true;
Expand All @@ -627,7 +589,7 @@ bool GribSpec::has(const std::string& name) const {


bool GribSpec::get(const std::string& name, std::string& value) const {
util::lock_guard<util::recursive_mutex> lock(MUTEX);
lock_type lock;

if (cache_.get(name, value)) {
return true;
Expand Down Expand Up @@ -666,7 +628,7 @@ bool GribSpec::get(const std::string& name, std::string& value) const {


bool GribSpec::get(const std::string& name, bool& value) const {
util::lock_guard<util::recursive_mutex> lock(MUTEX);
lock_type lock;

if (cache_.get(name, value)) {
return true;
Expand Down Expand Up @@ -701,7 +663,7 @@ bool GribSpec::get(const std::string& name, int& value) const {


bool GribSpec::get(const std::string& name, long& value) const {
util::lock_guard<util::recursive_mutex> lock(MUTEX);
lock_type lock;

if (cache_.get(name, value)) {
return true;
Expand Down Expand Up @@ -750,7 +712,7 @@ bool GribSpec::get(const std::string& name, float& value) const {


bool GribSpec::get(const std::string& name, double& value) const {
util::lock_guard<util::recursive_mutex> lock(MUTEX);
lock_type lock;

if (cache_.get(name, value)) {
return true;
Expand Down Expand Up @@ -797,7 +759,7 @@ bool GribSpec::get(const std::string& /*name*/, std::vector<int>& /*value*/) con


bool GribSpec::get(const std::string& name, std::vector<long>& value) const {
util::lock_guard<util::recursive_mutex> lock(MUTEX);
lock_type lock;

if (cache_.get(name, value)) {
return true;
Expand Down Expand Up @@ -865,7 +827,7 @@ bool GribSpec::get(const std::string& name, std::vector<float>& value) const {


bool GribSpec::get(const std::string& name, std::vector<double>& value) const {
util::lock_guard<util::recursive_mutex> lock(MUTEX);
lock_type lock;

if (cache_.get(name, value)) {
return true;
Expand Down
18 changes: 11 additions & 7 deletions tests/gribspec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@


int main(int argc, const char* argv[]) {
ASSERT_MSG(argc == 3, "ERROR: expected 2 arguments: grib_file json_file");
// FIXME processes only the first message in the GRIB file
ASSERT_MSG(argc == 2 || argc == 3, "ERROR: expected 1 or 2 arguments: grib_file [json_file]");


// create a JSON string ("GribSpec") from the GRIB file
// Create a JSON string from the GRIB file ("GribSpec")
std::string result_string;

auto* grib_file = std::fopen(argv[1], "r");
Expand All @@ -37,21 +38,24 @@ int main(int argc, const char* argv[]) {
ASSERT(err == CODES_SUCCESS);
ASSERT(h != nullptr);

result_string = (std::ostringstream{} << eccodes::geo::GribSpec(h)).str();
std::cout << result_string << std::endl;
if (argc == 2) {
std::cout << eccodes::geo::GribSpec(h) << std::endl;
return 0;
}

break; // FIXME processes only the first message
result_string = (std::ostringstream{} << eccodes::geo::GribSpec(h)).str();
break;
}

std::fclose(grib_file);


// Parse the resulting JSON ("GribSpec") from the GRIB file
// Parse result from the GRIB file ("GribSpec")
std::istringstream result_stream(result_string);
auto result = eckit::JSONParser(result_stream).parse();


// Parse the reference JSON from the JSON file
// Parse reference from the JSON file
std::ifstream json_stream(argv[2]);
auto reference = eckit::JSONParser(json_stream).parse();
json_stream.close();
Expand Down

0 comments on commit 8b3fdc7

Please sign in to comment.