ARTS 2.5.11 (git: 725533f0)
m_array.cc
Go to the documentation of this file.
1
10#include "artstime.h"
11#include "matpack_data.h"
12#include "matpack_arrays.h"
13#include "messages.h"
14#include "sorting.h"
15
16
17template <class T>
18Array<T> TimeSortTemplate(const Array<T>& arr, const ArrayOfTime& time_stamps)
19{
20 // Size of problem
21 const Index n=time_stamps.nelem();
22 if (arr.nelem() not_eq n)
23 throw std::runtime_error("Cannot sort, time array does not agree with sorting array size");
24
25 // Sorted index
26 ArrayOfIndex sortings(n);
27 get_sorted_indexes(sortings, time_stamps);
28
29 // Fill the data into a new array
30 Array<T> out(n);
31 for (Index i=0; i<n; i++)
32 out[i] = arr[sortings[i]];
33
34 return out;
35}
36
37#define TIME_SORT_MACRO(VAR) \
38void time_stampsSort(VAR & out, const ArrayOfTime& time_stamps, const VAR & in, const Verbosity&) \
39{out = TimeSortTemplate(in, time_stamps);}
40
42TIME_SORT_MACRO(ArrayOfVector)
43
44#undef TIME_SORT_MACRO
45
46template <class T>
48{
49 // Size of problem
50 Index n=0;
51 for (auto& array: in)
52 n += array.nelem();
53
54 // Allocate output
55 Array<T> out(n);
56
57 // Assignment
58 Index i=0;
59 for (auto& array: in) {
60 for (auto& val: array) {
61 out[i] = val;
62 i++;
63 }
64 }
65
66 return out;
67}
68
69#define FLATTEN_MACRO(VAR) \
70void Flatten(VAR & out, const Array< VAR > & in, const Verbosity&) \
71{out = FlattenArrayTemplate(in);}
72
74FLATTEN_MACRO(ArrayOfVector)
75
76#undef FLATTEN_MACRO
77
78void Flatten(Matrix& out, const ArrayOfVector& in, const Verbosity&)
79{
80 if (in.nelem() == 0) {
81 out = Matrix(0, 0);
82 } else {
83 const Index n = in.nelem();
84 const Index m = in[0].nelem();
85
86 if (not std::all_of(in.cbegin(), in.cend(), [m](auto& v){return m == v.nelem();}))
87 throw std::runtime_error("Can only flatten array of same length data");
88
89 out = Matrix(n, m);
90 for (Index i=0; i<n; i++)
91 out(i, joker) = in[i];
92 }
93}
94
95void Flatten(Tensor3& out, const ArrayOfMatrix& in, const Verbosity&)
96{
97 if (in.nelem() == 0) {
98 out = Tensor3(0, 0, 0);
99 } else {
100 const Index n = in.nelem();
101 const Index c = in[0].ncols();
102 const Index r = in[0].nrows();
103
104 if (not std::all_of(in.cbegin(), in.cend(), [c](auto& v){return c == v.ncols();})) {
105 throw std::runtime_error("Can only flatten array of same size data");
106 } else if (not std::all_of(in.cbegin(), in.cend(), [r](auto& v){return r == v.nrows();})) {
107 throw std::runtime_error("Can only flatten array of same size data");
108 }
109
110 out = Tensor3(n, r, c);
111 for (Index i=0; i<n; i++)
112 out(i, joker, joker) = in[i];
113 }
114}
115
116void Flatten(Tensor4& out, const ArrayOfTensor3& in, const Verbosity&)
117{
118 if (in.nelem() == 0) {
119 out = Tensor4(0, 0, 0, 0);
120 } else {
121 const Index n = in.nelem();
122 const Index c = in[0].ncols();
123 const Index r = in[0].nrows();
124 const Index p = in[0].npages();
125
126 if (not std::all_of(in.cbegin(), in.cend(), [c](auto& v){return c == v.ncols();})) {
127 throw std::runtime_error("Can only flatten array of same size data");
128 } else if (not std::all_of(in.cbegin(), in.cend(), [r](auto& v){return r == v.nrows();})) {
129 throw std::runtime_error("Can only flatten array of same size data");
130 } else if (not std::all_of(in.cbegin(), in.cend(), [p](auto& v){return p == v.npages();})) {
131 throw std::runtime_error("Can only flatten array of same size data");
132 }
133
134 out = Tensor4(n, p, r, c);
135 for (Index i=0; i<n; i++)
136 out(i, joker, joker, joker) = in[i];
137 }
138}
139
140void Flatten(Tensor5& out, const ArrayOfTensor4& in, const Verbosity&)
141{
142 if (in.nelem() == 0) {
143 out = Tensor5(0, 0, 0, 0, 0);
144 } else {
145 const Index n = in.nelem();
146 const Index c = in[0].ncols();
147 const Index r = in[0].nrows();
148 const Index p = in[0].npages();
149 const Index b = in[0].nbooks();
150
151 if (not std::all_of(in.cbegin(), in.cend(), [c](auto& v){return c == v.ncols();})) {
152 throw std::runtime_error("Can only flatten array of same size data");
153 } else if (not std::all_of(in.cbegin(), in.cend(), [r](auto& v){return r == v.nrows();})) {
154 throw std::runtime_error("Can only flatten array of same size data");
155 } else if (not std::all_of(in.cbegin(), in.cend(), [p](auto& v){return p == v.npages();})) {
156 throw std::runtime_error("Can only flatten array of same size data");
157 } else if (not std::all_of(in.cbegin(), in.cend(), [b](auto& v){return b == v.nbooks();})) {
158 throw std::runtime_error("Can only flatten array of same size data");
159 }
160
161 out = Tensor5(n, b, p, r, c);
162 for (Index i=0; i<n; i++)
163 out(i, joker, joker, joker, joker) = in[i];
164 }
165}
166
167void Flatten(Tensor6& out, const ArrayOfTensor5& in, const Verbosity&)
168{
169 if (in.nelem() == 0) {
170 out = Tensor6(0, 0, 0, 0, 0, 0);
171 } else {
172 const Index n = in.nelem();
173 const Index c = in[0].ncols();
174 const Index r = in[0].nrows();
175 const Index p = in[0].npages();
176 const Index b = in[0].nbooks();
177 const Index s = in[0].nshelves();
178
179 if (not std::all_of(in.cbegin(), in.cend(), [c](auto& v){return c == v.ncols();})) {
180 throw std::runtime_error("Can only flatten array of same size data");
181 } else if (not std::all_of(in.cbegin(), in.cend(), [r](auto& v){return r == v.nrows();})) {
182 throw std::runtime_error("Can only flatten array of same size data");
183 } else if (not std::all_of(in.cbegin(), in.cend(), [p](auto& v){return p == v.npages();})) {
184 throw std::runtime_error("Can only flatten array of same size data");
185 } else if (not std::all_of(in.cbegin(), in.cend(), [b](auto& v){return b == v.nbooks();})) {
186 throw std::runtime_error("Can only flatten array of same size data");
187 } else if (not std::all_of(in.cbegin(), in.cend(), [s](auto& v){return s == v.nshelves();})) {
188 throw std::runtime_error("Can only flatten array of same size data");
189 }
190
191 out = Tensor6(n, s, b, p, r, c);
192 for (Index i=0; i<n; i++)
193 out(i, joker, joker, joker, joker, joker) = in[i];
194 }
195}
196
197void Flatten(Tensor7& out, const ArrayOfTensor6& in, const Verbosity&)
198{
199 if (in.nelem() == 0) {
200 out = Tensor7(0, 0, 0, 0, 0, 0, 0);
201 } else {
202 const Index n = in.nelem();
203 const Index c = in[0].ncols();
204 const Index r = in[0].nrows();
205 const Index p = in[0].npages();
206 const Index b = in[0].nbooks();
207 const Index s = in[0].nshelves();
208 const Index w = in[0].nvitrines();
209
210 if (not std::all_of(in.cbegin(), in.cend(), [c](auto& v){return c == v.ncols();})) {
211 throw std::runtime_error("Can only flatten array of same size data");
212 } else if (not std::all_of(in.cbegin(), in.cend(), [r](auto& v){return r == v.nrows();})) {
213 throw std::runtime_error("Can only flatten array of same size data");
214 } else if (not std::all_of(in.cbegin(), in.cend(), [p](auto& v){return p == v.npages();})) {
215 throw std::runtime_error("Can only flatten array of same size data");
216 } else if (not std::all_of(in.cbegin(), in.cend(), [b](auto& v){return b == v.nbooks();})) {
217 throw std::runtime_error("Can only flatten array of same size data");
218 } else if (not std::all_of(in.cbegin(), in.cend(), [s](auto& v){return s == v.nshelves();})) {
219 throw std::runtime_error("Can only flatten array of same size data");
220 } else if (not std::all_of(in.cbegin(), in.cend(), [w](auto& v){return w == v.nvitrines();})) {
221 throw std::runtime_error("Can only flatten array of same size data");
222 }
223
224 out = Tensor7(n, w, s, b, p, r, c);
225 for (Index i=0; i<n; i++)
226 out(i, joker, joker, joker, joker, joker, joker) = in[i];
227 }
228}
Stuff related to time in ARTS.
This can be used to make arrays out of anything.
Definition array.h:31
Index nelem() const ARTS_NOEXCEPT
Definition array.h:75
Array< T > FlattenArrayTemplate(const Array< Array< T > > &in)
Definition m_array.cc:47
#define TIME_SORT_MACRO(VAR)
Definition m_array.cc:37
#define FLATTEN_MACRO(VAR)
Definition m_array.cc:69
Array< T > TimeSortTemplate(const Array< T > &arr, const ArrayOfTime &time_stamps)
Definition m_array.cc:18
void Flatten(Matrix &out, const ArrayOfVector &in, const Verbosity &)
WORKSPACE METHOD: Flatten.
Definition m_array.cc:78
Declarations having to do with the four output streams.
Contains sorting routines.
void get_sorted_indexes(ArrayOfIndex &sorted, const T &data)
get_sorted_indexes
Definition sorting.h:39
#define v
#define w
#define c
#define b