1
- // -*- C++ -*-
2
- // -*- coding: utf-8 -*-
3
- //
4
- // Source Author: Bryan Riel
5
- // Copyright 2017-2018
6
- //
7
-
8
1
#if !defined(ISCE_CORE_LUT1D_ICC)
9
2
#error "LUT1d.icc is an implementation detail of class LUT1d"
10
3
#endif
11
4
12
5
#include <pyre/journal.h>
13
6
14
- // Constructor from an LUT2d (values averaged in y-direction)
15
- /* * @param[in] lut2d LUT2d to copy data from */
16
- template <typename T>
17
- isce3::core::LUT1d<T>::
18
- LUT1d (const isce3::core::LUT2d<T> & lut2d) {
19
-
20
- // Check if LUT2d has actual data; if not, just store reference value
21
- if (!lut2d.haveData ()) {
22
- _haveData = false ;
23
- _refValue = lut2d.refValue ();
24
- return ;
25
- }
26
-
27
- // Get a reference to the LUT2d data
28
- const isce3::core::Matrix<T> & data = lut2d.data ();
29
-
30
- // Initialize working valarrays for computing mean along y-direction
31
- std::valarray<double > values (lut2d.width ()), count (lut2d.width ());
32
- values = 0.0 ;
33
- count = 0.0 ;
34
-
35
- // Compute sum along columns (y-direction)
36
- for (size_t i = 0 ; i < lut2d.length (); ++i) {
37
- for (size_t j = 0 ; j < lut2d.width (); ++j) {
38
- values[j] += data (i,j);
39
- count[j] += 1.0 ;
40
- }
41
- }
42
-
43
- // Compute final coordinates and values
44
- std::valarray<double > coords (lut2d.width ());
45
- for (size_t j = 0 ; j < lut2d.width (); ++j) {
46
- coords[j] = lut2d.xStart () + j * lut2d.xSpacing ();
47
- values[j] /= count[j];
48
- }
49
-
50
- // Save results
51
- _coords = coords;
52
- _values = values;
53
- _extrapolate = true ;
54
- _haveData = true ;
55
- _refValue = lut2d.refValue ();
56
- }
57
-
58
- // Assignment operator from an LUT2d (values averaged in y-direction)
59
- /* * @param[in] lut2d LUT2d to copy data from */
60
- template <typename T>
61
- isce3::core::LUT1d<T> &
62
- isce3::core::LUT1d<T>::
63
- operator =(const LUT2d<T> & lut2d) {
64
-
65
- // Check if LUT2d has actual data; if not, just store reference value
66
- if (!lut2d.haveData ()) {
67
- _haveData = false ;
68
- _refValue = lut2d.refValue ();
69
- return *this ;
70
- }
71
-
72
- // Get a reference to the LUT2d data
73
- const isce3::core::Matrix<T> & data = lut2d.data ();
74
-
75
- // Initialize working valarrays for computing mean along y-direction
76
- std::valarray<double > values (lut2d.width ()), count (lut2d.width ());
77
- values = 0.0 ;
78
- count = 0.0 ;
79
-
80
- // Compute sum along columns (y-direction)
81
- for (size_t i = 0 ; i < lut2d.length (); ++i) {
82
- for (size_t j = 0 ; j < lut2d.width (); ++j) {
83
- values[j] += data (i,j);
84
- count[j] += 1.0 ;
85
- }
86
- }
87
-
88
- // Compute final coordinates and values
89
- std::valarray<double > coords (lut2d.width ());
90
- for (size_t j = 0 ; j < lut2d.width (); ++j) {
91
- coords[j] = lut2d.xStart () + j * lut2d.xSpacing ();
92
- values[j] /= count[j];
93
- }
94
-
95
- // Save results
96
- _coords = coords;
97
- _values = values;
98
- _extrapolate = true ;
99
- return *this ;
100
- }
101
-
102
7
/** @param[in] x Point to evaluate the LUT
103
8
* @param[out] result Interpolated value */
104
9
template <typename T>
@@ -179,7 +84,7 @@ eval(double x) const {
179
84
// The indices of the x bounds
180
85
const int j0 = high - 1;
181
86
const int j1 = high;
182
-
87
+
183
88
// Get coordinates at bounds
184
89
double x1 = _coords[j0];
185
90
double x2 = _coords[j1];
@@ -189,4 +94,50 @@ eval(double x) const {
189
94
return result;
190
95
}
191
96
192
- // end of file
97
+ template <typename T>
98
+ isce3::core::LUT1d<T>
99
+ isce3::core::avgLUT2dToLUT1d(const isce3::core::LUT2d<T> & lut2d,
100
+ const int axis) {
101
+ if (axis != 0 && axis != 1) {
102
+ std::string error_msg = "ERROR axis not equal to 0 or 1";
103
+ throw isce3::except::InvalidArgument(ISCE_SRCINFO(), error_msg);
104
+ }
105
+
106
+ // Check if LUT2d has actual data; if not, just return LUT1d with reference value
107
+ if (!lut2d.haveData()) {
108
+ return isce3::core::LUT1d<T>(lut2d.refValue());
109
+ }
110
+
111
+ // Determine lut1d size and number of elements to sum
112
+ const auto lut1d_size = (axis == 0) ? lut2d.width() : lut2d.length();
113
+ const double n_to_sum_f = (axis == 0) ? static_cast<double>(lut2d.length()) :
114
+ static_cast<double>(lut2d.width());
115
+
116
+ // Get a reference to the LUT2d data
117
+ const Matrix<T> & data = lut2d.data();
118
+
119
+ // Compute sum and average
120
+ isce3::core::EArray2D<double> ea_values;
121
+ if (axis == 0)
122
+ // Sum along rows (x-direction)
123
+ ea_values = data.map().colwise().sum();
124
+ else
125
+ // Sum along columns (y-direction)
126
+ ea_values = data.map().rowwise().sum();
127
+ ea_values /= n_to_sum_f;
128
+
129
+ // Initialize working valarrays for computing mean along y-direction
130
+ std::valarray<double> values(0.0, lut1d_size);
131
+ std::copy(ea_values.data(), ea_values.data() + ea_values.size(), begin(values));
132
+
133
+ // Compute final coordinates and values
134
+ std::valarray<double> coords(lut1d_size);
135
+ for (size_t j = 0; j < lut1d_size; ++j) {
136
+ if (axis == 0)
137
+ coords[j] = lut2d.xStart() + j * lut2d.xSpacing();
138
+ else
139
+ coords[j] = lut2d.yStart() + j * lut2d.ySpacing();
140
+ }
141
+
142
+ return isce3::core::LUT1d<T>(coords, values, true);
143
+ }
0 commit comments