Skip to content

Commit

Permalink
Merge branch 'feature/MIR-679' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaciel committed Nov 22, 2024
2 parents 87af8ac + b2a3d46 commit 0379d79
Show file tree
Hide file tree
Showing 20 changed files with 481 additions and 179 deletions.
4 changes: 4 additions & 0 deletions src/mir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ list(APPEND mir_srcs
input/GribMemoryInput.h
input/GribStreamInput.cc
input/GribStreamInput.h
input/GriddefInput.cc
input/GriddefInput.h
input/MIRInput.cc
input/MIRInput.h
input/MultiDimensionalGribFileInput.cc
Expand Down Expand Up @@ -457,6 +459,8 @@ list(APPEND mir_srcs
output/GribOutput.h
output/GribStreamOutput.cc
output/GribStreamOutput.h
output/GriddefOutput.cc
output/GriddefOutput.h
output/MIROutput.cc
output/MIROutput.h
output/MultiDimensionalOutput.cc
Expand Down
98 changes: 47 additions & 51 deletions src/mir/input/ArtificialInput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@

#include "mir/input/ArtificialInput.h"

#include <iomanip>
#include <map>
#include <ostream>
#include <sstream>

#include "mir/data/MIRField.h"
#include "mir/input/GriddefInput.h"
#include "mir/param/CombinedParametrisation.h"
#include "mir/param/SimpleParametrisation.h"
#include "mir/util/Exceptions.h"
#include "mir/util/Log.h"
Expand Down Expand Up @@ -47,6 +51,12 @@ size_t ArtificialInput::dimensions() const {
}


ArtificialInput::ArtificialInput() :
inputParametrisation_(new param::CombinedParametrisation(parametrisation_, *this, parametrisation_)), calls_(0) {
ASSERT(inputParametrisation_);
}


param::SimpleParametrisation& ArtificialInput::parametrisation(size_t which) {
ASSERT(which == 0);
return parametrisation_;
Expand All @@ -55,7 +65,7 @@ param::SimpleParametrisation& ArtificialInput::parametrisation(size_t which) {

const param::MIRParametrisation& ArtificialInput::parametrisation(size_t which) const {
ASSERT(which == 0);
return parametrisation_;
return *inputParametrisation_;
}


Expand All @@ -70,84 +80,72 @@ void ArtificialInput::setAuxiliaryInformation(const util::ValueMap& map) {
parametrisation_.set("grid", grid);
}
}

if (!parametrisation_.has("rotation")) {
std::vector<double> rotation(2, 0);
if (parametrisation_.get("south_pole_latitude", rotation[0]) &&
parametrisation_.get("south_pole_longitude", rotation[1])) {
parametrisation_.set("rotation", rotation);
}
}
}

auto load = [](const eckit::PathName& path, std::vector<double>& values) {
Log::info() << "ArtificialInput::setAuxiliaryInformation: '" << path << "'" << std::endl;

void ArtificialInput::print(std::ostream& out) const {
out << "ArtificialInput[parametrisation=" << parametrisation_ << "]";
}
static const param::SimpleParametrisation empty;
std::unique_ptr<input::MIRInput> input(input::MIRInputFactory::build(path.asString(), empty));
ASSERT(input->next());

auto field = input->field();
ASSERT(field.dimensions() == 1);

bool ArtificialInput::sameAs(const MIRInput& other) const {
const auto* o = dynamic_cast<const ArtificialInput*>(&other);
return (o != nullptr) && parametrisation_.matchAll(o->parametrisation_);
}
values = field.values(0);
};

if (std::string path; parametrisation_.get("griddef", path)) {
GriddefInput::load(path, latitudes_, longitudes_);
}

bool ArtificialInput::has(const std::string& name) const {
return parametrisation_.has(name) || FieldParametrisation::has(name);
}
if (std::string lats, lons; parametrisation_.get("latitudes", lats) && parametrisation_.get("longitudes", lons)) {
load(lats, latitudes_);
load(lons, longitudes_);
}

ASSERT(latitudes_.size() == longitudes_.size());
if (!parametrisation_.has("numberOfDataPoints")) {
parametrisation_.set("numberOfDataPoints", latitudes_.size());
}

bool ArtificialInput::get(const std::string& name, std::string& value) const {
return parametrisation_.get(name, value) || FieldParametrisation::get(name, value);
if (!parametrisation_.has("paramId")) {
parametrisation_.set("paramId", 255L /*missing*/);
}
}


bool ArtificialInput::get(const std::string& name, bool& value) const {
return parametrisation_.get(name, value) || FieldParametrisation::get(name, value);
void ArtificialInput::print(std::ostream& out) const {
out << "ArtificialInput[parametrisation=" << parametrisation_ << "]";
}


bool ArtificialInput::get(const std::string& name, int& value) const {
return parametrisation_.get(name, value) || FieldParametrisation::get(name, value);
bool ArtificialInput::sameAs(const MIRInput& other) const {
const auto* o = dynamic_cast<const ArtificialInput*>(&other);
return (o != nullptr) && parametrisation_.matchAll(o->parametrisation_);
}


bool ArtificialInput::get(const std::string& name, long& value) const {
return parametrisation_.get(name, value) || FieldParametrisation::get(name, value);
}


bool ArtificialInput::get(const std::string& name, float& value) const {
return parametrisation_.get(name, value) || FieldParametrisation::get(name, value);
static const std::string PARAM_ID("paramId");
return name == PARAM_ID ? parametrisation_.get(name, value) : FieldParametrisation::get(name, value);
}


bool ArtificialInput::get(const std::string& name, double& value) const {
return parametrisation_.get(name, value) || FieldParametrisation::get(name, value);
void ArtificialInput::latitudes(std::vector<double>& values) const {
values = latitudes_;
}


bool ArtificialInput::get(const std::string& name, std::vector<int>& value) const {
return parametrisation_.get(name, value) || FieldParametrisation::get(name, value);
}


bool ArtificialInput::get(const std::string& name, std::vector<long>& value) const {
return parametrisation_.get(name, value) || FieldParametrisation::get(name, value);
}


bool ArtificialInput::get(const std::string& name, std::vector<float>& value) const {
return parametrisation_.get(name, value) || FieldParametrisation::get(name, value);
}


bool ArtificialInput::get(const std::string& name, std::vector<double>& value) const {
return parametrisation_.get(name, value) || FieldParametrisation::get(name, value);
}


bool ArtificialInput::get(const std::string& name, std::vector<std::string>& value) const {
return parametrisation_.get(name, value) || FieldParametrisation::get(name, value);
void ArtificialInput::longitudes(std::vector<double>& values) const {
values = longitudes_;
}


Expand Down Expand Up @@ -175,8 +173,6 @@ ArtificialInput* ArtificialInputFactory::build(const std::string& name, const pa
util::call_once(once, init);
util::lock_guard<util::recursive_mutex> lock(*local_mutex);

// Log::debug() << "ArtificialInputFactory: looking for '" << name << "'" << std::endl;

auto j = m->find(name);
if (j == m->end()) {
list(Log::error() << "ArtificialInputFactory: unknown '" << name << "', choices are: ");
Expand Down
27 changes: 12 additions & 15 deletions src/mir/input/ArtificialInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#pragma once

#include <memory>

#include "mir/input/MIRInput.h"
#include "mir/param/FieldParametrisation.h"
#include "mir/param/SimpleParametrisation.h"
Expand Down Expand Up @@ -54,7 +56,7 @@ class ArtificialInput : public MIRInput, protected param::FieldParametrisation {
protected:
// -- Constructors

ArtificialInput() : calls_(0) {}
ArtificialInput();

// -- Members
// None
Expand All @@ -74,20 +76,7 @@ class ArtificialInput : public MIRInput, protected param::FieldParametrisation {
bool sameAs(const MIRInput&) const override;

// From FieldParametrisation
bool has(const std::string& name) const override;

bool get(const std::string& name, std::string& value) const override;
bool get(const std::string& name, bool& value) const override;
bool get(const std::string& name, int& value) const override;
bool get(const std::string& name, long& value) const override;
bool get(const std::string& name, float& value) const override;
bool get(const std::string& name, double& value) const override;

bool get(const std::string& name, std::vector<int>& value) const override;
bool get(const std::string& name, std::vector<long>& value) const override;
bool get(const std::string& name, std::vector<float>& value) const override;
bool get(const std::string& name, std::vector<double>& value) const override;
bool get(const std::string& name, std::vector<std::string>& value) const override;

// -- Class members
// None
Expand All @@ -99,13 +88,21 @@ class ArtificialInput : public MIRInput, protected param::FieldParametrisation {
// -- Members

param::SimpleParametrisation parametrisation_;
std::unique_ptr<param::MIRParametrisation> inputParametrisation_;
size_t calls_;

// For unstructured grids
std::vector<double> latitudes_;
std::vector<double> longitudes_;

// -- Methods
// None

// -- Overridden methods
// None

// From FieldParametrisation
void latitudes(std::vector<double>&) const override;
void longitudes(std::vector<double>&) const override;

// -- Class members
// None
Expand Down
57 changes: 19 additions & 38 deletions src/mir/input/GribInput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
#include "eckit/config/Resource.h"
#include "eckit/io/Buffer.h"
#include "eckit/io/MemoryHandle.h"
#include "eckit/io/StdFile.h"
#include "eckit/serialisation/HandleStream.h"
#include "eckit/types/FloatCompare.h"
#include "eckit/types/Fraction.h"

#include "mir/config/LibMir.h"
#include "mir/data/MIRField.h"
#include "mir/grib/Config.h"
#include "mir/input/GriddefInput.h"
#include "mir/param/DefaultParametrisation.h"
#include "mir/repres/Representation.h"
#include "mir/util/Exceptions.h"
#include "mir/util/Grib.h"
Expand Down Expand Up @@ -1141,52 +1142,32 @@ bool GribInput::handle(grib_handle* h) {
}


void GribInput::auxilaryValues(const std::string& path, std::vector<double>& values) const {
void GribInput::setAuxiliaryInformation(const util::ValueMap& map) {
util::lock_guard<util::recursive_mutex> lock(mutex_);

eckit::AutoStdFile f(path);

int e;
grib_handle* h = nullptr;

// We cannot use GribFileInput to read these files, because lat/lon files are also
// has grid_type = triangular_grid, and we will create a loop

try {
h = codes_grib_handle_new_from_file(nullptr, f, &e);
grib_call(e, path.c_str());
size_t count;
GRIB_CALL(codes_get_size(h, "values", &count));

size_t size = count;
values.resize(count);
GRIB_CALL(codes_get_double_array(h, "values", values.data(), &size));
ASSERT(count == size);
auto load = [](const eckit::PathName& path, std::vector<double>& values) {
Log::info() << "GribInput::setAuxiliaryInformation: '" << path << "'" << std::endl;

long missingValuesPresent;
GRIB_CALL(codes_get_long(h, "missingValuesPresent", &missingValuesPresent));
ASSERT(!missingValuesPresent);

codes_handle_delete(h);
}
catch (...) {
codes_handle_delete(h);
throw;
}
}
static const param::DefaultParametrisation param;
std::unique_ptr<input::MIRInput> input(input::MIRInputFactory::build(path.asString(), param));
ASSERT(input->next());

auto field = input->field();
ASSERT(field.dimensions() == 1);

void GribInput::setAuxiliaryInformation(const util::ValueMap& map) {
util::lock_guard<util::recursive_mutex> lock(mutex_);
values = field.values(0);
};

for (const auto& kv : map) {
if (kv.first == "latitudes") {
Log::debug() << "Loading auxilary file '" << kv.second << "'" << std::endl;
auxilaryValues(kv.second, latitudes_);
if (kv.first == "griddef") {
GriddefInput::load(kv.second, latitudes_, longitudes_);
ASSERT(latitudes_.size() == longitudes_.size());
}
else if (kv.first == "latitudes") {
load(kv.second, latitudes_);
}
else if (kv.first == "longitudes") {
Log::debug() << "Loading auxilary file '" << kv.second << "'" << std::endl;
auxilaryValues(kv.second, longitudes_);
load(kv.second, longitudes_);
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/mir/input/GribInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ class GribInput : public MIRInput, public param::FieldParametrisation {
std::vector<double> latitudes_;
std::vector<double> longitudes_;

// -- Methods

void auxilaryValues(const std::string&, std::vector<double>& values) const;

// -- Overridden methods

// From MIRInput
Expand Down
Loading

0 comments on commit 0379d79

Please sign in to comment.