casacore
Loading...
Searching...
No Matches
MArrayBase.h
Go to the documentation of this file.
1// # MArrayBase.h: Base class for 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_MARRAYBASE_H
27#define CASA_MARRAYBASE_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Arrays/Array.h>
32#include <casacore/casa/Arrays/ArrayLogical.h>
33
34// # Define the mask value indicating a valid value.
35// # In this way it is easy to change it to another value (if ever needed).
36// # The current setting is the same as used in numpy's masked_array and
37// # in the MeasurementSet's FLAG column.
38// # But the opposite value sounds somewhat better (same as MaskedArray)
39// # because something like DATA[isnan(DATA)] = 0 is much more intuitive.
40// # #define MArrayValid False
41// # #define MArrayInvalid True
42
43namespace casacore { // # NAMESPACE CASACORE - BEGIN
44
45// <summary>
46// Base class for an array with an optional mask
47// </summary>
48
49// <use visibility=local>
50
51// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
52// </reviewed>
53
54// <prerequisite>
55// # Classes you should understand before using this one.
56// <li> <linkto class=Array>Array</linkto>
57// </prerequisite>
58
59// <synopsis>
60// This class is the base class of the templated class MArray. It contains
61// the functions that are not template dependent.
62//
63// MArray is developed to make it easier to handle arrays with an
64// optional mask. The array is always present, but the mask is optional.
65// MArrayMath contains functions to operate on such arrays.
66//
67// Similar to numpy.masked_array and the MeasurementSet FLAG definition,
68// a mask value True means that the corresponding value is masked off,
69// thus is not taken into account in reduction functions like <src>sum</src>.
70// on a masked array. In operations like addition, masked off values are
71// processed because testing the mask value is more expensive than an
72// addition (even if the value is a NaN). For an operation with multiple
73// operands, the mask of the result is the OR of the operand masks.
74//
75// MArray can be null meaning that the array is a null value. It can be
76// used to indicate that a table cell does not contain an array.
77// A null MArray has an empty array and mask. Operations where an operand
78// is a null MArray, result in a null MArray.
79// </synopsis>
80
82 protected:
83 // The default constructor creates an empty mask.
85
86 // Construct from a given array shape and mask.
88
89 // Construct from a given array shape and mask from another MArray.
91
92 // Reference the mask and set the shape.
93 void setBase(const ArrayBase& arr, const Array<Bool>& mask);
94
95 // Reference another MArray.
96 void referenceBase(const MArrayBase& other);
97
98 // Set the array shape and resize the mask.
99 void resizeBase(const ArrayBase& arr, Bool useMask);
100
101 public:
102 // Is the array null?
103 Bool isNull() const { return itsNull; }
104
105 // Remove the mask.
106 void removeMask() {
107 itsMask.resize();
109 }
110
111 // Is there a mask?
112 Bool hasMask() const { return !itsMask.empty(); }
113
114 // Set the mask. It checks if it matches the array shape.
116
117 // Get the mask. The returned array is empty if there is no mask.
118 const Array<Bool>& mask() const { return itsMask; }
119 Array<Bool>& wmask() { return itsMask; }
120
121 // Return the number of valid array values, thus unflagged elements.
122 Int64 nvalid() const {
123 if (itsNValid < 0) fillNValid();
124 return itsNValid;
125 }
126
127 // Is the array empty?
128 Bool empty() const { return itsSize == 0; }
129
130 // Get the dimensionality.
131 uInt ndim() const { return itsShape.size(); }
132
133 // Get the shape.
134 const IPosition& shape() const { return itsShape; }
135
136 // Get the size.
137 // <group>
138 size_t size() const { return itsSize; }
139 size_t nelements() const { return itsSize; }
140 // </group>
141
142 // Combine this and the other mask.
143 // One or both MArray-s can be unmasked.
144 Array<Bool> combineMask(const MArrayBase& other) const;
145
146 private:
147 // Initialize and check.
148 void init();
149
150 // Fill the number of valid values.
151 void fillNValid() const;
152
153 // # Data members.
156 size_t itsSize;
158 Bool itsNull; // True = array is null, thus undefined in a column
159};
160
161} // namespace casacore
162
163#endif
Non-templated base class for templated Array class.
Definition ArrayBase.h:69
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
Bool empty() const
Is the array empty?
Definition MArrayBase.h:128
Array< Bool > combineMask(const MArrayBase &other) const
Combine this and the other mask.
void setBase(const ArrayBase &arr, const Array< Bool > &mask)
Reference the mask and set the shape.
size_t nelements() const
Definition MArrayBase.h:139
Bool isNull() const
Is the array null?
Definition MArrayBase.h:103
MArrayBase(const ArrayBase &arr, const Array< Bool > &mask, Bool isNull)
Construct from a given array shape and mask.
void init()
Initialize and check.
void setMask(const Array< Bool > &mask)
Set the mask.
uInt ndim() const
Get the dimensionality.
Definition MArrayBase.h:131
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.
Array< Bool > & wmask()
Definition MArrayBase.h:119
MArrayBase(const ArrayBase &arr, const MArrayBase &marray)
Construct from a given array shape and mask from another MArray.
Array< Bool > itsMask
Definition MArrayBase.h:154
void fillNValid() const
Fill the number of valid values.
size_t size() const
Get the size.
Definition MArrayBase.h:138
void referenceBase(const MArrayBase &other)
Reference another MArray.
void removeMask()
Remove the mask.
Definition MArrayBase.h:106
Bool hasMask() const
Is there a mask?
Definition MArrayBase.h:112
const IPosition & shape() const
Get the shape.
Definition MArrayBase.h:134
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
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