casacore
Loading...
Searching...
No Matches
MArray.h
Go to the documentation of this file.
1//# MArray.h: Class to handle an Array with an optional mask
2//# Copyright (C) 2012
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_MARRAY_H
27#define CASA_MARRAY_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/tables/TaQL/MArrayBase.h>
32
33namespace casacore { //# NAMESPACE CASACORE - BEGIN
34
35 // <summary>
36 // Class to handle an Array with an optional mask
37 // </summary>
38
39 // <use visibility=local>
40
41 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
42 // </reviewed>
43
44 // <prerequisite>
45 //# Classes you should understand before using this one.
46 // <li> <linkto class=Array>Array</linkto>
47 // <li> <linkto class=MArrayBase>MArrayBase</linkto>
48 // </prerequisite>
49
50 // <synopsis>
51 // This class makes it easier to handle arrays with ot without mask.
52 // The array is always present, but the mask is optional. The mask is
53 // contained in the non-templated base class MArrayBase and functions
54 // to operate on the mask are defined there.
55 // <br> The class is primarily developed for TaQL masked arrays, but
56 // could be used elsewhere as well.
57 //
58 // A mask value True means that the corresponding value is masked off, thus
59 // not taken into account in reduction functions like <src>sum</src>. This
60 // is the same as the numpy masked array.
61 //
62 // MArrayMath.h contains many functions to operate on MArray objects
63 // (addition, sin, etc.).
64 // </synopsis>
65
66 template <typename T>
67 class MArray: public MArrayBase
68 {
69 public:
70 // Default constructor creates a null array.
73 {}
74
75 // Construct from an array without a mask.
76 // It references the given array.
77 explicit MArray (const Array<T>& array)
78 : MArrayBase (False),
80 {
82 }
83
84 // Construct from an array and a mask.
85 // It references the given arrays.
86 // <src>isNull=True</src> requires the arrays to be empty.
91
92 // Construct from an array with the mask and null from another MArray.
93 // It references the given arrays.
94 // The shapes of both arrays must match.
98 {}
99
100 // Construct from two MArrays, one the array, the other the mask.
101 // If one of them is null, the constructed MArray is null.
104 {
105 if (! isNull()) {
106 itsArray.reference (array.array());
107 setBase (itsArray, mask.array());
108 }
109 }
110
111 // Reference another array.
112 void reference (const MArray<T>& other)
113 {
114 itsArray.reference (other.itsArray);
115 referenceBase (other);
116 }
117
118 // Resize the array and optionally the mask.
119 // It always sets the MArray to non-null.
120 void resize (const IPosition& shape, Bool useMask)
121 {
122 itsArray.resize (shape);
123 resizeBase (itsArray, useMask);
124 }
125
126 // Copy the array data and possible mask from another one.
127 // The shapes do not need to match.
128 // The array data is copied, but the new mask references the possible
129 // mask in <src>from</src>.
130 template <typename U>
131 void fill (const MArray<U>& from)
132 {
133 itsArray.resize (from.shape());
134 convertArray (itsArray, from.array());
135 setBase (itsArray, from.mask());
136 }
137
138 // Copy the array from a normal Array. The possible mask is removed.
139 // The shapes do not need to match.
140 // The array data is always copied.
141 template <typename U>
142 void fill (const Array<U>& from)
143 {
144 itsArray.resize (from.shape());
145 convertArray (itsArray, from);
147 }
148
149 // Get access to the array.
150 // <group>
151 const Array<T>& array() const
152 { return itsArray; }
154 { return itsArray; }
155 // </group>
156
157 // Flatten the unmasked elements of the array to a vector.
159 // Copy the unmasked elements to the out. The argument <src>size</src>
160 // gives the size of the output buffer which should be at least the
161 // size of the array. It returns the nr of unmasked elements.
162 size_t flatten (T* out, size_t size) const;
163
164 // Get a subset of the array.
165 MArray<T> operator() (const IPosition& start, const IPosition& end,
166 const IPosition& stride)
167 {
168 if (hasMask()) {
169 return MArray<T> (itsArray(start, end, stride),
170 mask()(start, end, stride));
171 }
172 return MArray<T> (itsArray(start, end, stride));
173 }
174
175 private:
177 };
178
179
180 //# Implement functions.
181 template<typename T>
183 {
184 Vector<T> vec(nvalid());
185 // We lie about the size, because we know the buffer has the right size.
186 flatten (vec.data(), itsArray.size());
187 return vec;
188 }
189
190 template<typename T>
191 size_t MArray<T>::flatten (T* out, size_t size) const
192 {
193 if (size < itsArray.size()) {
194 throw ArrayError ("MArray::flatten - size " + std::to_string(size) +
195 " of output buffer is too small");
196 }
197 size_t nr = 0;
198 if (!hasMask()) {
199 // No mask, so copy all elements.
200 Array<T> arr(itsArray.shape(), out, SHARE);
201 arr = itsArray;
202 nr = arr.size();
203 } else {
204 // Copy only the valid elements.
205 if (itsArray.contiguousStorage() && mask().contiguousStorage()) {
206 typename Array<Bool>::const_contiter miter = mask().cbegin();
207 typename Array<T>::const_contiter iterEnd = itsArray.cend();
208 for (typename Array<T>::const_contiter iter=itsArray.cbegin();
209 iter!=iterEnd; ++iter, ++miter) {
210 if (!*miter) out[nr++] = *iter;
211 }
212 } else {
213 typename Array<Bool>::const_iterator miter = mask().begin();
214 typename Array<T>::const_iterator iterEnd = itsArray.end();
215 for (typename Array<T>::const_iterator iter=itsArray.begin();
216 iter!=iterEnd; ++iter, ++miter) {
217 if (!*miter) out[nr++] = *iter;
218 }
219 }
220 }
221 return nr;
222 }
223
224
225} //# NAMESPACE CASACORE - END
226
227#endif
size_t size() const
Definition ArrayBase.h:103
const IPosition & shape() const
The length of each axis.
Definition ArrayBase.h:123
T * data()
Get a pointer to the beginning of the array.
Definition Array.h:592
const T * const_contiter
Definition Array.h:840
void setBase(const ArrayBase &arr, const Array< Bool > &mask)
Reference the mask and set the shape.
Bool isNull() const
Is the array null?
Definition MArrayBase.h:109
const Array< Bool > & mask() const
Get the mask.
Definition MArrayBase.h:124
void resizeBase(const ArrayBase &arr, Bool useMask)
Set the array shape and resize the mask.
size_t size() const
Get the size.
Definition MArrayBase.h:150
void referenceBase(const MArrayBase &other)
Reference another MArray.
Bool hasMask() const
Is there a mask?
Definition MArrayBase.h:117
const IPosition & shape() const
Get the shape.
Definition MArrayBase.h:145
MArray(const MArray< T > &array, const MArray< Bool > &mask)
Construct from two MArrays, one the array, the other the mask.
Definition MArray.h:102
Vector< T > flatten() const
Flatten the unmasked elements of the array to a vector.
Definition MArray.h:182
MArray(const Array< T > &array, const MArrayBase &marray)
Construct from an array with the mask and null from another MArray.
Definition MArray.h:95
void reference(const MArray< T > &other)
Reference another array.
Definition MArray.h:112
size_t flatten(T *out, size_t size) const
Copy the unmasked elements to the out.
Definition MArray.h:191
MArray< T > operator()(const IPosition &start, const IPosition &end, const IPosition &stride)
Get a subset of the array.
Definition MArray.h:165
const Array< T > & array() const
Get access to the array.
Definition MArray.h:151
void fill(const Array< U > &from)
Copy the array from a normal Array.
Definition MArray.h:142
void fill(const MArray< U > &from)
Copy the array data and possible mask from another one.
Definition MArray.h:131
Array< T > itsArray
Definition MArray.h:176
MArray(const Array< T > &array)
Construct from an array without a mask.
Definition MArray.h:77
void resize(const IPosition &shape, Bool useMask)
Resize the array and optionally the mask.
Definition MArray.h:120
MArray()
Default constructor creates a null array.
Definition MArray.h:71
Array< T > & array()
Definition MArray.h:153
MArray(const Array< T > &array, const Array< Bool > &mask, Bool isNull=False)
Construct from an array and a mask.
Definition MArray.h:87
@ SHARE
Share means that the Array will just use the pointer (no copy), however the Array will NOT delete it ...
Definition ArrayBase.h:60
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
TableExprNode marray(const TableExprNode &array, const TableExprNode &mask)
Form a masked array.
Definition ExprNode.h:1939
const Bool True
Definition aipstype.h:41