ARTS  2.4.0(git:4fb77825)
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 */
38 template <class T>
39 void 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 */
77 inline void Select(Workspace& /* ws */,
78  // WS Generic Output:
79  ArrayOfAgenda& needles,
80  // WS Generic Input:
81  const ArrayOfAgenda& haystack,
82  const ArrayOfIndex& needleind,
83  const Verbosity& verbosity) {
84  Select(needles, haystack, needleind, verbosity);
85 }
86 
87 /* Workspace method: Doxygen documentation will be auto-generated */
88 inline void Select( // WS Generic Output:
89  Vector& needles,
90  // WS Generic Input:
91  const Vector& haystack,
92  const ArrayOfIndex& needleind,
93  const Verbosity&) {
94  // We construct the output in this dummy variable, so that the
95  // method also works properly if needles and haystack are the same
96  // variable.
97  Vector dummy(needleind.nelem());
98 
99  // If needleind only contains -1 as the only element, copy the whole thing
100  if (needleind.nelem() == 1 && needleind[0] == -1) {
101  needles = haystack;
102  return;
103  }
104 
105  for (Index i = 0; i < needleind.nelem(); i++) {
106  if (haystack.nelem() <= needleind[i]) {
107  ostringstream os;
108  os << "The input vector only has " << haystack.nelem()
109  << " elements. But one of the needle indexes is " << needleind[i]
110  << "." << endl;
111  os << "The indexes must be between 0 and " << haystack.nelem() - 1;
112  throw runtime_error(os.str());
113  } else if (needleind[i] < 0) {
114  ostringstream os;
115  os << "One of the needle indexes is " << needleind[i] << "." << endl;
116  os << "The indexes must be between 0 and " << haystack.nelem() - 1;
117  throw runtime_error(os.str());
118  } else
119  dummy[i] = haystack[needleind[i]];
120  }
121 
122  needles = dummy;
123 }
124 
125 /* Workspace method: Doxygen documentation will be auto-generated */
126 inline void Select( // WS Generic Output:
127  Matrix& needles,
128  // WS Generic Input:
129  const Matrix& 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  Matrix dummy(needleind.nelem(), haystack.ncols());
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.nrows() <= needleind[i]) {
145  ostringstream os;
146  os << "The input matrix only has " << haystack.nrows()
147  << " rows. But one of the needle indexes is " << needleind[i] << "."
148  << endl;
149  os << "The indexes must be between 0 and " << haystack.nrows() - 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.nrows() - 1;
155  throw runtime_error(os.str());
156  } else
157  dummy(i, joker) = haystack(needleind[i], joker);
158  }
159 
160  needles = dummy;
161 }
162 
163 /* Workspace method: Doxygen documentation will be auto-generated */
164 inline void Select( // WS Generic Output:
165  Sparse& needles,
166  // WS Generic Input:
167  const Sparse& haystack,
168  const ArrayOfIndex& needleind,
169  const Verbosity& verbosity) {
170  CREATE_OUT3;
171 
172  // We construct the output in this dummy variable, so that the
173  // method also works properly if needles and haystack are the same
174  // variable.
175  Sparse dummy(needleind.nelem(), haystack.ncols());
176 
177  // If needleind only contains -1 as the only element, copy the whole thing
178  if (needleind.nelem() == 1 && needleind[0] == -1) {
179  needles = haystack;
180  return;
181  }
182 
183  for (Index i = 0; i < needleind.nelem(); i++) {
184  if (haystack.nrows() <= needleind[i]) {
185  ostringstream os;
186  os << "The input matrix only has " << haystack.nrows()
187  << " rows. But one of the needle indexes is " << needleind[i] << "."
188  << endl;
189  os << "The indexes must be between 0 and " << haystack.nrows() - 1;
190  throw runtime_error(os.str());
191  } else if (needleind[i] < 0) {
192  ostringstream os;
193  os << "One of the needle indexes is " << needleind[i] << "." << endl;
194  os << "The indexes must be between 0 and " << haystack.nrows() - 1;
195  throw runtime_error(os.str());
196  } else {
197  // Copy this row of the sparse matrix.
198  // This code is inefficient for Sparse, but I leave it like
199  // this to be consistent with the other data types for which
200  // Select is implemented.
201  for (Index j = 0; j < haystack.ncols(); ++j) {
202  Numeric value = haystack(needleind[i], j);
203  if (0 != value) dummy.rw(i, j) = value;
204  }
205  }
206  }
207 
208  if (dummy.nnz() == haystack.nnz()) {
209  // No data was actually removed.
210  out3 << " Number of nonzero elements has stayed the same.\n";
211  } else {
212  out3 << " Number of nonzero elements reduced from " << haystack.nnz()
213  << " to " << dummy.nnz() << ".\n";
214  }
215 
216  needles = dummy;
217 }
218 
219 #endif // m_select_h
Matrix
The Matrix class.
Definition: matpackI.h:1193
Sparse::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackII.cc:69
joker
const Joker joker
ARTS::Var::verbosity
Verbosity verbosity(Workspace &ws) noexcept
Definition: autoarts.h:7112
Sparse
The Sparse class.
Definition: matpackII.h:60
ConstMatrixView::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackI.cc:429
Array
This can be used to make arrays out of anything.
Definition: array.h:108
agenda_class.h
Declarations for agendas.
CREATE_OUT3
#define CREATE_OUT3
Definition: messages.h:207
messages.h
Declarations having to do with the four output streams.
Sparse::nnz
Index nnz() const
Returns the number of nonzero elements.
Definition: matpackII.cc:72
ConstMatrixView::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackI.cc:432
ConstVectorView::nelem
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:51
Numeric
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
Verbosity
Definition: messages.h:49
workspace_ng.h
This file contains the Workspace class.
Sparse::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackII.cc:66
Sparse::rw
Numeric & rw(Index r, Index c)
Read and write index operator.
Definition: matpackII.cc:91
Workspace
Workspace class.
Definition: workspace_ng.h:40
matpackII.h
Header file for sparse matrices.
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
Select
void Select(Array< T > &needles, const Array< T > &haystack, const ArrayOfIndex &needleind, const Verbosity &)
Definition: m_select.h:39
Vector
The Vector class.
Definition: matpackI.h:860
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:195
mystring.h
This file contains the definition of String, the ARTS string class.