Loading...
Searching...
No Matches
RadarWindows.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31#ifndef RACK_RADAR_WINDOWS_H
32#define RACK_RADAR_WINDOWS_H
33
34#include <math.h>
35
36#include <drain/Log.h>
37#include <drain/TypeUtils.h>
38#include <drain/image/Window.h>
39#include <drain/image/SlidingWindow.h>
40//#include <drain/image/SequentialImageOp.h>
41
42#include "data/PolarODIM.h"
43
44using namespace drain::image;
45
46// file RadarFunctorOp?
47
48namespace rack {
49
50
52
56class RadarWindowGeom : public drain::UniTuple<double,2> {
57
58public:
59
60 RadarWindowGeom() : widthM(this->next()), heightD(this->next()) {
61 }
62
63 RadarWindowGeom(const RadarWindowGeom & geom) : widthM(next()), heightD(next()) {
64 assignSequence(geom.tuple());
65 }
66
67 // Beam-directional window width in metres
68 double & widthM; // = 1500.0;
69
70 // Azimuthal window height in degrees
71 double & heightD; // = 3.0;
72
73};
74
76
77public:
78
80 double contributionThreshold = 0.5; //
81
83 bool invertPolar = false;
84
86 bool relativeScale = false; //
87
93 RadarWindowConfig(int widthM=1500, double heightD=3.0, double contributionThreshold = 0.5, bool invertPolar=false, bool relativeScale=false) :
94 drain::image::WindowConfig(1, 1), // drain::image::WindowConfig(width, height),
95 // widthM(widthM), heightD(heightD),
97 this->widthM = widthM;
98 this->heightD = heightD;
99 }
100
102 RadarWindowGeom(conf),
103 drain::image::WindowConfig(conf),
104 //widthM(conf.widthM),
105 //heightD(conf.heightD),
109
110 }
111
120 //RadarWindowConfig(drain::UnaryFunctor & ftor, int width=3, int height=3, double contributionThreshold = 0.5) :
121 template <class FT>
122 RadarWindowConfig(const FT & ftor, int widthM=1500, double heightD=3.0,
123 double contributionThreshold = 0.5, bool invertPolar=false, bool relativeScale=false) :
124 drain::image::WindowConfig(0, 0, ftor), //drain::image::WindowConfig(ftor, width, height),
125 //widthM(widthM), heightD(heightD),
127 // invertPolar(false), contributionThreshold(contributionThreshold) {
128 this->widthM = widthM;
129 this->heightD = heightD;
130 }
131
132 // Perhaphs deprecating.
134 void setPixelConf(RadarWindowConfig & conf, const PolarODIM & inputODIM) const;
135
136 // NEW, "Inverted" setPixelConf
138 void adjustMyConf(const RadarWindowConfig & conf, const PolarODIM & inputODIM);
139
140
141 void updatePixelSize(const PolarODIM & inputODIM);
142
143};
144
145
146
148
149
150public:
151
153
156 }
157
158 PolarODIM odimSrc;
159
162 mutable double NI;
163
164};
165
166
168
174template <class C, class R=RadarWindowCore>
176public:
177
178 SlidingRadarWindowBase(int width=0, int height=0) : drain::image::SlidingWindow<C,R>(width,height), rangeNorm(1), rangeNormEnd(2), countMin(0) {
179 this->resetAtEdges = true;
180 };
181
182 SlidingRadarWindowBase(const C & conf) : drain::image::SlidingWindow<C,R>(conf), rangeNorm(1), rangeNormEnd(2), countMin(0) {
183 this->resetAtEdges = conf.invertPolar;
184 //this->resetAtEdges = true; //conf.invertPolar;
185 };
186
187 virtual
189
191 // Redefines Window<C,R>::setSrcFrame(ImageFrame)
196
197 drain::Logger mout("SlidingRadarWindow", __FUNCTION__);
198 //mout.debug("src Scaling0: " , src.getScaling() );
199 mout.debug2("src props for odim: " , src.getProperties() );
200
201 this->odimSrc.updateFromMap(src.getProperties());
202 mout.info("NI=" , this->odimSrc.getNyquist(LOG_WARNING) );
203 mout.info("copied odim: " , EncodingODIM(this->odimSrc) );
204
206 // direct should be:
207 // R::setSrcFrame(src);
208 mout.debug2("src Scaling: " , src.getScaling() );
209 }
210
211 /*
212 void setDst(drain::image::ImageFrame & dst){
213 drain::Logger mout("SlidingRadarWindow", __FUNCTION__);
214 //this->odimSrc.updateFromMap(src.getProperties());
215 //mout.debug("copied odim: " , this->odimSrc );
216 SlidingWindow<C, RadarWindowCore>::setDst(dst);
217 }
218 */
219
220protected:
221
222 virtual
223 void initialize() override {
225 setRangeNorm(); // interplay setLoopLimits(), with reset() and
226 //if (drain::Type::call<drain::typeIsSmallInt>(this->src.getType()) && drain::Type::call<drain::drain::typeIsSmallInt>(this->dst.getType())){
227 if (drain::Type::call<drain::typeIsSmallInt>(this->dst.getType())){
228 drain::Logger mout("SlidingRadarWindow", __FUNCTION__);
229 //this->conf.ftor.setScale(1.0);
230 mout.info("(not implemented: functor scaling for small int dst)" ); // << this->odimSrc
231 }
232 // what about reset(); ?
233 reset();
234 };
235
236
237 inline
238 void setImageLimits() const {
239 this->coordinateHandler.set(this->src.getGeometry(), this->src.getCoordinatePolicy());
240 // this->src.adjustCoordinateHandler(this->coordinateHandler);
241 }
242
245
246 drain::Logger mout("SlidingRadarWindow", __FUNCTION__);
247
248 if (this->odimSrc.area.height == 0)
249 mout.error("src odim.area.height==0" );
250
252 const double r = static_cast<double>(this->odimSrc.area.height) / (2.0*M_PI);
253 const int max = static_cast<int>(this->odimSrc.area.width);
254
255 rangeNorm = static_cast<int>(r);
257 rangeNormEnd = static_cast<int>(r * static_cast<double>(this->conf.frame.height));
258 if ((rangeNorm <= 0) || (rangeNormEnd >= max)){
259 mout.note(rangeNorm , '-' , rangeNormEnd );
260 }
261 //
262 }
263
264 int rangeNorm;
265 int rangeNormEnd;
266 int countMin; // raise
267
268
270 virtual inline
271 bool reset(){
272
273 if (this->location.x <= rangeNorm){
274 this->setLoopLimits(this->conf.frame.width, this->conf.frame.height);
275 }
276 else if (this->location.x < rangeNormEnd){ // from 'height' down to 1
277 this->setLoopLimits(this->conf.frame.width, (rangeNorm * this->conf.frame.height)/(this->location.x+1) );
278 //std::cerr << "loop limits " << this->jMax << std::endl;
279 }
280 else {
281 this->setLoopLimits(this->conf.frame.width, 1);
282 }
283 //drain::Logger mout("SlidingRadarWindow", __FUNCTION__);
284 //mout.warn(this->iMax , ',' , this->jMax , '\t' , this->getSamplingArea( ));
285 countMin = this->conf.contributionThreshold * this->getSamplingArea();
286
288 };
289
290};
291
292
293template <class C, class R=RadarWindowCore>
294class SlidingRadarWindow : public SlidingRadarWindowBase<C,R> { // drain::image::WeightedWindowCore
295public:
296
297 inline
298 SlidingRadarWindow(int width=0, int height=0) : SlidingRadarWindowBase<C,R>(width, height) {
299 };
300
301 inline
302 SlidingRadarWindow(const C & conf) : SlidingRadarWindowBase<C,R>(conf) {
303 };
304
305
306 virtual inline
308 if (this->coordinateHandler.validate(p)){
309 double x = this->src.template get<double>(p);
310 if ((x != this->odimSrc.nodata) && (x != this->odimSrc.undetect))
311 removeTrailingValue(this->odimSrc.scaleForward(x));
312 }
313 };
314
315 virtual inline
317 if (this->coordinateHandler.validate(p)){
318 double x = this->src.template get<double>(p);
319 if ((x != this->odimSrc.nodata) && (x != this->odimSrc.undetect))
320 addLeadingValue(this->odimSrc.scaleForward(x));
321 }
322 };
323
325 virtual inline
326 void removeTrailingValue(double x){}; // = 0;
327
329 virtual inline
330 void addLeadingValue(double x){}; // = 0;
331
332
333
334};
335
340template <class C>
342public:
343
344 //drain::UnaryFunctor & myFunctor;
345
346 RadarWindowAvg(int width=0, int height=0) :
347 SlidingRadarWindow<C>(width,height),
348 //myFunctor(this->conf.getFunctor()),
349 sum(0.0),
350 count(0) {
351 };
352
353 RadarWindowAvg(const RadarWindowAvg & window) :
354 SlidingRadarWindow<C>(window),
355 //myFunctor(this->conf.getFunctor(window.conf.getFunctorName())), // kludge
356 sum(0.0),
357 count(0) {
358 };
359
360 RadarWindowAvg(const C & conf) :
362 // myFunctor(this->conf.getFunctor(conf.getFunctorName())),
363 sum(0.0),
364 count(0) {
365 };
366
367
368
369
370 virtual
371 inline
372 ~RadarWindowAvg(){};
373
375
376protected:
377
378 double sum;
379 int count;
380
381 virtual
382 inline
383 void clear(){
384 sum = 0.0;
385 count = 0;
386 };
387
388 virtual inline
389 void removeTrailingValue(double x){
390 sum -= x;
391 --count;
392 };
393
394 virtual inline
395 void addLeadingValue(double x){
396 sum += x;
397 ++count;
398 };
399
400 virtual inline
401 void write(){
402 if (count > 0){
403 //if (this->location.x == this->location.y)
404 // std::cerr << sum << '\t' << count << '\n';
405 this->dst.putScaled(this->location.x, this->location.y, this->myFunctor(sum/static_cast<double>(count)));
406 }
407 else
408 this->dst.put(this->location, this->odimSrc.undetect); // ?
409 };
410
411
412};
413
418template <class C>
420public:
421
422
423 // Consider metres & degrees?
424 RadarWindowWeightedAvg(int width=0, int height=0) : SlidingRadarWindow<C>(width,height) {
425 };
426
428 };
429
430 RadarWindowWeightedAvg(const C & conf) : SlidingRadarWindow<C>(conf) {
431 };
432
433 virtual inline
435
436protected:
437
438 int count = 0;
439
440 // Copied from SlidingStripeAverage
441 typedef float sum_t;
442 sum_t w = 0.0;
443 sum_t sum = 0.0;
444 sum_t sumW = 0.0;
445
446 virtual inline
447 void clear() final {
448 this->sum = 0.0;
449 this->sumW = 0.0;
450 this->count = 0;
451 };
452
453
454 virtual inline
456 if (this->coordinateHandler.validate(p)){
457 double x = this->src.template get<double>(p);
458 if (this->odimSrc.isValue(x)){
459 this->w = this->srcWeight.template get<sum_t>(p);
460 this->sum += w*this->odimSrc.scaling.fwd(x);
461 this->sumW += w;
462 ++this->count;
463 }
464 }
465 };
466
467 virtual inline
469 if (this->coordinateHandler.validate(p)){
470 double x = this->src.template get<double>(p);
471 if (this->odimSrc.isValue(x)){
472 this->w = this->srcWeight.template get<sum_t>(p);
473 this->sum -= w*this->odimSrc.scaling.fwd(x);
474 this->sumW -= w;
475 --this->count;
476 }
477 }
478 };
479
480 virtual inline
481 void write() final {
482 if (sumW > 0.0){ // then also count>0
483 this->dst.put(this->location, this->odimSrc.scaling.inv(this->sum/sumW));
484 //this->dstWeight.put(this->location, this->scalingW.inv(sumW/static_cast<sum_t>(this->count)));
485 this->dstWeight.put(this->location, sumW/static_cast<sum_t>(this->count)); // "uses" original scaling
486 }
487 else {
488 this->dst.put(this->location, this->odimSrc.undetect); // change
489 this->dstWeight.put(this->location, 0);
490 }
491
492 }
493
494 /*
495 virtual inline
496 void removeTrailingValue(double x) final {
497 sum -= x;
498 --count;
499 };
500
501 virtual inline
502 void addLeadingValue(double x) final {
503 sum += x;
504 ++count;
505 };
506
507 virtual inline
508 void write(){
509 if (count > 0){
510 //if (this->location.x == this->location.y)
511 // std::cerr << sum << '\t' << count << '\n';
512 this->dst.putScaled(this->location.x, this->location.y, this->myFunctor(sum/static_cast<double>(count)));
513 }
514 else
515 this->dst.put(this->location, this->odimSrc.undetect); // ?
516 };
517 */
518
519
520};
521
522
526template <class C>
528public:
529
530 RadarWindowSoftMax(int width=0, int height=0, double coeff=1.0) : SlidingRadarWindow<C>(width,height), coeff(1.0), coeffInv(1.0/coeff), sum(0.0), count(0) {};
531
532
533 virtual inline
535
536 void setCoeff(double c){
537 if (c==0.0)
538 throw std::runtime_error("RadarWindowSoftMax: zero coeff");
539 coeff = c;
540 coeffInv = 1.0/c;
541 };
542
543protected:
544
545 double coeff;
546 double coeffInv;
547
548 double sum;
549 int count;
550
551 virtual inline
552 void clear(){
553 sum = 0.0;
554 count = 0;
555 };
556
557 virtual inline
558 void removeTrailingValue(double x){
559 sum -= ::exp(coeff * x);
560 --count;
561 };
562
563 virtual inline
564 void addLeadingValue(double x){
565 sum += ::exp(coeff * x);
566 ++count;
567 //if ((p.x == p.y) && (x > -15.0))
568 // std::cerr << "handleLeadingPixel" << p << ':' << x << '\t' << count << ':' << sum << std::endl;
569 };
570
571 virtual inline
572 void write(){
573 if (count > 0)
574 this->dst.put(this->location, this->fuzzifier(coeffInv*::log(sum/static_cast<double>(count))));
575 //this->dst.put(this->location, this->functor(sum/static_cast<double>(count)));
576 };
577
578
579};
580
582
587template <class F>
589public:
590
591 RadarWindowStdDev(int width=0, int height=0) : SlidingRadarWindow<F>(width,height), sum(0.0), sum2(0.0), count(0) {};
592
593
594 virtual inline
596
597protected:
598
599 double sum;
600 double sum2;
601 int count;
602
603 virtual inline
604 void clear(){
605 sum = 0.0;
606 sum2 = 0.0;
607 count = 0;
608 };
609
610 virtual
611 inline
612 void removeTrailingValue(double x){
613 sum -= x;
614 sum2 -= x*x;
615 --count;
616 };
617
618 virtual
619 inline
620 void addLeadingValue(double x){
621 sum += x;
622 sum2 += x*x;
623 ++count;
624 //if ((this->p.x == this->p.y) && (x > -15.0))
625 // std::cerr << "handleLeadingPixel" << this->p << ':' << x << '\t' << count << ':' << sum << std::endl;
626 };
627
628 virtual
629 inline
630 void write(){
631
632 if (count > 0){
633 double countD = static_cast<double>(count);
634 this->dst.put(this->location, this->fuzzifier( ::sqrt(sum2/countD - sum*sum/(countD*countD)) ));
635 /*
636 countD = sum2/countD - sum*sum/(countD*countD);
637 if ((this->p.x == this->p.y) && (count > 5))
638 std::cerr << __FUNCTION__ << this->p << ':' << countD << std::endl;
639 */
640 }
641 else
642 this->dst.put(this->location, 0);
643
644 };
645
646
647};
648
649
650
651
652
653
654
655} // rack::
656
657
658
659
660#endif
661
662// Rack
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Logger & note(const TT &... args)
For top-level information.
Definition Log.h:490
Logger & error(const TT &... args)
Echoes.
Definition Log.h:417
Logger & debug2(const TT &... args)
Debug information.
Definition Log.h:677
tuplebase_t & assignSequence(T &sequence, bool LENIENT=false)
Proposed for tuples only; derived classes should not shadow this.
Definition TupleBase.h:287
Tuple of N elements of type T.
Definition UniTuple.h:65
bool validate(Point2D< int > &p) const
Handles the coordinate, returning true if the position is reversible.
Definition CoordinateHandler.h:235
Image with static geometry.
Definition ImageFrame.h:62
void put(size_t i, T x)
Sets the intensity in location i to x. See \address.
Definition ImageFrame.h:187
void putScaled(size_t i, size_t j, double x)
Put intensity using original physical value.
Definition ImageFrame.h:236
const std::type_info & getType() const
Get the storage type.
Definition ImageLike.h:100
Window implementation that uses incremental update of state.
Definition SlidingWindow.h:50
virtual bool reset()
Returns false, if traversal should be ended.
Definition SlidingWindow.h:215
Definition Window.h:266
Base class for configurations applied in image processing windows, e.g. for operators of type WindowO...
Definition Window.h:55
bool resetAtEdges
To avoid accumulated numerical errors esp. with floats, reset the statistics at row/cols ends....
Definition Window.h:527
Point2D< int > location
Current location of this window.
Definition Window.h:521
size_t getSamplingArea()
Returns the area which has eventually been scaled (in a non-linear coordinate system)
Definition Window.h:490
void setLoopLimits()
Sets the actual traversal range inside the window. Sometimes applied dynamically by reset().
Definition Window.h:606
Structure for data storage type, scaling and marker codes. Does not contain quantity.
Definition EncodingODIM.h:70
Metadata structure for single-radar data (polar scans, volumes and products).
Definition PolarODIM.h:45
Definition RadarWindows.h:341
virtual void write()
Write the result in the target image.
Definition RadarWindows.h:401
virtual void removeTrailingValue(double x)
Handles the converted (natural-scaled) value.
Definition RadarWindows.h:389
virtual void addLeadingValue(double x)
Handles the converted (natural-scaled) value.
Definition RadarWindows.h:395
virtual void clear()
Clears the applied statistics. Redefined in derived classes.
Definition RadarWindows.h:383
Definition RadarWindows.h:75
RadarWindowConfig(int widthM=1500, double heightD=3.0, double contributionThreshold=0.5, bool invertPolar=false, bool relativeScale=false)
Definition RadarWindows.h:93
RadarWindowConfig(const FT &ftor, int widthM=1500, double heightD=3.0, double contributionThreshold=0.5, bool invertPolar=false, bool relativeScale=false)
Definition RadarWindows.h:122
void adjustMyConf(const RadarWindowConfig &conf, const PolarODIM &inputODIM)
Copies configuration from conf and update pixel frame (width x height).
Definition RadarWindows.cpp:54
void setPixelConf(RadarWindowConfig &conf, const PolarODIM &inputODIM) const
Copies configuration to conf and update its pixel frame (width x height).
Definition RadarWindows.cpp:37
bool invertPolar
Compensate the polar coordinate system to correspond the Cartesian one in computations.
Definition RadarWindows.h:83
bool relativeScale
If true, use speed up to -1.0...+1.0 instead of -Vnyq...+Vnyq.
Definition RadarWindows.h:86
double contributionThreshold
Minimum percentage of detected values in a window (not undetect and nodata)
Definition RadarWindows.h:80
Definition RadarWindows.h:147
double NI
Definition RadarWindows.h:162
RadarWindowCore()
Will act as base class: Window<RadarWindowCore> : public RadarWindowCore {...}, init currently not su...
Definition RadarWindows.h:155
Like pixel window, but width is metres and height is degrees.
Definition RadarWindows.h:56
Definition RadarWindows.h:527
virtual void write()
Write the result in the target image.
Definition RadarWindows.h:572
virtual void removeTrailingValue(double x)
Handles the converted (natural-scaled) value.
Definition RadarWindows.h:558
virtual void addLeadingValue(double x)
Handles the converted (natural-scaled) value.
Definition RadarWindows.h:564
virtual void clear()
Clears the applied statistics. Redefined in derived classes.
Definition RadarWindows.h:552
Sliding window for computing standard deviation of scalar quantities.
Definition RadarWindows.h:588
virtual void write()
Write the result in the target image.
Definition RadarWindows.h:630
virtual void removeTrailingValue(double x)
Handles the converted (natural-scaled) value.
Definition RadarWindows.h:612
virtual void addLeadingValue(double x)
Handles the converted (natural-scaled) value.
Definition RadarWindows.h:620
virtual void clear()
Clears the applied statistics. Redefined in derived classes.
Definition RadarWindows.h:604
Definition RadarWindows.h:419
virtual void addPixel(drain::Point2D< int > &p) final
Adds a pixel to window statistics. Unvalidated location.
Definition RadarWindows.h:455
virtual void removePixel(drain::Point2D< int > &p) final
Removes a pixel from window statistics. Unvalidated location.
Definition RadarWindows.h:468
virtual void write() final
Write the result in the target image.
Definition RadarWindows.h:481
virtual void clear() final
Clears the applied statistics. Redefined in derived classes.
Definition RadarWindows.h:447
A two-dimensional image processing window that handles the special ODIM codes and scales the result....
Definition RadarWindows.h:175
void setImageLimits() const
Sets internal limits corresponding to image geometries. Typically using coordHandler.
Definition RadarWindows.h:238
void setSrcFrame(const drain::image::ImageFrame &src)
Sets input image and retrieves ODIM metadata from image Properties.
Definition RadarWindows.h:195
virtual void initialize() override
Sets class-specific initial values. Does not change general window state (e.g. location)....
Definition RadarWindows.h:223
virtual bool reset()
Returns false, if traversal should be ended.
Definition RadarWindows.h:271
void setRangeNorm()
To compensate polar geometry, set applicable range for pixel area scaling.
Definition RadarWindows.h:244
Definition RadarWindows.h:294
virtual void addPixel(drain::Point2D< int > &p)
Adds a pixel to window statistics. Unvalidated location.
Definition RadarWindows.h:316
virtual void removePixel(drain::Point2D< int > &p)
Removes a pixel from window statistics. Unvalidated location.
Definition RadarWindows.h:307
virtual void removeTrailingValue(double x)
Handles the converted (natural-scaled) value.
Definition RadarWindows.h:326
virtual void addLeadingValue(double x)
Handles the converted (natural-scaled) value.
Definition RadarWindows.h:330
Namespace for images and image processing tools.
Definition AccumulationArray.cpp:45
Definition DataSelector.cpp:1277
Definition DataSelector.cpp:44
Definition Point.h:48