casacore
Loading...
Searching...
No Matches
CompressFloat.h
Go to the documentation of this file.
1//# CompressFloat.h: Virtual column engine to scale a table float array
2//# Copyright (C) 2001,2002
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_COMPRESSFLOAT_H
27#define TABLES_COMPRESSFLOAT_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/tables/DataMan/BaseMappedArrayEngine.h>
32#include <casacore/tables/Tables/ScalarColumn.h>
33#include <casacore/casa/Arrays/Array.h>
34
35
36namespace casacore { //# NAMESPACE CASACORE - BEGIN
37
38// <summary>
39// Virtual column engine to scale a table float array
40// </summary>
41
42// <use visibility=export>
43
44// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tCompressFloat.cc">
45// </reviewed>
46
47// <prerequisite>
48//# Classes you should understand before using this one.
49// <li> VirtualColumnEngine
50// <li> VirtualArrayColumn
51// </prerequisite>
52
53// <synopsis>
54// CompressFloat is a virtual column engine which scales an array
55// of one type to another type to save disk storage.
56// This resembles the classic AIPS compress method which scales the
57// data from float to short.
58// The scale factor and offset values can be given in two ways:
59// <ul>
60// <li> As a fixed values which is used for all arrays in the column.
61// These values have to be given when constructing of the engine.
62// <li> As the name of a column. In this way each array in the
63// column has its own scale and offset value.
64// By default it uses auto-scaling (see below).
65// Otherwise the scale and offset value in a row must be put
66// before the array is put and should not be changed anymore.
67// </ul>
68// Auto-scaling means that the engine will determine the scale
69// and offset value itself when an array (or a slice) is put.
70// It does it by mapping the values in the array to the range [-32767,32767].
71// At each put the scale/offset values are changed as needed.
72// Note that with auto-scaling <src>putSlice</src> can be somewhat
73// slower, because the entire array might need to be rescaled.
74//
75// As in FITS the scale and offset values are used as:
76// <br><src> True_value = Stored_value * scale + offset; </src>
77//
78// An engine object should be used for one column only, because the stored
79// column name is part of the engine. If it would be used for more than
80// one column, they would all share the same stored column.
81// When the engine is bound to a column, it is checked if the name
82// of that column matches the given virtual column name.
83//
84// The engine can be used for a column containing any kind of array
85// (thus direct or indirect, fixed or variable shaped)) as long as the
86// virtual array can be stored in the stored array. Thus a fixed shaped
87// virtual can use a variable shaped stored, but not vice versa.
88// A fixed shape indirect virtual can use a stored with direct arrays.
89//
90// This class can also serve as an example of how to implement
91// a virtual column engine.
92// </synopsis>
93
94// <motivation>
95// This class allows to store data in a smaller representation.
96// It is needed to resemble the classic AIPS compress option.
97//
98// Because the engine can serve only one column, it was possible to
99// combine the engine and the column functionality in one class.
100// </motivation>
101
102// <example>
103// <srcblock>
104// // Create the table description and 2 columns with indirect arrays in it.
105// // The Int column will be stored, while the double will be
106// // used as virtual.
107// TableDesc tableDesc ("", TableDesc::Scratch);
108// tableDesc.addColumn (ArrayColumnDesc<Short> ("storedArray"));
109// tableDesc.addColumn (ArrayColumnDesc<Float> ("virtualArray"));
110// tableDesc.addColumn (ScalarColumnDesc<Float> ("scale"));
111// tableDesc.addColumn (ScalarColumnDesc<Float> ("offset"));
112//
113// // Create a new table using the table description.
114// SetupNewTable newtab (tableDesc, "tab.data", Table::New);
115//
116// // Create the array scaling engine (with auto-scale)
117// // and bind it to the float column.
118// CompressFloat scalingEngine("virtualArray", "storedArray",
119// "scale", "offset");
120// newtab.bindColumn ("virtualArray", scalingEngine);
121// // Create the table.
122// Table table (newtab);
123//
124// // Store a 3-D array (with dim. 2,3,4) into each row of the column.
125// // The shape of each array in the column is implicitly set by the put
126// // function. This will also set the shape of the underlying Int array.
127// ArrayColumn data (table, "virtualArray");
128// Array<double> someArray(IPosition(4,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
137class CompressFloat : public BaseMappedArrayEngine<Float, Short>
138{
139public:
140
141 // Construct an engine to scale all arrays in a column with
142 // the given offset and scale factor.
143 // StoredColumnName is the name of the column where the scaled
144 // data will be put and must have data type Short.
145 // The virtual column using this engine must have data type Float.
146 CompressFloat (const String& virtualColumnName,
147 const String& storedColumnName,
148 Float scale,
149 Float offset = 0);
150
151 // Construct an engine to scale the arrays in a column.
152 // The scale and offset values are taken from a column with
153 // the given names. In that way each array has its own scale factor
154 // and offset value.
155 // An exception is thrown if these columns do not exist.
156 // VirtualColumnName is the name of the virtual column and is used to
157 // check if the engine gets bound to the correct column.
158 // StoredColumnName is the name of the column where the scaled
159 // data will be put and must have data type Short.
160 // The virtual column using this engine must have data type Float.
161 CompressFloat (const String& virtualColumnName,
162 const String& storedColumnName,
163 const String& scaleColumnName,
164 const String& offsetColumnName,
165 Bool autoScale = True);
166
167 // Construct from a record specification as created by getmanagerSpec().
168 CompressFloat (const Record& spec);
169
170 // Destructor is mandatory.
172
173 // Assignment is not needed and therefore forbidden
175
176 // Return the type name of the engine (i.e. its class name).
177 virtual String dataManagerType() const;
178
179 // Get the name given to the engine (is the virtual column name).
180 virtual String dataManagerName() const;
181
182 // Record a record containing data manager specifications.
183 virtual Record dataManagerSpec() const;
184
185 // Return the name of the class.
186 // This includes the names of the template arguments.
188
189 // Register the class name and the static makeObject "constructor".
190 // This will make the engine known to the table system.
191 static void registerClass();
192
193private:
194 // Copy constructor is only used by clone().
195 // (so it is made private).
197
198 // Clone the engine object.
199 virtual DataManager* clone() const;
200
201 // Initialize the object for a new table.
202 // It defines the keywords containing the engine parameters.
203 virtual void create64 (rownr_t initialNrrow);
204
205 // Preparing consists of setting the writable switch and
206 // adding the initial number of rows in case of create.
207 // Furthermore it reads the keywords containing the engine parameters.
208 virtual void prepare();
209
210 // Reopen the engine for read/write access.
211 // It makes the column writable if the underlying column is writable.
212 virtual void reopenRW();
213
214 // Add rows to the table.
215 // If auto-scaling, it initializes the scale column with 0
216 // to indicate that no data has been processed yet.
217 virtual void addRowInit (rownr_t startRow, rownr_t nrrow);
218
219 // Get an array in the given row.
220 // This will scale and offset from the underlying array.
221 virtual void getArray (rownr_t rownr, Array<Float>& array);
222
223 // Put an array in the given row.
224 // This will scale and offset to the underlying array.
225 virtual void putArray (rownr_t rownr, const Array<Float>& array);
226
227 // Get a section of the array in the given row.
228 // This will scale and offset from the underlying array.
229 virtual void getSlice (rownr_t rownr, const Slicer& slicer,
231
232 // Put into a section of the array in the given row.
233 // This will scale and offset to the underlying array.
234 virtual void putSlice (rownr_t rownr, const Slicer& slicer,
235 const Array<Float>& array);
236
237 // Get an entire column.
238 // This will scale and offset from the underlying array.
240
241 // Put an entire column.
242 // This will scale and offset to the underlying array.
243 virtual void putArrayColumn (const Array<Float>& array);
244
245 // Get some array values in the column.
246 // This will scale and offset from the underlying array.
247 virtual void getArrayColumnCells (const RefRows& rownrs,
248 Array<Float>& data);
249
250 // Put some array values in the column.
251 // This will scale and offset to the underlying array.
252 virtual void putArrayColumnCells (const RefRows& rownrs,
253 const Array<Float>& data);
254
255 // Get a section of all arrays in the column.
256 // This will scale and offset from the underlying array.
257 virtual void getColumnSlice (const Slicer& slicer, Array<Float>& array);
258
259 // Put a section of all arrays in the column.
260 // This will scale and offset to the underlying array.
261 virtual void putColumnSlice (const Slicer& slicer,
262 const Array<Float>& array);
263
264 // Get a section of some arrays in the column.
265 // This will scale and offset from the underlying array.
266 virtual void getColumnSliceCells (const RefRows& rownrs,
267 const Slicer& slicer,
268 Array<Float>& data);
269
270 // Put into a section of some arrays in the column.
271 // This will scale and offset to the underlying array.
272 virtual void putColumnSliceCells (const RefRows& rownrs,
273 const Slicer& slicer,
274 const Array<Float>& data);
275
276 // Scale and/or offset target to array.
277 // This is meant when reading an array from the stored column.
278 // It optimizes for scale=1 and/or offset=0.
279 void scaleOnGet (Float scale, Float offset,
281 const Array<Short>& target);
282
283 // Scale and/or offset array to target.
284 // This is meant when writing an array into the stored column.
285 // It optimizes for scale=1 and/or offset=0.
286 void scaleOnPut (Float scale, Float offset,
287 const Array<Float>& array,
288 Array<Short>& target);
289
290 // Scale and/or offset target to array for the entire column.
291 // When the scale and offset are fixed, it will do the entire array.
292 // Otherwise it iterates through the array and applies the scale
293 // and offset per row.
295 const Array<Short>& target);
296
297 // Scale and/or offset array to target for the entire column.
298 // When the scale and offset are fixed, it will do the entire array.
299 // Otherwise it iterates through the array and applies the scale
300 // and offset per row.
302 Array<Short>& target);
303
304
305 //# Now define the data members.
306 String scaleName_p; //# name of scale column
307 String offsetName_p; //# name of offset column
308 Float scale_p; //# fixed scale factor
309 Float offset_p; //# fixed offset value
310 Bool fixed_p; //# scale/offset is fixed
311 Bool autoScale_p; //# determine scale/offset automatically
312 ScalarColumn<Float>* scaleColumn_p; //# column with scale value
313 ScalarColumn<Float>* offsetColumn_p; //# column with offset value
314 Array<Short> buffer_p; //# buffer to avoid Array constructions
315
316 // Get the scale value for this row.
317 Float getScale (rownr_t rownr);
318
319 // Get the offset value for this row.
320 Float getOffset (rownr_t rownr);
321
322 // Find minimum and maximum from the array data.
323 // NaN and infinite values are ignored. If no values are finite,
324 // minimum and maximum are set to NaN.
325 void findMinMax (Float& minVal, Float& maxVal,
326 const Array<Float>& array) const;
327
328 // Make scale and offset from the minimum and maximum of the array data.
329 // If minVal is NaN, scale is set to 0.
330 void makeScaleOffset (Float& scale, Float& offset,
331 Float minVal, Float maxVal) const;
332
333 // Put a part of an array in a row using given scale/offset values.
334 void putPart (rownr_t rownr, const Slicer& slicer,
335 const Array<Float>& array,
336 Float scale, Float offset);
337
338 // Fill the array part into the full array and put it using the
339 // given min/max values.
340 void putFullPart (rownr_t rownr, const Slicer& slicer,
341 Array<Float>& fullArray,
342 const Array<Float>& partArray,
343 Float minVal, Float maxVal);
344
345public:
346 // Define the "constructor" to construct this engine when a
347 // table is read back.
348 // This "constructor" has to be registered by the user of the engine.
349 // If the engine is commonly used, its registration can be added
350 // to the registerAllCtor function in DataManager.cc.
351 // That function gets automatically invoked by the table system.
353 const Record& spec);
354};
355
356
358{
359 return (fixed_p ? scale_p : (*scaleColumn_p)(rownr));
360}
362{
363 return (fixed_p ? offset_p : (*offsetColumn_p)(rownr));
364}
365
366
367
368} //# NAMESPACE CASACORE - END
369
370#endif
Float getOffset(rownr_t rownr)
Get the offset value for this row.
virtual Record dataManagerSpec() const
Record a record containing data manager specifications.
virtual void getArray(rownr_t rownr, Array< Float > &array)
Get an array in the given row.
virtual void getColumnSlice(const Slicer &slicer, Array< Float > &array)
Get a section of all arrays in the column.
Float getScale(rownr_t rownr)
Get the scale value for this row.
virtual void getArrayColumn(Array< Float > &array)
Get an entire column.
CompressFloat(const String &virtualColumnName, const String &storedColumnName, const String &scaleColumnName, const String &offsetColumnName, Bool autoScale=True)
Construct an engine to scale the arrays in a column.
virtual void getSlice(rownr_t rownr, const Slicer &slicer, Array< Float > &array)
Get a section of the array in the given row.
CompressFloat(const CompressFloat &)
Copy constructor is only used by clone().
virtual void reopenRW()
Reopen the engine for read/write access.
void findMinMax(Float &minVal, Float &maxVal, const Array< Float > &array) const
Find minimum and maximum from the array data.
void scaleColumnOnGet(Array< Float > &array, const Array< Short > &target)
Scale and/or offset target to array for the entire column.
virtual void putSlice(rownr_t rownr, const Slicer &slicer, const Array< Float > &array)
Put into a section of the array in the given row.
virtual DataManager * clone() const
Clone the engine object.
ScalarColumn< Float > * scaleColumn_p
ScalarColumn< Float > * offsetColumn_p
static String className()
Return the name of the class.
virtual void putArray(rownr_t rownr, const Array< Float > &array)
Put an array in the given row.
virtual void prepare()
Preparing consists of setting the writable switch and adding the initial number of rows in case of cr...
void scaleColumnOnPut(const Array< Float > &array, Array< Short > &target)
Scale and/or offset array to target for the entire column.
~CompressFloat()
Destructor is mandatory.
void scaleOnPut(Float scale, Float offset, const Array< Float > &array, Array< Short > &target)
Scale and/or offset array to target.
static DataManager * makeObject(const String &dataManagerType, const Record &spec)
Define the "constructor" to construct this engine when a table is read back.
virtual void putColumnSliceCells(const RefRows &rownrs, const Slicer &slicer, const Array< Float > &data)
Put into a section of some arrays in the column.
void putFullPart(rownr_t rownr, const Slicer &slicer, Array< Float > &fullArray, const Array< Float > &partArray, Float minVal, Float maxVal)
Fill the array part into the full array and put it using the given min/max values.
static void registerClass()
Register the class name and the static makeObject "constructor".
virtual String dataManagerName() const
Get the name given to the engine (is the virtual column name).
virtual void getArrayColumnCells(const RefRows &rownrs, Array< Float > &data)
Get some array values in the column.
void putPart(rownr_t rownr, const Slicer &slicer, const Array< Float > &array, Float scale, Float offset)
Put a part of an array in a row using given scale/offset values.
virtual void putArrayColumnCells(const RefRows &rownrs, const Array< Float > &data)
Put some array values in the column.
CompressFloat(const String &virtualColumnName, const String &storedColumnName, Float scale, Float offset=0)
Construct an engine to scale all arrays in a column with the given offset and scale factor.
virtual void putArrayColumn(const Array< Float > &array)
Put an entire column.
virtual void putColumnSlice(const Slicer &slicer, const Array< Float > &array)
Put a section of all arrays in the column.
virtual void addRowInit(rownr_t startRow, rownr_t nrrow)
Add rows to the table.
virtual void getColumnSliceCells(const RefRows &rownrs, const Slicer &slicer, Array< Float > &data)
Get a section of some arrays in the column.
virtual void create64(rownr_t initialNrrow)
Initialize the object for a new table.
CompressFloat & operator=(const CompressFloat &)=delete
Assignment is not needed and therefore forbidden.
void scaleOnGet(Float scale, Float offset, Array< Float > &array, const Array< Short > &target)
Scale and/or offset target to array.
void makeScaleOffset(Float &scale, Float &offset, Float minVal, Float maxVal) const
Make scale and offset from the minimum and maximum of the array data.
virtual String dataManagerType() const
Return the type name of the engine (i.e.
CompressFloat(const Record &spec)
Construct from a record specification as created by getmanagerSpec().
Abstract base class for a data manager.
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
float Float
Definition aipstype.h:52
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41
uInt64 rownr_t
Define the type of a row number in a table.
Definition aipsxtype.h:44