casacore
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: aips2-request@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 //# $Id$
27 
28 #ifndef TABLES_SCALEDCOMPLEXDATA_H
29 #define TABLES_SCALEDCOMPLEXDATA_H
30 
31 
32 //# Includes
33 #include <casacore/casa/aips.h>
34 #include <casacore/tables/DataMan/BaseMappedArrayEngine.h>
35 
36 namespace casacore { //# NAMESPACE CASACORE - BEGIN
37 
38 //# Forward Declarations
39 template<class T> class ScalarColumn;
40 
41 
42 // <summary>
43 // Templated virtual column engine to scale a complex table array
44 // </summary>
45 
46 // <use visibility=export>
47 
48 // <reviewed reviewer="Gareth Hunt" date="94Nov17" tests="">
49 // </reviewed>
50 
51 // <prerequisite>
52 //# Classes you should understand before using this one.
53 // <li> VirtualColumnEngine
54 // <li> VirtualArrayColumn
55 // </prerequisite>
56 
57 // <synopsis>
58 // ScaledComplexData is a virtual column engine which scales an array
59 // of a complex type to 2 values of another type (to save disk storage).
60 // For example, <src>ScaledComplexData<Complex,Short></src> resembles the
61 // classic AIPS compress method which scales the data from float to short.
62 // The (complex) scale factor and offset value can be given in two ways:
63 // <ul>
64 // <li> As a fixed value which is used for all arrays in the column.
65 // <li> As the name of a column. In this way each array in a
66 // column can have its own scale and offset value.
67 // The scale and offset value in a row must be put before
68 // the array is put and should not be changed anymore.
69 // </ul>
70 // It is also possible to have a variable scale factor with a fixed offset
71 // value.
72 // As in FITS the scale and offset values are used as:
73 // <br><src> True_value = Stored_value * scale + offset; </src>
74 //
75 // An engine object should be used for one column only, because the stored
76 // column name is part of the engine. If it would be used for more than
77 // one column, they would all share the same stored column.
78 // When the engine is bound to a column, it is checked if the name
79 // of that column matches the given virtual column name.
80 //
81 // The engine can be used for a column containing any kind of array
82 // (thus direct or indirect, fixed or variable shaped)) as long as the
83 // virtual array can be stored in the stored array. Thus a fixed shaped
84 // virtual can use a variable shaped stored, but not vice versa.
85 // A fixed shape indirect virtual can use a stored with direct arrays.
86 //
87 // This class can also serve as an example of how to implement
88 // a virtual column engine.
89 // </synopsis>
90 
91 // <motivation>
92 // This class allows to store data in a smaller representation.
93 // It is needed to resemble the classic AIPS compress option.
94 // It adds the scale and offset value on a per row basis.
95 //
96 // Because the engine can serve only one column, it was possible to
97 // combine the engine and the column functionality in one class.
98 // This has been achieved using multiple inheritance.
99 // The advantage of this is that only one templated class is used,
100 // so less template instantiations are needed.
101 //
102 // Class ScaledArrayEngine could not be used, because complex integer
103 // types are not supported in the tabe system.
104 // </motivation>
105 
106 // <example>
107 // <srcblock>
108 // // Create the table description and 2 columns with indirect arrays in it.
109 // // The Int column will be stored, while the double will be
110 // // used as virtual.
111 // TableDesc tableDesc ("", TableDesc::Scratch);
112 // tableDesc.addColumn (ArrayColumnDesc<Short> ("storedArray"));
113 // tableDesc.addColumn (ArrayColumnDesc<Complex> ("virtualArray"));
114 //
115 // // Create a new table using the table description.
116 // SetupNewTable newtab (tableDesc, "tab.data", Table::New);
117 //
118 // // Create the array scaling engine to scale from double to Int
119 // // and bind it to the double column.
120 // // Create the table.
121 // ScaledComplexData<Complex,Short> scalingEngine("virtualArray",
122 // "storedArray", 10);
123 // newtab.bindColumn ("virtualArray", scalingEngine);
124 // Table table (newtab);
125 //
126 // // Store a 2-D array (with dim. 3,4) into each row of the column.
127 // // The shape of each array in the column is implicitly set by the put
128 // // function. This will also set the shape of the underlying Int array
129 // // (as a 3-D array with shape 2,3,4).
130 // ArrayColumn data (table, "virtualArray");
131 // Array<DComplex> someArray(IPosition(2,3,4));
132 // someArray = 0;
133 // for (rownr_t i=0, i<10; i++) { // table will have 10 rows
134 // table.addRow();
135 // data.put (i, someArray)
136 // }
137 // </srcblock>
138 // </example>
139 
140 // <templating arg=VirtualType>
141 // <li> only complex data types
142 // </templating>
143 // <templating arg=StoredType>
144 // <li> only built-in numerics data types
145 // </templating>
146 
147 template<class VirtualType, class StoredType>
148 class ScaledComplexData : public BaseMappedArrayEngine<VirtualType, StoredType>
149 {
150  //# Make members of parent class known.
151 public:
153 protected:
158 
159 public:
160  // Construct an engine to scale all arrays in a column with
161  // the given offset and scale factor.
162  // StoredColumnName is the name of the column where the scaled
163  // data will be put and must have data type StoredType.
164  // The virtual column using this engine must have data type VirtualType.
165  ScaledComplexData (const String& virtualColumnName,
166  const String& storedColumnName,
167  VirtualType scale,
168  VirtualType offset = 0);
169 
170  // Construct an engine to scale the arrays in a column.
171  // The scale and offset values are taken from a column with
172  // the given names. In that way each array has its own scale factor
173  // and offset value.
174  // An exception is thrown if these columns do not exist.
175  // VirtualColumnName is the name of the virtual column and is used to
176  // check if the engine gets bound to the correct column.
177  // StoredColumnName is the name of the column where the scaled
178  // data will be put and must have data type StoredType.
179  // The virtual column using this engine must have data type VirtualType.
180  // <group>
181  ScaledComplexData (const String& virtualColumnName,
182  const String& storedColumnName,
183  const String& scaleColumnName,
184  VirtualType offset = 0);
185  ScaledComplexData (const String& virtualColumnName,
186  const String& storedColumnName,
187  const String& scaleColumnName,
188  const String& offsetColumnName);
189  // </group>
190 
191  // Construct from a record specification as created by getmanagerSpec().
192  ScaledComplexData (const Record& spec);
193 
194  // Destructor is mandatory.
196 
197  // Return the type name of the engine (i.e. its class name).
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.
205  static String className();
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 ScaledComplexData<double,Int>.
211  // Any other instantiation of this class must be registered "manually"
212  // (or added to DataManReg.cc).
213  static void registerClass();
214 
215 private:
216  // The default constructor is required for reconstruction of the
217  // engine when a table is read back.
219 
220  // Copy constructor is only used by clone().
221  // (so it is made private).
223 
224  // Assignment is not needed and therefore forbidden
225  // (so it is made private and not implemented).
228 
229  // Clone the engine object.
230  virtual DataManager* clone() const;
231 
232  // Initialize the object for a new table.
233  // It defines the keywords containing the engine parameters.
234  virtual void create64 (rownr_t initialNrrow);
235 
236  // Preparing consists of setting the writable switch and
237  // adding the initial number of rows in case of create.
238  // Furthermore it reads the keywords containing the engine parameters.
239  virtual void prepare();
240 
241  // Set the shape of the FixedShape arrays in the column.
242  // This function only gets called if the column has FixedShape arrays.
243  // The shape gets saved and used to set the shape of the arrays
244  // in the stored in case the stored has non-FixedShape arrays.
245  virtual void setShapeColumn (const IPosition& shape);
246 
247  // Define the shape of the array in the given row.
248  // When the shape of the (underlying) stored array has already been
249  // defined, it checks whether its latter dimensions match the given
250  // virtual shape. When matching, nothing will be done.
251  // When mismatching or when the stored shape has not been defined
252  // yet, the stored shape will be defined from the virtual shape and
253  // the virtual element shape.
254  // E.g. in case of a StokesVector a virtual shape of (512,512)
255  // results in a stored shape of (4,512,512).
256  virtual void setShape (rownr_t rownr, const IPosition& shape);
257 
258  // Get the dimensionality of the array in the given row.
259  virtual uInt ndim (rownr_t rownr);
260 
261  // Get the shape of the array in the given row.
262  // This is done by stripping the first dimension from the shape
263  // of the underlying stored array.
264  virtual IPosition shape (rownr_t rownr);
265 
266  // Get an array in the given row.
267  // This will scale and offset from the underlying array.
268  virtual void getArray (rownr_t rownr, Array<VirtualType>& array);
269 
270  // Put an array in the given row.
271  // This will scale and offset to the underlying array.
272  virtual void putArray (rownr_t rownr, const Array<VirtualType>& array);
273 
274  // Get a section of the array in the given row.
275  // This will scale and offset from the underlying array.
276  virtual void getSlice (rownr_t rownr, const Slicer& slicer,
278 
279  // Put into a section of the array in the given row.
280  // This will scale and offset to the underlying array.
281  virtual void putSlice (rownr_t rownr, const Slicer& slicer,
282  const Array<VirtualType>& array);
283 
284  // Get an entire column.
285  // This will scale and offset from the underlying array.
287 
288  // Put an entire column.
289  // This will scale and offset to the underlying array.
290  virtual void putArrayColumn (const Array<VirtualType>& array);
291 
292  // Get some array values in the column.
293  // This will scale and offset from the underlying array.
294  virtual void getArrayColumnCells (const RefRows& rownrs,
295  Array<VirtualType>& data);
296 
297  // Put some array values in the column.
298  // This will scale and offset to the underlying array.
299  virtual void putArrayColumnCells (const RefRows& rownrs,
300  const Array<VirtualType>& data);
301 
302  // Get a section of all arrays in the column.
303  // This will scale and offset from the underlying array.
304  virtual void getColumnSlice (const Slicer& slicer,
306 
307  // Put a section of all arrays in the column.
308  // This will scale and offset to the underlying array.
309  virtual void putColumnSlice (const Slicer& slicer,
310  const Array<VirtualType>& array);
311 
312  // Get a section of some arrays in the column.
313  // This will scale and offset from the underlying array.
314  virtual void getColumnSliceCells (const RefRows& rownrs,
315  const Slicer& slicer,
316  Array<VirtualType>& data);
317 
318  // Put into a section of some arrays in the column.
319  // This will scale and offset to the underlying array.
320  virtual void putColumnSliceCells (const RefRows& rownrs,
321  const Slicer& slicer,
322  const Array<VirtualType>& data);
323 
324  // Scale and/or offset stored to array.
325  // This is meant when reading an array from the stored column.
326  // It optimizes for scale=1 and/or offset=0.
327  void scaleOnGet (VirtualType scale, VirtualType offset,
329  const Array<StoredType>& stored);
330 
331  // Scale and/or offset array to stored.
332  // This is meant when writing an array into the stored column.
333  // It optimizes for scale=1 and/or offset=0.
334  void scaleOnPut (VirtualType scale, VirtualType offset,
335  const Array<VirtualType>& array,
336  Array<StoredType>& stored);
337 
338  // Scale and/or offset stored to array for the entire column.
339  // When the scale and offset are fixed, it will do the entire array.
340  // Otherwise it iterates through the array and applies the scale
341  // and offset per row.
343  const Array<StoredType>& stored);
344 
345  // Scale and/or offset array to stored for the entire column.
346  // When the scale and offset are fixed, it will do the entire array.
347  // Otherwise it iterates through the array and applies the scale
348  // and offset per row.
350  Array<StoredType>& stored);
351 
352  // Scale and/or offset stored to array for some cells in the column.
353  // When the scale and offset are fixed, it will do the entire array.
354  // Otherwise it iterates through the array and applies the scale
355  // and offset per row.
357  const Array<StoredType>& stored,
358  const RefRows& rownrs);
359 
360  // Scale and/or offset array to stored for some cells in the column.
361  // When the scale and offset are fixed, it will do the entire array.
362  // Otherwise it iterates through the array and applies the scale
363  // and offset per row.
365  Array<StoredType>& stored,
366  const RefRows& rownrs);
367 
368  // Determine the shape of an array in the stored column.
369  IPosition storedShape (const IPosition& virtualShape) const
370  { return IPosition(1,2).concatenate (virtualShape); }
371 
372  // Convert the Slicer for a virtual to a Slicer for the stored.
373  Slicer storedSlicer (const Slicer& virtualSlicer) const;
374 
375  //# Now define the data members.
376  String scaleName_p; //# name of scale column
377  String offsetName_p; //# name of offset column
378  VirtualType scale_p; //# scale factor
379  VirtualType offset_p; //# offset value
380  Bool fixedScale_p; //# scale is a fixed column
381  Bool fixedOffset_p; //# offset is a fixed column
382  ScalarColumn<VirtualType>* scaleColumn_p; //# column with scale value
383  ScalarColumn<VirtualType>* offsetColumn_p; //# column with offset value
384 
385  // Get the scale value for this row.
386  VirtualType getScale (rownr_t rownr);
387 
388  // Get the offset value for this row.
389  VirtualType getOffset (rownr_t rownr);
390 
391 public:
392  // Define the "constructor" to construct this engine when a
393  // table is read back.
394  // This "constructor" has to be registered by the user of the engine.
395  // If the engine is commonly used, its registration can be added
396  // to the registerAllCtor function in DataManReg.cc.
397  // That function gets automatically invoked by the table system.
399  const Record& spec);
400 };
401 
402 
403 
404 } //# NAMESPACE CASACORE - END
405 
406 #ifndef CASACORE_NO_AUTO_TEMPLATES
407 #include <casacore/tables/DataMan/ScaledComplexData.tcc>
408 #endif //# CASACORE_NO_AUTO_TEMPLATES
409 #endif
Abstract base class for a data manager.
Definition: DataManager.h:221
IPosition concatenate(const IPosition &other) const
Return an IPosition as the concetanation of this and another IPosition.
virtual DataManager * clone() const
Clone the engine object.
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.
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.
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:225
this file contains all the compiler specific defines
Definition: mainpage.dox:28
unsigned int uInt
Definition: aipstype.h:51
TableExprNode array(const TableExprNode &values, const TableExprNodeSet &shape)
Create an array of the given shape and fill it with the values.
Definition: ExprNode.h:1929
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
uInt64 rownr_t
Define the type of a row number in a table.
Definition: aipsxtype.h:46