casacore
Loading...
Searching...
No Matches
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: 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 CASA_ARRAYIO_2_H
27#define CASA_ARRAYIO_2_H
28
29// # Includes
30#include <vector>
31#include <ostream>
32#include <regex>
33
34#include <casacore/casa/Arrays/ArrayFwd.h>
35
36namespace casacore { // # NAMESPACE CASACORE - BEGIN
37
38class AipsIO;
39class LogIO;
40class IPosition;
41template <typename T>
42class Block;
43class String;
44
45// <summary>
46// Input/output operators for Arrays.
47// </summary>
48
49// <use visibility=export>
50
51// <reviewed reviewer="Paul Shannon" date="1995/02/21" tests="" demos="">
52// This header was reviewed and revised with the goal of making it an
53// example for those writing global function header files.
54// </reviewed>
55
56// <prerequisite>
57// <li> <linkto class=Array>Array</linkto>
58// <li> ostream
59// <li> <linkto class=AipsIO>AipsIO</linkto>
60// </prerequisite>
61
62// <etymology>
63// ArrayIO is simply the conventional shorthand for "array input/output".
64// </etymology>
65
66// <synopsis>
67// These global functions provide easy input and output of (possibly)
68// large and (possibly) multi-dimensional arrays. Iteration through
69// entire arrays is done behind the scenes, with no effort required
70// of the client programmer.
71// These functions are global, rather than member functions of the
72// Array class, because of the well-known C++ requirement that the first
73// argument to an operator function (as it is declared) is the
74// left operand when the function is called.
75// </synopsis>
76
77// <example>
78// <srcblock>
79// IPosition shape (3,10,10,3);
80// Array <float> array (shape);
81// // ...initialize and manipulate the array...
82// cout << "result: " << array;
83// </srcblock>
84//
85// <motivation>
86// Effortless input/output is clearly a big win.
87// </motivation>
88//
89// <todo asof="1997/01/15">
90// </todo>
91
92// <linkfrom anchor="Array IO" classes="Array Vector Matrix Cube">
93// <here>Array IO</here> -- Input/output operators for Arrays.
94// </linkfrom>
95//
96// <group name="Array IO">
98// Write a formatted copy of the array to the LogIO output object. Merely calls
99// the ostream operator<< in turn.
100template <typename T, typename Alloc>
102
103// Read or write a binary representation of an Array to a file. Very
104// useful for saving arrays and restoring them later.
105// <br>The putArray function is put in for forwards compatibility
106// of images (so new images can be read with old release).
107//
108// <group>
109
110template <typename T, typename Alloc>
112
113template <typename T, typename Alloc>
114void putArray(AipsIO&, const Array<T>&, const char* name);
115
116template <typename T, typename Alloc>
118
119// </group>
120
121// </group>
122
123// <summary>
124// Global functions to read/write binary arrays from/to a file.
125// </summary>
126
127// <use visibility=export>
128
129// <reviewed reviewer="Gareth Hunt" date="95Mar31" tests="" demos="">
130// </reviewed>
131
132// <synopsis>
133// These global functions provide disk read/write functions for an Array of
134// binary numbers. The write operation is useful, for example, to dump an
135// image in binary form to disk so that it can be displayed with an external
136// utility such as SAOimage.
137// </synopsis>
138
139// <example>
140// <srcblock>
141// Matrix<float> picture(256, 256); picture = 0.0;
142// String fileName="picture.data";
143//
144// // operations to populate picture
145// // ...
146//
147// write_array (picture, fileName);
148// </srcblock>
149// </example>
150
151// <todo asof="">
152// <li> These functions should eventually be replaced with something
153// more sophisticated.
154// </todo>
155
156// <linkfrom anchor="Array binary IO" classes="Array Vector Matrix Cube">
157// <here>Array binary IO</here> -- Simple binary input/output for Arrays.
158// </linkfrom>
159
160// <group name=Array binary IO>
162// Write the values of an array in binary format into a file with
163// the given name.
164// The values are stored in local format, thus are not converted
165// to a canonical format as
166// <linkto class="AipsIO:description">AipsIO</linkto>
167// does.
168// <note role=warning>
169// This function is only suitable for built-in data types.
170// </note>
171// <group>
172template <typename T, typename Alloc>
173void write_array(const Array<T>& the_array, const std::string& fileName);
174
175template <typename T, typename Alloc>
176inline void write_array(const Array<T>& the_array, const char* fileName) {
177 write_array(the_array, std::string(fileName));
178}
179// </group>
180
181// Read the values of an array in binary format from a file with
182// the given name.
183// The number of values read is the size of the Array, thus the file
184// should at least contain that number of values.
185// <note role=warning>
186// This function is only suitable for built-in data types.
187// </note>
188// <group>
189template <typename T, typename Alloc>
190void read_array(Array<T>& the_array, const std::string& fileName);
191
192template <typename T, typename Alloc>
193inline void read_array(Array<T>& the_array, const char* fileName) {
194 read_array(the_array, std::string(fileName));
195}
196// </group>
197
198// </group>
199
200// These two functions read and write a Vector of data. The input
201// may be arranged in any format (i.e. It may be recorded as one value per
202// line or it may be recorded with all values on a single line).
203// Values must be separated by whitespace.
204
205// <group>
206template <typename T, typename Alloc>
207void readAsciiVector(Vector<T>& vec, const char* fileName);
208
209template <typename T, typename Alloc>
210void writeAsciiVector(const Vector<T>& vec, const char* fileName);
211// </group>
212
213// </group>
214
215AipsIO& operator<<(AipsIO& aio, const IPosition& ip);
216AipsIO& operator>>(AipsIO& aio, IPosition& ip);
217LogIO& operator<<(LogIO& os, const IPosition& ip);
218
219template <typename T, typename Alloc>
220Block<T> makeBlock(const Array<T>& array);
221
222template <typename T>
223Vector<T> makeVector(const Block<T>& block);
224
225Vector<String> stringToVector(const String& string, char delim = ',');
226Vector<String> stringToVector(const String& string, const std::regex& delim);
227
228} // namespace casacore
229
230#include <casacore/casa/IO/ArrayIO.tcc>
231
232#endif
AipsIO & operator<<(AipsIO &aio, const IPosition &ip)
AipsIO & operator>>(AipsIO &aio, IPosition &ip)
Vector< T > makeVector(const Block< T > &block)
Vector< String > stringToVector(const String &string, char delim=',')
Block< T > makeBlock(const Array< T > &array)
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
void readAsciiVector(Vector< T > &vec, const char *fileName)
These two functions read and write a Vector of data.
friend AipsIO & operator>>(AipsIO &os, Record &rec)
Read the Record from an input stream.
Definition Record.h:431
ostream & operator<<(ostream &os, const IComplex &)
Show on ostream.
T * array
The actual storage.
Definition Block.h:689
String name() const
Return the name of the field.
void writeAsciiVector(const Vector< T > &vec, const char *fileName)
LogIO & operator<<(LogIO &os, const Array< T > &a)
Write a formatted copy of the array to the LogIO output object.
void putArray(AipsIO &, const Array< T > &, const char *name)
AipsIO & operator>>(AipsIO &, Array< T > &)
AipsIO & operator<<(AipsIO &, const Array< T > &)
Read or write a binary representation of an Array to a file.
Global functions to read/write binary arrays from/to a file.
Definition ArrayIO.h:161
void write_array(const Array< T > &the_array, const char *fileName)
Definition ArrayIO.h:176
void write_array(const Array< T > &the_array, const std::string &fileName)
Write the values of an array in binary format into a file with the given name.
void read_array(Array< T > &the_array, const char *fileName)
Definition ArrayIO.h:193
void read_array(Array< T > &the_array, const std::string &fileName)
Read the values of an array in binary format from a file with the given name.