casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ArrayIO.h
Go to the documentation of this file.
1 //# ArrayIO.h: text output and binary IO for an array of any dimensionality.
2 //# Copyright (C) 1993,1994,1995,1997,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 CASA_ARRAYIO_H
29 #define CASA_ARRAYIO_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
34 
35 //# Forward declarations
36 #include <casacore/casa/iosfwd.h>
37 
38 namespace casacore { //# NAMESPACE CASACORE - BEGIN
39 
40 class AipsIO;
41 class LogIO;
42 class IPosition;
43 template<class T> class Array;
44 template<class T> class Matrix;
45 template<class T> class Vector;
46 template<class T> class Cube;
47 template<class T> class Block;
48 
49 // <summary>
50 // Input/output operators for Arrays.
51 // </summary>
52 
53 // <use visibility=export>
54 
55 // <reviewed reviewer="Paul Shannon" date="1995/02/21" tests="" demos="">
56 // This header was reviewed and revised with the goal of making it an
57 // example for those writing global function header files.
58 // </reviewed>
59 
60 // <prerequisite>
61 // <li> <linkto class=Array>Array</linkto>
62 // <li> ostream
63 // <li> <linkto class=AipsIO>AipsIO</linkto>
64 // </prerequisite>
65 
66 // <etymology>
67 // ArrayIO is simply the conventional shorthand for "array input/output".
68 // </etymology>
69 
70 // <synopsis>
71 // These global functions provide easy input and output of (possibly)
72 // large and (possibly) multi-dimensional arrays. Iteration through
73 // entire arrays is done behind the scenes, with no effort required
74 // of the client programmer.
75 // These functions are global, rather than member functions of the
76 // Array class, because of the well-known C++ requirement that the first
77 // argument to an operator function (as it is declared) is the
78 // left operand when the function is called.
79 // </synopsis>
80 
81 
82 // <example>
83 // <srcblock>
84 // IPosition shape (3,10,10,3);
85 // Array <Float> array (shape);
86 // // ...initialize and manipulate the array...
87 // cout << "result: " << array;
88 // </srcblock>
89 //
90 // <motivation>
91 // Effortless input/output is clearly a big win.
92 // </motivation>
93 //
94 // <todo asof="1997/01/15">
95 // </todo>
96 
97 // <linkfrom anchor="Array IO" classes="Array Vector Matrix Cube">
98 // <here>Array IO</here> -- Input/output operators for Arrays.
99 // </linkfrom>
100 //
101 // <group name="Array IO">
103 
104 // Write out an ascii representation of an array of any dimensionality.
105 // Arrays of dimensionality 3 or greater are written out vector by vector,
106 // preceeded by the position of the start of the vector. If the origin of
107 // the array isn't zero it is printed. The shape of the array is always
108 // printed.
109 
110 template<class T> ostream &operator << (ostream &, const Array<T> &);
111 
112 // Write a formatted copy of the array to the LogIO output object. Merely calls
113 // the ostream operator<< in turn.
114 template<class T> LogIO &operator<<(LogIO &os, const Array<T> &a);
115 
116 // Read an ascii representation of an array. All types with an <src><<</src>
117 // operator can be handled. The basic format of the input should be:
118 // <srcblock>
119 // [element element element ....]
120 // </srcblock>
121 // Elements are separated by whitespace, or a comma, optionally surrounded
122 // by white space. <br>
123 // <note role=warning> Some input routines read fields between blank spaces. This
124 // is (at the moment) especially true for Quantities and Strings.
125 // In those cases
126 // the separator should be blank (or a comma following a blank), and the
127 // end ']' should have a blank in front.
128 // A crude fix for String arrays having separators <src>,</src> and <src>]</src>
129 // without blanks preceding has been made; but slows routines down </note>
130 // The default input is a vector of unspecified length. The input shape
131 // can be changed by pre-pending the input with:
132 // <srcblock>
133 // {[shape]}
134 // </srcblock>
135 // where shape is an unsigned integer vector. The shape will be used to check
136 // the input length; and, depending on the possibility, to resize/reshape the
137 // result. However, reshaping of e.g. a Vector to a Matrix cannot be done, and
138 // the result will stay in the form asked.<br>
139 // Input order is row major, however by preceding the input with:
140 // <srcblock>
141 // {T[shape]}
142 // </srcblock>
143 // the order will be reversed.<br>
144 // Reshaping of the Array provided will depend on the type of Array and its
145 // state. If a general Array, the shape will be
146 // as defined by user. If fixed Array (e.g. Matrix, Vector, Cube) the number
147 // of dimesnsions will be kept. If the user specified more dimensions
148 // then supported (e.g. 3 for Matrix), the last dimesions will be collapsed.
149 // If less dimensions are specified, the missing ones will be set to 1.
150 // will be kept.<br>
151 // The read() version can be used to force a shape (ip), or an input
152 // transpose (it) (which can be undone by the user specifying transpose).
153 //
154 // <group>
155 
156 template<class T> istream &operator >> (istream &s, Array<T> &x);
157 template<class T> Bool read(istream &s, Array<T> &x,
158  const IPosition *ip=0, Bool it=False);
159 // </group>
160 
161 // General read support function for matrices.
162 // In principle these functions will not be
163 // used by general user, but could be. They can be used by Array type
164 // classes (like Slice, Lattice) to do the work of comparable input
165 // functions as the one for Arrays.
166 // In these functions p is the shape
167 // of the returned Block x. This shape is either deduced from the user
168 // specification; made equal to (1, nelements) if no user shape is
169 // given; is set to ip if specified. The function will return False (and
170 // p = (0)) in the case of an invalid input element; a number of elements
171 // input not equal to ip (if specified); the shape given by user as input
172 // does not conform to ip (if given) or the number of elements input.<br>
173 // trans will be True if transpose asked by user; or if forced by it.
174 
175 
176 template<class T> Bool readArrayBlock(istream &s, Bool &trans,
177  IPosition &p,
178  Block<T> &x,
179  const IPosition *ip=0, Bool it=False);
180 
181 
182 // Read or write a binary representation of an Array to a file. Very
183 // useful for saving arrays and restoring them later.
184 // <br>The putArray function is put in for forwards compatibility
185 // of images (so new images can be read with old release).
186 //
187 // <group>
188 
189 template<class T> AipsIO &operator<< (AipsIO &, const Array<T> &);
190 template<class T> void putArray (AipsIO &, const Array<T> &, const Char* name);
191 template<class T> AipsIO &operator>> (AipsIO &, Array<T> &);
192 
193 // </group>
194 
195 // </group>
196 
197 
198 
199 
200 // <summary>
201 // Global functions to read/write binary arrays from/to a file.
202 // </summary>
203 
204 // <use visibility=export>
205 
206 // <reviewed reviewer="Gareth Hunt" date="95Mar31" tests="" demos="">
207 // </reviewed>
208 
209 // <synopsis>
210 // These global functions provide disk read/write functions for an Array of
211 // binary numbers. The write operation is useful, for example, to dump an
212 // image in binary form to disk so that it can be displayed with an external
213 // utility such as SAOimage.
214 // </synopsis>
215 
216 // <example>
217 // <srcblock>
218 // Matrix<Float> picture(256, 256); picture = 0.0;
219 // String fileName="picture.data";
220 //
221 // // operations to populate picture
222 // // ...
223 //
224 // write_array (picture, fileName);
225 // </srcblock>
226 // </example>
227 
228 // <todo asof="">
229 // <li> These functions should eventually be replaced with something
230 // more sophisticated.
231 // </todo>
232 
233 // <linkfrom anchor="Array binary IO" classes="Array Vector Matrix Cube">
234 // <here>Array binary IO</here> -- Simple binary input/output for Arrays.
235 // </linkfrom>
236 
237 // <group name=Array binary IO>
239 // Write the values of an array in binary format into a file with
240 // the given name.
241 // The values are stored in local format, thus are not converted
242 // to a canonical format as
243 // <linkto class="AipsIO:description">AipsIO</linkto>
244 // does.
245 // <note role=warning>
246 // This function is only suitable for built-in data types.
247 // </note>
248 // <group>
249 template <class T>
250 void write_array (const Array<T>& the_array, const String& fileName);
251 
252 template <class T>
253 inline void write_array (const Array<T>& the_array, const Char* fileName)
254  { write_array (the_array, String(fileName)); }
255 // </group>
256 
257 // Read the values of an array in binary format from a file with
258 // the given name.
259 // The number of values read is the size of the Array, thus the file
260 // should at least contain that number of values.
261 // <note role=warning>
262 // This function is only suitable for built-in data types.
263 // </note>
264 // <group>
265 template <class T>
266 void read_array (Array<T>& the_array, const String& fileName);
267 
268 template <class T>
269 inline void read_array (Array<T>& the_array, const Char* fileName)
270  { read_array (the_array, String(fileName)); }
271 // </group>
272 
273 // </group>
274 
275 
276 
277 // <summary>
278 // Global functions for Matrix/Vector input/output using ASCII format.
279 // </summary>
280 
281 // <use visibility=export>
282 
283 // <prerequisite>
284 // <li> <linkto class=Matrix>Matrix</linkto>
285 // <li> <linkto class=Vector>Vector</linkto>
286 // </prerequisite>
287 
288 // <synopsis>
289 // These global functions support file I/O between ASCII files and
290 // Matrices or Vectors.
291 // </synopsis>
292 
293 // <example>
294 // <srcblock>
295 // Matrix<Float> picture(256, 256); picture = 0.0;
296 // String fileName="picture.data";
297 //
298 // // operations to populate picture
299 // // ...
300 //
301 // writeAsciiMatrix (picture, fileName);
302 // </srcblock>
303 // </example>
304 
305 // <linkfrom anchor="Array Ascii IO" classes="Vector Matrix">
306 // <here>Array Ascii IO</here> -- Simple Ascii input/output for Arrays.
307 // </linkfrom>
308 
309 // <group name=Array Ascii IO>
311 // These routines read and write a Matrix of data. The first line of
312 // input will be examined to determine the number of columns in the matrix.
313 // The maximum number of columns provided for is 100. Each item may be up
314 // to 50 characters long.
315 //
316 // Each item must be separated from others by one (or more) blank column.
317 // The "line" may be up to 1024 characters long. Each subsequent line must
318 // contain the SAME number of items as the first line but may be any length
319 // (up to 1024 characters).
320 //
321 // The matrix need NOT be square.
322 //
323 // The matrix should be declared but NOT dimensioned in the calling program.
324 
325 // <group>
326 template <class T>
327 void readAsciiMatrix (Matrix<T>& mat, const Char* fileName);
328 
329 template <class T>
330 void writeAsciiMatrix (const Matrix<T>& mat, const Char* fileName);
331 // </group>
332 
333 
334 // These two functions read and write a Vector of data. The input
335 // may be arranged in any format (i.e. It may be recorded as one value per
336 // line or it may be recorded with all values on a single line).
337 // Values must be separated by whitespace.
338 
339 // <group>
340 template <class T>
341 void readAsciiVector (Vector<T>& vec, const Char* fileName);
342 
343 template <class T>
344 void writeAsciiVector (const Vector<T>& vec, const Char* fileName);
345 // </group>
346 
347 // </group>
348 
349 
350 
351 } //# NAMESPACE CASACORE - END
352 
353 #ifndef CASACORE_NO_AUTO_TEMPLATES
354 #include <casacore/casa/Arrays/ArrayIO.tcc>
355 #endif //# CASACORE_NO_AUTO_TEMPLATES
356 #endif
A Vector of integers, for indexing into Array&lt;T&gt; objects.
Definition: IPosition.h:119
A 1-D Specialization of the Array class.
std::vector< double > Vector
Definition: ds9context.h:24
AipsIO is the object persistency mechanism of Casacore.
Definition: AipsIO.h:168
ostream & operator>>(ostream &is, MVTime &meas)
char Char
Definition: aipstype.h:46
A 2-D Specialization of the Array class.
ostream-like interface to creating log messages.
Definition: LogIO.h:167
ABSTRACT CLASSES Abstract class for colors Any implementation of color should be able to provide a hexadecimal form of the if a human readable name(i.e."black").In many places throughout the plotter
void write_array(const Array< T > &the_array, const Char *fileName)
Definition: ArrayIO.h:253
void read_array(Array< T > &the_array, const Char *fileName)
Definition: ArrayIO.h:269
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
const Bool False
Definition: aipstype.h:44
template &lt;class T, class U&gt; class vector;
Definition: MSFlagger.h:37
simple 1-D array
String: the storage and methods of handling collections of characters.
Definition: String.h:223
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42