ARTS 2.5.11 (git: 6827797f)
m_select.h
Go to the documentation of this file.
1
11#ifndef m_select_h
12#define m_select_h
13
14#include "agenda_class.h"
15#include "matpack_data.h"
16#include "messages.h"
17#include "mystring.h"
18#include "workspace_ng.h"
19
20/* Workspace method: Doxygen documentation will be auto-generated */
21template <class T>
22void Select( // WS Generic Output:
23 Array<T>& needles,
24 // WS Generic Input:
25 const Array<T>& haystack,
26 const ArrayOfIndex& needleind,
27 const Verbosity&) {
28 // We construct the output in this dummy variable, so that the
29 // method also works properly if needles and haystack are the same
30 // variable.
31 Array<T> dummy(needleind.nelem());
32
33 // If needleind only contains -1 as the only element, copy the whole thing
34 if (needleind.nelem() == 1 && needleind[0] == -1) {
35 needles = haystack;
36 return;
37 }
38
39 for (Index i = 0; i < needleind.nelem(); i++) {
40 if (haystack.nelem() <= needleind[i]) {
41 ostringstream os;
42 os << "The input vector only has " << haystack.nelem()
43 << " elements. But one of the needle indexes is " << needleind[i]
44 << "." << endl;
45 os << "The indexes must be between 0 and " << haystack.nelem() - 1;
46 throw runtime_error(os.str());
47 } else if (needleind[i] < 0) {
48 ostringstream os;
49 os << "One of the needle indexes is " << needleind[i] << "." << endl;
50 os << "The indexes must be between 0 and " << haystack.nelem() - 1;
51 throw runtime_error(os.str());
52 } else
53 dummy[i] = haystack[needleind[i]];
54 }
55
56 needles = dummy;
57}
58
59/* Workspace method: Doxygen documentation will be auto-generated */
60inline void Select( // WS Generic Output:
61ArrayOfSpeciesTag& needles,
62// WS Generic Input:
63const ArrayOfSpeciesTag& haystack,
64const ArrayOfIndex& needleind,
65const Verbosity&) {
66 // We construct the output in this dummy variable, so that the
67 // method also works properly if needles and haystack are the same
68 // variable.
69 ArrayOfSpeciesTag dummy(needleind.nelem());
70
71 // If needleind only contains -1 as the only element, copy the whole thing
72 if (needleind.nelem() == 1 && needleind[0] == -1) {
73 needles = haystack;
74 return;
75 }
76
77 for (Index i = 0; i < needleind.nelem(); i++) {
78 if (haystack.nelem() <= needleind[i]) {
79 ostringstream os;
80 os << "The input vector only has " << haystack.nelem()
81 << " elements. But one of the needle indexes is " << needleind[i]
82 << "." << endl;
83 os << "The indexes must be between 0 and " << haystack.nelem() - 1;
84 throw runtime_error(os.str());
85 } else if (needleind[i] < 0) {
86 ostringstream os;
87 os << "One of the needle indexes is " << needleind[i] << "." << endl;
88 os << "The indexes must be between 0 and " << haystack.nelem() - 1;
89 throw runtime_error(os.str());
90 } else
91 dummy[i] = haystack[needleind[i]];
92 }
93
94 needles = dummy;
95}
96
97/* Workspace method: Doxygen documentation will be auto-generated */
98inline void Select(Workspace& /* ws */,
99 // WS Generic Output:
100 ArrayOfAgenda& needles,
101 // WS Generic Input:
102 const ArrayOfAgenda& haystack,
103 const ArrayOfIndex& needleind,
104 const Verbosity& verbosity) {
105 Select(needles, haystack, needleind, verbosity);
106}
107
108/* Workspace method: Doxygen documentation will be auto-generated */
109inline void Select( // WS Generic Output:
110 Vector& needles,
111 // WS Generic Input:
112 const Vector& haystack,
113 const ArrayOfIndex& needleind,
114 const Verbosity&) {
115 // We construct the output in this dummy variable, so that the
116 // method also works properly if needles and haystack are the same
117 // variable.
118 Vector dummy(needleind.nelem());
119
120 // If needleind only contains -1 as the only element, copy the whole thing
121 if (needleind.nelem() == 1 && needleind[0] == -1) {
122 needles = haystack;
123 return;
124 }
125
126 for (Index i = 0; i < needleind.nelem(); i++) {
127 if (haystack.nelem() <= needleind[i]) {
128 ostringstream os;
129 os << "The input vector only has " << haystack.nelem()
130 << " elements. But one of the needle indexes is " << needleind[i]
131 << "." << endl;
132 os << "The indexes must be between 0 and " << haystack.nelem() - 1;
133 throw runtime_error(os.str());
134 } else if (needleind[i] < 0) {
135 ostringstream os;
136 os << "One of the needle indexes is " << needleind[i] << "." << endl;
137 os << "The indexes must be between 0 and " << haystack.nelem() - 1;
138 throw runtime_error(os.str());
139 } else
140 dummy[i] = haystack[needleind[i]];
141 }
142
143 needles = dummy;
144}
145
146/* Workspace method: Doxygen documentation will be auto-generated */
147inline void Select( // WS Generic Output:
148 Matrix& needles,
149 // WS Generic Input:
150 const Matrix& haystack,
151 const ArrayOfIndex& needleind,
152 const Verbosity&) {
153 // We construct the output in this dummy variable, so that the
154 // method also works properly if needles and haystack are the same
155 // variable.
156 Matrix dummy(needleind.nelem(), haystack.ncols());
157
158 // If needleind only contains -1 as the only element, copy the whole thing
159 if (needleind.nelem() == 1 && needleind[0] == -1) {
160 needles = haystack;
161 return;
162 }
163
164 for (Index i = 0; i < needleind.nelem(); i++) {
165 if (haystack.nrows() <= needleind[i]) {
166 ostringstream os;
167 os << "The input matrix only has " << haystack.nrows()
168 << " rows. But one of the needle indexes is " << needleind[i] << "."
169 << endl;
170 os << "The indexes must be between 0 and " << haystack.nrows() - 1;
171 throw runtime_error(os.str());
172 } else if (needleind[i] < 0) {
173 ostringstream os;
174 os << "One of the needle indexes is " << needleind[i] << "." << endl;
175 os << "The indexes must be between 0 and " << haystack.nrows() - 1;
176 throw runtime_error(os.str());
177 } else
178 dummy(i, joker) = haystack(needleind[i], joker);
179 }
180
181 needles = dummy;
182}
183
184/* Workspace method: Doxygen documentation will be auto-generated */
185inline void Select( // WS Generic Output:
186 Sparse& needles,
187 // WS Generic Input:
188 const Sparse& haystack,
189 const ArrayOfIndex& needleind,
190 const Verbosity& verbosity) {
192
193 // We construct the output in this dummy variable, so that the
194 // method also works properly if needles and haystack are the same
195 // variable.
196 Sparse dummy(needleind.nelem(), haystack.ncols());
197
198 // If needleind only contains -1 as the only element, copy the whole thing
199 if (needleind.nelem() == 1 && needleind[0] == -1) {
200 needles = haystack;
201 return;
202 }
203
204 for (Index i = 0; i < needleind.nelem(); i++) {
205 if (haystack.nrows() <= needleind[i]) {
206 ostringstream os;
207 os << "The input matrix only has " << haystack.nrows()
208 << " rows. But one of the needle indexes is " << needleind[i] << "."
209 << endl;
210 os << "The indexes must be between 0 and " << haystack.nrows() - 1;
211 throw runtime_error(os.str());
212 } else if (needleind[i] < 0) {
213 ostringstream os;
214 os << "One of the needle indexes is " << needleind[i] << "." << endl;
215 os << "The indexes must be between 0 and " << haystack.nrows() - 1;
216 throw runtime_error(os.str());
217 } else {
218 // Copy this row of the sparse matrix.
219 // This code is inefficient for Sparse, but I leave it like
220 // this to be consistent with the other data types for which
221 // Select is implemented.
222 for (Index j = 0; j < haystack.ncols(); ++j) {
223 Numeric value = haystack(needleind[i], j);
224 if (0 != value) dummy.rw(i, j) = value;
225 }
226 }
227 }
228
229 if (dummy.nnz() == haystack.nnz()) {
230 // No data was actually removed.
231 out3 << " Number of nonzero elements has stayed the same.\n";
232 } else {
233 out3 << " Number of nonzero elements reduced from " << haystack.nnz()
234 << " to " << dummy.nnz() << ".\n";
235 }
236
237 needles = dummy;
238}
239
240#endif // m_select_h
Declarations for agendas.
This can be used to make arrays out of anything.
Definition: array.h:31
Index nelem() const ARTS_NOEXCEPT
Definition: array.h:75
Workspace class.
Definition: workspace_ng.h:36
void Select(Array< T > &needles, const Array< T > &haystack, const ArrayOfIndex &needleind, const Verbosity &)
Definition: m_select.h:22
Declarations having to do with the four output streams.
#define CREATE_OUT3
Definition: messages.h:189
This file contains the definition of String, the ARTS string class.
This file contains the Workspace class.