casacore
Loading...
Searching...
No Matches
ScaledComplexData.h
Go to the documentation of this file.
1// # ScaledComplexData.h: Templated virtual column engine to scale a complex table array
2// # Copyright (C) 1999,2000,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_SCALEDCOMPLEXDATA_H
27#define TABLES_SCALEDCOMPLEXDATA_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 complex 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// ScaledComplexData is a virtual column engine which scales an array
56// of a complex type to 2 values of another type (to save disk storage).
57// For example, <src>ScaledComplexData<Complex,Short></src> resembles the
58// classic AIPS compress method which scales the data from float to short.
59// The (complex) 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//
99// Class ScaledArrayEngine could not be used, because complex integer
100// types are not supported in the tabe system.
101// </motivation>
102
103// <example>
104// <srcblock>
105// // Create the table description and 2 columns with indirect arrays in it.
106// // The Int column will be stored, while the double will be
107// // used as virtual.
108// TableDesc tableDesc ("", TableDesc::Scratch);
109// tableDesc.addColumn (ArrayColumnDesc<Short> ("storedArray"));
110// tableDesc.addColumn (ArrayColumnDesc<Complex> ("virtualArray"));
111//
112// // Create a new table using the table description.
113// SetupNewTable newtab (tableDesc, "tab.data", Table::New);
114//
115// // Create the array scaling engine to scale from double to Int
116// // and bind it to the double column.
117// // Create the table.
118// ScaledComplexData<Complex,Short> scalingEngine("virtualArray",
119// "storedArray", 10);
120// newtab.bindColumn ("virtualArray", scalingEngine);
121// Table table (newtab);
122//
123// // Store a 2-D array (with dim. 3,4) into each row of the column.
124// // The shape of each array in the column is implicitly set by the put
125// // function. This will also set the shape of the underlying Int array
126// // (as a 3-D array with shape 2,3,4).
127// ArrayColumn data (table, "virtualArray");
128// Array<DComplex> someArray(IPosition(2,3,4));
129// someArray = 0;
130// for (rownr_t i=0, i<10; i++) { // table will have 10 rows
131// table.addRow();
132// data.put (i, someArray)
133// }
134// </srcblock>
135// </example>
136
137// <templating arg=VirtualType>
138// <li> only complex data types
139// </templating>
140// <templating arg=StoredType>
141// <li> only built-in numerics data types
142// </templating>
143
144template <class VirtualType, class StoredType>
145class ScaledComplexData : public BaseMappedArrayEngine<VirtualType, StoredType> {
146 // # Make members of parent class known.
147 public:
148 using BaseMappedArrayEngine<VirtualType, StoredType>::virtualName;
149
150 protected:
151 using BaseMappedArrayEngine<VirtualType, StoredType>::storedName;
152 using BaseMappedArrayEngine<VirtualType, StoredType>::table;
153 using BaseMappedArrayEngine<VirtualType, StoredType>::column;
154 using BaseMappedArrayEngine<VirtualType, StoredType>::setNames;
155
156 public:
157 // Construct an engine to scale all arrays in a column with
158 // the given offset and scale factor.
159 // StoredColumnName is the name of the column where the scaled
160 // data will be put and must have data type StoredType.
161 // The virtual column using this engine must have data type VirtualType.
162 ScaledComplexData(const String& virtualColumnName, const String& storedColumnName,
163 VirtualType scale, VirtualType offset = 0);
164
165 // Construct an engine to scale the arrays in a column.
166 // The scale and offset values are taken from a column with
167 // the given names. In that way each array has its own scale factor
168 // and offset value.
169 // An exception is thrown if these columns do not exist.
170 // VirtualColumnName is the name of the virtual column and is used to
171 // check if the engine gets bound to the correct column.
172 // StoredColumnName is the name of the column where the scaled
173 // data will be put and must have data type StoredType.
174 // The virtual column using this engine must have data type VirtualType.
175 // <group>
176 ScaledComplexData(const String& virtualColumnName, const String& storedColumnName,
177 const String& scaleColumnName, VirtualType offset = 0);
178 ScaledComplexData(const String& virtualColumnName, const String& storedColumnName,
179 const String& scaleColumnName, const String& offsetColumnName);
180 // </group>
181
182 // Construct from a record specification as created by getmanagerSpec().
184
185 // Destructor is mandatory.
187
188 // Assignment is not needed and therefore forbidden.
191
192 // Return the type name of the engine (i.e. its class name).
194
195 // Record a record containing data manager specifications.
196 virtual Record dataManagerSpec() const;
197
198 // Return the name of the class.
199 // This includes the names of the template arguments.
201
202 // Register the class name and the static makeObject "constructor".
203 // This will make the engine known to the table system.
204 // The automatically invoked registration function in DataManReg.cc
205 // contains ScaledComplexData<double,Int>.
206 // Any other instantiation of this class must be registered "manually"
207 // (or added to DataManReg.cc).
208 static void registerClass();
209
210 private:
211 // The default constructor is required for reconstruction of the
212 // engine when a table is read back.
214
215 // Copy constructor is only used by clone().
216 // (so it is made private).
218
219 // Clone the engine object.
220 virtual DataManager* clone() const;
221
222 // Initialize the object for a new table.
223 // It defines the keywords containing the engine parameters.
224 virtual void create64(rownr_t initialNrrow);
225
226 // Preparing consists of setting the writable switch and
227 // adding the initial number of rows in case of create.
228 // Furthermore it reads the keywords containing the engine parameters.
229 virtual void prepare();
230
231 // Set the shape of the FixedShape arrays in the column.
232 // This function only gets called if the column has FixedShape arrays.
233 // The shape gets saved and used to set the shape of the arrays
234 // in the stored in case the stored has non-FixedShape arrays.
235 virtual void setShapeColumn(const IPosition& shape);
236
237 // Define the shape of the array in the given row.
238 // When the shape of the (underlying) stored array has already been
239 // defined, it checks whether its latter dimensions match the given
240 // virtual shape. When matching, nothing will be done.
241 // When mismatching or when the stored shape has not been defined
242 // yet, the stored shape will be defined from the virtual shape and
243 // the virtual element shape.
244 // E.g. in case of a StokesVector a virtual shape of (512,512)
245 // results in a stored shape of (4,512,512).
246 virtual void setShape(rownr_t rownr, const IPosition& shape);
247
248 // Get the dimensionality of the array in the given row.
249 virtual uInt ndim(rownr_t rownr);
250
251 // Get the shape of the array in the given row.
252 // This is done by stripping the first dimension from the shape
253 // of the underlying stored array.
254 virtual IPosition shape(rownr_t rownr);
255
256 // Get an array in the given row.
257 // This will scale and offset from the underlying array.
259
260 // Put an array in the given row.
261 // This will scale and offset to the underlying array.
262 virtual void putArray(rownr_t rownr, const Array<VirtualType>& array);
263
264 // Get a section of the array in the given row.
265 // This will scale and offset from the underlying array.
266 virtual void getSlice(rownr_t rownr, const Slicer& slicer, Array<VirtualType>& array);
267
268 // Put into a section of the array in the given row.
269 // This will scale and offset to the underlying array.
270 virtual void putSlice(rownr_t rownr, const Slicer& slicer, const Array<VirtualType>& array);
271
272 // Get an entire column.
273 // This will scale and offset from the underlying array.
275
276 // Put an entire column.
277 // This will scale and offset to the underlying array.
279
280 // Get some array values in the column.
281 // This will scale and offset from the underlying array.
282 virtual void getArrayColumnCells(const RefRows& rownrs, Array<VirtualType>& data);
283
284 // Put some array values in the column.
285 // This will scale and offset to the underlying array.
286 virtual void putArrayColumnCells(const RefRows& rownrs, const Array<VirtualType>& data);
287
288 // Get a section of all arrays in the column.
289 // This will scale and offset from the underlying array.
290 virtual void getColumnSlice(const Slicer& slicer, Array<VirtualType>& array);
291
292 // Put a section of all arrays in the column.
293 // This will scale and offset to the underlying array.
294 virtual void putColumnSlice(const Slicer& slicer, const Array<VirtualType>& array);
295
296 // Get a section of some arrays in the column.
297 // This will scale and offset from the underlying array.
298 virtual void getColumnSliceCells(const RefRows& rownrs, const Slicer& slicer,
299 Array<VirtualType>& data);
300
301 // Put into a section of some arrays in the column.
302 // This will scale and offset to the underlying array.
303 virtual void putColumnSliceCells(const RefRows& rownrs, const Slicer& slicer,
304 const Array<VirtualType>& data);
305
306 // Scale and/or offset stored to array.
307 // This is meant when reading an array from the stored column.
308 // It optimizes for scale=1 and/or offset=0.
309 void scaleOnGet(VirtualType scale, VirtualType offset, Array<VirtualType>& array,
310 const Array<StoredType>& stored);
311
312 // Scale and/or offset array to stored.
313 // This is meant when writing an array into the stored column.
314 // It optimizes for scale=1 and/or offset=0.
315 void scaleOnPut(VirtualType scale, VirtualType offset, const Array<VirtualType>& array,
316 Array<StoredType>& stored);
317
318 // Scale and/or offset stored to array for the entire column.
319 // When the scale and offset are fixed, it will do the entire array.
320 // Otherwise it iterates through the array and applies the scale
321 // and offset per row.
323
324 // Scale and/or offset array to stored for the entire column.
325 // When the scale and offset are fixed, it will do the entire array.
326 // Otherwise it iterates through the array and applies the scale
327 // and offset per row.
329
330 // Scale and/or offset stored to array for some cells in the column.
331 // When the scale and offset are fixed, it will do the entire array.
332 // Otherwise it iterates through the array and applies the scale
333 // and offset per row.
335 const RefRows& rownrs);
336
337 // Scale and/or offset array to stored for some cells in the column.
338 // When the scale and offset are fixed, it will do the entire array.
339 // Otherwise it iterates through the array and applies the scale
340 // and offset per row.
342 const RefRows& rownrs);
343
344 // Determine the shape of an array in the stored column.
345 IPosition storedShape(const IPosition& virtualShape) const {
346 return IPosition(1, 2).concatenate(virtualShape);
347 }
348
349 // Convert the Slicer for a virtual to a Slicer for the stored.
350 Slicer storedSlicer(const Slicer& virtualSlicer) const;
351
352 // # Now define the data members.
353 String scaleName_p; // # name of scale column
354 String offsetName_p; // # name of offset column
355 VirtualType scale_p; // # scale factor
356 VirtualType offset_p; // # offset value
357 Bool fixedScale_p; // # scale is a fixed column
358 Bool fixedOffset_p; // # offset is a fixed column
359 ScalarColumn<VirtualType>* scaleColumn_p; // # column with scale value
360 ScalarColumn<VirtualType>* offsetColumn_p; // # column with offset value
361
362 // Get the scale value for this row.
363 VirtualType getScale(rownr_t rownr);
364
365 // Get the offset value for this row.
366 VirtualType getOffset(rownr_t rownr);
367
368 public:
369 // Define the "constructor" to construct this engine when a
370 // table is read back.
371 // This "constructor" has to be registered by the user of the engine.
372 // If the engine is commonly used, its registration can be added
373 // to the registerAllCtor function in DataManReg.cc.
374 // That function gets automatically invoked by the table system.
375 static DataManager* makeObject(const String& dataManagerType, const Record& spec);
376};
377
378} // namespace casacore
379
380#ifndef CASACORE_NO_AUTO_TEMPLATES
381#include <casacore/tables/DataMan/ScaledComplexData.tcc>
382#endif // # CASACORE_NO_AUTO_TEMPLATES
383#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.
IPosition concatenate(const IPosition &other) const
Return an IPosition as the concetanation of this and another IPosition.
void scaleColumnOnPut(const Array< VirtualType > &array, Array< StoredType > &stored)
Scale and/or offset array to stored for the entire column.
IPosition storedShape(const IPosition &virtualShape) const
Determine the shape of an array in the stored column.
virtual IPosition shape(rownr_t rownr)
Get the shape of the array in the given row.
ScaledComplexData(const Record &spec)
Construct from a record specification as created by getmanagerSpec().
void scaleColumnOnGet(Array< VirtualType > &array, const Array< StoredType > &stored)
Scale and/or offset stored to array for the entire column.
virtual void putColumnSliceCells(const RefRows &rownrs, const Slicer &slicer, const Array< VirtualType > &data)
Put into a section of some arrays in the column.
virtual DataManager * clone() const
Clone the engine object.
void scaleOnPut(VirtualType scale, VirtualType offset, const Array< VirtualType > &array, Array< StoredType > &stored)
Scale and/or offset array to stored.
virtual void putSlice(rownr_t rownr, const Slicer &slicer, const Array< VirtualType > &array)
Put into a section of the array in the given row.
void scaleOnGet(VirtualType scale, VirtualType offset, Array< VirtualType > &array, const Array< StoredType > &stored)
Scale and/or offset stored to array.
static DataManager * makeObject(const String &dataManagerType, const Record &spec)
Define the "constructor" to construct this engine when a table is read back.
virtual void setShape(rownr_t rownr, const IPosition &shape)
Define the shape of the array in the given row.
virtual void getSlice(rownr_t rownr, const Slicer &slicer, Array< VirtualType > &array)
Get a section of the array in the given row.
virtual void create64(rownr_t initialNrrow)
Initialize the object for a new table.
virtual void putColumnSlice(const Slicer &slicer, const Array< VirtualType > &array)
Put a section of all arrays in the column.
ScaledComplexData(const String &virtualColumnName, const String &storedColumnName, const String &scaleColumnName, const String &offsetColumnName)
virtual void getArrayColumn(Array< VirtualType > &array)
Get an entire column.
static String className()
Return the name of the class.
ScaledComplexData(const ScaledComplexData< VirtualType, StoredType > &)
Copy constructor is only used by clone().
virtual void setShapeColumn(const IPosition &shape)
Set the shape of the FixedShape arrays in the column.
ScaledComplexData< VirtualType, StoredType > & operator=(const ScaledComplexData< VirtualType, StoredType > &)=delete
Assignment is not needed and therefore forbidden.
virtual void putArray(rownr_t rownr, const Array< VirtualType > &array)
Put an array in the given row.
Slicer storedSlicer(const Slicer &virtualSlicer) const
Convert the Slicer for a virtual to a Slicer for the stored.
virtual uInt ndim(rownr_t rownr)
Get the dimensionality of the array in the given row.
void scaleCellsOnGet(Array< VirtualType > &array, const Array< StoredType > &stored, const RefRows &rownrs)
Scale and/or offset stored to array for some cells in the column.
virtual void prepare()
Preparing consists of setting the writable switch and adding the initial number of rows in case of cr...
ScaledComplexData(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.
String dataManagerType() const
Return the type name of the engine (i.e.
virtual void getColumnSliceCells(const RefRows &rownrs, const Slicer &slicer, Array< VirtualType > &data)
Get a section of some arrays in the column.
ScalarColumn< VirtualType > * offsetColumn_p
virtual void getColumnSlice(const Slicer &slicer, Array< VirtualType > &array)
Get a section of all arrays in the column.
virtual void putArrayColumnCells(const RefRows &rownrs, const Array< VirtualType > &data)
Put some array values in the column.
ScaledComplexData(const String &virtualColumnName, const String &storedColumnName, const String &scaleColumnName, VirtualType offset=0)
Construct an engine to scale the arrays in a column.
virtual void getArrayColumnCells(const RefRows &rownrs, Array< VirtualType > &data)
Get some array values in the column.
VirtualType getScale(rownr_t rownr)
Get the scale value for this row.
ScalarColumn< VirtualType > * scaleColumn_p
VirtualType getOffset(rownr_t rownr)
Get the offset value for this row.
static void registerClass()
Register the class name and the static makeObject "constructor".
virtual void getArray(rownr_t rownr, Array< VirtualType > &array)
Get an array in the given row.
~ScaledComplexData()
Destructor is mandatory.
virtual Record dataManagerSpec() const
Record a record containing data manager specifications.
virtual void putArrayColumn(const Array< VirtualType > &array)
Put an entire column.
void scaleCellsOnPut(const Array< VirtualType > &array, Array< StoredType > &stored, const RefRows &rownrs)
Scale and/or offset array to stored for some cells in the column.
ScaledComplexData()
The default constructor is required for reconstruction of the engine when a table is read back.
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
unsigned int uInt
Definition aipstype.h:49
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