ARTS  2.2.66
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 "messages.h"
32 #include "mystring.h"
33 #include "workspace_ng.h"
34 #include "agenda_class.h"
35 #include "matpackII.h"
36 
37 
38 /* Workspace method: Doxygen documentation will be auto-generated */
39 template< class T >
40 void Select(// WS Generic Output:
41  Array<T>& needles,
42  // WS Generic Input:
43  const Array<T>& haystack,
44  const ArrayOfIndex& needleind,
45  const Verbosity&)
46 {
47  // We construct the output in this dummy variable, so that the
48  // method also works properly if needles and haystack are the same
49  // variable.
50  Array<T> dummy( needleind.nelem() );
51 
52  for( Index i = 0; i < needleind.nelem(); i++)
53  {
54  if (haystack.nelem() <= needleind[i])
55  {
56  ostringstream os;
57  os << "The input vector only has " << haystack.nelem()
58  << " elements. But one of the needle indexes is "
59  << needleind[i] << "." << endl;
60  os << "The indexes must be between 0 and " << haystack.nelem() - 1;
61  throw runtime_error (os.str());
62  }
63  else
64  dummy[i] = haystack[needleind[i]];
65  }
66 
67  needles = dummy;
68 }
69 
70 
71 /* Workspace method: Doxygen documentation will be auto-generated */
72 void Select(// WS Generic Output:
73  Vector& needles,
74  // WS Generic Input:
75  const Vector& haystack,
76  const ArrayOfIndex& needleind,
77  const Verbosity&)
78 {
79  // We construct the output in this dummy variable, so that the
80  // method also works properly if needles and haystack are the same
81  // variable.
82  Vector dummy( needleind.nelem() );
83 
84  for( Index i = 0; i < needleind.nelem(); i++)
85  {
86  if (haystack.nelem() <= needleind[i])
87  {
88  ostringstream os;
89  os << "The input vector only has " << haystack.nelem()
90  << " elements. But one of the needle indexes is "
91  << needleind[i] << "." << endl;
92  os << "The indexes must be between 0 and " << haystack.nelem() - 1;
93  throw runtime_error (os.str());
94  }
95  else
96  dummy[i] = haystack[needleind[i]];
97  }
98 
99  needles = dummy;
100 }
101 
102 
103 /* Workspace method: Doxygen documentation will be auto-generated */
104 void Select(// WS Generic Output:
105  Matrix& needles,
106  // WS Generic Input:
107  const Matrix& haystack,
108  const ArrayOfIndex& needleind,
109  const Verbosity&)
110 {
111  // We construct the output in this dummy variable, so that the
112  // method also works properly if needles and haystack are the same
113  // variable.
114  Matrix dummy( needleind.nelem(), haystack.ncols() );
115 
116  for( Index i = 0; i < needleind.nelem(); i++)
117  {
118  if (haystack.nrows() <= needleind[i])
119  {
120  ostringstream os;
121  os << "The input matrix only has " << haystack.nrows()
122  << " rows. But one of the needle indexes is "
123  << needleind[i] << "." << endl;
124  os << "The indexes must be between 0 and " << haystack.nrows() - 1;
125  throw runtime_error (os.str());
126  }
127  else
128  dummy(i, joker) = haystack(needleind[i], joker);
129  }
130 
131  needles = dummy;
132 }
133 
134 
135 /* Workspace method: Doxygen documentation will be auto-generated */
136 void Select(// WS Generic Output:
137  Sparse& needles,
138  // WS Generic Input:
139  const Sparse& haystack,
140  const ArrayOfIndex& needleind,
141  const Verbosity& verbosity)
142 {
143  CREATE_OUT3;
144 
145  // We construct the output in this dummy variable, so that the
146  // method also works properly if needles and haystack are the same
147  // variable.
148  Sparse dummy( needleind.nelem(), haystack.ncols() );
149 
150  for( Index i = 0; i < needleind.nelem(); i++)
151  {
152  if (haystack.nrows() <= needleind[i])
153  {
154  ostringstream os;
155  os << "The input matrix only has " << haystack.nrows()
156  << " rows. But one of the needle indexes is "
157  << needleind[i] << "." << endl;
158  os << "The indexes must be between 0 and " << haystack.nrows() - 1;
159  throw runtime_error (os.str());
160  }
161  else
162  {
163  // Copy this row of the sparse matrix.
164  // This code is inefficient for Sparse, but I leave it like
165  // this to be consistent with the other data types for which
166  // Select is implemented.
167  for ( Index j=0; j<haystack.ncols(); ++j)
168  {
169  Numeric value = haystack(needleind[i],j);
170  if (0 != value)
171  dummy.rw(i,j) = value;
172  }
173  }
174  }
175 
176  if (dummy.nnz()==haystack.nnz())
177  {
178  // No data was actually removed.
179  out3 << " Number of nonzero elements has stayed the same.\n";
180  }
181  else
182  {
183  out3 << " Number of nonzero elements reduced from "
184  << haystack.nnz() << " to " << dummy.nnz() << ".\n";
185  }
186 
187  needles = dummy;
188 }
189 
190 
191 #endif // m_select_h
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
Index nnz() const
Returns the number of nonzero elements.
Definition: matpackII.cc:68
Index nelem() const
Number of elements.
Definition: array.h:176
Declarations having to do with the four output streams.
The Vector class.
Definition: matpackI.h:556
The Sparse class.
Definition: matpackII.h:55
Index ncols() const
Returns the number of columns.
Definition: matpackII.cc:62
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:180
This file contains the declaration and partly the implementation of the workspace class...
Index ncols() const
Returns the number of columns.
Definition: matpackI.cc:838
Header file for sparse matrices.
Declarations for agendas.
Index nrows() const
Returns the number of rows.
Definition: matpackII.cc:56
const Joker joker
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:29
The Matrix class.
Definition: matpackI.h:788
This can be used to make arrays out of anything.
Definition: array.h:40
#define CREATE_OUT3
Definition: messages.h:214
Index nrows() const
Returns the number of rows.
Definition: matpackI.cc:832
This file contains the definition of String, the ARTS string class.
void Select(Array< T > &needles, const Array< T > &haystack, const ArrayOfIndex &needleind, const Verbosity &)
Definition: m_select.h:40