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) : drain::UniTuple<double,2>(), 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
92 RadarWindowConfig(int widthM=1500, double heightD=3.0, double contributionThreshold = 0.5, bool invertPolar=false, bool relativeScale=false) :
93 drain::image::WindowConfig(1, 1), // drain::image::WindowConfig(width, height),
94 // widthM(widthM), heightD(heightD),
96 this->widthM = widthM;
97 this->heightD = heightD;
98 }
99
101 RadarWindowGeom(conf),
102 drain::image::WindowConfig(conf),
103 //widthM(conf.widthM),
104 //heightD(conf.heightD),
108
109 }
110
118 //RadarWindowConfig(drain::UnaryFunctor & ftor, int width=3, int height=3, double contributionThreshold = 0.5) :
119 template <class FT>
120 RadarWindowConfig(const FT & ftor, int widthM=1500, double heightD=3.0,
121 double contributionThreshold = 0.5, bool invertPolar=false, bool relativeScale=false) :
122 drain::image::WindowConfig(0, 0, ftor), //drain::image::WindowConfig(ftor, width, height),
123 //widthM(widthM), heightD(heightD),
125 // invertPolar(false), contributionThreshold(contributionThreshold) {
126 this->widthM = widthM;
127 this->heightD = heightD;
128 }
129
130 // Perhaphs deprecating.
132 void setPixelConf(RadarWindowConfig & conf, const PolarODIM & inputODIM) const;
133
134 // NEW, "Inverted" setPixelConf
136 void adjustMyConf(const RadarWindowConfig & conf, const PolarODIM & inputODIM);
137
138
139 void updatePixelSize(const PolarODIM & inputODIM);
140
141};
142
143
144
146
147
148public:
149
151
154 }
155
156 PolarODIM odimSrc;
157
160 mutable double NI;
161
162};
163
164
166
172template <class C, class R=RadarWindowCore>
174public:
175
176 SlidingRadarWindowBase(int width=0, int height=0) : drain::image::SlidingWindow<C,R>(width,height), rangeNorm(1), rangeNormEnd(2), countMin(0) {
177 this->resetAtEdges = true;
178 };
179
180 SlidingRadarWindowBase(const C & conf) : drain::image::SlidingWindow<C,R>(conf), rangeNorm(1), rangeNormEnd(2), countMin(0) {
181 this->resetAtEdges = conf.invertPolar;
182 //this->resetAtEdges = true; //conf.invertPolar;
183 };
184
185 virtual
187
189 // Redefines Window<C,R>::setSrcFrame(ImageFrame)
194
195 drain::Logger mout("SlidingRadarWindow", __FUNCTION__);
196 //mout.debug("src Scaling0: " , src.getScaling() );
197 mout.debug2("src props for odim: " , src.getProperties() );
198
199 this->odimSrc.updateFromMap(src.getProperties());
200 mout.info("NI=" , this->odimSrc.getNyquist(LOG_WARNING) );
201 mout.info("copied odim: " , EncodingODIM(this->odimSrc) );
202
204 // direct should be:
205 // R::setSrcFrame(src);
206 mout.debug2("src Scaling: " , src.getScaling() );
207 }
208
209 /*
210 void setDst(drain::image::ImageFrame & dst){
211 drain::Logger mout("SlidingRadarWindow", __FUNCTION__);
212 //this->odimSrc.updateFromMap(src.getProperties());
213 //mout.debug("copied odim: " , this->odimSrc );
214 SlidingWindow<C, RadarWindowCore>::setDst(dst);
215 }
216 */
217
218protected:
219
220 virtual
221 void initialize() override {
223 setRangeNorm(); // interplay setLoopLimits(), with reset() and
224 //if (drain::Type::call<drain::typeIsSmallInt>(this->src.getType()) && drain::Type::call<drain::drain::typeIsSmallInt>(this->dst.getType())){
225 if (drain::Type::call<drain::typeIsSmallInt>(this->dst.getType())){
226 drain::Logger mout("SlidingRadarWindow", __FUNCTION__);
227 //this->conf.ftor.setScale(1.0);
228 mout.info("(not implemented: functor scaling for small int dst)" ); // << this->odimSrc
229 }
230 // what about reset(); ?
231 reset();
232 };
233
234
235 inline
236 void setImageLimits() const {
237 this->coordinateHandler.set(this->src.getGeometry(), this->src.getCoordinatePolicy());
238 // this->src.adjustCoordinateHandler(this->coordinateHandler);
239 }
240
243
244 drain::Logger mout("SlidingRadarWindow", __FUNCTION__);
245
246 if (this->odimSrc.area.height == 0)
247 mout.error("src odim.area.height==0" );
248
250 const double r = static_cast<double>(this->odimSrc.area.height) / (2.0*M_PI);
251 const int max = static_cast<int>(this->odimSrc.area.width);
252
253 rangeNorm = static_cast<int>(r);
255 rangeNormEnd = static_cast<int>(r * static_cast<double>(this->conf.frame.height));
256 if ((rangeNorm <= 0) || (rangeNormEnd >= max)){
257 mout.note(rangeNorm , '-' , rangeNormEnd );
258 }
259 //
260 }
261
262 int rangeNorm;
263 int rangeNormEnd;
264 int countMin; // raise
265
266
268 virtual inline
269 bool reset(){
270
271 if (this->location.x <= rangeNorm){
272 this->setLoopLimits(this->conf.frame.width, this->conf.frame.height);
273 }
274 else if (this->location.x < rangeNormEnd){ // from 'height' down to 1
275 this->setLoopLimits(this->conf.frame.width, (rangeNorm * this->conf.frame.height)/(this->location.x+1) );
276 //std::cerr << "loop limits " << this->jMax << std::endl;
277 }
278 else {
279 this->setLoopLimits(this->conf.frame.width, 1);
280 }
281 //drain::Logger mout("SlidingRadarWindow", __FUNCTION__);
282 //mout.warn(this->iMax , ',' , this->jMax , '\t' , this->getSamplingArea( ));
283 countMin = this->conf.contributionThreshold * this->getSamplingArea();
284
286 };
287
288};
289
290
291template <class C, class R=RadarWindowCore>
292class SlidingRadarWindow : public SlidingRadarWindowBase<C,R> { // drain::image::WeightedWindowCore
293public:
294
295 inline
296 SlidingRadarWindow(int width=0, int height=0) : SlidingRadarWindowBase<C,R>(width, height) {
297 };
298
299 inline
300 SlidingRadarWindow(const C & conf) : SlidingRadarWindowBase<C,R>(conf) {
301 };
302
303
304 virtual inline
306 if (this->coordinateHandler.validate(p)){
307 double x = this->src.template get<double>(p);
308 if ((x != this->odimSrc.nodata) && (x != this->odimSrc.undetect))
309 removeTrailingValue(this->odimSrc.scaleForward(x));
310 }
311 };
312
313 virtual inline
315 if (this->coordinateHandler.validate(p)){
316 double x = this->src.template get<double>(p);
317 if ((x != this->odimSrc.nodata) && (x != this->odimSrc.undetect))
318 addLeadingValue(this->odimSrc.scaleForward(x));
319 }
320 };
321
323 virtual inline
324 void removeTrailingValue(double x){}; // = 0;
325
327 virtual inline
328 void addLeadingValue(double x){}; // = 0;
329
330
331
332};
333
338template <class C>
340public:
341
342 //drain::UnaryFunctor & myFunctor;
343
344 RadarWindowAvg(int width=0, int height=0) :
345 SlidingRadarWindow<C>(width,height),
346 //myFunctor(this->conf.getFunctor()),
347 sum(0.0),
348 count(0) {
349 };
350
353 //myFunctor(this->conf.getFunctor(window.conf.getFunctorName())), // kludge
354 sum(0.0),
355 count(0) {
356 };
357
358 RadarWindowAvg(const C & conf) :
360 // myFunctor(this->conf.getFunctor(conf.getFunctorName())),
361 sum(0.0),
362 count(0) {
363 };
364
365
366
367
368 virtual
369 inline
370 ~RadarWindowAvg(){};
371
373
374protected:
375
376 double sum;
377 int count;
378
379 virtual
380 inline
381 void clear(){
382 sum = 0.0;
383 count = 0;
384 };
385
386 virtual inline
387 void removeTrailingValue(double x){
388 sum -= x;
389 --count;
390 };
391
392 virtual inline
393 void addLeadingValue(double x){
394 sum += x;
395 ++count;
396 };
397
398 virtual inline
399 void write(){
400 if (count > 0){
401 //if (this->location.x == this->location.y)
402 // std::cerr << sum << '\t' << count << '\n';
403 this->dst.putScaled(this->location.x, this->location.y, this->myFunctor(sum/static_cast<double>(count)));
404 }
405 else
406 this->dst.put(this->location, this->odimSrc.undetect); // ?
407 };
408
409
410};
411
416template <class C>
418public:
419
420
421 // Consider metres & degrees?
422 RadarWindowWeightedAvg(int width=0, int height=0) : SlidingRadarWindow<C>(width,height) {
423 };
424
426 };
427
428 RadarWindowWeightedAvg(const C & conf) : SlidingRadarWindow<C>(conf) {
429 };
430
431 virtual inline
433
434protected:
435
436 int count = 0;
437
438 // Copied from SlidingStripeAverage
439 typedef float sum_t;
440 sum_t w = 0.0;
441 sum_t sum = 0.0;
442 sum_t sumW = 0.0;
443
444 virtual inline
445 void clear() final {
446 this->sum = 0.0;
447 this->sumW = 0.0;
448 this->count = 0;
449 };
450
451
452 virtual inline
454 if (this->coordinateHandler.validate(p)){
455 double x = this->src.template get<double>(p);
456 if (this->odimSrc.isValue(x)){
457 this->w = this->srcWeight.template get<sum_t>(p);
458 this->sum += w*this->odimSrc.scaling.fwd(x);
459 this->sumW += w;
460 ++this->count;
461 }
462 }
463 };
464
465 virtual inline
467 if (this->coordinateHandler.validate(p)){
468 double x = this->src.template get<double>(p);
469 if (this->odimSrc.isValue(x)){
470 this->w = this->srcWeight.template get<sum_t>(p);
471 this->sum -= w*this->odimSrc.scaling.fwd(x);
472 this->sumW -= w;
473 --this->count;
474 }
475 }
476 };
477
478 virtual inline
479 void write() final {
480 if (sumW > 0.0){ // then also count>0
481 this->dst.put(this->location, this->odimSrc.scaling.inv(this->sum/sumW));
482 //this->dstWeight.put(this->location, this->scalingW.inv(sumW/static_cast<sum_t>(this->count)));
483 this->dstWeight.put(this->location, sumW/static_cast<sum_t>(this->count)); // "uses" original scaling
484 }
485 else {
486 this->dst.put(this->location, this->odimSrc.undetect); // change
487 this->dstWeight.put(this->location, 0);
488 }
489
490 }
491
492 /*
493 virtual inline
494 void removeTrailingValue(double x) final {
495 sum -= x;
496 --count;
497 };
498
499 virtual inline
500 void addLeadingValue(double x) final {
501 sum += x;
502 ++count;
503 };
504
505 virtual inline
506 void write(){
507 if (count > 0){
508 //if (this->location.x == this->location.y)
509 // std::cerr << sum << '\t' << count << '\n';
510 this->dst.putScaled(this->location.x, this->location.y, this->myFunctor(sum/static_cast<double>(count)));
511 }
512 else
513 this->dst.put(this->location, this->odimSrc.undetect); // ?
514 };
515 */
516
517
518};
519
520
524template <class C>
526public:
527
528 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) {};
529
530
531 virtual inline
533
534 void setCoeff(double c){
535 if (c==0.0)
536 throw std::runtime_error("RadarWindowSoftMax: zero coeff");
537 coeff = c;
538 coeffInv = 1.0/c;
539 };
540
541protected:
542
543 double coeff;
544 double coeffInv;
545
546 double sum;
547 int count;
548
549 virtual inline
550 void clear(){
551 sum = 0.0;
552 count = 0;
553 };
554
555 virtual inline
556 void removeTrailingValue(double x){
557 sum -= ::exp(coeff * x);
558 --count;
559 };
560
561 virtual inline
562 void addLeadingValue(double x){
563 sum += ::exp(coeff * x);
564 ++count;
565 //if ((p.x == p.y) && (x > -15.0))
566 // std::cerr << "handleLeadingPixel" << p << ':' << x << '\t' << count << ':' << sum << std::endl;
567 };
568
569 virtual inline
570 void write(){
571 if (count > 0)
572 this->dst.put(this->location, this->fuzzifier(coeffInv*::log(sum/static_cast<double>(count))));
573 //this->dst.put(this->location, this->functor(sum/static_cast<double>(count)));
574 };
575
576
577};
578
580
585template <class F>
587public:
588
589 RadarWindowStdDev(int width=0, int height=0) : SlidingRadarWindow<F>(width,height), sum(0.0), sum2(0.0), count(0) {};
590
591
592 virtual inline
594
595protected:
596
597 double sum;
598 double sum2;
599 int count;
600
601 virtual inline
602 void clear(){
603 sum = 0.0;
604 sum2 = 0.0;
605 count = 0;
606 };
607
608 virtual
609 inline
610 void removeTrailingValue(double x){
611 sum -= x;
612 sum2 -= x*x;
613 --count;
614 };
615
616 virtual
617 inline
618 void addLeadingValue(double x){
619 sum += x;
620 sum2 += x*x;
621 ++count;
622 //if ((this->p.x == this->p.y) && (x > -15.0))
623 // std::cerr << "handleLeadingPixel" << this->p << ':' << x << '\t' << count << ':' << sum << std::endl;
624 };
625
626 virtual
627 inline
628 void write(){
629
630 if (count > 0){
631 double countD = static_cast<double>(count);
632 this->dst.put(this->location, this->fuzzifier( ::sqrt(sum2/countD - sum*sum/(countD*countD)) ));
633 /*
634 countD = sum2/countD - sum*sum/(countD*countD);
635 if ((this->p.x == this->p.y) && (count > 5))
636 std::cerr << __FUNCTION__ << this->p << ':' << countD << std::endl;
637 */
638 }
639 else
640 this->dst.put(this->location, 0);
641
642 };
643
644
645};
646
647
648
649
650
651
652
653} // rack::
654
655
656
657
658#endif
659
660// Rack
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:326
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 putScaled(size_t i, double x) const
Put intensity using original physical value.
Definition ImageFrame.h:239
void put(size_t i, T x)
Sets the intensity in location i to x.
Definition ImageFrame.h:191
const std::type_info & getType() const
Get the storage type.
Definition ImageLike.h:105
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
A map of radar data, indexed by quantity code (DBZH, VRAD, etc).
Definition Data.h:1293
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:339
virtual void write()
Write the result in the target image.
Definition RadarWindows.h:399
virtual void removeTrailingValue(double x)
Handles the converted (natural-scaled) value.
Definition RadarWindows.h:387
virtual void addLeadingValue(double x)
Handles the converted (natural-scaled) value.
Definition RadarWindows.h:393
virtual void clear()
Clears the applied statistics. Redefined in derived classes.
Definition RadarWindows.h:381
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:92
RadarWindowConfig(const FT &ftor, int widthM=1500, double heightD=3.0, double contributionThreshold=0.5, bool invertPolar=false, bool relativeScale=false)
Definition RadarWindows.h:120
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:145
double NI
Definition RadarWindows.h:160
RadarWindowCore()
Will act as base class: Window<RadarWindowCore> : public RadarWindowCore {...}, init currently not su...
Definition RadarWindows.h:153
Like pixel window, but width is metres and height is degrees.
Definition RadarWindows.h:56
Definition RadarWindows.h:525
virtual void write()
Write the result in the target image.
Definition RadarWindows.h:570
virtual void removeTrailingValue(double x)
Handles the converted (natural-scaled) value.
Definition RadarWindows.h:556
virtual void addLeadingValue(double x)
Handles the converted (natural-scaled) value.
Definition RadarWindows.h:562
virtual void clear()
Clears the applied statistics. Redefined in derived classes.
Definition RadarWindows.h:550
Sliding window for computing standard deviation of scalar quantities.
Definition RadarWindows.h:586
virtual void write()
Write the result in the target image.
Definition RadarWindows.h:628
virtual void removeTrailingValue(double x)
Handles the converted (natural-scaled) value.
Definition RadarWindows.h:610
virtual void addLeadingValue(double x)
Handles the converted (natural-scaled) value.
Definition RadarWindows.h:618
virtual void clear()
Clears the applied statistics. Redefined in derived classes.
Definition RadarWindows.h:602
Definition RadarWindows.h:417
virtual void addPixel(drain::Point2D< int > &p) final
Adds a pixel to window statistics. Unvalidated location.
Definition RadarWindows.h:453
virtual void removePixel(drain::Point2D< int > &p) final
Removes a pixel from window statistics. Unvalidated location.
Definition RadarWindows.h:466
virtual void write() final
Write the result in the target image.
Definition RadarWindows.h:479
virtual void clear() final
Clears the applied statistics. Redefined in derived classes.
Definition RadarWindows.h:445
A two-dimensional image processing window that handles the special ODIM codes and scales the result....
Definition RadarWindows.h:173
void setImageLimits() const
Sets internal limits corresponding to image geometries. Typically using coordHandler.
Definition RadarWindows.h:236
void setSrcFrame(const drain::image::ImageFrame &src)
Sets input image and retrieves ODIM metadata from image Properties.
Definition RadarWindows.h:193
virtual void initialize() override
Sets class-specific initial values. Does not change general window state (e.g. location)....
Definition RadarWindows.h:221
virtual bool reset()
Returns false, if traversal should be ended.
Definition RadarWindows.h:269
void setRangeNorm()
To compensate polar geometry, set applicable range for pixel area scaling.
Definition RadarWindows.h:242
Definition RadarWindows.h:292
virtual void addPixel(drain::Point2D< int > &p)
Adds a pixel to window statistics. Unvalidated location.
Definition RadarWindows.h:314
virtual void removePixel(drain::Point2D< int > &p)
Removes a pixel from window statistics. Unvalidated location.
Definition RadarWindows.h:305
virtual void removeTrailingValue(double x)
Handles the converted (natural-scaled) value.
Definition RadarWindows.h:324
virtual void addLeadingValue(double x)
Handles the converted (natural-scaled) value.
Definition RadarWindows.h:328
Namespace for images and image processing tools.
Definition AccumulationArray.cpp:45
Definition Data.h:1473
Definition AltitudeOp.cpp:42
Definition Point.h:48