casacore
Loading...
Searching...
No Matches
ScaledArrayEngine.h
Go to the documentation of this file.
1// # ScaledArrayEngine.h: Templated virtual column engine to scale a table array
2// # Copyright (C) 1994,1995,1996,1999,2001
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 TABLES_SCALEDARRAYENGINE_H
27#define TABLES_SCALEDARRAYENGINE_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/tables/DataMan/BaseMappedArrayEngine.h>
32
33namespace casacore { // # NAMESPACE CASACORE - BEGIN
34
35// # Forward Declarations
36template <class T>
37class ScalarColumn;
38
39// <summary>
40// Templated virtual column engine to scale a table array
41// </summary>
42
43// <use visibility=export>
44
45// <reviewed reviewer="Gareth Hunt" date="94Nov17" tests="">
46// </reviewed>
47
48// <prerequisite>
49// # Classes you should understand before using this one.
50// <li> VirtualColumnEngine
51// <li> VirtualArrayColumn
52// </prerequisite>
53
54// <synopsis>
55// ScaledArrayEngine is a virtual column engine which scales an array
56// of one type to another type to save disk storage.
57// This resembles the classic AIPS compress method which scales the
58// data from float to short.
59// The scale factor and offset value can be given in two ways:
60// <ul>
61// <li> As a fixed value which is used for all arrays in the column.
62// <li> As the name of a column. In this way each array in a
63// column can have its own scale and offset value.
64// The scale and offset value in a row must be put before
65// the array is put and should not be changed anymore.
66// </ul>
67// It is also possible to have a variable scale factor with a fixed offset
68// value.
69// As in FITS the scale and offset values are used as:
70// <br><src> True_value = Stored_value * scale + offset; </src>
71//
72// An engine object should be used for one column only, because the stored
73// column name is part of the engine. If it would be used for more than
74// one column, they would all share the same stored column.
75// When the engine is bound to a column, it is checked if the name
76// of that column matches the given virtual column name.
77//
78// The engine can be used for a column containing any kind of array
79// (thus direct or indirect, fixed or variable shaped)) as long as the
80// virtual array can be stored in the stored array. Thus a fixed shaped
81// virtual can use a variable shaped stored, but not vice versa.
82// A fixed shape indirect virtual can use a stored with direct arrays.
83//
84// This class can also serve as an example of how to implement
85// a virtual column engine.
86// </synopsis>
87
88// <motivation>
89// This class allows to store data in a smaller representation.
90// It is needed to resemble the classic AIPS compress option.
91// It adds the scale and offset value on a per row basis.
92//
93// Because the engine can serve only one column, it was possible to
94// combine the engine and the column functionality in one class.
95// This has been achieved using multiple inheritance.
96// The advantage of this is that only one templated class is used,
97// so less template instantiations are needed.
98// </motivation>
99
100// <example>
101// <srcblock>
102// // Create the table description and 2 columns with indirect arrays in it.
103// // The Int column will be stored, while the double will be
104// // used as virtual.
105// TableDesc tableDesc ("", TableDesc::Scratch);
106// tableDesc.addColumn (ArrayColumnDesc<Int> ("storedArray"));
107// tableDesc.addColumn (ArrayColumnDesc<double> ("virtualArray"));
108//
109// // Create a new table using the table description.
110// SetupNewTable newtab (tableDesc, "tab.data", Table::New);
111//
112// // Create the array scaling engine to scale from double to Int
113// // and bind it to the double column.
114// // Create the table.
115// ScaledArrayEngine<double,Int> scalingEngine("virtualArray",
116// "storedArray", 10);
117// newtab.bindColumn ("virtualArray", scalingEngine);
118// Table table (newtab);
119//
120// // Store a 3-D array (with dim. 2,3,4) into each row of the column.
121// // The shape of each array in the column is implicitly set by the put
122// // function. This will also set the shape of the underlying Int array.
123// ArrayColumn data (table, "virtualArray");
124// Array<double> someArray(IPosition(4,2,3,4));
125// someArray = 0;
126// for (rownr_t i=0, i<10; i++) { // table will have 10 rows
127// table.addRow();
128// data.put (i, someArray)
129// }
130// </srcblock>
131// </example>
132
133// <templating arg=VirtualType>
134// <li> only suited for built-in numerics data types
135// </templating>
136// <templating arg=StoredType>
137// <li> only suited for built-in numerics data types
138// </templating>
139
140template <class VirtualType, class StoredType>
141class ScaledArrayEngine : public BaseMappedArrayEngine<VirtualType, StoredType> {
142 // # Make members of parent class known.
143 public:
144 using BaseMappedArrayEngine<VirtualType, StoredType>::virtualName;
145
146 protected:
147 using BaseMappedArrayEngine<VirtualType, StoredType>::storedName;
148 using BaseMappedArrayEngine<VirtualType, StoredType>::table;
149 using BaseMappedArrayEngine<VirtualType, StoredType>::column;
150 using BaseMappedArrayEngine<VirtualType, StoredType>::setNames;
151
152 public:
153 // Construct an engine to scale all arrays in a column with
154 // the given offset and scale factor.
155 // StoredColumnName is the name of the column where the scaled
156 // data will be put and must have data type StoredType.
157 // The virtual column using this engine must have data type VirtualType.
158 ScaledArrayEngine(const String& virtualColumnName, const String& storedColumnName,
159 VirtualType scale, VirtualType offset = 0);
160
161 // Construct an engine to scale the arrays in a column.
162 // The scale and offset values are taken from a column with
163 // the given names. In that way each array has its own scale factor
164 // and offset value.
165 // An exception is thrown if these columns do not exist.
166 // VirtualColumnName is the name of the virtual column and is used to
167 // check if the engine gets bound to the correct column.
168 // StoredColumnName is the name of the column where the scaled
169 // data will be put and must have data type StoredType.
170 // The virtual column using this engine must have data type VirtualType.
171 // <group>
172 ScaledArrayEngine(const String& virtualColumnName, const String& storedColumnName,
173 const String& scaleColumnName, VirtualType offset = 0);
174 ScaledArrayEngine(const String& virtualColumnName, const String& storedColumnName,
175 const String& scaleColumnName, const String& offsetColumnName);
176 // </group>
177
178 // Construct from a record specification as created by getmanagerSpec().
180
181 // Destructor is mandatory.
183
184 // Assignment is not needed and therefore forbidden.
187
188 // Return the type name of the engine (i.e. its class name).
189 virtual String dataManagerType() const;
190
191 // Get the name given to the engine (is the virtual column name).
192 virtual String dataManagerName() const;
193
194 // Record a record containing data manager specifications.
195 virtual Record dataManagerSpec() const;
196
197 // Return the name of the class.
198 // This includes the names of the template arguments.
200
201 // Register the class name and the static makeObject "constructor".
202 // This will make the engine known to the table system.
203 // The automatically invoked registration function in DataManReg.cc
204 // contains ScaledArrayEngine<double,Int>.
205 // Any other instantiation of this class must be registered "manually"
206 // (or added to DataManReg.cc).
207 static void registerClass();
208
209 private:
210 // Copy constructor is only used by clone().
211 // (so it is made private).
213
214 // Clone the engine object.
216
217 // Initialize the object for a new table.
218 // It defines the keywords containing the engine parameters.
219 void create64(rownr_t initialNrrow);
220
221 // Preparing consists of setting the writable switch and
222 // adding the initial number of rows in case of create.
223 // Furthermore it reads the keywords containing the engine parameters.
224 void prepare();
225
226 // Get an array in the given row.
227 // This will scale and offset from the underlying array.
229
230 // Put an array in the given row.
231 // This will scale and offset to the underlying array.
233
234 // Get a section of the array in the given row.
235 // This will scale and offset from the underlying array.
236 void getSlice(rownr_t rownr, const Slicer& slicer, Array<VirtualType>& array);
237
238 // Put into a section of the array in the given row.
239 // This will scale and offset to the underlying array.
240 void putSlice(rownr_t rownr, const Slicer& slicer, const Array<VirtualType>& array);
241
242 // Get an entire column.
243 // This will scale and offset from the underlying array.
245
246 // Put an entire column.
247 // This will scale and offset to the underlying array.
249
250 // Get a section of all arrays in the column.
251 // This will scale and offset from the underlying array.
253
254 // Put a section of all arrays in the column.
255 // This will scale and offset to the underlying array.
256 void putColumnSlice(const Slicer& slicer, const Array<VirtualType>& array);
257
258 // Scale and/or offset stored to array.
259 // This is meant when reading an array from the stored column.
260 // It optimizes for scale=1 and/or offset=0.
261 void scaleOnGet(VirtualType scale, VirtualType offset, Array<VirtualType>& array,
262 const Array<StoredType>& stored);
263
264 // Scale and/or offset array to stored.
265 // This is meant when writing an array into the stored column.
266 // It optimizes for scale=1 and/or offset=0.
267 void scaleOnPut(VirtualType scale, VirtualType offset, const Array<VirtualType>& array,
268 Array<StoredType>& stored);
269
270 // Scale and/or offset stored to array for the entire column.
271 // When the scale and offset are fixed, it will do the entire array.
272 // Otherwise it iterates through the array and applies the scale
273 // and offset per row.
275
276 // Scale and/or offset array to stored for the entire column.
277 // When the scale and offset are fixed, it will do the entire array.
278 // Otherwise it iterates through the array and applies the scale
279 // and offset per row.
281
282 // # Now define the data members.
283 String scaleName_p; // # name of scale column
284 String offsetName_p; // # name of offset column
285 VirtualType scale_p; // # scale factor
286 VirtualType offset_p; // # offset value
287 Bool fixedScale_p; // # scale is a fixed column
288 Bool fixedOffset_p; // # scale is a fixed column
289 ScalarColumn<VirtualType>* scaleColumn_p; // # column with scale value
290 ScalarColumn<VirtualType>* offsetColumn_p; // # column with offset value
291
292 // Get the scale value for this row.
293 VirtualType getScale(rownr_t rownr);
294
295 // Get the offset value for this row.
296 VirtualType getOffset(rownr_t rownr);
297
298 public:
299 //*display 4
300 // Define the "constructor" to construct this engine when a
301 // table is read back.
302 // This "constructor" has to be registered by the user of the engine.
303 // If the engine is commonly used, its registration can be added
304 // to the registerAllCtor function in DataManReg.cc.
305 // That function gets automatically invoked by the table system.
306 static DataManager* makeObject(const String& dataManagerType, const Record& spec);
307};
308
309} // namespace casacore
310
311#ifndef CASACORE_NO_AUTO_TEMPLATES
312#include <casacore/tables/DataMan/ScaledArrayEngine.tcc>
313#endif // # CASACORE_NO_AUTO_TEMPLATES
314#endif
ArrayColumn< StoredType > & column()
Give access to the stored column.
void setNames(const String &virtualName, const String &storedName)
Set the virtual and stored column name.
const String & storedName() const
Get the stored column name.
BaseMappedArrayEngine(const String &virtualColumnName, const String &storedColumnName)
Construct an engine to convert the virtual column to the stored column.
const String & virtualName() const
Get the virtual column name.
Abstract base class for a data manager.
Table & table() const
Get the table this object is associated with.
static String className()
Return the name of the class.
void getArray(rownr_t rownr, Array< VirtualType > &array)
Get an array in the given row.
virtual String dataManagerName() const
Get the name given to the engine (is the virtual column name).
VirtualType getOffset(rownr_t rownr)
Get the offset value for this row.
virtual Record dataManagerSpec() const
Record a record containing data manager specifications.
void putArrayColumn(const Array< VirtualType > &array)
Put an entire column.
void scaleOnPut(VirtualType scale, VirtualType offset, const Array< VirtualType > &array, Array< StoredType > &stored)
Scale and/or offset array to stored.
ScaledArrayEngine(const ScaledArrayEngine< VirtualType, StoredType > &)
Copy constructor is only used by clone().
void putColumnSlice(const Slicer &slicer, const Array< VirtualType > &array)
Put a section of all arrays in the column.
ScaledArrayEngine(const String &virtualColumnName, const String &storedColumnName, VirtualType scale, VirtualType offset=0)
Construct an engine to scale all arrays in a column with the given offset and scale factor.
void scaleColumnOnGet(Array< VirtualType > &array, const Array< StoredType > &stored)
Scale and/or offset stored to array for the entire column.
void scaleColumnOnPut(const Array< VirtualType > &array, Array< StoredType > &stored)
Scale and/or offset array to stored for the entire column.
DataManager * clone() const
Clone the engine object.
void scaleOnGet(VirtualType scale, VirtualType offset, Array< VirtualType > &array, const Array< StoredType > &stored)
Scale and/or offset stored to array.
void getColumnSlice(const Slicer &slicer, Array< VirtualType > &array)
Get a section of all arrays in the column.
ScaledArrayEngine(const Record &spec)
Construct from a record specification as created by getmanagerSpec().
VirtualType getScale(rownr_t rownr)
Get the scale value for this row.
void putSlice(rownr_t rownr, const Slicer &slicer, const Array< VirtualType > &array)
Put into a section of the array in the given row.
virtual String dataManagerType() const
Return the type name of the engine (i.e.
static DataManager * makeObject(const String &dataManagerType, const Record &spec)
~ScaledArrayEngine()
Destructor is mandatory.
static void registerClass()
Register the class name and the static makeObject "constructor".
void getArrayColumn(Array< VirtualType > &array)
Get an entire column.
ScaledArrayEngine(const String &virtualColumnName, const String &storedColumnName, const String &scaleColumnName, VirtualType offset=0)
Construct an engine to scale the arrays in a column.
ScaledArrayEngine< VirtualType, StoredType > & operator=(const ScaledArrayEngine< VirtualType, StoredType > &)=delete
Assignment is not needed and therefore forbidden.
ScaledArrayEngine(const String &virtualColumnName, const String &storedColumnName, const String &scaleColumnName, const String &offsetColumnName)
ScalarColumn< VirtualType > * offsetColumn_p
void create64(rownr_t initialNrrow)
Initialize the object for a new table.
void prepare()
Preparing consists of setting the writable switch and adding the initial number of rows in case of cr...
void getSlice(rownr_t rownr, const Slicer &slicer, Array< VirtualType > &array)
Get a section of the array in the given row.
ScalarColumn< VirtualType > * scaleColumn_p
void putArray(rownr_t rownr, const Array< VirtualType > &array)
Put an array in the given row.
String: the storage and methods of handling collections of characters.
Definition String.h:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
int offset(int, int) const
compute a linear offset from array indicies
T * array
The actual storage.
Definition Block.h:689
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
uInt64 rownr_t
Define the type of a row number in a table.
Definition aipsxtype.h:44