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
66template <typename T>
67class MArray : public MArrayBase {
68 public:
69 // Default constructor creates a null array.
71
72 // Construct from an array without a mask.
73 // It references the given array.
77
78 // Construct from an array and a mask.
79 // It references the given arrays.
80 // <src>isNull=True</src> requires the arrays to be empty.
83
84 // Construct from an array with the mask and null from another MArray.
85 // It references the given arrays.
86 // The shapes of both arrays must match.
89
90 // Construct from two MArrays, one the array, the other the mask.
91 // If one of them is null, the constructed MArray is null.
94 if (!isNull()) {
95 itsArray.reference(array.array());
96 setBase(itsArray, mask.array());
97 }
98 }
99
100 // Reference another array.
101 void reference(const MArray<T>& other) {
102 itsArray.reference(other.itsArray);
103 referenceBase(other);
104 }
105
106 // Resize the array and optionally the mask.
107 // It always sets the MArray to non-null.
108 void resize(const IPosition& shape, Bool useMask) {
109 itsArray.resize(shape);
110 resizeBase(itsArray, useMask);
111 }
112
113 // Copy the array data and possible mask from another one.
114 // The shapes do not need to match.
115 // The array data is copied, but the new mask references the possible
116 // mask in <src>from</src>.
117 template <typename U>
118 void fill(const MArray<U>& from) {
119 itsArray.resize(from.shape());
120 convertArray(itsArray, from.array());
121 setBase(itsArray, from.mask());
122 }
123
124 // Copy the array from a normal Array. The possible mask is removed.
125 // The shapes do not need to match.
126 // The array data is always copied.
127 template <typename U>
128 void fill(const Array<U>& from) {
129 itsArray.resize(from.shape());
130 convertArray(itsArray, from);
132 }
133
134 // Get access to the array.
135 // <group>
136 const Array<T>& array() const { return itsArray; }
137 Array<T>& array() { return itsArray; }
138 // </group>
139
140 // Flatten the unmasked elements of the array to a vector.
142 // Copy the unmasked elements to the out. The argument <src>size</src>
143 // gives the size of the output buffer which should be at least the
144 // size of the array. It returns the nr of unmasked elements.
145 size_t flatten(T* out, size_t size) const;
146
147 // Get a subset of the array.
148 MArray<T> operator()(const IPosition& start, const IPosition& end, const IPosition& stride) {
149 if (hasMask()) {
150 return MArray<T>(itsArray(start, end, stride), mask()(start, end, stride));
151 }
152 return MArray<T>(itsArray(start, end, stride));
153 }
154
155 private:
157};
158
159// # Implement functions.
160template <typename T>
162 Vector<T> vec(nvalid());
163 // We lie about the size, because we know the buffer has the right size.
164 flatten(vec.data(), itsArray.size());
165 return vec;
166}
167
168template <typename T>
169size_t MArray<T>::flatten(T* out, size_t size) const {
170 if (size < itsArray.size()) {
171 throw ArrayError("MArray::flatten - size " + std::to_string(size) +
172 " of output buffer is too small");
173 }
174 size_t nr = 0;
175 if (!hasMask()) {
176 // No mask, so copy all elements.
177 Array<T> arr(itsArray.shape(), out, SHARE);
178 arr = itsArray;
179 nr = arr.size();
180 } else {
181 // Copy only the valid elements.
182 if (itsArray.contiguousStorage() && mask().contiguousStorage()) {
183 typename Array<Bool>::const_contiter miter = mask().cbegin();
184 typename Array<T>::const_contiter iterEnd = itsArray.cend();
185 for (typename Array<T>::const_contiter iter = itsArray.cbegin(); iter != iterEnd;
186 ++iter, ++miter) {
187 if (!*miter) out[nr++] = *iter;
188 }
189 } else {
190 typename Array<Bool>::const_iterator miter = mask().begin();
191 typename Array<T>::const_iterator iterEnd = itsArray.end();
192 for (typename Array<T>::const_iterator iter = itsArray.begin(); iter != iterEnd;
193 ++iter, ++miter) {
194 if (!*miter) out[nr++] = *iter;
195 }
196 }
197 }
198 return nr;
199}
200
201} // namespace casacore
202
203#endif
size_t size() const
Definition ArrayBase.h:99
const IPosition & shape() const
The length of each axis.
Definition ArrayBase.h:116
T * data()
Get a pointer to the beginning of the array.
Definition Array.h:582
const T * const_contiter
Definition Array.h:810
ConstIteratorSTL const_iterator
Definition Array.h:808
MArrayBase(Bool isNull)
The default constructor creates an empty mask.
Definition MArrayBase.h:84
Int64 nvalid() const
Return the number of valid array values, thus unflagged elements.
Definition MArrayBase.h:122
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:103
const Array< Bool > & mask() const
Get the mask.
Definition MArrayBase.h:118
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:138
void referenceBase(const MArrayBase &other)
Reference another MArray.
Bool hasMask() const
Is there a mask?
Definition MArrayBase.h:112
const IPosition & shape() const
Get the shape.
Definition MArrayBase.h:134
MArray(const MArray< T > &array, const MArray< Bool > &mask)
Construct from two MArrays, one the array, the other the mask.
Definition MArray.h:92
Vector< T > flatten() const
Flatten the unmasked elements of the array to a vector.
Definition MArray.h:161
MArray(const Array< T > &array, const MArrayBase &marray)
Construct from an array with the mask and null from another MArray.
Definition MArray.h:87
void reference(const MArray< T > &other)
Reference another array.
Definition MArray.h:101
MArray< T > operator()(const IPosition &start, const IPosition &end, const IPosition &stride)
Get a subset of the array.
Definition MArray.h:148
const Array< T > & array() const
Get access to the array.
Definition MArray.h:136
void fill(const Array< U > &from)
Copy the array from a normal Array.
Definition MArray.h:128
void fill(const MArray< U > &from)
Copy the array data and possible mask from another one.
Definition MArray.h:118
Array< T > itsArray
Definition MArray.h:156
MArray(const Array< T > &array)
Construct from an array without a mask.
Definition MArray.h:74
void resize(const IPosition &shape, Bool useMask)
Resize the array and optionally the mask.
Definition MArray.h:108
MArray()
Default constructor creates a null array.
Definition MArray.h:70
Array< T > & array()
Definition MArray.h:137
MArray(const Array< T > &array, const Array< Bool > &mask, Bool isNull=False)
Construct from an array and a mask.
Definition MArray.h:81
@ SHARE
Share means that the Array will just use the pointer (no copy), however the Array will NOT delete it ...
Definition ArrayBase.h:59
For temporary backward namespace compatibility, use casa as alias for casacore.
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:1567
const Bool True
Definition aipstype.h:41
iterator end()
Definition Block.h:601