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