31#ifndef DRAIN_SAMPLER_H_
32#define DRAIN_SAMPLER_H_
40#include "drain/util/BeanLike.h"
41#include "drain/util/IosFormat.h"
42#include "drain/util/Range.h"
43#include "drain/util/ReferenceMap.h"
44#include "drain/util/StringMapper.h"
45#include "drain/image/Image.h"
66 infoMap[
"width"].link(width = 0);
67 infoMap[
"height"].link(height = 0);
73 void setSize(
int w,
int h){
170 parameters.
copyStruct(sampler.getParameters(), sampler, *
this);
190 std::string voidComment =
"void";
193 std::string handleVoid =
"null";
204 std::string
getFormat(
const std::string & formatStr)
const;
219 template <
class D,
class P>
220 void sample(
const std::map<std::string, D> & images,
const P & picker,
const std::string & formatStr, std::ostream & ostr = std::cout)
const {
225 mout.
error(
"no image data (channels) provided: " );
229 const bool SKIP_VOID = (handleVoid ==
"skip") || (
skipVoid ==
true);
231 const bool MARK_VOID = (handleVoid !=
"skip");
233 double voidMarker = NAN;
234 if (handleVoid !=
"null"){
248 std::string format =
getFormat(formatStr);
249 formatter.
parse(format,
true);
250 formatter.iosFormat.copyFrom(ostr);
251 mout.
special(
"iosFormat: ", formatter.iosFormat);
257 std::map<std::string, double> values;
258 static const std::string minusStr(
"-");
259 for (
typename std::map<std::string, D>::const_iterator it = images.begin(); it != images.end(); ++it){
260 const std::string & key = it->first;
261 mout.
debug2(
"referencing: " , key ,
',' , minusStr , key );
270 const int iStep = this->iStep;
271 const int jStep = (this->jStep > 0) ? this->jStep : iStep;
281 ostr << commentChar << commentChar <<
" input properties " <<
'\n';
283 for (
const auto & entry: picker.infoMap){
284 ostr << commentChar <<
' ' << entry.first <<
'=';
290 ostr << commentChar << commentChar <<
" sampling parameters " <<
'\n';
292 for (
const auto & entry: parameters){
293 ostr << commentChar <<
' ' << entry.first <<
'=';
298 if (!formatStr.empty())
299 ostr << commentChar <<
" format='" << formatStr <<
"'\n";
301 ostr << commentChar <<
" format='" << format <<
"'\n";
303 ostr << commentChar << commentChar <<
" resulting geometry " <<
'\n';
304 const int iN = picker.width/iStep;
305 const int jN = picker.height/jStep;
306 ostr << commentChar <<
" rows=" << iN <<
"\n";
307 ostr << commentChar <<
" rols=" << jN <<
"\n";
308 ostr << commentChar <<
" samples=" << (iN*jN) <<
"\n";
314 int iStart = this->iRange.min;
318 int jStart = this->jRange.min;
322 int iEnd = this->iRange.max;
324 iEnd = picker.width-1;
326 int jEnd = this->jRange.max;
328 jEnd = picker.height-1;
341 for (
int j = jStart; j<=jEnd; j+=jStep){
342 for (
int i = iStart; i<=iEnd; i+=iStep){
344 picker.setPosition(i, j);
348 for (
typename std::map<std::string, D>::const_iterator it = images.begin(); it != images.end(); ++it){
349 const std::string & quantity = it->first;
350 const D & data = it->second;
362 if (picker.getValue(data, x)){
364 values[quantity+
"_raw"] = x;
367 if (!picker.handleValue(data, x)){
377 values[quantity] = x;
378 values[minusStr+quantity] = -x;
381 if (dataOk || !SKIP_VOID){
385 if ((!dataOk) && (commentChar))
386 ostr <<
' ' << commentChar << voidComment;
Something which has a name, a description and possibly some parameters of varying type.
Definition BeanLike.h:58
virtual const std::string & getName() const
Return the name of an instance.
Definition BeanLike.h:80
A map of FlexVariable:s.
Definition VariableMap.h:138
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition Log.h:431
Logger & debug(const TT &... args)
Debug information.
Definition Log.h:667
Logger & special(const TT &... args)
Other useful information.
Definition Log.h:532
Logger & attention(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition Log.h:477
Logger & error(const TT &... args)
Echoes.
Definition Log.h:417
Logger & debug2(const TT &... args)
Debug information.
Definition Log.h:677
A map of references to base type scalars, arrays or std::string; changing values in either are equiva...
Definition ReferenceMap.h:69
ref_t & link(const std::string &key, F &x)
Associates a map entry with a variable.
Definition ReferenceMap.h:84
void copyStruct(const ReferenceMap &m, const T &src, T &dst, extLinkPolicy policy=RESERVE)
Experimental. Copies references and values of a structure to another.
Definition ReferenceMap.h:415
static std::ostream & toStream(std::ostream &ostr, const std::initializer_list< T > &x, const SprinterLayout &layout=defaultLayout)
New (experimental)
Definition Sprinter.h:423
static const SprinterLayout jsonLayout
Resembles JSON structure: {"a":1,"b":22,"c":3}.
Definition Sprinter.h:224
Definition StringMapper.h:114
StringMapper & parse(const std::string &s, bool convertEscaped=false)
Converts a std::string containing variables like in "Hello, ${NAME}!" to a list of StringLet's.
Definition StringMapper.cpp:68
std::ostream & toStream(std::ostream &ostr) const
Output a concatenated chain of stringlets: literals as such and variables surrounded with "${" and "}...
Definition StringMapper.h:204
Image with static geometry.
Definition ImageFrame.h:62
double getScaled(size_t i, size_t j) const
Get intensity in original physical scale.
Definition ImageFrame.h:282
Reads image channels, returning scaled (physical) values.
Definition Sampler.h:120
bool handleValue(const ImageFrame &data, double &value) const
Definition Sampler.h:150
bool getValue(const ImageFrame &image, double &value) const
Definition Sampler.h:137
Utility for sampling images (2D data), outputting formatted text data.
Definition Sampler.h:162
char getCommentChar() const
Returns character, also supporting numeric ASCII values between 32 and 128.
Definition Sampler.cpp:58
std::string commentPrefix
Escape std::string for prefixing text no to be handled as data values.
Definition Sampler.h:183
ReferenceMap2 variableMap
Interface that links coordinates and image data.
Definition Sampler.h:198
std::string getFormat(const std::string &formatStr) const
Use given format or generate default "${var},${var2}, ...".
Definition Sampler.cpp:90
bool skipVoid
Skip lines, if contain missing values.
Definition Sampler.h:188
void sample(const std::map< std::string, D > &images, const P &picker, const std::string &formatStr, std::ostream &ostr=std::cout) const
Main function.
Definition Sampler.h:220
Definition DataSelector.cpp:1277
Interprets data values for Sampler.
Definition Sampler.h:58
int current_j2
Vertical inversed coordinate.
Definition Sampler.h:102
ReferenceMap2 & variableMap
Definition Sampler.h:112
int current_i
Optional utility. Called prior to writing the actual data to output stream.
Definition Sampler.h:96
SamplePicker(ReferenceMap2<> &variableMap)
Definition Sampler.h:65
FlexVariableMap infoMap
Information to be should in output file header.
Definition Sampler.h:108
int current_j
Vertical coordinate.
Definition Sampler.h:99
virtual void setPosition(int i, int j) const
Sets current position in image coordinates. To be redefined to compute also geographical projections,...
Definition Sampler.h:81