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
43
44namespace casacore { //# NAMESPACE CASACORE - BEGIN
45
46 // <summary>
47 // Base class for an array with an optional mask
48 // </summary>
49
50 // <use visibility=local>
51
52 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
53 // </reviewed>
54
55 // <prerequisite>
56 //# Classes you should understand before using this one.
57 // <li> <linkto class=Array>Array</linkto>
58 // </prerequisite>
59
60 // <synopsis>
61 // This class is the base class of the templated class MArray. It contains
62 // the functions that are not template dependent.
63 //
64 // MArray is developed to make it easier to handle arrays with an
65 // optional mask. The array is always present, but the mask is optional.
66 // MArrayMath contains functions to operate on such arrays.
67 //
68 // Similar to numpy.masked_array and the MeasurementSet FLAG definition,
69 // a mask value True means that the corresponding value is masked off,
70 // thus is not taken into account in reduction functions like <src>sum</src>.
71 // on a masked array. In operations like addition, masked off values are
72 // processed because testing the mask value is more expensive than an
73 // addition (even if the value is a NaN). For an operation with multiple
74 // operands, the mask of the result is the OR of the operand masks.
75 //
76 // MArray can be null meaning that the array is a null value. It can be
77 // used to indicate that a table cell does not contain an array.
78 // A null MArray has an empty array and mask. Operations where an operand
79 // is a null MArray, result in a null MArray.
80 // </synopsis>
81
83 {
84 protected:
85 // The default constructor creates an empty mask.
87 : itsSize (0),
88 itsNValid (0),
90 {}
91
92 // Construct from a given array shape and mask.
94
95 // Construct from a given array shape and mask from another MArray.
96 MArrayBase (const ArrayBase& arr, const MArrayBase& marray);
97
98 // Reference the mask and set the shape.
99 void setBase (const ArrayBase& arr, const Array<Bool>& mask);
100
101 // Reference another MArray.
102 void referenceBase (const MArrayBase& other);
103
104 // Set the array shape and resize the mask.
105 void resizeBase (const ArrayBase& arr, Bool useMask);
106
107 public:
108 // Is the array null?
109 Bool isNull() const
110 { return itsNull; }
111
112 // Remove the mask.
115
116 // Is there a mask?
117 Bool hasMask() const
118 { return !itsMask.empty(); }
119
120 // Set the mask. It checks if it matches the array shape.
121 void setMask (const Array<Bool>& mask);
122
123 // Get the mask. The returned array is empty if there is no mask.
124 const Array<Bool>& mask() const
125 { return itsMask; }
127 { return itsMask; }
128
129 // Return the number of valid array values, thus unflagged elements.
130 Int64 nvalid() const
131 {
132 if (itsNValid < 0) fillNValid();
133 return itsNValid;
134 }
135
136 // Is the array empty?
137 Bool empty() const
138 { return itsSize == 0; }
139
140 // Get the dimensionality.
141 uInt ndim() const
142 { return itsShape.size(); }
143
144 // Get the shape.
145 const IPosition& shape() const
146 { return itsShape; }
147
148 // Get the size.
149 // <group>
150 size_t size() const
151 { return itsSize; }
152 size_t nelements() const
153 { return itsSize; }
154 // </group>
155
156 // Combine this and the other mask.
157 // One or both MArray-s can be unmasked.
158 Array<Bool> combineMask (const MArrayBase& other) const;
159
160 private:
161 // Initialize and check.
162 void init();
163
164 // Fill the number of valid values.
165 void fillNValid() const;
166
167 //# Data members.
170 size_t itsSize;
172 Bool itsNull; // True = array is null, thus undefined in a column
173 };
174
175} //# NAMESPACE CASACORE - END
176
177#endif
Non-templated base class for templated Array class.
Definition ArrayBase.h:71
bool empty() const
Is the array empty (i.e.
Definition ArrayBase.h:108
void resize()
Make this array a different shape.
size_t size() const
Definition IPosition.h:570
MArrayBase(Bool isNull)
The default constructor creates an empty mask.
Definition MArrayBase.h:86
Int64 nvalid() const
Return the number of valid array values, thus unflagged elements.
Definition MArrayBase.h:130
Bool empty() const
Is the array empty?
Definition MArrayBase.h:137
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:152
Bool isNull() const
Is the array null?
Definition MArrayBase.h:109
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:141
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.
Array< Bool > & wmask()
Definition MArrayBase.h:126
MArrayBase(const ArrayBase &arr, const MArrayBase &marray)
Construct from a given array shape and mask from another MArray.
Array< Bool > itsMask
Definition MArrayBase.h:168
void fillNValid() const
Fill the number of valid values.
size_t size() const
Get the size.
Definition MArrayBase.h:150
void referenceBase(const MArrayBase &other)
Reference another MArray.
void removeMask()
Remove the mask.
Definition MArrayBase.h:113
Bool hasMask() const
Is there a mask?
Definition MArrayBase.h:117
const IPosition & shape() const
Get the shape.
Definition MArrayBase.h:145
this file contains all the compiler specific defines
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:1939