OpenZGY/C++ API and Internals (ALPHA)
Access seismic data stored in ZGY format.
statisticdata.h
Go to the documentation of this file.
1 // Copyright 2017-2020, Schlumberger
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //Adapted from: Zgy/UtilityLib/StatisticData
16 #pragma once
17 
18 #include "../declspec.h"
19 
20 #include <cstdint>
21 #include <math.h>
22 #include <cmath>
23 
24 namespace InternalZGY {
25 #if 0
26 }
27 #endif
28 
37 class OPENZGY_TEST_API StatisticData
38 {
39 public:
40  typedef std::int64_t count_type;
41  count_type getcnt() const { return cnt_; }
42  count_type getinf() const { return inf_; }
43  double getsum() const { return sum_; }
44  double getssq() const { return ssq_; }
45  double getmin() const { return min_; }
46  double getmax() const { return max_; }
48  StatisticData();
49  StatisticData(count_type cnt, count_type inf, double sum, double ssq, double min, double max);
50  StatisticData(const count_type *bins, int nbins, double range_min, double range_max, bool is_int8);
51 
52  inline void add(double value);
53 
54  StatisticData& operator+=(const StatisticData& other);
55  StatisticData& operator-=(const StatisticData& other);
56  StatisticData& operator*=(count_type factor);
57 
58  inline void scale(double oldmin, double oldmax, double newmin, double newmax);
59  void trimRange(const count_type *bins, int nbins, double range_min, double range_max);
60 
61 private:
62  count_type cnt_;
63  count_type inf_;
64  double sum_;
65  double ssq_;
66  double min_;
67  double max_;
68 };
69 
73 inline void
74 StatisticData::add(double value)
75 {
76  if (std::isfinite(value)) {
77  sum_ += value;
78  ssq_ += value*value;
79  if (cnt_ == 0)
80  min_ = max_ = value;
81  if (value < min_)
82  min_ = value;
83  if (value > max_)
84  max_ = value;
85  ++cnt_;
86  }
87  else
88  ++inf_;
89 }
90 
101 inline void StatisticData::scale(double oldmin, double oldmax, double newmin, double newmax)
102 {
103  /*
104  The decoded value Y is given by a linear transform of the coded value X:
105 
106  Y = a + b*X
107 
108  where a and b are given by the coding range and the value range of type T (see
109  below). The statistics of Y are then:
110 
111  SUM_Y = SUM(a + b*x)
112  = n*a + b*SUM(x) = n*a + b*SUM_X
113 
114  SSQ_Y = SUM((a + b*x)^2)
115  = SUM(a^2 + 2*a*b*x + b^2*x^2)
116  = n*a^2 + 2*a*b*SUM(x) + b^2*SUM(x^2)
117  = n*a^2 + 2*a*b*SUM_X + b^2*SSQ_X
118 
119  MIN_Y = MIN(a + b*x)
120  = a + b*MIN(x)
121  = a + b*MIN_X
122 
123  and similar for MAX_Y.
124  */
125 
126  const double b = (newmax - newmin) / (oldmax - oldmin);
127  const double a = newmin - oldmin*b;
128 
129  ssq_ = cnt_*a*a + 2*a*b*sum_ + b*b*ssq_;
130  sum_ = cnt_*a + b*sum_;
131  min_ = a + b*min_;
132  max_ = a + b*max_;
133 }
134 
135 } // end namespace
InternalZGY::StatisticData
Holds the result of computing statistics.
Definition: statisticdata.h:37
InternalZGY
Implementation not visible to clients.