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