casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ArrayIO.h
Go to the documentation of this file.
00001 //# ArrayIO.h: text output and binary IO for an array of any dimensionality.
00002 //# Copyright (C) 1993,1994,1995,1997,1999,2000,2001
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //# 
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //# 
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //# 
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //# 
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: ArrayIO.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00027 
00028 #ifndef CASA_ARRAYIO_H
00029 #define CASA_ARRAYIO_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 #include <casa/BasicSL/String.h>
00034 
00035 //# Forward declarations
00036 #include <casa/iosfwd.h>
00037 
00038 #include <set>
00039 #include <vector>
00040 #include <map>
00041 
00042 namespace casa { //# NAMESPACE CASA - BEGIN
00043 
00044 class AipsIO;
00045 class LogIO;
00046 class IPosition;
00047 template<class T> class Array;
00048 template<class T> class Matrix;
00049 template<class T> class Vector;
00050 template<class T> class Cube;
00051 template<class T> class Block;
00052 
00053 
00054 // <summary>
00055 //    Input/output operators for Arrays.
00056 // </summary>
00057 
00058 // <use visibility=export>
00059 
00060 // <reviewed reviewer="Paul Shannon" date="1995/02/21" tests="" demos="">
00061 // This header was reviewed and revised with the goal of making it an
00062 // example for those writing global function header files.
00063 // </reviewed>
00064 
00065 // <prerequisite>
00066 //   <li> <linkto class=Array>Array</linkto>
00067 //   <li> ostream
00068 //   <li> <linkto class=AipsIO>AipsIO</linkto>
00069 // </prerequisite>
00070 
00071 // <etymology>
00072 //  ArrayIO is simply the conventional shorthand for "array input/output".
00073 // </etymology>
00074 
00075 // <synopsis> 
00076 // These global functions provide easy input and output of (possibly)
00077 // large and (possibly) multi-dimensional arrays.  Iteration through
00078 // entire arrays is done behind the scenes, with no effort required
00079 // of the client programmer.  
00080 // These functions are global, rather than member functions of the
00081 // Array class, because of the well-known C++ requirement that the first
00082 // argument to an operator function (as it is declared) is the 
00083 // left operand when the function is called.
00084 // </synopsis>
00085 
00086 
00087 // <example>
00088 // <srcblock>
00089 // IPosition shape (3,10,10,3);
00090 // Array <Float> array (shape);
00091 // // ...initialize and manipulate the array...
00092 // cout << "result: " << array;
00093 // </srcblock>
00094 //
00095 // <motivation>
00096 // Effortless input/output is clearly a big win.
00097 // </motivation>
00098 //
00099 // <todo asof="1997/01/15">
00100 // </todo>
00101 
00102 // <linkfrom anchor="Array IO" classes="Array Vector Matrix Cube">
00103 //    <here>Array IO</here> -- Input/output operators for Arrays.
00104 // </linkfrom>
00105 //
00106 // <group name="Array IO">
00107 
00108 
00109 // Write out an ascii representation of an array of any dimensionality.
00110 // Arrays of dimensionality 3 or greater are written out vector by vector,
00111 // preceeded by the position of the start of the vector. If the origin of
00112 // the array isn't zero it is printed. The shape of the array is always
00113 // printed.
00114 
00115 template<class T> ostream &operator << (ostream &, const Array<T> &);
00116 
00117 template<class T, class U> ostream &operator << (ostream &, const std::map<T, U> &);
00118 template<class T> ostream &operator << (ostream &, const std::set<T> &);
00119 template<class T> ostream &operator << (ostream &, const std::vector<T> &);
00120 
00121 // Write a formatted copy of the array to the LogIO output object. Merely calls
00122 // the ostream operator<< in turn.
00123 template<class T> LogIO &operator<<(LogIO &os, const Array<T> &a);
00124 
00125 template<class T, class U> LogIO &operator<<(LogIO &os, const std::map<T,U> &a);
00126 template<class T> LogIO &operator<<(LogIO &os, const std::set<T> &a);
00127 template<class T> LogIO &operator<<(LogIO &os, const std::vector<T> &a);
00128 
00129 
00130 // Read an ascii representation of an array. All types with an <src><<</src>
00131 // operator can be handled. The basic format of the input should be:
00132 // <srcblock>
00133 //      [element element element ....] 
00134 // </srcblock>
00135 // Elements are separated by whitespace, or a comma, optionally surrounded
00136 // by white space. <br>
00137 // <note role=warning> Some input routines read fields between blank spaces. This
00138 // is (at the moment) especially true for Quantities and Strings.
00139 // In those cases
00140 // the separator should be blank (or a comma following a blank), and the
00141 // end ']' should have a blank in front.
00142 // A crude fix for String arrays having separators <src>,</src> and <src>]</src>
00143 // without blanks preceding has been made; but slows routines down </note>
00144 // The default input is a vector of unspecified length. The input shape
00145 // can be changed by pre-pending the input with:
00146 // <srcblock>
00147 //      {[shape]}
00148 // </srcblock>
00149 // where shape is an unsigned integer vector. The shape will be used to check
00150 // the input length; and, depending on the possibility, to resize/reshape the
00151 // result. However, reshaping of e.g. a Vector to a Matrix cannot be done, and
00152 // the result will stay in the form asked.<br>
00153 // Input order is row major, however by preceding the input with:
00154 // <srcblock>
00155 //      {T[shape]}      
00156 // </srcblock>
00157 // the order will be reversed.<br>
00158 // Reshaping of the Array provided will depend on the type of Array and its
00159 // state. If a general Array, the shape will be
00160 // as defined by user. If fixed Array (e.g. Matrix, Vector, Cube) the number
00161 // of dimesnsions will be kept. If the user specified more dimensions
00162 // then supported (e.g. 3 for Matrix), the last dimesions will be collapsed.
00163 // If less dimensions are specified, the missing ones will be set to 1.
00164 // will be kept.<br>
00165 // The read() version can be used to force a shape (ip), or an input
00166 // transpose (it) (which can be undone by the user specifying transpose).
00167 //
00168 // <group>
00169 
00170 template<class T> istream &operator >> (istream &s, Array<T> &x);
00171 template<class T> Bool read(istream &s, Array<T> &x,
00172                             const IPosition *ip=0, Bool it=False);
00173 // </group>
00174 
00175 // General read support function for matrices.
00176 // In principle these functions will not be
00177 // used by general user, but could be. They can be used by Array type
00178 // classes (like Slice, Lattice) to do the work of comparable input
00179 // functions as the one for Arrays.
00180 // In these functions p is the shape
00181 // of the returned Block x. This shape is either deduced from the user
00182 // specification; made equal to (1, nelements) if no user shape is
00183 // given; is set to ip if specified. The function will return False (and
00184 // p = (0)) in the case of an invalid input element; a number of elements
00185 // input not equal to ip (if specified); the shape given by user as input
00186 // does not conform to ip (if given) or the number of elements input.<br>
00187 // trans will be True if transpose asked by user; or if forced by it.
00188 
00189 
00190 template<class T> Bool readArrayBlock(istream &s, Bool &trans,
00191                                       IPosition &p,
00192                                       Block<T> &x,
00193                                       const IPosition *ip=0, Bool it=False);
00194 
00195 
00196 // Read or write a binary representation of an Array to a file. Very
00197 // useful for saving arrays and restoring them later.
00198 // <br>The putArray function is put in for forwards compatibility
00199 // of images (so new images can be read with old release).
00200 //
00201 // <group>
00202 
00203 template<class T> AipsIO &operator<< (AipsIO &, const Array<T> &);
00204 template<class T> void putArray (AipsIO &, const Array<T> &, const Char* name);
00205 template<class T> AipsIO &operator>> (AipsIO &, Array<T> &);
00206 
00207 // </group>
00208 
00209 // </group>
00210 
00211 
00212 
00213 
00214 // <summary>
00215 // Global functions to read/write binary arrays from/to a file.
00216 // </summary>
00217 
00218 // <use visibility=export>
00219 
00220 // <reviewed reviewer="Gareth Hunt" date="95Mar31" tests="" demos="">
00221 // </reviewed>
00222 
00223 // <synopsis>
00224 // These global functions provide disk read/write functions for an Array of
00225 // binary numbers.  The write operation is useful, for example, to dump an
00226 // image in binary form to disk so that it can be displayed with an external
00227 // utility such as SAOimage.
00228 // </synopsis>
00229 
00230 // <example>
00231 // <srcblock>
00232 //    Matrix<Float> picture(256, 256); picture = 0.0;
00233 //    String fileName="picture.data";
00234 //
00235 //    // operations to populate picture
00236 //    // ...
00237 //
00238 //    write_array (picture, fileName);
00239 // </srcblock>
00240 // </example>
00241 
00242 // <todo asof="">
00243 //   <li> These functions should eventually be replaced with something
00244 //        more sophisticated.
00245 // </todo>
00246 
00247 // <linkfrom anchor="Array binary IO" classes="Array Vector Matrix Cube">
00248 //    <here>Array binary IO</here> -- Simple binary input/output for Arrays.
00249 // </linkfrom>
00250 
00251 // <group name=Array binary IO>
00252 
00253 // Write the values of an array in binary format into a file with
00254 // the given name.
00255 // The values are stored in local format, thus are not converted
00256 // to a canonical format as
00257 // <linkto class="AipsIO:description">AipsIO</linkto>
00258 // does.
00259 // <note role=warning>
00260 // This function is only suitable for built-in data types.
00261 // </note>
00262 // <group>
00263 template <class T>
00264 void write_array (const Array<T>& the_array, const String& fileName);
00265 
00266 template <class T>
00267 inline void write_array (const Array<T>& the_array, const Char* fileName)
00268     { write_array (the_array, String(fileName)); }
00269 // </group>
00270 
00271 // Read the values of an array in binary format from a file with
00272 // the given name.
00273 // The number of values read is the size of the Array, thus the file
00274 // should at least contain that number of values.
00275 // <note role=warning>
00276 // This function is only suitable for built-in data types.
00277 // </note>
00278 // <group>
00279 template <class T>
00280 void read_array (Array<T>& the_array, const String& fileName);
00281 
00282 template <class T>
00283 inline void read_array (Array<T>& the_array, const Char* fileName)
00284     { read_array (the_array, String(fileName)); }
00285 // </group>
00286 
00287 // </group>
00288 
00289 
00290 
00291 // <summary>
00292 // Global functions for Matrix/Vector input/output using ASCII format.
00293 // </summary>
00294 
00295 // <use visibility=export>
00296 
00297 // <prerequisite>
00298 //   <li> <linkto class=Matrix>Matrix</linkto>
00299 //   <li> <linkto class=Vector>Vector</linkto>
00300 // </prerequisite>
00301 
00302 // <synopsis>
00303 // These global functions support file I/O between ASCII files and
00304 // Matrices or Vectors.
00305 // </synopsis>
00306 
00307 // <example>
00308 // <srcblock>
00309 //    Matrix<Float> picture(256, 256); picture = 0.0;
00310 //    String fileName="picture.data";
00311 //
00312 //    // operations to populate picture
00313 //    // ...
00314 //
00315 //    writeAsciiMatrix (picture, fileName);
00316 // </srcblock>
00317 // </example>
00318 
00319 // <linkfrom anchor="Array Ascii IO" classes="Vector Matrix">
00320 //    <here>Array Ascii IO</here> -- Simple Ascii input/output for Arrays.
00321 // </linkfrom>
00322 
00323 // <group name=Array Ascii IO>
00324 
00325 // These routines read and write a Matrix of data.  The first line of
00326 // input will be examined to determine the number of columns in the matrix.
00327 // The maximum number of columns provided for is 100.  Each item may be up 
00328 // to 50 characters long.
00329 //
00330 // Each item must be separated from others by one (or more) blank column.
00331 // The "line" may be up to 1024 characters long.  Each subsequent line must
00332 // contain the SAME number of items as the first line but may be any length
00333 // (up to 1024 characters).
00334 //
00335 // The matrix need NOT be square.  
00336 //
00337 // The matrix should be declared but NOT dimensioned in the calling program.
00338 
00339 // <group>
00340 template <class T>
00341 void readAsciiMatrix (Matrix<T>& mat, const Char* fileName);
00342 
00343 template <class T>
00344 void writeAsciiMatrix (const Matrix<T>& mat, const Char* fileName);
00345 // </group>
00346 
00347 
00348 // These two functions read and write a Vector of data.  The input
00349 // may be arranged in any format (i.e. It may be recorded as one value per
00350 // line or it may be recorded with all values on a single line).
00351 // Values must be separated by whitespace.
00352 
00353 // <group>
00354 template <class T>
00355 void readAsciiVector (Vector<T>& vec, const Char* fileName);
00356 
00357 template <class T>
00358 void writeAsciiVector (const Vector<T>& vec, const Char* fileName);
00359 // </group>
00360 
00361 // </group>
00362 
00363 
00364 
00365 } //# NAMESPACE CASA - END
00366 
00367 #ifndef CASACORE_NO_AUTO_TEMPLATES
00368 #include <casa/Arrays/ArrayIO.tcc>
00369 #endif //# CASACORE_NO_AUTO_TEMPLATES
00370 #endif