Skip to content

Commit

Permalink
scaling working metadata mapping missing
Browse files Browse the repository at this point in the history
  • Loading branch information
stefaniereuter committed Nov 14, 2024
1 parent 425e024 commit 7b0b96a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 64 deletions.
File renamed without changes.
42 changes: 19 additions & 23 deletions src/multio/action/scale/Scale.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@

#include "multio/action/scale/Scale.h"

#include "eckit/config/Configuration.h"
#include "eckit/exception/Exceptions.h"
#include "eckit/log/Log.h"


#include "multio/LibMultio.h"
#include "multio/util/PrecisionTag.h"
#include "multio/message/Message.h"
#include "multio/config/ComponentConfiguration.h"



namespace multio::action {

Scale::Scale(const ComponentConfiguration& compConf) :
ChainedAction(compConf),
cfg_{compConf},
scalingFactor_{compConf.parsedConfig().getDouble("scaling-Factor"),1.0} {}
scalingFactor_{compConf.parsedConfig().getDouble("scaling-Factor",1.0)} {}

void Scale::executeImpl(message::Message msg) {
if (msg.tag() != message::Message::Tag::Field) {
Expand All @@ -43,34 +43,30 @@ template <typename Precision>
message::Message Scale::ScaleMessage(message::Message&& msg) const {
LOG_DEBUG_LIB(LibMultio) << "Scale :: Metadata of the input message :: " <<std::endl
<<msg.metadata() <<std::endl;
eckit::Buffer& buffer = msg.payload();

size_t size = msg.payload().size()/sizeof(Precision);

auto data = static_cast<Precision*>(msg.payload().modifyData());

Precision* data = reinterpret_cast<Precision *>(buffer.data());
const size_t size = msg.payload().size()/sizeof(Precision);
cfg_.haveMissingValue() ? computeWithMissing(data, size, scalingFactor_) : computeWithoutMissing(data, size, scalingFactor_);

scaling(data, size, scalingFactor_);
//TODO LOOK WHAT IS NEEDED FOR METADATA then do message::Metadata md = msg.metadata(); modify md in return pass md instead of msg.metatdata()
return {message::Message::Header{msg.tag(), msg.source(), msg.destination(), std::move(msg.metadata())},
std::move(buffer)};
std::move(msg.payload())};
}

template <typename Precision>
void Scale::scaling(Precision* data, std::size_t size, double scalingFactor) const {
// Apply the scaling in place to each element
std::transform(data, data + size, data, [scalingFactor](Precision value) {
return static_cast<Precision>(value * scalingFactor);
});
}

void Scale::print(std::ostream & os) const {
os << "Scale (Scaling factor = " << scalingFactor_ << " )" ;
}
static ActionBuilder<Scale> ScaleBuilder("scale");

private:
template <typename T>
void scaleWithMissing(T* data, std::size_t size, double scalingFactor) {
const double m = cfg_.missingValue();
std::transform(data, data + size, data, [scalingFactor, m](T value) {return static_cast<T>(m==value ? m : value * scalingFactor);});
}

template <typename T>
void scaleWithoutMissing(T* data, std::size_t size, double scalingFactor) {
// Apply the scaling in place to each element
std::transform(data, data + size, data, [scalingFactor](T value) {
return static_cast<T>(value * scalingFactor);
});
}
static ActionBuilder<Scale> ScaleBuilder("scale");

}
41 changes: 10 additions & 31 deletions src/multio/action/scale/Scale.h
Original file line number Diff line number Diff line change
@@ -1,47 +1,26 @@
/*
* (C) Copyright 1996- 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 "multio/action/ChainedAction.h"
#include "multio/config/ComponentConfiguration.h" // Ensure this include is present

namespace multio::action {

namespace multio::action::scale {


/**
* \class MultIO Action to scale fields
*/
class Scale final : public ChainedAction {
public:
using ChainedAction::ChainedAction;
explicit Scale(const ComponentConfiguration& compConf); // Constructor declaration
void executeImpl(message::Message) override;

private:
template <typename T>
message::Message ScaleMessage(message::Message&&) const;
// Template function to scale messages based on the Precision type
template <typename T>
message::Message ScaleMessage(message::Message&& msg) const;

// Methods to scale data with and without missing values
template <typename T>
void scaleWithMissing(T* data, std::size_t size, double scalingFactor);
const double scalingFactor_;

template <typename Precision>
message::Message ScaleMessage(message::Message&& msg) const;

template <typename T>
void scaleWithoutMissing(T* data, std::size_t size, double scalingFactor);
template <typename Precision>
void scaling(Precision* data, std::size_t size, double scalingFactor) const;

void print(std::ostream&) const override;
void executeImpl(message::Message) override;
};


} // namespace multio::action::scale
10 changes: 0 additions & 10 deletions src/multio/config/PlanConfiguration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,6 @@ eckit::LocalConfiguration generate_interpolate_action(const metkit::mars::MarsPa
return interpolate_action;
};

eckit::LocalConfiguration generate_scale_action(const metkit::mars::MarsParsedRequest& request,
const eckit::LocalConfiguration& componentConfig,
const std::string& templatesPath) {
eckit::LocalConfiguration scale_action;
scale_action.set("type", "scale");
scale_action.set("scale-factor", 1.0);

return scale_action;
};

eckit::LocalConfiguration generate_encode_action(const metkit::mars::MarsParsedRequest& request,
const eckit::LocalConfiguration& componentConfig,
const std::string& templatesPath) {
Expand Down

0 comments on commit 7b0b96a

Please sign in to comment.