ARTS  2.4.0(git:4fb77825)
m_array.cc
Go to the documentation of this file.
1 /* Copyright (C) 2020
2  * Richard Larsson <larsson@mps.mpg.de>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2, or (at your option) any
7  * later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17  * USA. */
18 
28 #include "artstime.h"
29 #include "matpackVII.h"
30 #include "messages.h"
31 #include "sorting.h"
32 
33 
34 template <class T>
36 {
37  // Size of problem
38  const Index n=time_stamps.nelem();
39  if (arr.nelem() not_eq n)
40  throw std::runtime_error("Cannot sort, time array does not agree with sorting array size");
41 
42  // Sorted index
43  ArrayOfIndex sortings(n);
45 
46  // Fill the data into a new array
47  Array<T> out(n);
48  for (Index i=0; i<n; i++)
49  out[i] = arr[sortings[i]];
50 
51  return out;
52 }
53 
54 #define TIME_SORT_MACRO(VAR) \
55 void time_stampsSort(VAR & out, const ArrayOfTime& time_stamps, const VAR & in, const Verbosity&) \
56 {out = TimeSortTemplate(in, time_stamps);}
57 
60 
61 #undef TIME_SORT_MACRO
62 
63 template <class T>
65 {
66  // Size of problem
67  Index n=0;
68  for (auto& array: in)
69  n += array.nelem();
70 
71  // Allocate output
72  Array<T> out(n);
73 
74  // Assignment
75  Index i=0;
76  for (auto& array: in) {
77  for (auto& val: array) {
78  out[i] = val;
79  i++;
80  }
81  }
82 
83  return out;
84 }
85 
86 #define FLATTEN_MACRO(VAR) \
87 void Flatten(VAR & out, const Array< VAR > & in, const Verbosity&) \
88 {out = FlattenArrayTemplate(in);}
89 
92 
93 #undef FLATTEN_MACRO
94 
95 void Flatten(Matrix& out, const ArrayOfVector& in, const Verbosity&)
96 {
97  if (in.nelem() == 0) {
98  out = Matrix(0, 0);
99  } else {
100  const Index n = in.nelem();
101  const Index m = in[0].nelem();
102 
103  if (not std::all_of(in.cbegin(), in.cend(), [m](auto& v){return m == v.nelem();}))
104  throw std::runtime_error("Can only flatten array of same length data");
105 
106  out = Matrix(n, m);
107  for (Index i=0; i<n; i++)
108  out(i, joker) = in[i];
109  }
110 }
111 
112 void Flatten(Tensor3& out, const ArrayOfMatrix& in, const Verbosity&)
113 {
114  if (in.nelem() == 0) {
115  out = Tensor3(0, 0, 0);
116  } else {
117  const Index n = in.nelem();
118  const Index c = in[0].ncols();
119  const Index r = in[0].nrows();
120 
121  if (not std::all_of(in.cbegin(), in.cend(), [c](auto& v){return c == v.ncols();})) {
122  throw std::runtime_error("Can only flatten array of same size data");
123  } else if (not std::all_of(in.cbegin(), in.cend(), [r](auto& v){return r == v.nrows();})) {
124  throw std::runtime_error("Can only flatten array of same size data");
125  }
126 
127  out = Tensor3(n, r, c);
128  for (Index i=0; i<n; i++)
129  out(i, joker, joker) = in[i];
130  }
131 }
132 
133 void Flatten(Tensor4& out, const ArrayOfTensor3& in, const Verbosity&)
134 {
135  if (in.nelem() == 0) {
136  out = Tensor4(0, 0, 0, 0);
137  } else {
138  const Index n = in.nelem();
139  const Index c = in[0].ncols();
140  const Index r = in[0].nrows();
141  const Index p = in[0].npages();
142 
143  if (not std::all_of(in.cbegin(), in.cend(), [c](auto& v){return c == v.ncols();})) {
144  throw std::runtime_error("Can only flatten array of same size data");
145  } else if (not std::all_of(in.cbegin(), in.cend(), [r](auto& v){return r == v.nrows();})) {
146  throw std::runtime_error("Can only flatten array of same size data");
147  } else if (not std::all_of(in.cbegin(), in.cend(), [p](auto& v){return p == v.npages();})) {
148  throw std::runtime_error("Can only flatten array of same size data");
149  }
150 
151  out = Tensor4(n, p, r, c);
152  for (Index i=0; i<n; i++)
153  out(i, joker, joker, joker) = in[i];
154  }
155 }
156 
157 void Flatten(Tensor5& out, const ArrayOfTensor4& in, const Verbosity&)
158 {
159  if (in.nelem() == 0) {
160  out = Tensor5(0, 0, 0, 0, 0);
161  } else {
162  const Index n = in.nelem();
163  const Index c = in[0].ncols();
164  const Index r = in[0].nrows();
165  const Index p = in[0].npages();
166  const Index b = in[0].nbooks();
167 
168  if (not std::all_of(in.cbegin(), in.cend(), [c](auto& v){return c == v.ncols();})) {
169  throw std::runtime_error("Can only flatten array of same size data");
170  } else if (not std::all_of(in.cbegin(), in.cend(), [r](auto& v){return r == v.nrows();})) {
171  throw std::runtime_error("Can only flatten array of same size data");
172  } else if (not std::all_of(in.cbegin(), in.cend(), [p](auto& v){return p == v.npages();})) {
173  throw std::runtime_error("Can only flatten array of same size data");
174  } else if (not std::all_of(in.cbegin(), in.cend(), [b](auto& v){return b == v.nbooks();})) {
175  throw std::runtime_error("Can only flatten array of same size data");
176  }
177 
178  out = Tensor5(n, b, p, r, c);
179  for (Index i=0; i<n; i++)
180  out(i, joker, joker, joker, joker) = in[i];
181  }
182 }
183 
184 void Flatten(Tensor6& out, const ArrayOfTensor5& in, const Verbosity&)
185 {
186  if (in.nelem() == 0) {
187  out = Tensor6(0, 0, 0, 0, 0, 0);
188  } else {
189  const Index n = in.nelem();
190  const Index c = in[0].ncols();
191  const Index r = in[0].nrows();
192  const Index p = in[0].npages();
193  const Index b = in[0].nbooks();
194  const Index s = in[0].nshelves();
195 
196  if (not std::all_of(in.cbegin(), in.cend(), [c](auto& v){return c == v.ncols();})) {
197  throw std::runtime_error("Can only flatten array of same size data");
198  } else if (not std::all_of(in.cbegin(), in.cend(), [r](auto& v){return r == v.nrows();})) {
199  throw std::runtime_error("Can only flatten array of same size data");
200  } else if (not std::all_of(in.cbegin(), in.cend(), [p](auto& v){return p == v.npages();})) {
201  throw std::runtime_error("Can only flatten array of same size data");
202  } else if (not std::all_of(in.cbegin(), in.cend(), [b](auto& v){return b == v.nbooks();})) {
203  throw std::runtime_error("Can only flatten array of same size data");
204  } else if (not std::all_of(in.cbegin(), in.cend(), [s](auto& v){return s == v.nshelves();})) {
205  throw std::runtime_error("Can only flatten array of same size data");
206  }
207 
208  out = Tensor6(n, s, b, p, r, c);
209  for (Index i=0; i<n; i++)
210  out(i, joker, joker, joker, joker, joker) = in[i];
211  }
212 }
213 
214 void Flatten(Tensor7& out, const ArrayOfTensor6& in, const Verbosity&)
215 {
216  if (in.nelem() == 0) {
217  out = Tensor7(0, 0, 0, 0, 0, 0, 0);
218  } else {
219  const Index n = in.nelem();
220  const Index c = in[0].ncols();
221  const Index r = in[0].nrows();
222  const Index p = in[0].npages();
223  const Index b = in[0].nbooks();
224  const Index s = in[0].nshelves();
225  const Index w = in[0].nvitrines();
226 
227  if (not std::all_of(in.cbegin(), in.cend(), [c](auto& v){return c == v.ncols();})) {
228  throw std::runtime_error("Can only flatten array of same size data");
229  } else if (not std::all_of(in.cbegin(), in.cend(), [r](auto& v){return r == v.nrows();})) {
230  throw std::runtime_error("Can only flatten array of same size data");
231  } else if (not std::all_of(in.cbegin(), in.cend(), [p](auto& v){return p == v.npages();})) {
232  throw std::runtime_error("Can only flatten array of same size data");
233  } else if (not std::all_of(in.cbegin(), in.cend(), [b](auto& v){return b == v.nbooks();})) {
234  throw std::runtime_error("Can only flatten array of same size data");
235  } else if (not std::all_of(in.cbegin(), in.cend(), [s](auto& v){return s == v.nshelves();})) {
236  throw std::runtime_error("Can only flatten array of same size data");
237  } else if (not std::all_of(in.cbegin(), in.cend(), [w](auto& v){return w == v.nvitrines();})) {
238  throw std::runtime_error("Can only flatten array of same size data");
239  }
240 
241  out = Tensor7(n, w, s, b, p, r, c);
242  for (Index i=0; i<n; i++)
243  out(i, joker, joker, joker, joker, joker, joker) = in[i];
244  }
245 }
Matrix
The Matrix class.
Definition: matpackI.h:1193
FLATTEN_MACRO
#define FLATTEN_MACRO(VAR)
Definition: m_array.cc:86
w
Complex w(Complex z) noexcept
The Faddeeva function.
Definition: linefunctions.cc:42
Tensor3
The Tensor3 class.
Definition: matpackIII.h:339
joker
const Joker joker
ARTS::Group::Tensor3
Tensor3 Tensor3
Definition: autoarts.h:104
oem::Matrix
invlib::Matrix< ArtsMatrix > Matrix
invlib wrapper type for ARTS matrices.
Definition: oem.h:34
ARTS::Group::Tensor4
Tensor4 Tensor4
Definition: autoarts.h:105
Tensor4
The Tensor4 class.
Definition: matpackIV.h:421
TIME_SORT_MACRO
#define TIME_SORT_MACRO(VAR)
Definition: m_array.cc:54
artstime.h
Stuff related to time in ARTS.
Array
This can be used to make arrays out of anything.
Definition: array.h:108
get_sorted_indexes
void get_sorted_indexes(ArrayOfIndex &sorted, const T &data)
get_sorted_indexes
Definition: sorting.h:73
Flatten
void Flatten(Matrix &out, const ArrayOfVector &in, const Verbosity &)
WORKSPACE METHOD: Flatten.
Definition: m_array.cc:95
messages.h
Declarations having to do with the four output streams.
TimeSortTemplate
Array< T > TimeSortTemplate(const Array< T > &arr, const ArrayOfTime &time_stamps)
Definition: m_array.cc:35
ARTS::Group::Tensor6
Tensor6 Tensor6
Definition: autoarts.h:107
Verbosity
Definition: messages.h:49
ARTS::Group::Tensor5
Tensor5 Tensor5
Definition: autoarts.h:106
FlattenArrayTemplate
Array< T > FlattenArrayTemplate(const Array< Array< T >> &in)
Definition: m_array.cc:64
Tensor5
The Tensor5 class.
Definition: matpackV.h:506
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
ARTS::Group::Tensor7
Tensor7 Tensor7
Definition: autoarts.h:108
Tensor6
The Tensor6 class.
Definition: matpackVI.h:1088
ARTS::Var::time_stamps
ArrayOfTime time_stamps(Workspace &ws) noexcept
Definition: autoarts.h:7057
sorting.h
Contains sorting routines.
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:195
Tensor7
The Tensor7 class.
Definition: matpackVII.h:2382
matpackVII.h