31 #ifndef DRAIN_SAMPLER_H_
32 #define DRAIN_SAMPLER_H_
40 #include <drain/FlexibleVariable.h>
43 #include "drain/util/BeanLike.h"
44 #include "drain/util/IosFormat.h"
45 #include "drain/util/Range.h"
46 #include "drain/util/ReferenceMap.h"
47 #include "drain/util/StringMapper.h"
48 #include "drain/image/Image.h"
69 infoMap[
"width"].link(width = 0);
70 infoMap[
"height"].link(height = 0);
76 void setSize(
int w,
int h){
135 bool getValue(
const ImageFrame & image,
double & value)
const {
160 parameters.
copyStruct(sampler.getParameters(), sampler, *
this);
180 std::string voidComment =
"void";
183 std::string handleVoid =
"null";
194 std::string
getFormat(
const std::string & formatStr)
const;
209 template <
class D,
class P>
210 void sample(
const std::map<std::string, D> & images,
const P & picker,
const std::string & formatStr, std::ostream & ostr = std::cout)
const {
215 mout.
error(
"no image data (channels) provided: " );
219 const bool SKIP_VOID = (handleVoid ==
"skip") || (
skipVoid ==
true);
221 const bool MARK_VOID = (handleVoid !=
"skip");
223 double voidMarker = NAN;
224 if (handleVoid !=
"null"){
238 std::string format =
getFormat(formatStr);
239 formatter.
parse(format,
true);
240 formatter.iosFormat.copyFrom(ostr);
241 mout.
special(
"iosFormat: " , formatter.iosFormat );
247 std::map<std::string, double> values;
248 static const std::string minusStr(
"-");
249 for (
typename std::map<std::string, D>::const_iterator it = images.begin(); it != images.end(); ++it){
250 const std::string & key = it->first;
251 mout.
debug2(
"referencing: " , key ,
',' , minusStr , key );
259 const int iStep = this->iStep;
260 const int jStep = (this->jStep > 0) ? this->jStep : iStep;
270 ostr << commentChar << commentChar <<
" input properties " <<
'\n';
272 for (
const auto & entry: picker.infoMap){
273 ostr << commentChar <<
' ' << entry.first <<
'=';
279 ostr << commentChar << commentChar <<
" sampling parameters " <<
'\n';
281 for (
const auto & entry: parameters){
282 ostr << commentChar <<
' ' << entry.first <<
'=';
287 if (!formatStr.empty())
288 ostr << commentChar <<
" format='" << formatStr <<
"'\n";
290 ostr << commentChar <<
" format='" << format <<
"'\n";
292 ostr << commentChar << commentChar <<
" resulting geometry " <<
'\n';
293 const int iN = picker.width/iStep;
294 const int jN = picker.height/jStep;
295 ostr << commentChar <<
" rows=" << iN <<
"\n";
296 ostr << commentChar <<
" rols=" << jN <<
"\n";
297 ostr << commentChar <<
" samples=" << (iN*jN) <<
"\n";
303 int iStart = this->iRange.min;
307 int jStart = this->jRange.min;
311 int iEnd = this->iRange.max;
313 iEnd = picker.width-1;
315 int jEnd = this->jRange.max;
317 jEnd = picker.height-1;
324 for (
int j = jStart; j<=jEnd; j+=jStep){
325 for (
int i = iStart; i<=iEnd; i+=iStep){
327 picker.setPosition(i, j);
331 for (
typename std::map<std::string, D>::const_iterator it = images.begin(); it != images.end(); ++it){
332 const std::string & quantity = it->first;
333 const D & data = it->second;
334 if (!picker.getValue(data, x)){
342 values[quantity] = x;
343 values[minusStr+quantity] = -x;
346 if (dataOk || !SKIP_VOID){
350 if ((!dataOk) && (commentChar))
351 ostr <<
' ' << commentChar << voidComment;
Something which has a name, a description and possibly some parameters of varying type.
Definition: BeanLike.h:60
virtual const std::string & getName() const
Return the name of an instance.
Definition: BeanLike.h:69
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:308
Logger & error(const TT &... args)
Echoes.
Definition: Log.h:412
Logger & special(const TT &... args)
Other useful information.
Definition: Log.h:527
Logger & debug(const TT &... args)
Public, yet typically used "internally", when TIMING=true.
Definition: Log.h:676
Logger & debug2(const TT &... args)
Debug information.
Definition: Log.h:686
A map of references to base type scalars, arrays or std::string; changing values in either are equiva...
Definition: ReferenceMap.h:67
ref_t & link(const std::string &key, F &x)
Associates a map entry with a variable.
Definition: ReferenceMap.h:82
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:399
static std::ostream & toStream(std::ostream &ostr, const std::initializer_list< T > &x, const SprinterLayout &layout=defaultLayout)
New (experimental)
Definition: Sprinter.h:420
static const SprinterLayout jsonLayout
Resembles JSON structure: {"a":1,"b":22,"c":3}.
Definition: Sprinter.h:221
A tool for expanding variables embedded in a std::string to literals.
Definition: StringMapper.h:275
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:53
std::ostream & toStream(std::ostream &ostr) const
Output a concatenated chain of stringlets: literals as such and variables surrounded with "${" and "}...
Definition: StringMapper.h:364
Image with static geometry.
Definition: ImageFrame.h:67
double getScaled(size_t i, size_t j) const
Get intensity in original physical scale.
Definition: ImageFrame.h:287
Reads image channels, returning scaled (physical) values.
Definition: Sampler.h:123
Utility for sampling images (2D data), outputting formatted text data.
Definition: Sampler.h:152
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:173
ReferenceMap2 variableMap
Interface that links coordinates and image data.
Definition: Sampler.h:188
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:178
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:210
Definition: DataSelector.cpp:1277
Interprets data values for Sampler.
Definition: Sampler.h:61
int current_j2
Vertical inversed coordinate.
Definition: Sampler.h:105
ReferenceMap2 & variableMap
Definition: Sampler.h:115
int current_i
Optional utility. Called prior to writing the actual data to output stream.
Definition: Sampler.h:99
SamplePicker(ReferenceMap2<> &variableMap)
Definition: Sampler.h:68
FlexVariableMap infoMap
Information to be should in output file header.
Definition: Sampler.h:111
int current_j
Vertical coordinate.
Definition: Sampler.h:102
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:84