ARTS 2.5.4 (git: 31ce4f0e)
m_select.h
Go to the documentation of this file.
1/* Copyright (C) 2002-2012 Oliver Lemke <olemke@core-dump.info>
2
3 This program is free software; you can redistribute it and/or modify it
4 under the terms of the GNU General Public License as published by the
5 Free Software Foundation; either version 2, or (at your option) any
6 later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 USA. */
17
28#ifndef m_select_h
29#define m_select_h
30
31#include "agenda_class.h"
32#include "matpackII.h"
33#include "messages.h"
34#include "mystring.h"
35#include "workspace_ng.h"
36
37/* Workspace method: Doxygen documentation will be auto-generated */
38template <class T>
39void Select( // WS Generic Output:
40 Array<T>& needles,
41 // WS Generic Input:
42 const Array<T>& haystack,
43 const ArrayOfIndex& needleind,
44 const Verbosity&) {
45 // We construct the output in this dummy variable, so that the
46 // method also works properly if needles and haystack are the same
47 // variable.
48 Array<T> dummy(needleind.nelem());
49
50 // If needleind only contains -1 as the only element, copy the whole thing
51 if (needleind.nelem() == 1 && needleind[0] == -1) {
52 needles = haystack;
53 return;
54 }
55
56 for (Index i = 0; i < needleind.nelem(); i++) {
57 if (haystack.nelem() <= needleind[i]) {
58 ostringstream os;
59 os << "The input vector only has " << haystack.nelem()
60 << " elements. But one of the needle indexes is " << needleind[i]
61 << "." << endl;
62 os << "The indexes must be between 0 and " << haystack.nelem() - 1;
63 throw runtime_error(os.str());
64 } else if (needleind[i] < 0) {
65 ostringstream os;
66 os << "One of the needle indexes is " << needleind[i] << "." << endl;
67 os << "The indexes must be between 0 and " << haystack.nelem() - 1;
68 throw runtime_error(os.str());
69 } else
70 dummy[i] = haystack[needleind[i]];
71 }
72
73 needles = dummy;
74}
75
76/* Workspace method: Doxygen documentation will be auto-generated */
77inline void Select( // WS Generic Output:
78ArrayOfSpeciesTag& needles,
79// WS Generic Input:
80const ArrayOfSpeciesTag& haystack,
81const ArrayOfIndex& needleind,
82const Verbosity&) {
83 // We construct the output in this dummy variable, so that the
84 // method also works properly if needles and haystack are the same
85 // variable.
86 ArrayOfSpeciesTag dummy(needleind.nelem());
87
88 // If needleind only contains -1 as the only element, copy the whole thing
89 if (needleind.nelem() == 1 && needleind[0] == -1) {
90 needles = haystack;
91 return;
92 }
93
94 for (Index i = 0; i < needleind.nelem(); i++) {
95 if (haystack.nelem() <= needleind[i]) {
96 ostringstream os;
97 os << "The input vector only has " << haystack.nelem()
98 << " elements. But one of the needle indexes is " << needleind[i]
99 << "." << endl;
100 os << "The indexes must be between 0 and " << haystack.nelem() - 1;
101 throw runtime_error(os.str());
102 } else if (needleind[i] < 0) {
103 ostringstream os;
104 os << "One of the needle indexes is " << needleind[i] << "." << endl;
105 os << "The indexes must be between 0 and " << haystack.nelem() - 1;
106 throw runtime_error(os.str());
107 } else
108 dummy[i] = haystack[needleind[i]];
109 }
110
111 needles = dummy;
112}
113
114/* Workspace method: Doxygen documentation will be auto-generated */
115inline void Select(Workspace& /* ws */,
116 // WS Generic Output:
117 ArrayOfAgenda& needles,
118 // WS Generic Input:
119 const ArrayOfAgenda& haystack,
120 const ArrayOfIndex& needleind,
121 const Verbosity& verbosity) {
122 Select(needles, haystack, needleind, verbosity);
123}
124
125/* Workspace method: Doxygen documentation will be auto-generated */
126inline void Select( // WS Generic Output:
127 Vector& needles,
128 // WS Generic Input:
129 const Vector& haystack,
130 const ArrayOfIndex& needleind,
131 const Verbosity&) {
132 // We construct the output in this dummy variable, so that the
133 // method also works properly if needles and haystack are the same
134 // variable.
135 Vector dummy(needleind.nelem());
136
137 // If needleind only contains -1 as the only element, copy the whole thing
138 if (needleind.nelem() == 1 && needleind[0] == -1) {
139 needles = haystack;
140 return;
141 }
142
143 for (Index i = 0; i < needleind.nelem(); i++) {
144 if (haystack.nelem() <= needleind[i]) {
145 ostringstream os;
146 os << "The input vector only has " << haystack.nelem()
147 << " elements. But one of the needle indexes is " << needleind[i]
148 << "." << endl;
149 os << "The indexes must be between 0 and " << haystack.nelem() - 1;
150 throw runtime_error(os.str());
151 } else if (needleind[i] < 0) {
152 ostringstream os;
153 os << "One of the needle indexes is " << needleind[i] << "." << endl;
154 os << "The indexes must be between 0 and " << haystack.nelem() - 1;
155 throw runtime_error(os.str());
156 } else
157 dummy[i] = haystack[needleind[i]];
158 }
159
160 needles = dummy;
161}
162
163/* Workspace method: Doxygen documentation will be auto-generated */
164inline void Select( // WS Generic Output:
165 Matrix& needles,
166 // WS Generic Input:
167 const Matrix& haystack,
168 const ArrayOfIndex& needleind,
169 const Verbosity&) {
170 // We construct the output in this dummy variable, so that the
171 // method also works properly if needles and haystack are the same
172 // variable.
173 Matrix dummy(needleind.nelem(), haystack.ncols());
174
175 // If needleind only contains -1 as the only element, copy the whole thing
176 if (needleind.nelem() == 1 && needleind[0] == -1) {
177 needles = haystack;
178 return;
179 }
180
181 for (Index i = 0; i < needleind.nelem(); i++) {
182 if (haystack.nrows() <= needleind[i]) {
183 ostringstream os;
184 os << "The input matrix only has " << haystack.nrows()
185 << " rows. But one of the needle indexes is " << needleind[i] << "."
186 << endl;
187 os << "The indexes must be between 0 and " << haystack.nrows() - 1;
188 throw runtime_error(os.str());
189 } else if (needleind[i] < 0) {
190 ostringstream os;
191 os << "One of the needle indexes is " << needleind[i] << "." << endl;
192 os << "The indexes must be between 0 and " << haystack.nrows() - 1;
193 throw runtime_error(os.str());
194 } else
195 dummy(i, joker) = haystack(needleind[i], joker);
196 }
197
198 needles = dummy;
199}
200
201/* Workspace method: Doxygen documentation will be auto-generated */
202inline void Select( // WS Generic Output:
203 Sparse& needles,
204 // WS Generic Input:
205 const Sparse& haystack,
206 const ArrayOfIndex& needleind,
207 const Verbosity& verbosity) {
209
210 // We construct the output in this dummy variable, so that the
211 // method also works properly if needles and haystack are the same
212 // variable.
213 Sparse dummy(needleind.nelem(), haystack.ncols());
214
215 // If needleind only contains -1 as the only element, copy the whole thing
216 if (needleind.nelem() == 1 && needleind[0] == -1) {
217 needles = haystack;
218 return;
219 }
220
221 for (Index i = 0; i < needleind.nelem(); i++) {
222 if (haystack.nrows() <= needleind[i]) {
223 ostringstream os;
224 os << "The input matrix only has " << haystack.nrows()
225 << " rows. But one of the needle indexes is " << needleind[i] << "."
226 << endl;
227 os << "The indexes must be between 0 and " << haystack.nrows() - 1;
228 throw runtime_error(os.str());
229 } else if (needleind[i] < 0) {
230 ostringstream os;
231 os << "One of the needle indexes is " << needleind[i] << "." << endl;
232 os << "The indexes must be between 0 and " << haystack.nrows() - 1;
233 throw runtime_error(os.str());
234 } else {
235 // Copy this row of the sparse matrix.
236 // This code is inefficient for Sparse, but I leave it like
237 // this to be consistent with the other data types for which
238 // Select is implemented.
239 for (Index j = 0; j < haystack.ncols(); ++j) {
240 Numeric value = haystack(needleind[i], j);
241 if (0 != value) dummy.rw(i, j) = value;
242 }
243 }
244 }
245
246 if (dummy.nnz() == haystack.nnz()) {
247 // No data was actually removed.
248 out3 << " Number of nonzero elements has stayed the same.\n";
249 } else {
250 out3 << " Number of nonzero elements reduced from " << haystack.nnz()
251 << " to " << dummy.nnz() << ".\n";
252 }
253
254 needles = dummy;
255}
256
257#endif // m_select_h
Declarations for agendas.
This can be used to make arrays out of anything.
Definition: array.h:48
Index nelem() const ARTS_NOEXCEPT
Definition: array.h:92
Index nrows() const noexcept
Definition: matpackI.h:1055
Index ncols() const noexcept
Definition: matpackI.h:1056
Index nelem() const noexcept
Returns the number of elements.
Definition: matpackI.h:536
The Matrix class.
Definition: matpackI.h:1261
The Vector class.
Definition: matpackI.h:899
Workspace class.
Definition: workspace_ng.h:53
void Select(Array< T > &needles, const Array< T > &haystack, const ArrayOfIndex &needleind, const Verbosity &)
Definition: m_select.h:39
Header file for sparse matrices.
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
const Joker joker
Declarations having to do with the four output streams.
#define CREATE_OUT3
Definition: messages.h:206
This file contains the definition of String, the ARTS string class.
The Sparse class.
Definition: matpackII.h:75
Index nnz() const
Returns the number of nonzero elements.
Definition: matpackII.cc:68
Numeric & rw(Index r, Index c)
Read and write index operator.
Definition: matpackII.cc:87
Index nrows() const
Returns the number of rows.
Definition: matpackII.cc:62
Index ncols() const
Returns the number of columns.
Definition: matpackII.cc:65
This file contains the Workspace class.